process_flag(trap_exit, true)
在进程设置了process_flag(trap_exit, true)的进程会把收到的exit信号转换为{‘EXIT’, From, Reason} 发送到本进程的消息邮箱列表,在使用exit(PID, Flag)可向目标发送一个exit信号,或者link的进程挂掉,都会产生一个exit信号。
设置了trap_exti的进程,只能通过exit(PID, kill)杀死,或者进程本身发生了异常。
When trap_exit is set to true, exit signals arriving to a process are converted to {‘EXIT’, From, Reason} messages, which can be received as ordinary messages. If trap_exit is set to false, the process exits if it receives an exit signal other than normal and the exit signal is propagated to its linked processes. Application processes are normally not to trap exits.
不同情况下收到的消息
对比一下两个进程在monitor和link分别设置不同trap_exit 情况下会发生什么:
ProcessB monitor ProcessA
Action | exit(PidA, xxx) | exit(PidA, normal) | exit(PidA, kill) | PidA ! error | |
---|---|---|---|---|---|
trap_exit | recv action | ||||
ProcessA | false | dead | do-nothing | dead | dead, and ERROR_REPORT |
ProcessB | false | recv {’DOWN‘, Ref, process, PidA, xxx} | do-nothing | recv {’DOWN‘, Ref, process, PidA, killed} | recv {’DOWN‘, Ref, process, PidA, ErrorMsg} |
ProcessA | false | dead | do-nothing | dead | dead, and ERROR_REPORT |
ProcessB | true | recv {’DOWN‘, Ref, process, PidA, xxx} | do-nothing | recv {’DOWN‘, Ref, process, PidA, killed} | recv {’DOWN‘, Ref, process, PidA, ErrorMsg} |
ProcessA | true | recv {‘EXIT’, FromPid, xxx} | recv {‘EXIT’, FromPid, normal} | dead | dead, and ERROR_REPORT |
ProcessB | true | do-nothing | do-nothing | recv {’DOWN‘, Ref, process, PidA, killed} | recv {’DOWN‘, Ref, process, PidA, ErrorMsg} |
ProcessA | true | recv {‘EXIT’, FromPid, xxx} | recv {‘EXIT’, FromPid, normal} | dead | dead, and ERROR_REPORT |
ProcessB | false | do-nothing | do-nothing | recv {’DOWN‘, Ref, process, PidA, killed} | recv {’DOWN‘, Ref, process, PidA, ErrorMsg} |
ProcessB link ProcessA
Action | exit(PidA, xxx) | exit(PidA, normal) | exit(PidA, kill) | PidA ! error | |
---|---|---|---|---|---|
trap_exit | recv action | ||||
ProcessA | false | dead | do-nothing | dead | dead, and ERROR_REPORT |
ProcessB | false | dead | do-nothing | dead | dead |
ProcessA | false | dead | do-nothing | dead | dead, and ERROR_REPORT |
ProcessB | true | recv {‘EXIT’, PidA, xxx} | do-nothing | recv {‘EXIT’, PidA, killed} | recv {’EXIT‘, PidA, ErrorMsg} |
ProcessA | true | recv {‘EXIT’, FromPid, xxx} | recv {‘EXIT’, FromPid, normal} | dead | dead, and ERROR_REPORT |
ProcessB | true | do-nothing | do-nothing | recv {‘EXIT’, PidA, killed} | recv {’EXIT‘, PidA, ErrorMsg} |
ProcessA | true | recv {‘EXIT’, FromPid, xxx} | recv {‘EXIT’, FromPid, normal} | dead | dead, and ERROR_REPORT |
ProcessB | false | do-nothing | do-nothing | dead | dead |
测试程序
1 | -module(test). |