diff options
author | Trygve Laugstøl <trygvis@inamo.no> | 2024-02-23 07:08:18 +0100 |
---|---|---|
committer | Trygve Laugstøl <trygvis@inamo.no> | 2024-02-23 07:08:18 +0100 |
commit | 5a9cdd3cc89507d4d74f8bded56ce5e037b3b56e (patch) | |
tree | 982ca2e7f9ac4e8c350dfb5c4f60bcfdfff5afaf /learn-you-some-erlang/linkmon.erl | |
parent | 05ae56e5e89abf2993f84e6d52b250131f247c35 (diff) | |
download | erlang-workshop-5a9cdd3cc89507d4d74f8bded56ce5e037b3b56e.tar.gz erlang-workshop-5a9cdd3cc89507d4d74f8bded56ce5e037b3b56e.tar.bz2 erlang-workshop-5a9cdd3cc89507d4d74f8bded56ce5e037b3b56e.tar.xz erlang-workshop-5a9cdd3cc89507d4d74f8bded56ce5e037b3b56e.zip |
wip
Diffstat (limited to 'learn-you-some-erlang/linkmon.erl')
-rw-r--r-- | learn-you-some-erlang/linkmon.erl | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/learn-you-some-erlang/linkmon.erl b/learn-you-some-erlang/linkmon.erl new file mode 100644 index 0000000..f93e9e5 --- /dev/null +++ b/learn-you-some-erlang/linkmon.erl @@ -0,0 +1,83 @@ +-module(linkmon). +-compile([export_all]). + +myproc() -> + timer:sleep(5000), + exit(reason). + +chain(0) -> + receive + _ -> ok + after 2000 -> + exit("chain dies here") + end; +chain(N) -> + Pid = spawn(fun() -> chain(N-1) end), + link(Pid), + receive + _ -> ok + end. + +start_critic() -> + spawn(?MODULE, critic, []). + +judge(Pid, Band, Album) -> + Pid ! {self(), {Band, Album}}, + receive + {Pid, Criticism} -> Criticism + after 2000 -> + timeout + end. + +critic() -> + receive + {From, {"Rage Against the Turing Machine", "Unit Testify"}} -> + From ! {self(), "They are great!"}; + {From, {"System of a Downtime", "Memoize"}} -> + From ! {self(), "They're not Johnny Crash but they're good."}; + {From, {"Johnny Crash", "The Token Ring of Fire"}} -> + From ! {self(), "Simply incredible."}; + {From, {_Band, _Album}} -> + From ! {self(), "They are terrible!"} + end, + critic(). + + +start_critic2() -> + spawn(?MODULE, restarter, []). + +restarter() -> + process_flag(trap_exit, true), + Pid = spawn_link(?MODULE, critic2, []), + register(critic, Pid), + receive + {'EXIT', Pid, normal} -> % not a crash + ok; + {'EXIT', Pid, shutdown} -> % manual shutdown, not a crash + ok; + {'EXIT', Pid, _} -> + restarter() + end. + +judge2(Band, Album) -> + Ref = make_ref(), + critic ! {self(), Ref, {Band, Album}}, + receive + {Ref, Criticism} -> Criticism + after 2000 -> + timeout + end. + +critic2() -> + receive + {From, Ref, {"Rage Against the Turing Machine", "Unit Testify"}} -> + From ! {Ref, "They are great!"}; + {From, Ref, {"System of a Downtime", "Memoize"}} -> + From ! {Ref, "They're not Johnny Crash but they're good."}; + {From, Ref, {"Johnny Crash", "The Token Ring of Fire"}} -> + From ! {Ref, "Simply incredible."}; + {From, Ref, {_Band, _Album}} -> + From ! {Ref, "They are terrible!"} + end, + critic2(). + |