全部博文(372)
2012年(372)
分类: 云计算
2012-02-24 15:16:01
请看文档说明:
Special Process is a process which comply to the OTP design principles. Such a process should:
System messages are messages with special meaning, used in the supervision tree. Typical system messages are requests for trace output, and requests to suspend or resume process execution (used during release handling). Processes implemented using standard behaviours automatically understand these messages.
特殊进程是遵循OTP设计原则的进程,具有以下特性:
(1) 该进程启动后能被加到监控树里;
(2) 支持sys的调试设施;
(3) 能处理系统消息。
系统消息是用于监控树的有特殊含义的消息。典型的系统消息有trace输出、挂起和恢复进程执行(用于发布更新)。用标准behavior实现的进程自动能够处理这些消息。
特殊进程可以使用sys和proc_lib模块来实现。
二、proc_lib模块
看文档说明:
This module is used to start processes adhering to the . Specifically, the functions in this module are used by the OTP standard behaviors (gen_server, gen_fsm, ...) when starting new processes. The functions can also be used to start special processes, user defined processes which comply to the OTP design principles. See in OTP Design Principles for an example.
Some useful information is initialized when a process starts. The registered names, or the process identifiers, of the parent process, and the parent ancestors, are stored together with information about the function initially called in the process.
该模块用于启动符合OTP设计原则的进程。特别地,OTP的标准behavior(gen_server, gen_fsm等)使用该模块的函数来启动新进程。这些函数也可用于启动特殊进程。进程启动时会初始化一些有用的信息:
{dictionary,[{'$ancestors',[kernel_sup,<0.9.0>]},
{'$initial_call',{kernel_config,init,1}}]},
三、特殊进程的退出处理
先看文档说明:
While in "plain Erlang" a process is said to terminate normally only for the exit reason normal, a process started using proc_lib is also said to terminate normally if it exits with reason shutdown or {shutdown,Term}. shutdown is the reason used when an application (supervision tree) is stopped.
When a process started using proc_lib terminates abnormally -- that is, with another exit reason than normal, shutdown, or {shutdown,Term} -- a crash report is generated, which is written to terminal by the default SASL event handler. That is, the crash report is normally only visible if the SASL application is started.
The crash report contains the previously stored information such as ancestors and initial function, the termination reason, and information regarding other processes which terminate as a result of this process terminating.
普通进程只有退出原因为normal时才被认为是正常退出,而由proc_lib启动的特殊进程除了退出原因为normal,为shutdown或{shutdown,Term}时也被认为是正常退出,shutdown是一个application(监控树)终止时引起。
其他退出原因都被认为是异常退出,这时会产生crash report(由默认的SASL事件处理器写到终端,即crash report一般在SASL application启动了才能显示)。
crash report包含之前存储的信息,比如ancestors、initial function、终止原因,以及由该进程终止而跟着终止的其他进程的相关信息。
直接看proc_lib模块的相关代码:
四、关于exit(Reason)
停止当前进程的执行,退出原因是Reason。该函数导致进程终止,所以无返回值。
关于trap_exit的相关说明,请猛击:http://blog.csdn.net/skyman_2001/article/details/6952894