Zombie process, zombie Process
In a UNIX system, a zombie process is executed
Exit System Call, a fatal error occurs during operation, or a termination signal is received. However, there is still a table item (Process Control Block PCB) in the operating system table ), a process in the "terminated" status. This occurs when the child process needs to retain table items to allow its parent process to read the exit status of the child process: Once the exit state passes
When the wait system calls read data, zombie process entries are deleted from the Progress table, which is called "reaped )". Under normal circumstances, the process is directly owned by its parent processwait
It is recycled by the system. A process that remains zombie for a long time is generally incorrect and causes resource leakage.
TermsZombie processDerived from en: zombie-undead, It is a metaphor that sub-processes are dead but still not harvested. Unlike normal processes,
The kill command is invalid for zombie processes. Unlike a zombie process, an orphan process has been killed, but it can still be executed normally, but it will not become a zombie process because it is
Init (process ID 1) adopted andwait
It exits.
After a child process dies, the system sends a SIGCHLD signal to the parent process. The parent process ignores the signal by default. If you want to respond to this message, the parent process usually uses
Wait system calls to respond to the termination of the sub-process.
After a zombie process is harvested, its process ID (PID) and table items in the progress table can be reused by the system. However, if the parent process does not callwait
, The zombie process will keep the table items in the table, resulting in resource leakage. In some cases, this is expected: the parent process creates another sub-process and wants to have different process numbers. If the parent process sets the event handler functionSIG_IGN
Explicitly ignore the SIGCHLD signal, instead of implicitly ignoring the signal by default, orSA_NOCLDWAIT
Indicates that the exit status of all sub-processes will be discarded and directly recycled by the system.
UNIX Commands
The State ("STAT") column of the processes listed in ps is marked as"Z
"It is a zombie process.
The method for harvesting botnets is throughkill
Command to manually send a SIGCHLD signal to the parent process. If the parent process still refuses to harvest botnets, terminate the parent processinit
Processes adopt zombie processes.init
Process execution cyclewait
The system calls to harvest all zombie processes it adopted.
To avoid zombie processes, the following methods are generally used in actual applications:
Example:
12345678910111213141516171819202122 |
# Include & lt; sys/wait. h & gt; # include & lt; stdlib. h & gt; # include & lt; unistd. h & gt; int main (void) {pid_t pids [10]; int I; for (I = 9; I & gt; = 0; -- I) {pids [I] = fork (); if (pids [I] = 0) {sleep (I + 1); _ exit (0 );}} for (I = 9; I & gt; = 0; -- I) waitpid (pids [I], NULL, 0); return 0 ;} |
Our public account