Exit () and _exit () functions
Function description
The create process uses the fork () function, the execution process uses the EXEC function family, and the exit () and the _exit () function are used to terminate the process. When a process executes to the exit () or _exit () function, the process unconditionally stops all remaining operations, clears the various data structures, and terminates the running of the process. However, there is a difference between the two functions, and the invocation process is as shown in Figure 1:
As you can see from Figure 1, the _exit () function is to simply stop the process, clear the memory space it uses, and clear its various data structures in the kernel, while the exit () function does some packaging on these foundations, adding several operations before executing the exit. The biggest difference between the exit () function and the _exit () function is that the exit () function checks which files the process has opened before terminating the current process, and writes the contents of the file buffer back to the file, which is the clean I/o buffer in Figure 1.
In Linux's standard function library, there is an operation called buffered I/O (buffered I/Os), which is characterized by a buffer in memory for each open file.
Every time you read a file, several records are read consecutively, so that the next time you read the file, you can read it directly from the memory buffer; Similarly, every time you write a file, it is only written in memory buffer, and so satisfied with certain conditions (such as reaching a certain number or encountered a certain character, etc., the most typical is our vim used: w command), and then write the contents of the buffer to the file at once.
This technology greatly increases the speed of file reading and writing, but it also brings some trouble to our programming. For example, some data you think has been written to the file, in fact, because the specific conditions are not met, they are only stored in the buffer, then use the _exit () function to directly shut down the process, the data in the buffer will be lost. Therefore, if you want to ensure the integrity of the data, it is best to use the exit () function.
function syntax
The following table lists the syntax points for the exit () and _exit () functions:
Basic experiment
The following two basic experiment 1 compares the difference between exit () and _exit () functions. Because the printf () function uses buffered I/O, the function is automatically read from the buffer when it encounters the "\ n" newline character, and the following two basic experiments use this property to compare. The following is the code for Experiment 1: