supervisor可以等待子worker结束后销毁自身,但是有的情况下我们并不是所有的进程都挂载在supervisor下面,而是link互相关联
monitor(Type, Item) -> MonitorRef
Types:
The calling process starts monitoring Item which is an object of type Type.
Currently only processes can be monitored, i.e. the only allowed Type is process, but other types may be allowed in the future.
Item can be:
-
pid()
-
The pid of the process to monitor.
{RegName, Node}
-
A tuple consisting of a registered name of a process and a node name. The process residing on the node Node with the registered name RegName will be monitored.
RegName
-
The process locally registered as RegName will be monitored.
When a process is monitored by registered name, the process that has the registered name at the time when monitor/2 is called will be monitored. The monitor will not be effected, if the registered name is unregistered.
A 'DOWN' message will be sent to the monitoring process if Item dies, if Item does not exist, or if the connection is lost to the node which Item resides on. A 'DOWN' message has the following pattern:
{'DOWN', MonitorRef, Type, Object, Info}
where MonitorRef and Type are the same as described above, and:
-
Object
-
A reference to the monitored object:
- the pid of the monitored process, if Item was specified as a pid.
- {RegName, Node}, if Item was specified as {RegName, Node}.
- {RegName, Node}, if Item was specified as RegName. Node will in this case be the name of the local node (node()).
Info
-
Either the exit reason of the process, noproc (non-existing process), or noconnection (no connection to Node).
If/when monitor/2 is extended (e.g. to handle other item types than process), other possible values for Object, and Info in the 'DOWN' message will be introduced.
The monitoring is turned off either when the 'DOWN' message is sent, or when demonitor/1 is called.
If an attempt is made to monitor a process on an older node (where remote process monitoring is not implemented or one where remote process monitoring by registered name is not implemented), the call fails with badarg.
Making several calls to monitor/2 for the same Item is not an error; it results in as many, completely independent, monitorings.
The format of the 'DOWN' message changed in the 5.2 version of the emulator (OTP release R9B) for monitor by registered name. The Object element of the 'DOWN' message could in earlier versions sometimes be the pid of the monitored process and sometimes be the registered name. Now the Object element is always a tuple consisting of the registered name and the node name. Processes on new nodes (emulator version 5.2 or greater) will always get 'DOWN' messages on the new format even if they are monitoring processes on old nodes. Processes on old nodes will always get 'DOWN' messages on the old format.