Erlang process monitoring: link and monitor

Erlang was originally developed as a language for telecommunications products, and because of this purpose, her strict requirements for error handling were determined. In addition to providing syntax such as exception and try catch, Erlang also supports two mechanisms for monitoring processes, Link and Monitor, so that all processes can be connected to form a whole. When a process exits with an error, other processes will receive a message notification that the process exits. With these features, it is not difficult to build a simple and robust system using erlang.

Process two-way monitoring-Link

The link method can establish a two-way link relationship between processes. When one of the processes exits, the other process will receive a message that the process has exited.

Example 1:

[plain]  view plain copy
  1. -module(test).  
  2. -export([start/0]).  
  3.   
  4. start() ->  
  5.     Pid = spawn(fun() ->loop() end),  
  6.     Pid2 = spawn(fun() ->loop_link(Pid) end),  
  7.     io:format("Pid ~p~nPid2 ~p~n", [Pid,Pid2]).  
  8.   
  9. loop_link(Pid) ->  
  10.     process_flag(trap_exit, true),  
  11.     erlang:link(Pid),  
  12.     receive  
  13.         Msg ->  
  14.             io:format("pid exit: ~p~n", [Msg])  
  15.     end.  
  16.   
  17. loop() ->  
  18.     process_flag(trap_exit, true),  
  19.     receive  
  20.         Msg ->  
  21.             io:format("pid2 exit: ~p~n", [Msg])  
  22.     end.  

Run the code:

[plain]  view plain copy
  1. 1> test:start().  
  2. Pid <0.63.0>  
  3. Pid2 <0.64.0>  
  4. ok  
  5. %% 杀掉Pid进程,进程Pid2收到通知  
  6. 2> exit(pid(0,63,0),kill).  
  7. pid exit: {'EXIT',<0.63.0>,killed}  
  8. true  
  9.   
  10. 3> test:start().  
  11. Pid <0.67.0>  
  12. Pid2 <0.68.0>  
  13. ok  
  14. %% 杀掉Pid2进程,进程Pid收到通知  
  15. 4> exit(pid(0,68,0),kill).  
  16. pid2 exit: {'EXIT',<0.68.0>,killed}  
  17. true  

注:erlang进程默认不捕捉Exit信号,可以使用process_flag(trap_exit, true)改变这一默认行为。

注2:解除link监控用erlang:unlink(Pid)

进程单向监控-Monitor

Monitor方式则实现进程的单向监控,当被监控进程退出时,监控进程会收到该进程退出的消息。

例子2:

[plain]  view plain copy
  1. -module(test).  
  2.   
  3. -export([start/0]).  
  4.   
  5. start() ->  
  6.     Pid = spawn(fun() ->loop() end),  
  7.     Pid3 = spawn(fun() ->loop_monitor(Pid) end),  
  8.     io:format("Pid ~p~nPid3 ~p~n", [Pid,Pid3]).  
  9.       
  10. loop_monitor(Pid) ->  
  11.     _MonitorRef = erlang:monitor(process, Pid),  
  12.     receive  
  13.         Msg ->  
  14.             io:format("pid exit: ~p~n", [Msg])  
  15.     end.  
  16.   
  17. loop() ->  
  18.     receive  
  19.         Msg ->  
  20.             io:format("pid3 exit: ~p~n", [Msg])  
  21.     end.      

Run the code:

[plain]  view plain copy
  1. 1> test:start().  
  2. Pid <0.39.0>  
  3. Pid3 <0.40.0>  
  4. ok  
  5. %% Kill the Pid process, the process Pid3 receives the notification  
  6. 2> exit(pid(0,39,0),kill).  
  7. pid exit: {'DOWN',#Ref<0.0.0.80>,process,<0.39.0>,killed}  
  8. true  
  9.   
  10. 3> test:start().  
  11. Pid <0.43.0>  
  12. Pid3 <0.44.0>  
  13. ok  
  14. %% kill the Pid3 process, the process Pid does not receive notification  
  15. 4> exit(pid(0,44,0),kill).  
  16. true  

Note: Use erlang:demonitor(MonitorRef) to remove monitor monitoring

Erlang will not issue a process exit notification if the process exits in the normal way

 

[plain]  view plain copy
  1. 10> exit(pid(0,70,0), normal).  
  2. true  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324484878&siteId=291194637