Processes are like human beings, and we create a process through the fork () function, so how do we abort the process?
Process exit
1. In Linux any let a process exit
Process exit indicates that the process is about to end. In Linux, process exits are divided into normal exits and exception exits in two ways.
1> Normal exit
A. Execute return in the main () function.
B. Call the exit () function
C. Call the _exit () function
2> Abnormal exit
A. Calling the About function
B. The process receives a signal which causes the program to terminate.
Tiger-john Description:
Either way, the system will eventually execute the same code in the kernel. This code closes the open file descriptor used by the process, freeing the memory and other resources it consumes.
3> comparing the different points of the above exit methods
(1) The difference between exit and return:
A.exit is a function that has parameters. Leave control to the system after exit executes
B.return is the return after the function has finished executing. Renturn the control to the calling function when the execution is complete.
(2) The difference between exit and abort:
A.exit is the normal termination process
B.about is abnormally terminated.
Now let's focus on the exit () and _exit () functions
Study of 2.exit () and _exit ()
Both the 1>exit and _exit functions are used to terminate the process.
When the program executes to exit or _exit, the system unconditionally stops all remaining operations, clears the various data structures including the PCB, and terminates the operation of this process.
The 2>exit is declared in the header file Stdlib.h, and the _exit () declaration is declared in the header file unistd.h. The argument Exit_code in exit is 0 for normal termination of the process, and if other values indicate an error occurred during program execution.
The difference between 3>exit () and _exit ():
A._exit () returns to the kernel immediately after execution, and exit () performs some cleanup before handing control over to the kernel.
B. When the _exit function is called, it closes all file descriptors of the process, cleans up memory and some other kernel cleanup functions, but does not flush the stream (stdin, stdout, stderr ...). The Exit function is a wrapper over the _exit function that calls _exit and flushes the stream before the call.
Tiger-john Description:
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. Because of the standard library of Linux, there is an operation called "Buffered I/O", which is characterized by a buffer in memory for each open file. Each time a file is read, a number of records will be read sequentially, so that the next time you read the file can be read directly from the memory buffer, also, each time the file is written to the memory buffer, and so on to meet certain conditions (such as reaching a certain number or encountering a certain character, etc.), The contents of the buffer are then written to the file once. This technique greatly increases the speed of file read and write, but it also gives the programmer a bit of trouble. For example, there are some data that have been written to the file, in fact, because they do not meet the specific conditions, they are only in the buffer, then use the _exit () function to close the process directly, the buffer data will be lost. Therefore, to ensure the integrity of the data, you must use the exit () function.
C. Take a look at the difference between them through a function instance:
function Instance 1:exit.c
1 #include <stdio.h>
2 #include <stdlib.h>
3
4 int Main ()
5 {
6 printf ("Using exit----\ n");
7 printf ("This was the content in buffer\n");
8 exit (0);
3 ·
After the function has been debugged
[Email protected]:~/work/process_thread/exit$ gcc Exit.c-o exit
[Email protected]:~/work/process_thread/exit$./exit
The result of the execution is:
Using exit----
The content in buffer
function Instance 2:_exit.c
1 #include <stdio.h>
2 #include <unistd.h>
3
4 int main (void)
5 {
6 printf ("Using _exit--\n");
7 printf ("The content in buffer");
8 _exit (0);
3 ·
After the function has been debugged
[Email protected]:~/work/process_thread/exit$ gcc _exit.c-o _exit
[Email protected]:~/work/process_thread/exit$./_exit
The result of the execution is:
Using _exit--
Tiger-john Description:
The 1.printf function is how buffered I/O is used, which automatically reads the record from the buffer when it encounters a "\ n" line break. So exit () writes out the buffer's data before exiting, and the _exit () function exits directly.
2. You can also put the function in instance 2 of printf ("This is the content in buffer"), instead of printf ("This is the content in buffer\n") (that is, in printf the last add a \ n See what the running results are, and why do they produce such results? )
Tiger-john Supplement:
The order in which the parent-child process terminates will produce different results.
1> the parent process terminates before the child process:
This situation is the orphan process we used earlier. When the parent process exits first, the system causes the INIT process to take over the child process.
The 2> child process terminates before the parent process, and the parent process does not call the wait function
In this case, the child process goes into a zombie state and will persist until the system restarts. When a child process is in a zombie state, the kernel only saves some of the necessary information for the process in case the parent process needs it. At this point the child process always occupies resources and reduces the maximum number of processes that the system can create.
What is a zombie state?
A process that has been terminated, but whose parent process has not yet dealt with it (getting information about terminating the child process, releasing the resources it still occupies) is called the Zombie process (zombie). The PS command prints the status of the zombie process to Z
The 3> child process terminates before the parent process, and the parent process calls the wait function
At this point the parent process waits for the child process to end.
Exit () and _exit ()