Erlang Process monitoring: Link and monitor

Source: Internet
Author: User
Tags error handling try catch

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
  1. -module (Test).
  2. -export ([start/0]).
  3. Start ()
  4. Pid = Spawn (Fun ()->loop () end),
  5. Pid2 = Spawn (Fun ()->loop_link (Pid) end),
  6. Io:format ("Pid ~p~npid2 ~p~n", [Pid,pid2]).
  7. Loop_link (Pid)
  8. Process_flag (Trap_exit, True),
  9. Erlang:link (Pid),
  10. Receive
  11. MSG-
  12. Io:format ("pid exit: ~p~n", [MSG])
  13. End.
  14. Loop ()
  15. Process_flag (Trap_exit, True),
  16. Receive
  17. MSG-
  18. Io:format ("Pid2 exit: ~p~n", [MSG])
  19. End.

To run the code:

[Plain]View Plaincopy < param name= "wmode" value= "Transparent" >
    1. 1> Test:start ().
    2. Pid <0.63.0>
    3. Pid2 <0.64.0>
    4. Ok
    5. Percent kill PID process, process PID2 receive notification
    6. 2> exit (PID (0,63,0), kill).
    7. PID exit: {' exit ', <0.63.0>,killed}
    8. True
    9. 3> Test:start ().
    10. Pid <0.67.0>
    11. Pid2 <0.68.0>
    12. Ok
    13. Percent kill PID2 process, process PID receive notification
    14. 4> exit (PID (0,68,0), kill).
    15. Pid2 exit: {' exit ', <0.68.0>,killed}
    16. 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
  1. -module (Test).
  2. -export ([start/0]).
  3. Start ()
  4. Pid = Spawn (Fun ()->loop () end),
  5. Pid3 = Spawn (Fun ()->loop_monitor (Pid) end),
  6. Io:format ("Pid ~p~npid3 ~p~n", [PID,PID3]).
  7. Loop_monitor (Pid)
  8. _monitorref = Erlang:monitor (process, Pid),
  9. Receive
  10. MSG-
  11. Io:format ("pid exit: ~p~n", [MSG])
  12. End.
  13. Loop ()
  14. Receive
  15. MSG-
  16. Io:format ("Pid3 exit: ~p~n", [MSG])
  17. End.

To run the code:

[Plain]View Plaincopy < param name= "wmode" value= "Transparent" >
    1. 1> Test:start ().
    2. Pid <0.39.0>
    3. PID3 <0.40.0>
    4. Ok
    5. Percent kill PID process, process PID3 receive 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. 3> Test:start ().
    10. Pid <0.43.0>
    11. PID3 <0.44.0>
    12. Ok
    13. Percent kill PID3 process, process PID not received notification
    14. 4> exit (PID (0,44,0), kill).
    15. 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
    1. 10> exit (PID (0,70,0), normal).
    2. True

Transferred from: http://blog.csdn.net/mycwq/article/details/13171117

Erlang Process monitoring: Link and monitor

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.