[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),
  Pid.

and when I run I see

1>  c(error_handling).

{ok,error_handling}

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

created new process: <0.39.0>.

<0.39.0>

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

{<0.32.0>,{square,10}}

4> receive X->X end.

100

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

{<0.32.0>,{square,20}}

6> receive Y->Y end.

400

** exception error: killed

7>

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

Thanks
-------------- 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