Erlang was the first language to be developed for telecom products, and because of that, it decided her strict requirements for error handling. In addition to providing syntax such as Exception,try catch, Erlang also supports the mechanism of link and monitor two monitoring processes so that all processes can be joined together to form a whole. When a process exits with an error, the other process receives a message notification that the process exits. With these features, it is not difficult to build a simple, robust system using Erlang.
Process bidirectional monitoring-link
The link method establishes a two-way link relationship between processes, and when one of the processes exits, another process receives a message that the process exits.
Example 1:
[Plain]View Plaincopy
- -module (Test).
- -export ([start/0]).
- Start ()
- Pid = Spawn (Fun ()->loop () end),
- Pid2 = Spawn (Fun ()->loop_link (Pid) end),
- Io:format ("Pid ~p~npid2 ~p~n", [Pid,pid2]).
- Loop_link (Pid)
- Process_flag (Trap_exit, True),
- Erlang:link (Pid),
- Receive
- MSG-
- Io:format ("pid exit: ~p~n", [MSG])
- End.
- Loop ()
- Process_flag (Trap_exit, True),
- Receive
- MSG-
- Io:format ("Pid2 exit: ~p~n", [MSG])
- End.
To run the code:
[Plain]View Plaincopy < param name= "wmode" value= "Transparent" >
- 1> Test:start ().
- Pid <0.63.0>
- Pid2 <0.64.0>
- Ok
- Percent kill PID process, process PID2 receive notification
- 2> exit (PID (0,63,0), kill).
- PID exit: {' exit ', <0.63.0>,killed}
- True
- 3> Test:start ().
- Pid <0.67.0>
- Pid2 <0.68.0>
- Ok
- Percent kill PID2 process, process PID receive notification
- 4> exit (PID (0,68,0), kill).
- Pid2 exit: {' exit ', <0.68.0>,killed}
- True
Note: The Erlang process does not capture the exit signal by default, and you can use Process_flag (Trap_exit, true) to change this default behavior.
Note 2: Remove link monitoring with Erlang:unlink (PID)
Process one-way monitoring-monitor
The monitor mode realizes one-way monitoring of the process, and when the monitored process exits, the monitoring process receives a message that the process exits.
Example 2:
[Plain]View Plaincopy
- -module (Test).
- -export ([start/0]).
- Start ()
- Pid = Spawn (Fun ()->loop () end),
- Pid3 = Spawn (Fun ()->loop_monitor (Pid) end),
- Io:format ("Pid ~p~npid3 ~p~n", [PID,PID3]).
- Loop_monitor (Pid)
- _monitorref = Erlang:monitor (process, Pid),
- Receive
- MSG-
- Io:format ("pid exit: ~p~n", [MSG])
- End.
- Loop ()
- Receive
- MSG-
- Io:format ("Pid3 exit: ~p~n", [MSG])
- End.
To run the code:
[Plain]View Plaincopy < param name= "wmode" value= "Transparent" >
- 1> Test:start ().
- Pid <0.39.0>
- PID3 <0.40.0>
- Ok
- Percent kill PID process, process PID3 receive notification
- 2> exit (PID (0,39,0), kill).
- PID exit: {' Down ', #Ref <0.0.0.80>,process,<0.39.0>,killed}
- True
- 3> Test:start ().
- Pid <0.43.0>
- PID3 <0.44.0>
- Ok
- Percent kill PID3 process, process PID not received notification
- 4> exit (PID (0,44,0), kill).
- True
Note: Remove monitor monitoring with Erlang:demonitor (MONITORREF)
If the process exits as normal, Erlang will not issue a process exit notification
[Plain]View Plaincopy
- 10> exit (PID (0,70,0), normal).
- True
Transferred from: http://blog.csdn.net/mycwq/article/details/13171117
Erlang Process monitoring: Link and monitor