linux系统编程之进程(四):进程退出exit,_exit区别即atexit函数

一,进程终止有5种方式:

正常退出:

异常退出:

调用abort 由信号终止 二,exit和_exit区别:

关于_exit():

       #include <unistd.h>

       void _exit(int status);

       #include <stdlib.h>

       void _Exit(int status);

DESCRIPTION        The function _exit() terminates the calling process "immediately".  Any        open file descriptors belonging to the process are closed; any children        of the process are inherited by process 1, init, and the process’s par-        ent is sent a SIGCHLD signal.

       The value status is returned to the parent  process  as  the  process’s        exit  status,  and  can be collected using one of the wait(2) family of        calls.

       The function _Exit() is equivalent to _exit().

关于exit():

#include <stdlib.h>

void exit(int status);

DESCRIPTION        The  exit() function causes normal process termination and the value of        status & 0377 is returned to the parent (see wait(2)).

       All functions registered with atexit(3) and on_exit(3) are  called,  in       the  reverse  order  of their registration.  (It is possible for one of        these functions to use atexit(3) or on_exit(3)  to  register  an  addi-        tional  function  to be executed during exit processing; the new regis-        tration is added to the front of the list of functions that  remain  to        be  called.) If one of these functions does not return (e.g., it calls       _exit(2), or kills itself with a signal), then none  of  the  remaining       functions is called, and further exit processing (in particular, flush-       ing of stdio(3) streams) is abandoned.  If a function has  been  regis-        tered  multiple  times using atexit(3) or on_exit(3), then it is called        as many times as it was registered.

       All open stdio(3) streams are flushed and  closed.   Files  created  by       tmpfile(3) are removed.

       The  C standard specifies two constants, EXIT_SUCCESS and EXIT_FAILURE,        that may be passed to exit() to  indicate  successful  or  unsuccessful        termination, respectively.

和exit比较一下,exit()函数定义在stdlib.h中,而_exit()定义在unistd.h中,

注:exit()就是退出,传入的参数是程序退出时的状态码,0表示正常退出,美国服务器,其他表示非正常退出,一般都用-1或者1,标准C里有EXIT_SUCCESS和EXIT_FAILURE两个宏,网站空间,用exit(EXIT_SUCCESS);

_exit()函数的作用最为简单:直接使进程停止运行,清除其使用的内存空间,并销毁其在内核中的各种数据结构;exit() 函数则在这些基础上作了一些包装,在执行退出之前加了若干道工序。exit()函数与_exit()函数最大的区别就在于exit()函数在调用exit系统调用之前要检查文件的打开情况,把文件缓冲区中的内容写回文件,就是"清理I/O缓冲"。

exit()在结束调用它的进程之前,要进行如下步骤: 1.调用atexit()注册的函数(出口函数);按ATEXIT注册时相反的顺序调用所有由它注册的函数,这使得我们可以指定在程序终止时执行自己的清理动作.例如,保存程序状态信息于某个文件,解开对共享数据库上的锁等.

2.cleanup();关闭所有打开的流,这将导致写所有被缓冲的输出,删除用TMPFILE函数建立的所有临时文件.

3.最后调用_exit()函数终止进程。

_exit做3件事(man): 1,香港空间,Any  open file descriptors belonging to the process are closed 2,any children of the process are inherited  by process 1, init 3,the process’s parent is sent a SIGCHLD signal

exit执行完清理工作后就调用_exit来终止进程。

三,atexit()

atexit可以注册终止处理程序,ANSI C规定最多可以注册32个终止处理程序。

终止处理程序的调用与注册次序相反

       #include <stdlib.h>

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

向上攀爬的。

linux系统编程之进程(四):进程退出exit,_exit区别即atexit函数

相关文章:

你感兴趣的文章:

标签云: