<div dir="ltr">Thanks Michael for pointing to spawn_monitor/3, that is what I was looking for<div>+ Harit</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Jan 12, 2015 at 10:27 PM, Michael Loftis <span dir="ltr"><<a href="mailto:mloftis@wgops.com" target="_blank">mloftis@wgops.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">You could also do a spawn_monitor (or possibly spawn_link depending on<br>
how you want to handle things) to avoid racing a potentially crashing<br>
process.<br>
<br>
On Mon, Jan 12, 2015 at 7:52 PM, Harit Himanshu<br>
<div class="HOEnZb"><div class="h5"><<a href="mailto:harit.subscriptions@gmail.com">harit.subscriptions@gmail.com</a>> wrote:<br>
> Thank you very much Felix. really appreciate your time and effort<br>
><br>
> On Mon, Jan 12, 2015 at 3:50 PM, Felix Gallo <<a href="mailto:felixgallo@gmail.com">felixgallo@gmail.com</a>> wrote:<br>
>><br>
>> a) spawn() never fails.  You'll get a Pid back, or the BEAM will crash in<br>
>> which case mere erlang programs are the least of your problems.  The Pid<br>
>> will then either apply M:F to A, if M:F exists and is of arity befitting A,<br>
>> or apply error_handler:undefined_function to [M,F,A]), which in turns prints<br>
>> out that message you saw unless you've redefined the error handler, which<br>
>> you should definitely not do.<br>
>><br>
>> b) monitor() also will never fail.  If monitor() is called on a<br>
>> non-existent Pid, you will get a DOWN message immediately.  monitor() will<br>
>> still return a Ref.  (note: this applies to erlang released within the last<br>
>> few years; before, you would get a badarg exception).<br>
>><br>
>> os:timestamp() will also never fail.<br>
>><br>
>> In general your approach should be to assume that everything will work out<br>
>> fine and only program, at first, the 'happy path' for your code.  Then fix<br>
>> them up as you find valid inputs which cause them to crash.<br>
>><br>
>> <a href="http://erldocs.com/" target="_blank">http://erldocs.com/</a> and <a href="http://www.erlang.org/doc/" target="_blank">http://www.erlang.org/doc/</a> are really good, and<br>
>> can help you answer a lot of these questions; the functions you list are<br>
>> well documented, for example.  If you're using a Mac, I can recommend<br>
>> getting 'Dash' (a documentation-at-your-hotkey app with a search bar) as<br>
>> well.<br>
>><br>
>> F.<br>
>><br>
>> On Mon, Jan 12, 2015 at 3:14 PM, Harit Himanshu<br>
>> <<a href="mailto:harit.subscriptions@gmail.com">harit.subscriptions@gmail.com</a>> wrote:<br>
>>><br>
>>> I need to make my own function my_spawn(M,F,A) which work similar to<br>
>>> spawn(M,F,A) but if a spawned process dies, I need to print a message<br>
>>> telling how long a process lived.<br>
>>><br>
>>><br>
>>> My attempt looks like<br>
>>><br>
>>> -export([my_spawn/3]).<br>
>>><br>
>>> my_spawn(Mod, Func, Args) -><br>
>>>   Pid = spawn(Mod, Func, Args),<br>
>>>   Ref = monitor(process, Pid),<br>
>>>   Start = os:timestamp(),<br>
>>>   receive<br>
>>>     {'DOWN', Ref, process, Pid, _Why} -><br>
>>>       io:format("process died after ~p microseconds.~n",<br>
>>> [timer:now_diff(os:timestamp(), Start)])<br>
>>>   end,<br>
>>>   Pid.<br>
>>><br>
>>><br>
>>> so when I run this, I see<br>
>>><br>
>>> 1> Pid = error_handling:my_spawn(area_server1, area, []).<br>
>>> process died after 363 microseconds.<br>
>>><br>
>>> =ERROR REPORT==== 12-Jan-2015::15:06:53 ===<br>
>>> Error in process <0.33.0> with exit value:<br>
>>> {undef,[{area_server1,area,[],[]}]}<br>
>>><br>
>>> <0.33.0><br>
>>> 2><br>
>>><br>
>>> Problems?<br>
>>> - I am learning both Erlang and concurrency for the first time, so I see<br>
>>> following potential problems with this approach<br>
>>><br>
>>> a.) What if spawned process failed in the first place, so I get no Pid<br>
>>> b.) What if command fails executing either "Ref = .." or "Start = .."?<br>
>>><br>
>>> In such scenarios the program will not work effectively, what are the<br>
>>> recommended ways to solve such thing?<br>
>>><br>
>>> Also, do you see any potential race conditions here? I take a guess<br>
>>> saying no because even if multiple process trying to execute the same code,<br>
>>> they will return different Pid anyway, is that understanding correct?<br>
>>><br>
>>> Thanks a lot<br>
>>> + Harit<br>
>>><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>
>><br>
><br>
><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>
<br>
<br>
<br>
</div></div><span class="HOEnZb"><font color="#888888">--<br>
<br>
"Genius might be described as a supreme capacity for getting its possessors<br>
into trouble of all kinds."<br>
-- Samuel Butler<br>
</font></span></blockquote></div><br></div>