谈谈erlang:exit/2
转载请注明,来自:http://blog.csdn.net/skyman_2001
有同学用erlang:exit(Pid, normal)来关闭Pid进程,其实这样Pid进程是不会自动退出的。官方文档上对erlang:exit/2的说明讲得很清楚:
Sends an exit signal with exit reason Reason to the processPid.
The following behavior apply if Reason is any term exceptnormal orkill:
If Pid is not trapping exits, Pid itself will exit with exit reasonReason. IfPid is trapping exits, the exit signal is transformed into a message{'EXIT', From, Reason} and delivered to the message queue ofPid.From is the pid of the process which sent the exit signal. See alsoprocess_flag/2.
If Reason is the atom normal,Pid will not exit. If it is trapping exits, the exit signal is transformed into a message{'EXIT', From, normal} and delivered to its message queue.
If Reason is the atom kill, that is ifexit(Pid, kill) is called, an untrappable exit signal is sent toPid which will unconditionally exit with exit reasonkilled.
还有就是如果是gen_server进程,最好不要直接用exit(),推荐这样:
stop(Pid) when is_pid(Pid) ->
gen_server:cast(Pid, stop).
handle_cast(stop, State) ->
{stop, normal, State};
注意:通过eixt(Pid,shutdown)或exit(Pid,kill)来终止gen_server进程,该gen_server进程是不会调用terminate()回调的!因为没捕捉退出消息。