您的位置:首页 > 其它

进程退出(exit)处理函数 atexit

2011-11-22 18:57 537 查看
/*
* atexit.c
*
*  Created on: 2011-11-22
*      Author: lc
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

/*
* exit handler
* 终止处理程序
* 使用atexit函数对函数的退出绑定终止处理程序
*
* 该程序(函数)在函数exit后被调用(main函数return后会自动调用exit)
* 调用顺序与绑定顺序相反并且相同函数可以多次被绑定
*
* 调用_exit _Exit函数 直接返回到内核,不会调用exit,所以不会启动终止处理程序
*/
static void handler1();
static void handler2();
int main(int argc, char **argv) {
if (atexit(handler1) != 0) {
fprintf(stderr, "%s", strerror(errno));
}
if (atexit(handler2) != 0) {
fprintf(stderr, "%s", strerror(errno));
}

if (atexit(handler2)) {
fprintf(stderr, "%s", strerror(errno));
}

//return 0;
exit(0);
//_exit(0);
}

void handler1() {
fprintf(stderr, "handler1 invoked\n");
}

void handler2() {
fprintf(stderr, "handler2 invoked\n");
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: