晚上快速的实验了一下Erlang的monitor函数,实验中分两种情况,一种是被监控的进程自然消亡,另一种是被监控的进程异常退出。实验结论是两种情况下,负责监控的进程都可以收到DOWN信号。
下面是实验代码:
-module(monitor_test).
-export([run/0]).
-export([master_main/0, child_main/0]).
run() ->
{ok, Master, Child1, Child2} = proc_lib:start_link(?MODULE, master_main, []),
Master ! go,
Child1 ! normal,
Child2 ! exception,
ok.
master_main() ->
Child1 = spawn(?MODULE, child_main, []),
Child2 = spawn(?MODULE, child_main, []),
monitor(process, Child1),
monitor(process, Child2),
proc_lib:init_ack({ok, self(), Child1, Child2}),
wait(0).
wait(0) ->
receive
go -> wait(1)
end;
wait(3) ->
ok;
wait(C) ->
receive
{'DOWN', _, process, Pid, Info} ->
io:format("~p DOWN: ~p~n", [Pid, Info]),
wait(C + 1)
end.
child_main() ->
receive
normal -> ok;
exception -> true = filelib:is_file("no_this_file")
end.
原标签已从 2023-12-16 存档快照 恢复。本文按原站快照恢复排版,并接回已核实的本地图片。查看恢复说明