atexit函数
2016-02-24 22:59
369 查看
NAME
atexit - register a function to be called at normal process termination
函数的正常结束时候调用。当一个函数被kill命令杀死,或是使用其他方式总结如ctrl+C方式终结时,这个是不被调用的。
SYNOPSIS
#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.
调用时注册的反向调用。C++的析构函数和构造函数就是与这个类似,估计C++多少受到这个影响。
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 nonzero value.
该程序使用Ctrl+C或是kill命令杀死上述程序的时候,没有任何注册函数的打印信息。
该函数作为很好的一个调试接口或是很好定位手段。
atexit - register a function to be called at normal process termination
函数的正常结束时候调用。当一个函数被kill命令杀死,或是使用其他方式总结如ctrl+C方式终结时,这个是不被调用的。
SYNOPSIS
#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.
调用时注册的反向调用。C++的析构函数和构造函数就是与这个类似,估计C++多少受到这个影响。
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 nonzero value.
#include"apue.h" static void my_exit1(void); static void my_exit2(void); int main() { if(atexit(my_exit2) != 0) err_sys("can not register my_exit2\n"); if(atexit(my_exit1) != 0) err_sys("can not register my_exit1\n"); if(atexit(my_exit1) != 0) err_sys("can not register my_exit1\n"); printf("main is done\n"); while(1); return 0; printf("main has exited!\n"); } static void my_exit1(void){ printf("first exit handler\n"); } static void my_exit2(void){ printf("second exit handler\n"); }
该程序使用Ctrl+C或是kill命令杀死上述程序的时候,没有任何注册函数的打印信息。
该函数作为很好的一个调试接口或是很好定位手段。
相关文章推荐
- 第三百二十八天 how can I 坚持
- UVA253 Cube painting
- 制作2D追踪导弹
- linux如何编译和安装infer
- Exchange获取指定用户的邮箱同步的移动设备列表
- golang 的 channel 实现 生产者/消费者 模型
- 【TED 】改变世界的发明&埃及十大文明
- 堆排序
- Android中GridView实现长按多选功能
- 从C++转向java——3、判断与比较
- 初来驾到学java修饰符的使用
- 基于PaaS平台多租户实践
- linux 环境变量
- [Lintcode] Remove Duplicates from Sorted Array II
- 2016 与君共勉
- 再次讲解js中的回收机制是怎么一回事。
- SharedPreferences保存数据详解<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- Linux下搭建Wordpress环境
- XDU1019 阶乘因子的个数
- 【SPOJ-SARRAY】Suffix Array【后缀数组】