usually always use the exit () function, but today to see the source of the _exit () function, want to know their differences, so look up information, wrote down!
#include <stdlib.h> void exit (int status);
Not as difficult to understand as fork, from the name of exit can be seen, this system call is used to terminate a process.no matter where in the program, as long as the execution to the exit system call, the process will stop all the remaining operations,clears various data structures including PCBs and terminates the operation of this process. Take a look at the following program:
#include <stdlib.h> #include <stdio.h>main () { printf ("This process would exit!\n"); Exit (0);//The program exits at a time and will no longer execute downwards. printf ("Never Be displayed!\n");}
Operation Result:
As we can see, the program does not print the following "Never be displayed!\n" because, when executed to exit (0), the process is terminated, no further execution, and exits directly. The exit system calls the parameter status with an integer type, and we can use this parameter to pass the state at the end of the process:
(1) 0 means no unexpected normal end;
(2) Other values indicate an error occurred and the process is not properly terminated.
When we are actually programming, we can use the wait system call to receive the return value of the child process, thus dealing with different situations.
the usage and differences of the Linux exit () and _exit ():
Exit and _exit as a system call, _exit and exit are twins. Usually we think that there is no difference between them: but there is no difference there will be two functions, you have to know that the program ape, although bitter but not stupid, this kind of thing happens relatively low probability, so the answer is negative. This distinction is mainly reflected in their definitions in the library, and the prototype of _exit in the Linux libraries is:
#include <unistd.h> void _exit (int status);
(1) the exit () function is defined in Stdlib.h, while _exit () is defined in Unistd.h,
(2) The function of the _exit () is the simplest: direct the process to stop running, clear its memory space used, and destroy its various data structures in the kernel; the exit () function makes some packaging on these bases, and adds several operations to the exit before execution, and for this reason, Some people think that exit is not a purely system call.
Therefore, the most important difference between the exit () function and the _exit () function is that:The exit () function checks the opening of a file before calling the exit system, writes the contents of the file buffer back to the file, "Cleans I/O buffers", and _exit () exits directly, regardless of the buffer data, which can easily cause loss of data.
In the standard library of Linux, there is a set of functions called "Advanced I/O", which are known as printf (), fopen (), Fread (), fwrite (), which are also known as "Buffered I/O (buffered I/Os)", Its characteristics are corresponding to each open file, there is a buffer in memory, each time you read a file, you will read more than a few records, so that the next time you read a file can be read directly from the memory buffer, each time the file is written, it is only written in memory buffer, etc. to meet a certain number of conditions (reached a certain amount, or encounter specific characters, such as newline character \ n and file Terminator eof, and then write the contents of the buffer once to the file, which greatly increases the speed of file read and write, but also for our programming a little bit of trouble. If there is some data that we think has been written to the file, in fact, because the specific conditions are not met, they are only guaranteed to exist in the buffer,then we use the _exit () function to close the process directly, and the data in the buffer will be lost .Conversely, if you want to ensure the integrity of the data, you must use the exit () function.
Take a look at the following routines:
#include <stdlib.h>int main () { printf ("Output begin\n"); printf ("Content in buffer\n"); Exit (0);//Output The first two sentences, the program exits at a time;}
Operation Result:
From the running results, exit (0) is the function of normal exit;
#include <unistd.h>int main () { printf ("Output begin\n"); printf ("Content in buffer"); _exit (0);//empty cache space directly, may cause data loss, do not deal with the aftermath;}
Operation Result:
At this point, it can be seen that the _exit (0) function directly exits the purge data, hard exit, without the data of the tube buffer, also did not write the file or the buffer data to do the aftercare, only output a sentence, the second sentence of the data is lost, the opposite exit (0) will be processed after the buffer data is terminated, For example write will file, print and so on some follow.
Linux down Exit () and _exit () difference