atexit函数 exit 和_exit 的区别

来源:互联网 发布:空手道数据集 编辑:程序博客网 时间:2024/05/22 14:01

进程退出方式:

1.正常结束:

1)main  函数结束;

2)exit(3) 结束:调用atexit (3) ,  on_exit  (3) 函数.    

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

3)_exit(2) 结束:不会调用atexit  on_exit函数

 The function _exit() terminates the calling process "immediately".  Any open file descriptors belong‐
       ing to the process are closed; any children of the process are inherited by process 1, init, and  the
       process's parent is sent a SIGCHLD signal.

2.非正常结束:

1)信号结束:

2)abort函数:实际上也是给发送一个SIGABRT信号.


atexit函数的测试代码:


#include <iostream>#include <stdlib.h>using namespace std;void atexit1(){    cout <<  "in 1" << endl;}void atexit2(){    cout <<  "in 2" << endl;}int main(){    atexit(atexit1);    atexit(atexit2);    cout << "before end" << endl;    return -1;}
 ./testretval
before end
in 2
in 1



原创粉丝点击