博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
atexit 函数
阅读量:2396 次
发布时间:2019-05-10

本文共 1078 字,大约阅读时间需要 3 分钟。

按照 ISO C 的规定,一个进程可以登记多达 32 个函数,这些函数将由 exit 自动调用。我们称这些函数为终止处理函数 (exit handler),并调用 atexit 函数来登记这些函数。

#include 
int atexit(void (*func)(void));返回值:若成功则返回 0,或出错则返回非 0 值

其中,atexit 的参数是一个函数地址,当调用此函数时无需向它传送任何函数,也不期望它返回一个值。exit 调用这些函数的顺序与它们登记时候的顺序相反。同一函数如若被登记多次,则也会被调用多次。

atexit.c

#include 
/* external declarations */extern void (*_Atfuns[])(void);extern size_t _Atcount;int (atexit)(void (*func)(void)) /* function to call at exit */{ if (_Atcount == 0) return (-1); _Atfuns[--_Atcount] = func; /* list is full */ return (0);}

exit.c

#include 
#include
/* macros */#define NATS 32/* static data */void (*_Atfuns[NATS])(void) = {0};size_t _Atcount = {NATS};void (exit)(int status) /* tidy up and exit to system */{ while (_Atcount < NATS) { (*_Atfuns[_Atcount++])() ; } size_t ; for (i = 0; i < FOPEN_MAX; ++i) /* close all files */ if (_Files[i]) fclose(_Files[i]); _Exit (status);}

参考资料

[1] C 标准库.卢红星等译.人民邮电出版社

[2] UNIX 环境高级编程第 2 版.尤晋元等译.人民邮电出版社

转载于:https://my.oschina.net/iblackangel/blog/891063

你可能感兴趣的文章
Oracle 12CR2 Oracle Restart - ASM Startup fails with PRCR-1079
查看>>
poj 2140 Herd Sums
查看>>
poj 2524 Ubiquitous Religions
查看>>
poj 1611 The Suspects
查看>>
poj 3331 The Idiot of the Year Contest!
查看>>
poj 3233 Matrix Power Series
查看>>
poj 3070 Fibonacci
查看>>
poj 1656 Counting Black
查看>>
BestCoder Round #28
查看>>
poj1845 Sumdiv
查看>>
poj3299 Humidex
查看>>
poj2159 Ancient Cipher
查看>>
poj1083 Moving Tables
查看>>
poj2255 Tree Recovery
查看>>
poj3904 Sky Code
查看>>
zoj 1745 Are We There Yet?
查看>>
UVA100 The 3n + 1 problem
查看>>
hdu1754 I Hate It
查看>>
hdu 1166 敌兵布阵(求区间的和,单节点更新)
查看>>
hiho一下 第四十四周 题目1 : 博弈游戏·Nim游戏
查看>>