First to transcribe a description of the zombie process on the Web:
Zombie Process: A process uses fork to create a child process, assuming that the child process exits, and the parent process does not call wait or waitpid to get state information for the child process, then the process descriptive descriptor of the child process is still stored in the system.
Such a process is called a zombie process. At the time each process exits, the kernel frees all the resources of the process, including the open files, the memory used, and so on. But still keep certain information (including process ID, exit status of the termination status of the process, execution time the amount of the CPU taken by the Proces s, etc.). It is not released until the parent process passes wait/waitpid.
But that leads to the problem. Assume that the process does not call wait/waitpid. Then the reservation of the information will not be released, its process number will always be occupied, but the system can use the process number is limited, assuming that a large number of zombie processes, due to the absence of a process number can cause the system can not generate new processes. This is the threat to the zombie process and should be avoided.
From a system perspective. There are two ways to handle a zombie process:
1 The parent process of the zombie process is found and the parent process is killed. Then the zombie process will become an orphan process. The orphan process is taken over by the INIT process in the system. The INIT process will reclaim the resources of the zombie process
2 reboot system, because zombie process is not able to be killed
For example, the following test:
[Email protected] ~]# ps-ef|grep 21165root 21165 7459 0 05:51 pts/1 00:00:00./a.outroot 21166 21165 0 05:51 pts/1 00:00:00 [a.out] <defunct>root 21190 8866 0 05:52 pts/3 00:00:00 grep 21165[[email protected] ~]# kill 21165[[email protected] ~]# [[email protected] ~]# [[email protected] ~]# Ps-ef|gre P 21165 after killing the parent process. Child process also disappears root 21196 8866 0 05:52 pts/3 00:00:00 grep 21165
[[email protected] ~]# ps-ef|grep 16704root 16704 16703 0 04:06 pts/1 00:00:00 [A.out] <defunct>root 16719 8866 0 04:06 pts/3 00:00:00 grep 16704[[email protected] ~]# [[email protected] ~]# [[email protected] ~]# kill-9 16704[[email protected] ~]# [[email protected] ~]# ps-ef|gr EP 16704root 16704 16703 0 04:06 pts/1 00:00:00 [a.out] <defunct>root 16725 8866 0 04:06 PTS/3 00:0 0:00 grep 16704
</pre><p></p><p> from a development perspective. There are two ways to avoid zombie processes: 1 is handled by the signal function. Because each child process exits a sigchild signal to the parent process 2 through the Fork two sub-process implementation, so that the parent process of the child process is the init process (), the majority of the daemon is the implementation of a simple program to demonstrate the process of producing a zombie process: </p><pre Code_snippet_id= "554626" snippet_file_name= "blog_20141217_3_6668512" name= "code" class= "CPP" > A The parent process fork a child process and then exits without using the Waitpid function, and the child process exits # include <sys/types.h> #include <sys/wait.h> #include after sleep 120 seconds <errno.h> #include <unistd.h> #include <stdlib.h> #include <stdio.h> #include <string.h> int main (int argc, char *argv[]) {pid_t pid; PID = fork (); if (PID = = 0) {int iPid = (int) getpid (); fprintf (stderr, "I am child,%d\n", iPid); Sleep (120); fprintf (stderr, "Child exits\n"); return exit_success; } int iPid = (int) getpid (); fprintf (stderr, "I am parent,%d\n", iPid); fprintf (stderr, "parent exits\n"); return exit_success;} [[email protected] ~]# GCc ZOM.C [[email protected] ~]#./a.out [[email protected] ~]#./a.out I am parent,15019parent exitsI am child,15 020 from the output see the parent process exits first. Then the child process executes the sleep function [[email protected] ~]# ps-ef|grep 15019//view parent process root 15046 8866 0 03:29 pts/3 00:00:00 grep 15 019[[email protected] ~]# [[email protected] ~]# ps-ef|grep 15020//view sub-process root 15020 1 0 03:29 PTS/1 00:00:00./a.outroot 15056 8866 0 03:29 pts/3 00:00:00 grep 15020 from PS See the parent process has been destroyed, the child process's parent process number is 1, that is, the INIT process two parent process fork a child process After you use the Waitpid function, and then quit, and the child processes sleep 120 seconds after exiting # include <sys/types.h> #include <sys/wait.h> #include <errno.h> #include <unistd.h> #include <stdlib.h> #include <stdio.h> #include <string.h>int main (int argc , Char *argv[]) {pid_t pid; PID = fork (); if (PID = = 0) {int iPid = (int) getpid (); fprintf (stderr, "I am child,%d\n", iPid); Sleep (120); fprintf (stderr, "Child exits\n"); ReTurn exit_success; } int iPid = (int) getpid (); fprintf (stderr, "I am parent,%d\n", iPid); Waitpid (pid,null,0); fprintf (stderr, "parent exits\n"); return exit_success;} [[email protected] ~]# gcc zom.c [[email protected] ~]#./a.out I am parent,15187i am child,15188child exitspare NT exits from the output see the child process first exit, the parent process waits for the child process to exit before exiting [[email protected] ~]# ps-ef|grep 15187//view parent process root 15187 7459 0 03:32 pts /1 00:00:00./a.outroot 15188 15187 0 03:32 pts/1 00:00:00./a.outroot 15197 8866 0 03:33 pts/3 00:00: [15187[[email protected] ~]# ps-ef|grep 15188//view child process root 15188 15187 0 03:32 pts/1 00:00:00./a.out Root 15207 8866 0 03:33 pts/3 00:00:00 grep 151,883 parent Process fork a child process. Then use the Waitpid function, and finally use the sleep function, and the child process to sleep 40 seconds after exiting # include <sys/types.h> #include <sys/wait.h> #include < errno.h> #include <unistd.h> #include <stdlib.h> #include <stdio.h> #include <string.h>int Main (int argc, Char *argv[]) {pid_t pid; PID = fork (); if (PID = = 0) {int iPid = (int) getpid (); fprintf (stderr, "I am child,%d\n", iPid); Sleep (40); fprintf (stderr, "Child exits\n"); return exit_success; } int iPid = (int) getpid (); fprintf (stderr, "I am parent,%d\n", iPid); Waitpid (pid,null,0); fprintf (stderr, "sleep....\n"); Sleep (120); fprintf (stderr, "parent exits\n"); return exit_success;} [[email protected] ~]#./a.out I am parent,15673i am child,15674child exits//wait 40 seconds to output sleep ...//wait 40 seconds to output the parent ex Its from the above output to see the sleep function is not executed immediately, but wait for the child process to execute before execution, is actually waiting for the waitpid function return [[email protected] ~]# ps-ef|grep 15673// Program execution within 40 seconds root 15673 7459 0 03:43 pts/1 00:00:00./a.outroot 15674 15673 0 03:43 pts/1 00:00:00./a.outroot 15681 8866 0 03:44 pts/3 00:00:00 grep 15673[[email protected] ~]# ps-ef|grep 15674//program execution 40 seconds within root 156 74 15673 0 03:43 pts/1 00:00:00./a.outroot 15692 8866 0 03:44 pts/3 00:00:00 grep 15674[[email protected] ~ ]# [[email protected] ~]# ps-ef|grep 15673//program execution 40 seconds outside root 15673 7459 0 03:43 pts/1 00:00:00./a.outroot 15725 8866 0 03:44 pts/3 00:00:00 grep 15673[[email protected] ~]# ps-ef|grep 15674//program execution 40 seconds, child process has destroyed root 15727 8866 0 03:44 pts/3 00:00:00 grep 15674[[email protected] ~]# [[email protected] ~]# Ps-ef|grep 1 5673//program execution 120 seconds outside root 15798 8866 0 03:46 pts/3 00:00:00 grep 156,734 parent Process fork a child process. Do not use the Waitpid function, but sleep 120 seconds, and the child process to sleep 1 seconds after exiting # include <sys/types.h> #include <sys/wait.h> #include < errno.h> #include <unistd.h> #include <stdlib.h> #include <stdio.h> #include <string.h>int Main (int argc, char *argv[]) {pid_t pid; PID = fork (); if (PID = = 0) {int iPid = (int) getpid (); fprintf (stderr, "I am child,%d\n", iPid); Sleep (1); fprintf (stderr, "Child exits\n"); return exit_success; } int iPid = (int) getpid (); fprintf (stderr, "I am parent,%d\n", iPid); fprintf (stderr, "sleep....\n"); Sleep (120); fprintf (stderr, "parent exits\n"); return exit_success;} [[email protected] ~]#./a.out I am parent,16026sleep .... I am Child,16027child exitsparent exits from above output see child process exit before parent process [[email protected] ~]# ps-ef|grep 16026//view parent process root 1602 6 7459 0 03:51 pts/1 00:00:00/a.outroot 16027 16026 0 03:51 pts/1 00:00:00 [a.out] <defunct>root 16039 8866 0 03:51 pts/3 00:00:00 grep 16026 [[email protected] ~]# [[email protected] ~]# ps-ef|grep 1602 7//view child process, child process in zombie state root 16027 16026 0 03:51 pts/1 00:00:00 [a.out] <defunct>root 16046 8866 0 03:51 PTS/3 00:00:00 grep 16027[[email protected] ~]# top//top process shows there is a zombie process top-04:02:20 up 2:46, 4 users, load a verage:0.21, 0.0.15tasks:280 Total, 1 running, 278 sleeping, 0 stopped, 1 zombiecpu (s): 1.2%us, 0.6%sy, 0.0%ni, 97.4%id, 0 .7%wa, 0.0%hi, 0.0%si, 0.0%stmem:2050752k total, 1819480k used, 231272k free, 141876k buffersswap:6291448k to Tal, 0k used, 6291448k free, 775368k cached[[email protected] ~]# ps-ef|grep 16027//120 seconds after zombie process disappears root 16513 8866 0 04:01 pts/3 00:00:00 grep 16027
C program demonstrates the process of producing a zombie process