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

Harit Himanshu harit.subscriptions@REDACTED
Wed Jan 14 04:52:36 CET 2015

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

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]),
    {'DOWN', _, process, _, _} ->
      io:format("one of workers killed, starting one.~n"),
      monitor_workers_loop(F, 1)

and when I run, I see

1> c(error_handling).
2> error_handling:monitor_workers(fun()-> area_server0:loop() end, 2).
started workers: [{<0.40.0>,#Ref<>},{<0.41.0>,#Ref<>}].
3>  Worker1 = list_to_pid("<0.40.0>").
4> Worker2 = list_to_pid("<0.41.0>").
5> exit(Worker1, kill).
one of workers killed, starting one.
started workers: [{<0.45.0>,#Ref<>}].
6> exit(Worker2, kill).
one of workers killed, starting one.
started workers: [{<0.47.0>,#Ref<>}].


   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

I am very new to Erlang so have not much knowledge to solve such problems.

Thank you
+ Harit Himanshu
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20150113/b00505ce/attachment.htm>

More information about the erlang-questions mailing list