How to handle zombie processes

Source: Internet
Author: User
Tags terminates

1. What is a zombie process

If the parent process terminates before the child process, all the parent processes of the child processes will be changed to the INIT process, which we call the INIT process adopted. You can see that the parent process of the child process Ppid has changed to 1 by using the PS command.


When a child process terminates before the parent process, the kernel holds a certain amount of information for each terminating child process, so this information is available when the parent process of the terminating process calls wait or waitpid. This information includes at least the process ID, the terminating state of the process, and the total amount of CPU time used by the process. The storage area used by other processes, open files are freed by the kernel.


A process that has been terminated, but whose parent process has not yet dealt with it (getting information about terminating the child process, releasing the resources it still occupies) is called a zombie process. the PS command prints the status of the zombie process to Z.

Can imagine, for example, a Web server side, if each received a connection to create a child process to process, after the completion of the child process. If you do not use the wait or Waitpid function in the parent process to clean up, these child processes will all become zombie processes, the number of processes in the Linux system generally have a fixed limit value, the zombie process will gradually exhaust the system resources.

2. See examples of zombie processes
#include <stdio.h> #include <string.h> #include <unistd.h> #include <stdlib.h> #include < errno.h> int main (int argc, char **argv) {   pid_t pid;   for (int i=0; i<5; i++) {       if (PID = fork ()) < 0) {           printf ("Fork error,%s\n", Strerror (errno));           return-1;       }          /* Child */       if (PID = = 0) {           sleep (1);           Exit (0);       }     }     /* Parent *   /sleep (+);   return 0;}

After compiling, after executing the program's command, add the "&" symbol, indicating that the current program is running in the background.

Then enter:

Ps–e–o Pid,ppid,stat,command|grep [program name]

You can see that 5 sub-processes are already a zombie process.

3.SIGCHLD signal and process zombie processes

When the child process terminates, the kernel sends a SIGCHLD signal to its parent process, which can optionally ignore the signal or provide a handler function to receive the signal . The system default action for this signal is to ignore it.

We don't want to have too many zombie processes, so when the parent process receives the SIGCHLD signal, it should call the wait or Waitpid function to perform the child process and release the resources that the child process occupies.

The following is a simple example of capturing a sigchld signal and then using the wait function for processing:

#include <stdio.h> #include <string.h> #include <unistd.h> #include <stdlib.h> #include < errno.h> #include <signal.h> #include <sys/wait.h> void deal_child (int sig_no) {    wait (NULL);} int Main (int argc, char **argv) {    signal (SIGCHLD, deal_child);     pid_t pid;    for (int i=0; i<5; i++) {        if (PID = fork ()) < 0) {            printf ("Fork error,%s\n", Strerror (errno));            return-1;        }           /* Child */        if (PID = = 0) {            sleep (1);            Exit (0);        }      }      /* Parent *    /for (int i=0, i<100000; i++) {for        (int j=0; j<100000; J + +) {            int temp = 0;        }      }      return 0;}

Also use the PS command to view the status of the process after running in the background, the results



Of the 5 processes that were created, 3 were completely destroyed, but 2 were still in the zombie process.


this is because when 5 when a process terminates at the same time, the kernel will send to the parent process SIGCHLD the parent process may still be in signal processing at this time. Deal_child function, and before processing is complete, the indirectly received SIGCHLD the signal is lost, and the kernel does not use a queue to store the same signal.

4. Ways to properly handle zombie processes

To solve this problem, we need to use the Waitpid function.

The function prototypes are:


Pid_twaitpid (pid_t pid, int *statloc, int options);


If successful, the process ID is returned, and if set to non-blocking, return 0 indicates that the child process state has not changed and returns-1 on error.

Options parameter can be set to Wnohang constant, representing Waitpid without blocking, if by PID The specified child process is not immediately available and returns immediately 0 .

You only need to modify the SIGCHLD signal processing function:

void Deal_child (int sig_no) {for    (;;) {        if (waitpid ( -1, NULL, wnohang) = = 0) break            ;}  }

After executing the program again, use the PS command to see that no zombie process has been created.

How to handle zombie processes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.