<p dir="ltr">You need to keep a table of either pid-> task (function or whatever) or mref-> task.<br>
There are several representation choices for the table. A {Key, Value} list would be the simplest.<br>
Some of the key functions in the lists module might be of help - or you could do the manipulations directly. </p>
<p dir="ltr">/Erik</p>
<div class="gmail_quote">Den 14/01/2015 04.52 skrev "Harit Himanshu" <<a href="mailto:harit.subscriptions@gmail.com">harit.subscriptions@gmail.com</a>>:<br type="attribution"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">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<div><br></div><div><b>Question</b></div><div>Write a function that starts and monitors several worker processes. If any of the worker processes dies abnormally, restart it.<b><br></b></div><div><br></div><div>My attempt looks like </div><div><div>monitor_workers(F, NumberOfWorkers) -></div><div> spawn(error_handling, monitor_workers_loop, [F, NumberOfWorkers]).</div><div><br></div><div><br></div><div>monitor_workers_loop(F, NumberOfWorkers) -></div><div> Workers = [spawn_monitor(F) || _Worker <- lists:seq(1, NumberOfWorkers)],</div><div> io:format("started workers: ~p.~n", [Workers]),</div><div> receive</div><div> {'DOWN', _, process, _, _} -></div><div> io:format("one of workers killed, starting one.~n"),</div><div> monitor_workers_loop(F, 1)</div><div> end.</div></div><div><br></div><div>and when I run, I see </div><div><br></div><div><div>1> c(error_handling).</div><div>{ok,error_handling}</div><div>2> </div><div>2> error_handling:monitor_workers(fun()-> area_server0:loop() end, 2).</div><div>started workers: [{<0.40.0>,#Ref<0.0.0.81>},{<0.41.0>,#Ref<0.0.0.82>}].</div><div><0.39.0></div><div>3> </div><div>3> Worker1 = list_to_pid("<0.40.0>").</div><div><0.40.0></div><div>4> </div><div>4> Worker2 = list_to_pid("<0.41.0>").</div><div><0.41.0></div><div>5> </div><div>5> exit(Worker1, kill).</div><div>one of workers killed, starting one.</div><div>started workers: [{<0.45.0>,#Ref<0.0.0.98>}].</div><div>true</div><div>6> exit(Worker2, kill).</div><div>one of workers killed, starting one.</div><div>started workers: [{<0.47.0>,#Ref<0.0.0.105>}].</div><div>true</div><div>7> </div></div><div><br></div><div><b>Problem(s)?</b></div><div><ol><li>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.</li><li>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?</li></ol><div>I am very new to Erlang so have not much knowledge to solve such problems.</div></div><div><br></div><div>Thank you</div><div>+ Harit Himanshu</div></div>
<br>_______________________________________________<br>
erlang-questions mailing list<br>
<a href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</a><br>
<a href="http://erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://erlang.org/mailman/listinfo/erlang-questions</a><br>
<br></blockquote></div>