[erlang-questions] Can not kill the registered process
Harit Himanshu
harit.subscriptions@REDACTED
Tue Jan 13 23:33:39 CET 2015
you are right Felix, exit/1 did not help.
Instead I tried timer:kill_after() and this seems to work as expected. I
made no changes to code, only changed how to kill the registered process
and it started to work
1> c(error_handling).
{ok,error_handling}
2> error_handling:monitor_runner().
started new process <0.40.0> and reference #Ref<0.0.0.77>.
<0.39.0>
I am running.
I am running.
I am running.
I am running.
3> timer:kill_after(3000, whereis(runner)).
{ok,{1421188290386972,#Ref<0.0.0.88>}}
I am running.
runner got killed, restarting.
started new process <0.45.0> and reference #Ref<0.0.0.95>.
I am running.
I am running.
I am running.
4> timer:kill_after(3000, whereis(runner)).
{ok,{1421188310002128,#Ref<0.0.0.101>}}
runner got killed, restarting.
started new process <0.48.0> and reference #Ref<0.0.0.107>.
I am running.
I am running.
5>
Thank you
+ Harit Himanshu
On Tue, Jan 13, 2015 at 2:31 PM, Felix Gallo <felixgallo@REDACTED> wrote:
> exit/1 doesn't do what you think it does. Maybe try exit/2?
>
> On Tue, Jan 13, 2015 at 2:23 PM, Harit Himanshu <
> harit.subscriptions@REDACTED> wrote:
>
>> The problem I am trying while learning from exercises from Joe's book is
>>
>> Write a function that creates a registered process that writes out "I’m
>> still running" every five seconds. Write a function that monitors this
>> process and restarts it if it dies. Start the global process and the
>> monitor process. Kill the global process and check that it has been
>> restarted by the monitor process.
>>
>> So I created the runner and monitor_runner as
>>
>> runner() ->
>> receive
>> after 5 * 1000 ->
>> io:format("I am running.~n"),
>> runner()
>> end.
>>
>> monitor_runner() ->
>> spawn(fun() ->
>> {Pid, Ref} = spawn_monitor(error_handling, runner, []),
>> io:format("started new process ~p and reference ~p.~n", [Pid, Ref]),
>> register(runner, Pid),
>> receive
>> {'DOWN', Ref, process, Pid, _Why} ->
>> io:format("runner got killed, restarting"),
>> monitor_runner();
>> X -> X
>> end
>> end).
>>
>>
>> When I run this, I get
>>
>> 1> c(error_handling).
>>
>> {ok,error_handling}
>>
>> 2> Pid = error_handling:monitor_runner().
>>
>> started new process <0.40.0> and reference #Ref<0.0.0.77>.
>>
>> <0.39.0>
>>
>> I am running.
>>
>> 3>
>>
>> 3>
>>
>> I am running.
>>
>> 3> Pid.
>>
>> <0.39.0>
>>
>> I am running.
>>
>> I am running.
>>
>> I am running.
>>
>> I am running.
>>
>> I am running.
>>
>> 4> whereis(runner).
>>
>> <0.40.0>
>>
>> I am running.
>>
>> 6> exit(whereis(runner)).
>>
>> ** exception exit: <0.40.0>
>>
>> I am running.
>>
>> I am running.
>>
>> I am running.
>>
>> I am running.
>>
>> I am running.
>>
>> I am running.
>>
>> I am running.
>>
>> 7>
>>
>> *Problem?*
>> The runner is still running.
>> 1.) Why is the process not getting killed?
>> 2.) Why is it not caught by {'DOWN'....}
>>
>> What am I missing here?
>>
>> Thanks
>> + Harit
>>
>> _______________________________________________
>> erlang-questions mailing list
>> erlang-questions@REDACTED
>> http://erlang.org/mailman/listinfo/erlang-questions
>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20150113/d100deac/attachment.htm>
More information about the erlang-questions
mailing list