1. Basic Concepts:
Orphan process: The parent process exits, but its child processes are still running, so these child processes are called orphan processes, and the orphan process is adopted by the INIT process and treated as a aftercare;
Zombie Process: A process that has been terminated, but whose parent process has not yet been aligned, is called a zombie process, and the PS command status is z-process;
Aftercare: The kernel holds a certain amount of information for each terminating child process, which includes at least the process ID, the terminating state of the process, and the total CPU time used by the process. If the parent process does not use wait or waitpid to get information about terminating the child process, the information will be saved until the parent process is processed before releasing the resources it still occupies;
2. What is the harm:
Orphan process: The orphan process will be adopted by the INIT process and treated in the aftermath, so the orphan process is not harmful;
Zombie Process: If the parent process has not executed wait or waitpid, then the kernel will keep the state information of the child process, if there are a large number of zombie processes in the system, may result in the number of available process is less and even exhausted, so should avoid the emergence of the zombie process;
3. Workaround:
(1) The parent process waits for the child process to end with wait or waitpid;
(2) When the child process ends, the SIGCHLD signal is sent to its parent process, so the parent process can register SIGHLD signal processing and call wait or waitpid in the processing function;
(3) Fork two times, that is, the fork to produce the child process, the child process fork to generate the grandchild process, the child process ends, the main process uses wait or waitpid wait for the child process, and the sun process after its parent process exits, become an orphan process, was init adoption;
Another: If there are a large number of zombie processes in the current system, you need to find their parent process to kill their parent process, these zombie processes will be adopted by the Init and aftercare;
Orphan process with zombie process