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

Jack Orenstein jao@REDACTED
Tue Dec 9 05:30:58 CET 2008


Please excuse a very basic question.

How can an Erlang process avoid hanging when sending a message to a  
process that no longer exists, (as in the code below)? I understand  
that using the send operator (!) directly is pretty uncommon, and  
that applications would normally use OTP, but how does OTP deal with  
the problem?

Jack


-module(test).

-export([main/0]).

-define(DUMP(X), io:format("~p:~p - ~p = ~p~n", [?MODULE, ?LINE, ??X,  
X])).

main() ->
     P = spawn(fun() -> loop() end),
     send_and_receive(P, hello),
     send(P, exit),
     send_and_receive(P, hello).

send(Server, Request) ->
     ?DUMP({send, Request}),
     Server ! {self(), Request}.

send_and_receive(Server, Request) ->
     ?DUMP({send_and_receive, send, Request}),
     Server ! {self(), Request},
     receive
         {Server, Response} ->
             ?DUMP({send_and_receive, received, Response}),
             Response
     end.

loop() ->
     receive
         {From, hello} ->
             ?DUMP(hello),
             From ! {self(), hi},
             loop();
         {_From, exit} ->
             ?DUMP(exit),
             void
     end.




More information about the erlang-questions mailing list