[erlang-questions] How to identify the process which died and restart it?

Roberto Ostinelli roberto@REDACTED
Wed Jan 14 10:02:00 CET 2015


Is there any reason why you're not considering a supervisor [1]?

Best,
r.

[1] http://erlang.org/doc/man/supervisor.html

On Wed, Jan 14, 2015 at 4:52 AM, Harit Himanshu <
harit.subscriptions@REDACTED> wrote:

> I was working on the following problem and wrote the code that works, but
> I have conceptual questions which are still bothering me. So I thought to
> put them in community and seek recommendations and guidance
>
> *Question*
> Write a function that starts and monitors several worker processes. If any
> of the worker processes dies abnormally, restart it.
>
> My attempt looks like
> monitor_workers(F, NumberOfWorkers) ->
>   spawn(error_handling, monitor_workers_loop, [F, NumberOfWorkers]).
>
>
> monitor_workers_loop(F, NumberOfWorkers) ->
>   Workers = [spawn_monitor(F) || _Worker <- lists:seq(1, NumberOfWorkers)],
>   io:format("started workers: ~p.~n", [Workers]),
>   receive
>     {'DOWN', _, process, _, _} ->
>       io:format("one of workers killed, starting one.~n"),
>       monitor_workers_loop(F, 1)
>   end.
>
> and when I run, I see
>
> 1> c(error_handling).
> {ok,error_handling}
> 2>
> 2> error_handling:monitor_workers(fun()-> area_server0:loop() end, 2).
> started workers: [{<0.40.0>,#Ref<0.0.0.81>},{<0.41.0>,#Ref<0.0.0.82>}].
> <0.39.0>
> 3>
> 3>  Worker1 = list_to_pid("<0.40.0>").
> <0.40.0>
> 4>
> 4> Worker2 = list_to_pid("<0.41.0>").
> <0.41.0>
> 5>
> 5> exit(Worker1, kill).
> one of workers killed, starting one.
> started workers: [{<0.45.0>,#Ref<0.0.0.98>}].
> true
> 6> exit(Worker2, kill).
> one of workers killed, starting one.
> started workers: [{<0.47.0>,#Ref<0.0.0.105>}].
> true
> 7>
>
> *Problem(s)?*
>
>    1. A better way to solve this would be pass in different
>    functions(than same, what I did) and let workers work on different
>    problems. In such a scenario all we know if Pid and Ref to the workers.
>    2. When any process fails, it matches {'DOWN', Ref, process, Pid,
>    Why}, and given this context how could I identify what function this
>    process was running?
>
> I am very new to Erlang so have not much knowledge to solve such problems.
>
> Thank you
> + Harit Himanshu
>
> _______________________________________________
> 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/20150114/36716819/attachment.htm>


More information about the erlang-questions mailing list