atexit函数 exit 和_exit 的区别

本文详细介绍了进程的两种主要退出方式:正常结束与非正常结束。正常结束包括main函数结束、通过exit(3)函数结束及使用_exit(2)函数结束等三种情况,并解释了各自的特点。非正常结束则涉及到了通过信号结束以及调用abort函数结束进程的方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

进程退出方式:

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



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值