Refer to this article:
http://www.mike.org.cn/articles/treatment-of-zombie-processes-under-linux/
In the state of the Linux process, the zombie process is a very special one, it has abandoned almost all memory space, no executable code, and can not be scheduled, just keep a position in the process list, record the process's exit status and other information for other processes to collect, in addition, The zombie process no longer occupies any memory space. It needs its parent process to get the corpse of it.
If his parent process did not install the SIGCHLD signal handler, and did not call wait or waitpid () waiting for the end of the child process, and did not explicitly ignore the signal, then it remains zombie state, if the parent process is finished, then the INIT process will automatically take over the child process, for it to corpse, It can still be erased.
Avoidance of zombie processes
1. The parent process waits for the child process to end through functions such as wait and waitpid, which causes the parent process to hang
2. If the parent process is busy, you can use the signal function to install handler for SIGCHLD, because the parent process will receive the signal when the child process ends, and you can call the wait recycle in handler
3, if the parent process does not care about when the child process ends, then you can use signal (SIGCHLD, Sig_ign) to notify the kernel, the end of the child process is not interested, then the end of the process, the kernel will be recycled, and no longer send a signal to the parent process
4, there are some tricks, that is, fork two times, the parent process fork a child process, and then continue to work, the child process fork a grandchild process after the exit, then the Sun process is init takeover, after the end of the sun process, init will be recycled. But the recycling of the sub-process to do it yourself.
Why do I have to go into zombie state after the child process ends?
Because the parent process may want to get information such as the exit status of the child process.
S (State of the process)
O: The process is running on the processor
S: Sleep state (sleeping)
R: Waiting to run (runable)
I: Idle state (idle)
Z: Zombie Status (Zombie)
T: Tracking status (traced)
B: The process is waiting for more memory pages
Estimated value of C:CPU utilization (CPU usage)
Kill-18 PPID (PPID is its parent process)
"Todo" Zombie process learning & process Status list