[erlang-questions] Sending a message to a process that no longer exists

Robert Virding <>
Tue Dec 9 11:04:50 CET 2008


Using erlang:monitor/2 is better as the process could die after you check it
is alive but before it replies.

Do you know that it will alwys reply?

Check the module io.erl in stdlib which uses erlang:monitor/2 to make sure
that io servers are alive long enough to reply to an io request. There is a
bit of trickery needed when removing the monitor with erlang:demonitor/1 to
make sure no 'DOWN' messages are left in the mailbox. N.B. monitoring stacks
so you have to be careful to remove as many as you add.

If you are going to send many requests in one go to the process it would be
better to wrap the whole "transaction" with monitor/demonitor.

Robert

2008/12/9 Rapsey <>

> Or you can use a monitor (erlang:monitor/2), to receive a message when a
> process dies.
> Or you can check if a proces is alive with: erlang:is_process_alive(PID)
>
>
> Sergej
>
>
> On Tue, Dec 9, 2008 at 7:40 AM, Bernard Duggan <> wrote:
>
>> Jack Orenstein wrote:
>> > On Dec 9, 2008, at 12:58 AM, Rapsey wrote:
>> >
>> >
>> >> Using the send operator is very common. To avoid endless waiting in
>> >> a receive statement you can use after:
>> >>
>> >> receive
>> >>         {Server, Response} ->
>> >>             ?DUMP({send_and_receive, received, Response}),
>> >>             Response
>> >>        after 1000 ->
>> >>              void
>> >>   end.
>> >>
>> >
>> > But this won't distinguish a dead process from a slow one.
>> >
>> If you want to ensure the process is running, you can use
>> process_flag(trap_exit, true),
>> and then spawn_link(), instead of spawn() to create the server process.
>> Then, in your receive block above add a pattern
>>
>> {'EXIT', Server, Reason}
>>
>> This will be sent to you when the server process exits for whatever
>> reason.
>>
>> Cheers,
>>
>> Bernard
>>
>
>
> _______________________________________________
> erlang-questions mailing list
> 
> http://www.erlang.org/mailman/listinfo/erlang-questions
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20081209/ddf77bae/attachment.html>


More information about the erlang-questions mailing list