This article briefly describes the zombie process.
Before you introduce, first look at the 6 big states of the process in Linux.
D Uninterruptible sleep (usually IO) R Running or runnable (on run queue) S forevent to complete) T is being traced. W 2.6. XX kernel) X dead (should never be seen) Z Defunct (" Zombie") process, terminated but not reaped by its parent.
State transition Diagram:
What is a zombie process? A zombie process is a process that has finished running, but whose parent process has not yet been processed. So why is the parent process going after a process has finished running? What has been done to deal with the aftermath?
Specifically, after a process finishes running, the process is not completely destroyed, and the kernel still maintains some information about the process (the process ID, the process's terminating state, and the total CPU time used by the process ). The parent process can obtain this information by calling wait ()/waitpid (), and eventually destroy the process.
How to avoid the production of zombie process?
1. The parent process waits for the child process to end by calling wait ()/waitpid (). (This causes the parent process to be suspended)
2. If the parent process is busy and does not want to be suspended, you can set the handler of the SIGCHLD signal through the signal function and call Wait ()/waitpid () in handler.
( at the end of the child process, the kernel sends a SIGCHLD signal to the parent process, which is ignored by the parent process for that signal.) )
3. The parent process displays the ignore SIGCHLD signal through signal (SIGCHLD, sig_ign), so that after the process finishes, the kernel does not send a SIGCHLD signal to the parent process and the kernel is responsible for reclaiming the child process.
4. Two times fork ().
The parent Process fork () out of the child process, the child process fork () exits after the grandchild process, and after the grandchild process finishes running, the Init process takes over because its parent process has exited. The INIT process will be responsible for destroying the process.
How do I clear the zombie process?
The KILL command cannot kill the zombie process, but we can kill the parent process of the zombie process so that the zombie process becomes orphaned and then taken over by the INIT process.
Zombie Processes (Zombie process)