atexit函数

今天在学习unix进程环境的时候遇到了atexit函数,不知道是啥,从网上找了一些资料,整理如下:

资料:

http://hi.baidu.com/zzublog/blog/item/9bf4b8adb1855dc57dd92a43.html

http://hi.bccn.net/space-21499-do-blog-id-13251.html

http://linux.ctocio.com.cn/384/8881884.shtml

 


NAME
       atexit - register a function to be called at normal process termination

SYNOPSIS
       #include <stdlib.h>

       int atexit(void (*function)(void));

DESCRIPTION
       The atexit() function registers the given function to be called at normal process termination, either via exit(3) or via return from the program's main().  Functions so registered are called in the reverse order of their registration; no arguments are passed.

       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 non-zero value.

 

 

       很多时候我们需要在程序退出的时候做一些诸如释放资源的操作,但程序退出的方式有很多种,比如main()函数运行结束、在程序的某个地方用exit() 结束程序、用户通过Ctrl+C或Ctrl+break操作来终止程序等等,因此需要有一种与程序退出方式无关的方法来进行程序退出时的必要处理。方法就 是用atexit()函数来注册程序正常终止时要被调用的函数。

        atexit()函数的参数是一个函数指针,函数指针指向一个没有参数也没有返回值的函数。atexit()的函数原型是:int atexit (void (*)(void));

    在一个程序中最多可以用atexit()注册32个处理函数,这些处理函数的调用顺序与其注册的顺序相反,也即最先注册的最后调用,最后注册的最先调用

 

例子:

  1 #include<error.h>
  2 #include<stdio.h>
  3
  4 static void my_exit1(void);
  5
  6 static void my_exit2(void);
  7
  8 int main(void)
  9 {
 10         if(atexit(my_exit2) != 0)
 11         {
 12                 perror("can't register my_exit2");
 13         }
 14
 15         if(atexit(my_exit1) != 0)
 16         {
 17                 perror("can't registe my_exit1");
 18         }
 19
 20         if(atexit(my_exit1) != 0)
 21         {
 22                 perror("can't register my_exit1");
 23         }
 24
 25         printf("main is done /n");
 26         return 0;
 27
 28 }
 29
 30 static void my_exit1(void)
 31 {
 32         printf("first exit handler/n");
 33 }
 34
 35 static void my_exit2(void)
 36 {
 37         printf("second exit handler/n");
 38 }
执行结果:

magic@ubuntu:~/unix_programing/chapter7$ ./atexit
main is done
first exit handler
first exit handler
second exit handler


总结:

操作系统 装 载程序之后,首先运行的代码并不是main的第一行,而是某些别的代码,这些代码负责准备好main函数执行所需要的环境,并且负责调用main函数,这 时候你才可以在main函数里放心大胆地写各种代码:申请内存、使用系统调用、触发异常、访问I/O。在main返回之后,它会记录main函数的返回 值,调用atexit注册的函数,然后结束进程


可以这样理解:
当程序执行前的初始化阶段将atexit函数所指定的函数指针压栈,退出前退栈执行!  



函数说明

atexit()用来设置一个程序正常结束前调用的函数。当程序通过调用exit()或从main中返回时,参数function所指定的函数会先被调用,然后才真正由exit()结束程序。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值