linux进程退出的时exit,_exit区别和联系

进程退出exit,_exit区别即atexit函数

转自: http://www.cnblogs.com/mickole/p/3186606.html(最后五,六部分是自己的理解和原创)

一,进程终止有5种方式:

正常退出:

  • 从main函数返回
  • 调用exit
  • 调用_exit

异常退出:

  • 调用abort
  • 由信号终止

二,exit和_exit区别:

QQ截图20130712154916

关于_exit():

       #include <unistd.h>

       void _exit(int status);

       #include <stdlib.h>

       void _Exit(int status);

DESCRIPTION 
       The function _exit() terminates the calling process "immediately".  Any 
       open file descriptors belonging to the process are closed; any children 
       of the process are inherited by process 1, init, and the process’s par- 
       ent is sent a SIGCHLD signal.

       The value status is returned to the parent  process  as  the  process’s 
       exit  status,  and  can be collected using one of the wait(2) family of 
       calls.

       The function _Exit() is equivalent to _exit().

关于exit():

#include <stdlib.h>

void exit(int status);

DESCRIPTION 
       The  exit() function causes normal process termination and the value of 
       status & 0377 is returned to the parent (see wait(2)).

       All functions registered with atexit(3) and on_exit(3) are  called,  in 
       the  reverse  order  of their registration.
  (It is possible for one of 
       these functions to use atexit(3) or on_exit(3)  to  register  an  addi- 
       tional  function  to be executed during exit processing; the new regis- 
       tration is added to the front of the list of functions that  remain  to 
       be  called.) If one of these functions does not return (e.g., it calls 
       _exit(2), or kills itself with a signal), then none  of  the  remaining 
       functions is called, and further exit processing (in particular, flush- 
       ing of stdio(3) streams) is abandoned.
  If a function has  been  regis- 
       tered  multiple  times using atexit(3) or on_exit(3), then it is called 
       as many times as it was registered.

       All open stdio(3) streams are flushed and  closed.   Files  created  by 
       tmpfile(3) are removed.

       The  C standard specifies two constants, EXIT_SUCCESS and EXIT_FAILURE, 
       that may be passed to exit() to  indicate  successful  or  unsuccessful 
       termination, respectively. 

和exit比较一下,exit()函数定义在stdlib.h中,而_exit()定义在unistd.h中,

注:exit()就是退出,传入的参数是程序退出时的状态码,0表示正常退出,其他表示非正常退出,一般都用-1或者1,标准C里有EXIT_SUCCESS和EXIT_FAILURE两个宏,用exit(EXIT_SUCCESS);

_exit()函数的作用最为简单:直接使进程停止运行,清除其使用的内存空间,并销毁其在内核中的各种数据结构;exit() 函数则在这些基础上作了一些包装,在执行退出之前加了若干道工序。 
exit()函数与_exit()函数最大的区别就在于exit()函数在调用exit系统调用之前要检查文件的打开情况,把文件缓冲区中的内容写回文件,就是"清理I/O缓冲"。

exit()在结束调用它的进程之前,要进行如下步骤: 
1.调用atexit()注册的函数(出口函数);按ATEXIT注册时相反的顺序调用所有由它注册的函数,这使得我们可以指定在程序终止时执行自己的清理动作.例如,保存程序状态信息于某个文件,解开对共享数据库上的锁等.

2.cleanup();关闭所有打开的流,这将导致写所有被缓冲的输出,删除用TMPFILE函数建立的所有临时文件.

3.最后调用_exit()函数终止进程。

_exit做3件事(man): 
1,Any  open file descriptors belonging to the process are closed 
2,any children of the process are inherited  by process 1, init 
3,the process's parent is sent a SIGCHLD signal

exit执行完清理工作后就调用_exit来终止进程。

三,atexit()

atexit可以注册终止处理程序,ANSI C规定最多可以注册32个终止处理程序。

终止处理程序的调用与注册次序相反

       #include <stdlib.h>

       int atexit(void (*function)(void));

DESCRIPTION 
       The atexit() function registers the given function to be called at nor- 
       mal process termination, either via exit(3) or via return from the pro- 
       gram’s main(). Functions so registered are called in the reverse order 
       of their registration; no arguments are passed.

       The  same  function may be registered multiple times: it is called once 
       for each registration.

       POSIX.1-2001 requires that an implementation allow at least  ATEXIT_MAX 
       (32) such functions to be registered.  The actual limit supported by an 
       implementation can be obtained using sysconf(3).

       When a child process is created via fork(2), it inherits copies of  its 
       parent’s  registrations.
   Upon a successful call to one of the exec(3) 
       functions, all registrations are removed.

RETURN VALUE 
       The atexit() function returns the value 0 if successful;  otherwise  it 
       returns a non-zero value.

示例程序:

复制代码
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>

void fun1()
{
    printf("fun1 is called\n");
}

void fun2()
{
    printf("fun2 is called\n");
}

int main(void)
{
    printf("main function\n");
    atexit(fun1);
    atexit(fun2);
    atexit(fun1);
    exit(EXIT_SUCCESS);
}
复制代码

运行结果:

QQ截图20130712162502

 

当调用fork时,子进程继承父进程注册的atexit:

示例程序:

复制代码
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>

#define ERR_EXIT(m) \
    do\
    {\
        perror(m);\
        exit(EXIT_FAILURE);\
    }\
    while (0)\

void fun1()
{
    printf("fun1 is called\n");
}

void fun2()
{
    printf("fun2 is called\n");
}
int main(void)
{
    pid_t pid;
    pid = fork();
    atexit(fun1);
    atexit(fun2);
    atexit(fun1);
    if(pid == -1)
        ERR_EXIT("fork error");
    if(pid == 0){
        printf("this is child process\n");
    }
    if(pid > 0){
        printf("this is parent process\n");
    }

    return 0;
}
复制代码

运行结果:

QQ截图20130712163016

 

当atexit注册的函数中有一个没有正常返回或被kill,则后续的注册函数都不会被执行

示例程序:

复制代码
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <signal.h>

void fun1()
{
    printf("fun1 is called\n");
}

void fun2()
{
    printf("fun2 is called\n");
    kill(getpid(),SIGINT);
}

int main(void)
{
    printf("main function\n");
    if(signal(SIGINT,SIG_DFL) == SIG_ERR){
        perror("signal error");
        exit(EXIT_FAILURE);
    }
    atexit(fun1);
    atexit(fun2);
    atexit(fun1);
    exit(EXIT_SUCCESS);
}
复制代码

运行结果:

QQ截图20130712164236

可见最后那个fun1没有执行

四、abort意义

abort 产生 SIGABRT 信号。非正常退出,即在程序碰到灾难性错误时强制退出。由于是非正常退出,因此不会做其它任何操作。

五、进程和函数结束的总结

1、atexit 是在执行 exit 函数时设置要作的工作,实际就是调用要在进程退出想让程序执行的函数。

其参数是要调用的函数地址。参数函数是一个无参数无返回值的函数。atexit可以登记32个函数,这些函数由 exit 函数自动调用,登记的顺序和调用的顺序相反,即最后登记的先执行。同一函数登记多次也会调用多次。

 

2、exit :是一个C库标准函数。此函数执行会首先调用由 atexit注册的函数,然后执行关闭所有标准IO流,刷新流缓冲区等操作。

 

3、_exit 是一个系统调用。此函数不会调用 atexit 注册的函数,也不会运行信号处理程序。对标准IO流的缓冲区是否进行刷新取决于该函数在系统中的实现。一般UNIX下不会刷新。 exit函数会调用此函数。(在windows下的_exit函数会对标准IO流的缓冲进行刷新)

_Exit 是一个C库标准函数。其动作类似 _exit 

4、非main函数在使用return返回的时候表示函数调用栈的返回,return是语言级别的。而exit是系统调用级别的,它表示了一个进程的结束exit函数是退出应用程序,并将应用程序的一个状态返回给OS,这个状态标识了应用程序的一些运行信息。main函数里面return(0)exit(0)是一样的,子函数用return返回;而子进程用exit退出,调用exit时要调用一段终止处理程序,然后关闭所有I/O流。

   在进程操作中exit是结束当前进程或程序并把控制权返回给调用该程序或者进程的进程即父进程并告诉父进程该当前进程的运行状态,而return是从当前函数返回,如果是在main函数中,main函数结束时隐式地调用exit函数,自然也就结束了当前进程。

六、总结

exit_exit函数用于正常终止一个程序: _exit立即进入内核, exit则先执行一些清除处理(包括调用执行各终止处理程序,关闭所有标准O流等),然后进入内核。函数的返回的进程的退出之间有很大的不同,因此我们在使用的时候需要区分和深刻的理解。


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值