[erlang-questions] Print a message when a process dies

Felix Gallo felixgallo@REDACTED
Tue Jan 13 00:50:16 CET 2015


a) spawn() never fails.  You'll get a Pid back, or the BEAM will crash in
which case mere erlang programs are the least of your problems.  The Pid
will then either apply M:F to A, if M:F exists and is of arity befitting A,
or apply error_handler:undefined_function to [M,F,A]), which in turns
prints out that message you saw unless you've redefined the error handler,
which you should definitely not do.

b) monitor() also will never fail.  If monitor() is called on a
non-existent Pid, you will get a DOWN message immediately.  monitor() will
still return a Ref.  (note: this applies to erlang released within the last
few years; before, you would get a badarg exception).

os:timestamp() will also never fail.

In general your approach should be to assume that everything will work out
fine and only program, at first, the 'happy path' for your code.  Then fix
them up as you find valid inputs which cause them to crash.

http://erldocs.com/ and http://www.erlang.org/doc/ are really good, and can
help you answer a lot of these questions; the functions you list are well
documented, for example.  If you're using a Mac, I can recommend getting
'Dash' (a documentation-at-your-hotkey app with a search bar) as well.

F.

On Mon, Jan 12, 2015 at 3:14 PM, Harit Himanshu <
harit.subscriptions@REDACTED> wrote:

> I need to make my own function my_spawn(M,F,A) which work similar to
> spawn(M,F,A) but if a spawned process dies, I need to print a message
> telling how long a process lived.
>
>
> My attempt looks like
>
> -export([my_spawn/3]).
>
> my_spawn(Mod, Func, Args) ->
>   Pid = spawn(Mod, Func, Args),
>   Ref = monitor(process, Pid),
>   Start = os:timestamp(),
>   receive
>     {'DOWN', Ref, process, Pid, _Why} ->
>       io:format("process died after ~p microseconds.~n",
> [timer:now_diff(os:timestamp(), Start)])
>   end,
>   Pid.
>
>
> so when I run this, I see
>
> 1> Pid = error_handling:my_spawn(area_server1, area, []).
> process died after 363 microseconds.
>
> =ERROR REPORT==== 12-Jan-2015::15:06:53 ===
> Error in process <0.33.0> with exit value:
> {undef,[{area_server1,area,[],[]}]}
>
> <0.33.0>
> 2>
>
> *Problems**?*
> - I am learning both Erlang and concurrency for the first time, so I see
> following potential problems with this approach
>
> a.) What if spawned process failed in the first place, so I get no Pid
> b.) What if command fails executing either "Ref = .." or "Start = .."?
>
> In such scenarios the program will not work effectively, what are the
> recommended ways to solve such thing?
>
> Also, do you see any potential race conditions here? I take a guess saying
> no because even if multiple process trying to execute the same code, they
> will return different Pid anyway, is that understanding correct?
>
> Thanks a lot
> + Harit
>
> _______________________________________________
> 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/20150112/dd4095d1/attachment.htm>


More information about the erlang-questions mailing list