Chinaunix首页 | 论坛 | 博客
  • 博客访问: 846506
  • 博文数量: 372
  • 博客积分: 10063
  • 博客等级: 中将
  • 技术积分: 4220
  • 用 户 组: 普通用户
  • 注册时间: 2012-02-24 11:36
文章分类

全部博文(372)

文章存档

2012年(372)

分类: 云计算

2012-02-24 15:16:01

转载请注明,来自:http://blog.csdn.net/skyman_2001

一、什么是Special Process

请看文档说明:

Special Process is a process which comply to the OTP design principles. Such a process should:

  • be started in a way that makes the process fit into a supervision tree,
  • support the sys, and
  • take care of .

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_servergen_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 normalshutdown, 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模块的相关代码:

[plain] view plaincopy
  1. exit_p(Class, Reason) ->  
  2.     case get('$initial_call') of  
  3.     {M,F,A} when is_atom(M), is_atom(F), is_integer(A) ->  
  4.         MFA = {M,F,make_dummy_args(A, [])},  
  5.         crash_report(Class, Reason, MFA),  
  6.         exit(Reason);  
  7.     _ ->  
  8.         %% The process dictionary has been cleared or  
  9.         %% possibly modified.  
  10.         crash_report(Class, Reason, []),  
  11.         exit(Reason)  
  12.     end.  

  1. %% -----------------------------------------------------  
  2. %% Generate a crash report.  
  3. %% -----------------------------------------------------  
  4.   
  5. crash_report(exit, normal, _)       -> ok;  
  6. crash_report(exit, shutdown, _)     -> ok;  
  7. crash_report(exit, {shutdown,_}, _) -> ok;  
  8. crash_report(Class, Reason, StartF) ->  
  9.     OwnReport = my_info(Class, Reason, StartF),  
  10.     LinkReport = linked_info(self()),  
  11.     Rep = [OwnReport,LinkReport],  
  12.     error_logger:error_report(crash_report, Rep).  

可以看出:如果退出原因是normal, shutdown和{shutdown,_},则不生成crash report,其他退出原因会产生crash report(由error_logger:error_report生成)。

四、关于exit(Reason)

停止当前进程的执行,退出原因是Reason。该函数导致进程终止,所以无返回值。

[plain] view plaincopy
  1. > exit(foobar).  
  2. ** exception exit: foobar  
  3. > catch exit(foobar).  
  4. {'EXIT',foobar}  

关于trap_exit的相关说明,请猛击:http://blog.csdn.net/skyman_2001/article/details/6952894

阅读(665) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~