Erang trap_exit的简单说明

引用

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.

这里说明了当trap_exit即捕捉退出信号的设置情况,①process_flag(trap_exit,true),退出信号到达一个进程时会被当做普通消息,转换成{‘EXIT’, From, Reason}这种格式。②process_flag(trap_exit,false),如果进程收到除了normal以外的退出信号,信号会传播给与它相连的进程(通过link相连)。应用进程通常不捕捉退出。

  • Sends an exit signal with exit reason Reason to the process or port identified by Pid.The following behavior applies if Reason is any term, except normal or kill:
  1. If Pid is not trapping exits, Pid itself exits with exit reason Reason.
  2. If Pid is trapping exits, the exit signal is transformed into a message {‘EXIT’, From, Reason} and delivered to the message queue of Pid.
  3. From is the process identifier of the process that sent the exit signal.
  • If Reason is the atom normal, Pid does 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, if exit(Pid, kill) is called, an untrappable exit signal is sent to Pid, which unconditionally exits with exit reason killed.
    上述说到了使用Erlang BIFs的exit/2函数,它会给进程发送一个以Reason为由的退出信号。如果退出理由是除了normal或kill以外,会出现以下的情形:
  1. 如果进程不设置捕捉退出信号,不经过terminate直接以Reason为由退出。
  2. 如果进程设置了捕捉推出信号,退出信号会被转换成{‘EXIT’, From, Reason}的消息并投送到进程的消息队列中。
  3. From是指发送推出信号的进程pid,通常是由behaviour执行。
  • 如果原因Reason是原子normal,Pid不会退出。如果设置了捕捉信号,退出信号会被转换成{‘EXIT’, From, normal}消息并投送到进程的消息队列中。
  • 如果原子Reason是原子kill,即如果exit(Pid,kill)被调用,无法被捕捉的退出信号会被发送目标进程,使得该进程以killed为由无条件退出。

你可能感兴趣的:(erlang)