注册终止函数

来源:互联网 发布:剑网三道长捏脸数据 编辑:程序博客网 时间:2024/06/07 09:45

函数名: atexit  

功 能: 注册终止函数(即main执行结束后调用的函数)  

用 法: int atexit(void (*func)(void));  

注意:atexit()注册的函数类型应为不接受任何参数的void函数,exit调用这些注册函数的顺序与它们 登记时候的顺序相反。  

程序例:  #include <stdio.h> 

 #include <stdlib.h>  

void exit_fn1(void)  {  printf("Exit function #1 called\n");  } 

 void exit_fn2(void)  {  printf("Exit function #2 called\n");  }  

int main(void)  {  /* post exit function #1 */  atexit(exit_fn1);  /* post exit function #2 */  

atexit(exit_fn2);  return 0;  }  

输出:  Exit function #2 called  Exit function #1 called  

进程的终止方式:  

有8种方式使进程终止,其中前5种为正常终止,它们是  1:从 main 返回  2:调用 exit  3:调用 _exit 或 _Exit  4:最后一个线程从其启动例程返回  5:最后一个线程调用 pthread_exit  异常终止有3种,它们是  6:调用 abort  7:接到一个信号并终止  8:最后一个线程对取消请求做出响应  #include <stdlib.h?  void exit (int status);  void _Exit (int status);  #include <unistd.h>  void _exit (status);  其中调用 _exit,_Exit 都不会调用终止程序  异常终止也不会。


原创粉丝点击