As you can see, the _exit function is to stop the process from running, clear its used memory space, and clear its various data structures in the kernel; the exit function does some tricks on these bases and adds several operations to the exit before executing. The most important difference between the exit () function and the _exit () function is that the exit () function checks the opening of the file before calling the exit system, and writes the contents of the file buffer back to the file. That is, "clean I/O buffering" in the figure.
Header file required: Exit: #include <stdlib.h>
_exit: #include <unistd.h>
Function prototype: exit:void exit (int status)
_exit:void _exit (int status)
Function pass-through value: status is an integer parameter that can be used to pass the state at the end of the process. Generally speaking, 0 means the normal end; other values indicate an error and the process is not properly terminated. When actually programmed, the parent process can take advantage of the wait system call to receive the return value of the child process, thus handling different things differently.
Exit () and _exit () instance analysis
printf (const char *FMT,...) The function uses buffered I/O, which automatically reads the record from the buffer when it encounters a "\ n" line break.
< code samples >
#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 ("The content in the buffer000");
_exit (0);
}
Else
{
printf ("This is Exit test\n");
printf ("The content in the buffer");
Exit (0);
}
return 0;
}
Here is the result of the operation:
Results Analysis: running _exit (0) in the sub-process does not print thisis the content in the buffer000, and exit (0) running in the parent process will thisis the content in the buffer Printed out. Note that exit (0) clears the buffered I/O content before terminating the process, so even if no \ n is printed in printf, _exit (0) terminates the process directly and does not clean up the buffered I/O content, so it will not be printed.
Reprint Link: http://blog.csdn.net/lwj103862095/article/details/8640037
The difference between exit () and _exit ()