[erlang-questions] Process surviving disconnect

erlang erlang@REDACTED
Tue Aug 16 17:09:03 CEST 2011


 I am completely new to erlang and diving into this brave new world.

 I got the following setup:

  - 2 Servers with fqdn usa.local and gca.local
  - 1 erlang node on each of them named alice@REDACTED and 
 bob@REDACTED

 When I start Alice (`alice:start/0`) on alice@REDACTED it spawns 
 linked Bob (`bob:start/1`) on gca.local. Both processing are trapping 
 exits.

 When Alice dies of something, Bob gets notified and keeps on running.
 When Bob dies of something, Alice gets notified and keeps on running.

 When I cut the network connection, Alice gets notified that Bob has 
 died of `noconnection` and process bob dies on bob@REDACTED

 I do not want this to happen. I want Bob to keep on running although it 
 looses connection to Alice and at the same I want both threads to be 
 notified if their partner dies (of normal, of badarith, of whatever).

 **My questions are:**

  - Has this something to do that I initially spawn Bob from the Alice 
 node?
  - How can I make Bob to survive a connection loss?

 Thank you in advance
 Anchise

 ----------


 Here goes the code:

     -module (alice).
     -compile (export_all).

     start () ->
     	register (alice, spawn (fun init/0) ).

     stop () ->
     	whereis (alice) ! stop.

     init () ->
     	process_flag (trap_exit, true),
     	Bob = spawn_link ('bob@REDACTED', bob, start, [self () ] ),
     	loop (Bob).

     loop (Bob) ->
     	receive
     		stop -> ok;
     		{'EXIT', Bob, Reason} ->
     			io:format ("Bob died of ~p.~n", [Reason] ),
     			loop (Bob);
     		Msg ->
     			io:format ("Alice received ~p.~n", [Msg] ),
     			loop (Bob)
     	end.


 ----------

     -module (bob).
     -compile (export_all).

     start (Alice) ->
     	process_flag (trap_exit, true),
     	register (bob, self () ),
     	loop (Alice).

     loop (Alice) ->
     	receive
     		stop -> ok;
     		{'EXIT', Alice, Reason} ->
     			io:format ("Alice died of ~p.~n", [Reason] ),
     			loop (Alice);
     		Msg ->
     			io:format ("Bob received ~p.~n", [Msg] ),
     			loop (Alice)
     	after 5000 ->
     		Alice ! "Hi, this Bob",
     		loop (Alice)
     	end.






More information about the erlang-questions mailing list