Before we go into details of the supervision and error handling in an Erlang system, we need see how Erlang processes terminate, or in Erlang terminology, exit. html
咱們先來看看Erlang進程如何結束,或者說先來研究一下Erlang的關鍵字: exit. ide
A process which executes exit(normal) or simply runs out of things to do has a normal exit. 函數
當進程經過執行exit(normal)或者進程全部代碼都執行完成時,進程正常結束。 this
A process which encounters a runtime error (e.g. divide by zero, bad match, trying to call a function which doesn't exist etc) exits with an error, i.e. has an abnormal exit. A process which executes exit(Reason) where Reason is any Erlang term except the atom normal, also has an abnormal exit. atom
當進程遇到運行時錯誤(好比除0,非法匹配或者試圖調用一個不存在的函數)會終止併產生一個錯誤,也就是說進程非正常終止。當進程經過執行exit(Reason),Reasons是指除了normal的其餘任何Erlang term,也會使進程非正常終止。 spa
An Erlang process can set up links to other Erlang processes. If a process calls link(Other_Pid) it sets up a bidirectional link between itself and the process called Other_Pid. When a process terminates, it sends something called a signal to all the processes it has links to. code
Erlang進程能夠鏈接到其餘的Erlang進程。若是一個進程調用了link(Other_Pid)函數,這個進程就建立了一個它本身與進程Other_Pid之間的一個雙向鏈接。當一個進程結束,它會發送一個信號給鏈接到它的進程。 orm
The signal carries information about the pid it was sent from and the exit reason. htm
信號裏面包含發送進程的PID以及終止緣由。 隊列
The default behaviour of a process which receives a normal exit is to ignore the signal.
當一個進程收到一個正常終止信號時,它的默認處理方式是忽略這個信號。
The default behaviour in the two other cases (i.e. abnormal exit) above is to bypass all messages to the receiving process and to kill it and to propagate the same error signal to the killed process' links. In this way you can connect all processes in a transaction together using links and if one of the processes exits abnormally, all the processes in the transaction will be killed. As we often want to create a process and link to it at the same time, there is a special BIF, spawn_link which does the same as spawn, but also creates a link to the spawned process.
當一個進程收到一個非正常終止信號時,他的默認處理方式是忽略全部其餘消息而且殺掉本身,而後發送一個一樣的終止信號給鏈接到它的其餘進程。(也就是玉石俱焚!!多浪漫的基情 :P)經過這種方式,你可使用link把一個事務裏面的全部進程鏈接在一塊兒,這樣的話,當他們中有一個進程非正常終止,事務中的全部其餘進程都會被殺掉。因爲咱們常常須要在建立一個進程的同時鏈接到它,因此Erlang提供了一個內置函數spawn_link,他的功能基本與spawn相同,惟一不一樣的是會建立一個鏈接到spawn出來的進程。
It is possible to modify the default behaviour of a process so that it does not get killed when it receives abnormal exit signals, but all signals will be turned into normal messages on the format {'EXIT',FromPID,Reason} and added to the end of the receiving processes message queue. This behaviour is set by:
process_flag(trap_exit, true)
爲了讓進程收到非正常終止信號時不殺掉本身,咱們有必要改變進程的默認處理方式。經過調用process_flag(trap_exit, true)函數,全部的異常終止信號都會被轉換成普通的消息,消息格式爲:{'EXIT',FromPID,Reason},這些消息會被添加到接收進程的消息隊列。
There are several other process flags, see erlang(3). Changing the default behaviour of a process in this way is usually not done in standard user programs, but is left to the supervisory programs in OTP (but that's another tutorial).
經過這種方式改變進程的默認處理方式通常不會出如今標準的用戶程序當中,這種方式常常使用的地方是在OPT的監督程序當中。