Created
August 11, 2020 09:50
-
-
Save adhalanay/2f72b26e5db8b3622c3a03e89cd7e281 to your computer and use it in GitHub Desktop.
Supervisor assignment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-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