<div dir="ltr">I think the problem is you don't kill spawned process but parent. The code should be<div><div>my_spawn(Mod, Func, Args, Time) -></div><div> Pid = spawn(Mod, Func, Args),</div><div> io:format("created new process: ~p.~n", [Pid]),</div><div> timer:kill_after(Time * 1000, Pid),</div><div> Pid.</div></div><div><br></div><div>Note using timer:kill_after/2 instead of timer:kill_after/1</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Jan 13, 2015 at 7:55 PM, Harit Himanshu <span dir="ltr"><<a href="mailto:harit.subscriptions@gmail.com" target="_blank">harit.subscriptions@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">The recent problem I worked on is <div><br></div><div>Write a function my_spawn(Mod, Func, Args, Time) that behaves like spawn(Mod, Func, Args) but with one difference. If the spawned process lives for more than Time seconds, it should be killed.<br></div><div><br></div><div>My attempt looks like </div><div><br></div><div><div>my_spawn(Mod, Func, Args, Time) -></div><div> Pid = spawn(Mod, Func, Args),</div><div> io:format("created new process: ~p.~n", [Pid]),</div><div> timer:kill_after(Time * 1000),</div><div> Pid.</div></div><div><br></div><div>and when I run I see </div><div><p style="margin:0px;font-size:11px;font-family:Monaco;color:rgb(131,148,150);background-color:rgb(2,43,54)">1> c(error_handling).</p>
<p style="margin:0px;font-size:11px;font-family:Monaco;color:rgb(131,148,150);background-color:rgb(2,43,54)">{ok,error_handling}</p>
<p style="margin:0px;font-size:11px;font-family:Monaco;color:rgb(131,148,150);background-color:rgb(2,43,54)">2> Pid1 = error_handling:my_spawn(area_server0, loop, [], 30). </p>
<p style="margin:0px;font-size:11px;font-family:Monaco;color:rgb(131,148,150);background-color:rgb(2,43,54)">created new process: <0.39.0>.</p>
<p style="margin:0px;font-size:11px;font-family:Monaco;color:rgb(131,148,150);background-color:rgb(2,43,54)"><0.39.0></p>
<p style="margin:0px;font-size:11px;font-family:Monaco;color:rgb(131,148,150);background-color:rgb(2,43,54)">3> Pid1 ! {self(), {square, 10}}.</p>
<p style="margin:0px;font-size:11px;font-family:Monaco;color:rgb(131,148,150);background-color:rgb(2,43,54)">{<0.32.0>,{square,10}}</p>
<p style="margin:0px;font-size:11px;font-family:Monaco;color:rgb(131,148,150);background-color:rgb(2,43,54)">4> receive X->X end.</p>
<p style="margin:0px;font-size:11px;font-family:Monaco;color:rgb(131,148,150);background-color:rgb(2,43,54)">100</p>
<p style="margin:0px;font-size:11px;font-family:Monaco;color:rgb(131,148,150);background-color:rgb(2,43,54)">5> Pid1 ! {self(), {square, 20}}.</p>
<p style="margin:0px;font-size:11px;font-family:Monaco;color:rgb(131,148,150);background-color:rgb(2,43,54)">{<0.32.0>,{square,20}}</p>
<p style="margin:0px;font-size:11px;font-family:Monaco;color:rgb(131,148,150);background-color:rgb(2,43,54)">6> receive Y->Y end. </p>
<p style="margin:0px;font-size:11px;font-family:Monaco;color:rgb(131,148,150);background-color:rgb(2,43,54)">400</p>
<p style="margin:0px;font-size:11px;font-family:Monaco;color:rgb(131,148,150);background-color:rgb(2,43,54)">** exception error: killed</p>
<p style="margin:0px;font-size:11px;font-family:Monaco;color:rgb(131,148,150);background-color:rgb(2,43,54)">7> </p></div><div><br></div><div>Looking for suggestions on the code or ways to write it better</div><div><br></div><div>Thanks</div><div><br></div></div>
<br>_______________________________________________<br>
erlang-questions mailing list<br>
<a href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</a><br>
<a href="http://erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://erlang.org/mailman/listinfo/erlang-questions</a><br>
<br></blockquote></div><br></div>