The zombie process in the Linux system and the zombies in the real world (although I have not seen it) are similar, although they are dead, but because no one gives them a corpse, they can walk around. A zombie process is a process that has been terminated, but still retains some information, waiting for its parent process to bury it. How did the zombie process occur? If a process is to recycle all the resources allocated to it at the time it terminates, the system will not produce the so-called zombie process. So what information do we keep when we say that a process is terminated? Why do we need to keep this information after termination? A process terminates in many ways, and some information is useful to the parent process and the kernel after the process terminates, such as the ID number of the process, the exit status of the process, the CPU time the process is running, and so on. As a result, when the process terminates, it reclaims all of the kernel's memory allocated to it, closes all files it opens, and so on, but retains only a few of the above information for use by the parent process. The parent process can use the wait/Waitpid and other systems are used to clean up the sub-processes and do some finishing work. Thus, the process of a zombie process is that the parent process calls fork to create the child process, the child process runs until it terminates, it is immediately removed from memory, but the process descriptor remains in memory (the process descriptor occupies very little memory space). The state of the child process becomes Exit_zombie, and the parent process sends a SIGCHLD signal, and the parent process should now call the wait () system call to get the exit status of the child process and other information. After the wait call, the zombie process is completely removed from memory. Therefore, a zombie exists in its termination to the parent process call wait, and so on the time of the gap, generally disappear quickly, but if the programming is unreasonable, the parent process never calls wait and other system calls to collect the zombie process, then these processes will always exist in memory. Under Linux, we can use the PS and other commands to view the zombie process in the system, the status of the zombie process marked ' Z ':produce a zombie process according to the above description, it is easy to write a program to produce a zombie process, as follows: #include<stdio.h>#include<sys/types.h>intMain () {//Fork A child processpid_t PID =Fork (); if(PID >0)//Parent Process{printf ("in the parent process, sleep for one miniute...zz...\n"); Sleep ( -); printf ("After sleeping, and exit!\n"); } Else if(PID = =0) { //Child Process exit, and to is a zombie processprintf"in child process, and exit!\n"); Exit (0); } return 0;} The parent process does not write a system call function such as wait, so it becomes a zombie process after the child process exits, and the parent process does not pick up the corpse. We compile and run the process using the following command, and then look at the status of the process in the system: [email protected]:~/documents$ GCC zombie.c-o zombie[email protected]:~/documents$./ZombieinchParent process, sleep forOne miniute...zz ...inchChild process, and exit!# Open another terminal: [email protected]:~$ PS aux | Grep-w'Z' + 2211 1.2 0.0 0 0? Z -: - 6: -[Chromium-browse] <defunct> + 4400 0.0 0.0 0 0? Z October 160:xx[FCITX] <defunct> + 10871 0.0 0.0 0 0pts/4z+ A: + 0:xx[Zombie] <defunct> as can be seen from the above, there is a zombie process in the system. But if the parent process wakes up after waking up, we look at the system process information again and find that the zombie process is missing. [email protected]:~/documents$./ZombieinchParent process, sleep forOne miniute...zz ...inchChild process, and exit!After sleeping, and exit![email protected]:~/documents$ PS aux | Grep-w'Z' + 2211 1.2 0.0 0 0? Z -: - 6: -[Chromium-browse] <defunct> + 4400 0.0 0.0 0 0? Z October 160:xx[FCITX] <defunct> What is this for? The parent process has not died for its child process corpse, ah, how the parent process exits, the zombie process disappeared? does the parent process clean up the child process when it exits? Not actually .... The real reason: After the parent process dies, all its child processes are passed on to the Init process, and the INIT process becomes a new process for that zombie process, and the INIT process periodically calls the wait system call to clear its zombie child. As a result, you will find that the zombie process disappears after the parent process dies in the example above, in fact the INIT process is the body of the corpse! How to avoid the generation of zombie processes do not use kill to pick up the SIGKILL signal such as kill the zombie process as the normal process, because the zombie process is already dead process, it can no longer receive any signal. In fact, if there are not many zombie processes in the system, we do not need to eliminate them, and a few zombie processes do not have any effect on the performance of the system. So when programming, if you can avoid a large number of zombie processes in the system? According to the above described, the child process at the end of the parent process will send a SIGCHLD signal, Linux default is to ignore the signal, we can display the installation of this signal, in the signal processing function call wait and other functions to the corpse, so that the zombie process can be avoided long-term presence in the system. The sample code is as follows: #include<stdio.h>#include<signal.h>#include<string.h>#include<sys/types.h>#include<sys/wait.h>sig_atomic_t child_exit_status;voidClean_up_child_process (intsignal_num) { /*Clean up child process*/ intstatus; Wait (&status); /*store its exit status in a global variable*/Child_exit_status=status;}intMain () {/*handle SIGCHLD by calling Clean_up_child_process*/ structsigaction sigchild_action; memset (&sigchild_action,0,sizeof(sigchild_action)); Sigchild_action.sa_handler= &clean_up_child_process; Sigaction (SIGCHLD,&sigchild_action, NULL); /*Fork A child, and let the child process dies before parent*/pid_t C_pid; C_pid=Fork (); if(C_pid >0) {printf ("in the parent process, and sleep for on mininute...zz...\n"); Sleep ( -); } Else if(C_pid = =0) {printf ("in child process, and exit now\n"); Exit (0); } Else{printf ("Fork failed!\n"); } return 0;}
Http://www.cnblogs.com/hazir/p/zombie_process.html
Zombie Processes in Linux systems