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

Rick Pettit rpettit@REDACTED
Wed Jan 14 16:12:13 CET 2015


Harit,

Sorry for going off topic here, but in case someone hasn’t already suggested it when you are done with your current reading you might want to check out Fred Hebert’s book _Learn You Some Erlang For Great Good_.

Not to take anything away from your current read by any means, but I’ve been using Erlang for years and have developed a number of successful production systems with it and am now working on teaching others within the company by way of going through LYSEFGG with them.

I am finding it to be an excellent read, one I *really* wish was available back when I first started learning.

Check it out if you haven’t already, you won’t be disappointed.

-Rick

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

> Yes, because I am not on the chapter which teaches about it yet. My understanding is that the same semantics could be reaching without using it as well.
> 
> Having said that, I do believe supervisor would be the best solution, but  this problem has to be used without Supervisor. I guess the exercise is given to understand the semantics of Supervisor works
> 
> Thank you
> + Harit Himanshu
> 
> On Wed, Jan 14, 2015 at 1:02 AM, Roberto Ostinelli <roberto@REDACTED> wrote:
> 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)?
> 	• 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.
> 	• 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
> 
> 
> 
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions




More information about the erlang-questions mailing list