【发布时间】:2014-09-04 10:46:21
【问题描述】:
在 Erlang 中,我创建了 2 个相互关联的进程。如果我从一个进程退出,那么如果正常退出,另一个进程会忽略它。这种行为可以在a link 中看到。
我的问题是,在两次编译相同的代码时,我可以看到第二个进程也退出了。这是我的示例代码:
-module(exitnormal).
-export([f1/0]).
f1() ->
X = whereis(f2) =/= undefined,
if
X -> exit( whereis(f2), shutdown ), timer:sleep(1);
true -> ""
end,
register( f2, spawn_link( fun() -> f2() end )),
receive
kill -> { ok, f1 }
end.
f2() ->
receive
kill -> { ok, f2 }
end.
我运行它得到以下结果:
1> c(exitnormal).
{ok,exitnormal}
2> erlang:register( f1, spawn( exitnormal, f1, [] )).
true
3> whereis(f2) ! kill, ok.
ok
4> whereis(f2).
undefined
5> whereis(f1).
<0.40.0>
6> c(exitnormal).
{ok,exitnormal}
7> whereis(f1).
<0.40.0>
8> c(exitnormal).
{ok,exitnormal}
9> whereis(f1).
undefined
10> erlang:register( f1, spawn( exitnormal, f1, [] )).
true
11> whereis(f1) ! kill, ok.
ok
12> whereis(f1).
undefined
13> whereis(f2).
<0.59.0>
14> c(exitnormal).
{ok,exitnormal}
15> c(exitnormal).
{ok,exitnormal}
16> whereis(f2).
undefined
17>
【问题讨论】:
标签: erlang