Erlang进程:正常退出被链接进程忽略,在“退出”之后退出;“编辑两次”;

Erlang进程:正常退出被链接进程忽略,在“退出”之后退出;“编辑两次”;,erlang,Erlang,在Erlang中,我创建了两个相互链接的进程。如果我从一个进程退出,那么另一个进程会忽略它,如果我正常退出。这种行为可以在中看到记录 我的问题是,在两次编译相同的代码时,我可以看到第二个进程也退出了。以下是我的示例代码: -module(exitnormal). -export([f1/0]). f1() -> X = whereis(f2) =/= undefined, if X -> exit( whereis(f2), shutdown ),

在Erlang中,我创建了两个相互链接的进程。如果我从一个进程退出,那么另一个进程会忽略它,如果我正常退出。这种行为可以在中看到记录

我的问题是,在两次编译相同的代码时,我可以看到第二个进程也退出了。以下是我的示例代码:

-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> 
1>c(exitnormal)。
{好的,exitnormal}
2> erlang:寄存器(f1,spawn(exitnormal,f1,[]))。
真的
3> (f2)在哪里!杀了,好的。
好啊
4> 这里是(f2)。
未定义
5> (f1)在哪里。
6> c(exitnormal)。
{好的,exitnormal}
7> (f1)在哪里。
8> c(exitnormal)。
{好的,exitnormal}
9> (f1)在哪里。
未定义
10> erlang:寄存器(f1,spawn(exitnormal,f1,[]))。
真的
11> (f1)在哪里!杀了,好的。
好啊
12> (f1)在哪里。
未定义
13> 这里是(f2)。
14> c(exitnormal)。
{好的,exitnormal}
15> c(exitnormal)。
{好的,exitnormal}
16> 这里是(f2)。
未定义
17> 
如《Erlang参考手册》中所述,模块在运行系统中可以有两种变体:“当前”和“旧”。重新加载模块时,新版本变为“当前”版本,以前“当前”版本变为“旧”版本,以前“旧”版本被清除。当清除旧版本时,仍然使用该版本或引用旧版本中FUN的任何进程都将被终止。这就是为什么在重新加载代码两次后进程会被终止


即使模块在两个负载之间没有改变,也会发生这种情况;加载操作将无条件触发转换。

在编译之前,变体不会查找代码中的更改(我猜)。这就解释了进程的终止。我说得对吗。