Skip to content

Instantly share code, notes, and snippets.

@adhalanay
Created August 11, 2020 09:50
Show Gist options
  • Save adhalanay/2f72b26e5db8b3622c3a03e89cd7e281 to your computer and use it in GitHub Desktop.
Save adhalanay/2f72b26e5db8b3622c3a03e89cd7e281 to your computer and use it in GitHub Desktop.
Supervisor assignment
-module(super).
-export([super/0]).
super() ->
process_flag(trap_exit, true),
E = spawn_link(echo,listener,[]),
register(echo,E),
io:format("echo spawned.~n"),
T = spawn_link(talk,worker,[]),
register(talk,T),
io:format("worked spawned as Pid ~w.~n",[whereis(talk)]),
loop(E,T).
loop(E,T) ->
receive
{'EXIT', T, _} ->
NewT = spawn_link(talk,worker,[]),
register(talk,NewT),
io:format("worked re-spawned as Pid ~w.~n",[whereis(talk)]),
loop(E,NewT);
{'EXIT', E, _} ->
timer:sleep(1000),
NewE = spawn_link(echo,listener,[]),
register(echo,NewE),
io:format("echo re-spawned.~n"),
loop(NewE,T)
end.
%when super is not running killing echo gives a badarg error in talk
%when either talk or echo are killed they are respawned and the communication
%resumes.
%killing super kills also both clients
%using timer:sleep/1 does not seem to make a difference
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment