[erlang-questions] Can not kill the registered process

Harit Himanshu harit.subscriptions@REDACTED
Tue Jan 13 23:23:51 CET 2015


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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20150113/d0e6a56f/attachment.htm>


More information about the erlang-questions mailing list