Process exit can be divided into normal exit and abnormal Exit:
(1) Exit normally
A. Execute return in the main function.
B. Call the exit function.
C. Call the _ exit function.
(2) Exit unexpectedly
A. Call the about function.
B. The process is affected by a signal that terminates the program.
Conclusion: No matter the exit method, the same piece of code in the kernel will be executed. This code is used to close all opened file descriptors in the process and release the memory and other resources it occupies.
Exit Mode Comparison:
A. Difference between exit and return: exit is a function with parameters, while return is the return after the function is executed. Exit gives control to the system, while return gives control to the call function.
B. Difference between exit and abort: exit is a normal termination process, while about is an abnormal termination.
C. exit (int exit_cod): If the exit_code parameter in exit is 0, the process is terminated normally. If it is another value, an error occurs during program execution, such as overflow. The divisor is 0.
D. Difference between exit () and _ exit (): Declaration in the exit header file stdlib. h, and _ exit () declaration in the header file unistd. h. Both functions can terminate the process normally, but _ exit () will return to the kernel immediately after execution, while exit () must first perform some cleanup operations and then give control to the kernel.
The termination sequence of parent and child processes varies with the result. When the parent process exits before the child process exits, the system will connect the init process to the pipe process. When the child process is terminated before the parent process, and the parent process does not call the wait function to wait for the child process to end, the child process enters the dead State and will continue until the system restarts. When a sub-process is in the dead state, the kernel only saves the necessary information of the process for the parent process. At this time, sub-processes always occupy resources and reduce the maximum number of processes that can be created by the system. If the child process terminates before the parent process and the parent process calls the wait or waitpid function, the parent process waits until the child process ends.