exit() abort() 区别

  exit()函数结束程序,返回一个值给操作系统,告知程序的最后状态。在调用exit()函数之后,控制权会移交给操作系统。 在结束程序之前,exit()函数会调用之前使用atexit()注册过的所有函数,按照LIFO次序调用,关闭所有打开的文件,删除tmpfile()函数建立的所有临时文件。
  abort()函数会发出一个SIGABRT信号来终止程序的执行。不会调用之前用atexit()函数注册的清理函数。至于会不会清除缓冲区,这个与系统相关。如以下实现则清空了。但没有关闭。进程结束它自己会关闭。
(ubuntu,centOS测试会清空缓冲区。)
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

void
abort(void)			/* POSIX-style abort() function */
{
	sigset_t			mask;
	struct sigaction	action;

	/*
	 * Caller can't ignore SIGABRT, if so reset to default.
	 */
	sigaction(SIGABRT, NULL, &action);
	if (action.sa_handler == SIG_IGN) {
		action.sa_handler = SIG_DFL;
		sigaction(SIGABRT, &action, NULL);
	}
	if (action.sa_handler == SIG_DFL)
		fflush(NULL);			/* flush all open stdio streams */

	/*
	 * Caller can't block SIGABRT; make sure it's unblocked.
	 */
	sigfillset(&mask);
	sigdelset(&mask, SIGABRT);	/* mask has only SIGABRT turned off */
	sigprocmask(SIG_SETMASK, &mask, NULL);
	kill(getpid(), SIGABRT);	/* send the signal */

	/*
	 * If we're here, process caught SIGABRT and returned.
	 */
	fflush(NULL);				/* flush all open stdio streams */
	action.sa_handler = SIG_DFL;
	sigaction(SIGABRT, &action, NULL);	/* reset to default */
	sigprocmask(SIG_SETMASK, &mask, NULL);	/* just in case ... */
	kill(getpid(), SIGABRT);				/* and one more time */
	exit(1);	/* this should never be executed ... */
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值