先看下面一段代码:

#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<unistd.h>

int main()
{

     pid_t result;
     result = fork();
     if(result<0)
     perror("fork");
     if(result == 0)
     {
             printf("This is _exit test\n");
             printf("This is the content in the buffer000");
            _exit(0);
     }
     else
     {
             printf("This is exit test\n");
             printf("This is the content in the buffer");
             exit(0);
     }
     return 0;
}

我们知道printf在遇到”\n”情况下会把内容打印出来,所以if-else分支第一条打印都会打印出来,但是区别在于,子进程中运行_exit(0)并未将”Thisis the content in the buffer000″ 打印出来,而父进程中运行的exit(0)将”Thisis the content in the buffer”打印出来了。
造成这种区别的原因是什么呢?下面来探究一下。


下面一张图清晰地表示了exit和_exit的区别:

1)关于_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-rent 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().

2)关于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() 函数则在这些基础上作了一些包装,在执行退出之前加了若干道工序。
exit()函数与_exit()函数最大的区别就在于exit()函数在调用exit系统调用之前要检查文件的打开情况,把文件缓冲区中的内容写回文件,就是”清理I/O缓冲”。

exit()在结束调用它的进程之前,要进行如下步骤:
– 调用atexit()注册的函数(出口函数);按ATEXIT注册时相反的顺序调用所有由它注册的函数,这使得我们可以指定在程序终止时执行自己的清理动作.例如,保存程序状态信息于某个文件,解开对共享数据库上的锁等.
– cleanup();关闭所有打开的流,这将导致写所有被缓冲的输出,删除用TMPFILE函数建立的所有临时文件.
– 最后调用_exit()函数终止进程。


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

发表评论

电子邮件地址不会被公开。 必填项已用*标注