说说Erlang的Special Process和SASL Error Logging

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

一、什么是Special Process

请看文档说明:

Special Processisa 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 theOTP Design Principles. 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 startspecial processes, user defined processes which comply to the OTP design principles. SeeSys and Proc_Libin 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 reasonnormal, a process started usingproc_libis also said to terminate normally if it exits with reasonshutdownor{shutdown,Term}.shutdownis the reason used when an application (supervision tree) is stopped.

When a process started usingproc_libterminates abnormally -- that is, with another exit reason thannormal,shutdown, or{shutdown,Term}-- acrash reportis 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_p(Class, Reason) ->
    case get('$initial_call') of
	{M,F,A} when is_atom(M), is_atom(F), is_integer(A) ->
	    MFA = {M,F,make_dummy_args(A, [])},
	    crash_report(Class, Reason, MFA),
	    exit(Reason);
	_ ->
	    %% The process dictionary has been cleared or
	    %% possibly modified.
	    crash_report(Class, Reason, []),
	    exit(Reason)
    end.

%% -----------------------------------------------------
%% Generate a crash report.
%% -----------------------------------------------------

crash_report(exit, normal, _)       -> ok;
crash_report(exit, shutdown, _)     -> ok;
crash_report(exit, {shutdown,_}, _) -> ok;
crash_report(Class, Reason, StartF) ->
    OwnReport = my_info(Class, Reason, StartF),
    LinkReport = linked_info(self()),
    Rep = [OwnReport,LinkReport],
    error_logger:error_report(crash_report, Rep).

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

四、关于exit(Reason)

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

> exit(foobar).
** exception exit: foobar
> catch exit(foobar).
{'EXIT',foobar}

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



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值