[erlang-questions] Kill a process after T seconds

Harit Himanshu harit.subscriptions@REDACTED
Tue Jan 13 19:55:54 CET 2015

The recent problem I worked on is

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.

My attempt looks like

my_spawn(Mod, Func, Args, Time) ->
  Pid = spawn(Mod, Func, Args),
  io:format("created new process: ~p.~n", [Pid]),
  timer:kill_after(Time * 1000),

and when I run I see

1>  c(error_handling).


2> Pid1 = error_handling:my_spawn(area_server0, loop, [], 30).

created new process: <0.39.0>.


3> Pid1 ! {self(), {square, 10}}.


4> receive X->X end.


5> Pid1 ! {self(), {square, 20}}.


6> receive Y->Y end.


** exception error: killed


Looking for suggestions on the code or ways to write it better

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20150113/28e5e809/attachment.htm>

More information about the erlang-questions mailing list