When a process calls the Exit command to end its own life, it is not actually destroyed, but it leaves behind a data structure that becomes zombie. The zombie process is a very special kind of because it has given up almost all of the memory space, no executable code, can not be scheduled, only in the process list to keep a position, log the process of the exit status and other information for other processes to collect, in addition, the zombie process no longer occupies any memory space.
The zombie process needs its parent process to bury it, o_o!!. If its parent process does not install the SIGCHLD signal processing function call Wait () or waitpid () waits for the child process to finish and does not explicitly ignore the SIGCHLD signal, it remains zombie-state. If this is the end of the parent process, the init ancestor process will take over the child process and bury it. Even more unfortunate, if the parent process is a loop and does not end, the subprocess will remain zombie-unable to end, and even root identity kill-9 will not kill the zombie process. The remedy is to kill the parent process of the zombie process (the parent process of the zombie process is inevitable), and the zombie process becomes an orphan process, which is responsible for the removal of the zombie process by init. Because the system can use the process number is limited, if there are a large number of zombie processes, will cause the system does not have the available process number, resulting in the process can not be created. So we want to avoid zombie processes.
When a process creates a child process, they run asynchronously. That is, the parent process cannot predict when the child process will end, so if the parent process is busy too late to wait for the child process, then when the child process ends, will it lose the status information at the end of the subprocess? In this consideration UNIX provides a mechanism to ensure that the parent process is available if it wants to know the information at the end of the child process. This mechanism is that when each process exits, the kernel releases all resources for the process, including open files, memory consumption. However, some information (such as process number PID exit state run time, etc.) is still retained. These reserved information is not released until the process is invoked by the wait/waitpid. *^_^*
Here's a place to watch. If the child process ends first and the parent process ends after the child process finishes, the parent process continues to run but does not call wait/waitpid the child process becomes a zombie process.
But if the child process ends, the parent process ends, but the wait/waitpid is not invoked to wait for the end of the child process, the child process is still running, and the parent process has ended. Then there is no zombie process. At the end of each process, the system scans all processes running in the current system to see if any process is a child of the process that is just finished, and if so, there is init to take over it and become its parent process.
Similarly, in the case where the zombie process is generated, when the child process ends but the parent process continues to run (without invoking wait/waitpid), the child process is automatically taken over by Init if the parent process terminates unexpectedly. Then it is no longer a zombie process. Because Intit will discover and release the resources it occupies. (Of course, if the process table is larger, the process of init finding it takes over the zombie process becomes slower, so the zombie process still consumes the system's resources before Init discovers them).
We know the cause of the zombie process, so how to avoid the zombie process.
If the parent process is not very busy, we can wait for the end of the child process by simply calling the waiting () or waitpid (). However, this can cause the parent process to be suspended. But if the parent process is busy. We do not want the parent process to be suspended until the end of the child process, so we can use the signal function sigaction to set the wait handler function for SIGCHLD. After this process is finished, the parent process will receive a signal that the child process ends. and call the resource for the wait recycle child process.