Linux system Programming--special process zombie process

Source: Internet
Author: User

Zombie Processes (Zombie process)

The process has finished executing, but the resource occupied by the process has not been reclaimed. This process is called a zombie process .

At the time each process exits, the kernel frees all of the process's resources, contains open files, occupies memory, and so on. however , it still retains certain information, which mainly refers to the Process Control block information (including process number, exit status, execution time, etc.). Until the parent process obtains its state through wait () or waitpid (), and frees it (see "Waiting for Process End" for a detailed usage method). This causes a problem, assuming that the process does not call wait () or waitpid (), then the reserved piece of information is not released and its process number is always occupied. However, the system can use the process number is limited, assuming a large number of zombie processes. The system cannot produce a new process because no process number is available. This is the threat to the zombie process and should be avoided .

The child process has finished executing, and the parent process does not call the wait () or waitpid () function to reclaim the child process's resources as a result of the child process becoming a zombie process .

The zombie process test program is as follows:

#include <stdio.h> #include <unistd.h> #include <errno.h> #include <stdlib.h>int main (int argc, Char *argv[]) {    pid_t pid;    PID = fork ()//Create Process    if (PID < 0) {//Error        perror ("Fork Error:");        Exit (1);    } else if (0 = = pid) {//subprocess        printf ("I am child process. I am exiting.\n ");p rintf (" [son id]:%d\n ", Getpid ());        Exit (0);    } else if (PID > 0) {//parent process//parent process does not call Wati () or Watipid () sleep (1);//guarantees that the child process first executes printf ("I am father process. I'll sleep The seconds\n ");p rintf (" [Father ID]:%d\n ", Getpid ()); while (1); Do not let the parent process exit}    return 0;}

Execution Result:



At the terminal knock:Ps-ef | grep defunct. The defunct in the back angle brackets are zombie processes.

We'll start another terminal to see the status of the process. What are the zombie processes:


Or: With PS-E



How to avoid the zombie process?

1) The simplest way, the parent process waits for the child process to end through functions such as wait () and waitpid (), but this causes the parent process to hang.

For detailed usage, see Process Control: End process, wait for process end.




2) Assume that the parent process is dealing with a lot of things. Can not hang, through the signal () function artificial processing signal SIGCHLD, only to have the child process exit itself to invoke the specified good callback function, because the child process after the end of the parent process will receive the signal SIGCHLD, in its callback function can call wait () or waitpid () Recovery.

For more specific use of the signal, see "signal interruption processing".


Test code such as the following:

#include <stdio.h> #include <unistd.h> #include <errno.h> #include <stdlib.h> #include < Signal.h>void sig_child (int signo) {    pid_t  pid;       Handling the zombie process,-1 represents waiting for a sub-process, Wnohang represents not clogging while    (PID = Waitpid ( -1, NULL, Wnohang)) > 0) {printf ("Child%d terminated. \ n ", pid);}} int main () {    pid_t pid;    Create capture subprocess exit signal//Just to exit the child process, triggering sigchld. Own active call Sig_child ()    signal (SIGCHLD, sig_child);    PID = fork ()//Create Process    if (PID < 0) {//Error        perror ("Fork Error:");        Exit (1);    } else if (PID = = 0) {//Sub-process        printf ("I am child process,pid ID%d.i am exiting.\n", getpid ());        Exit (0);    } else if (PID > 0) {//Parent process sleep (2);//Ensure that the child process executes printf first ("I am father, I am exited\n\n"); System ("Ps-ef | grep defunct "); See if there is a zombie process}    return 0;

Execution Result:



3) Assume that the parent process does not care about when the child process ends. Then you can use signal (SIGCHLD, sig_ign) to notify the kernel, the end of their child process is not interested, the parent process ignores this signal, then the end of the child process, the kernel will be recycled. And no longer sends a signal to the parent process. For more specific use of the signal, see "signal interruption processing".

#include <stdio.h> #include <unistd.h> #include <errno.h> #include <stdlib.h> #include < Signal.h>int Main () {    pid_t pid;    Ignore signal of child process exit signal//Then the kernel is recycled after the child process has finished. And no longer sends a signal to    the parent process signal (SIGCHLD, sig_ign);    PID = fork ()//Create Process    if (PID < 0) {//Error        perror ("Fork Error:");        Exit (1);    } else if (PID = = 0) {//Sub-process        printf ("I am child process,pid ID%d.i am exiting.\n", getpid ());        Exit (0);    } else if (PID > 0) {//Parent process sleep (2);//Ensure that the child process executes printf first ("I am father, I am exited\n\n"); System ("Ps-ef | grep defunct "); See if there is a zombie process}    return 0;

Execution Result:



4) Some other tricks. is fork () two times, the parent process fork () a child process, and then continue to work, the child Process fork () after a grandchild process exits, then the sun process is init takeover, after the Sun process, init (process 1th) will be recycled. Just the recycling of the sub-process and do it yourself. "Advanced Programming for UNIX Environment" section 8.6 says very specific. The principle is to make the child process an orphan process. The parent process thus becomes the INIT process (process 1th), which can handle the zombie process through the init process (process 1th). Many other details. Please see the orphan process of the special process.

Source code such as the following:

#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <errno.h>int main () {    pid_t  pid;    Create the first child process    pid = fork ();    if (PID < 0) {//Error        perror ("Fork Error:");        Exit (1);    } else if (PID = = 0) {//Sub-process            //Sub-process re-create child process        printf ("I am the first children process.pid:%d\tppid:%d\n", Getpid (), Getppid () );        PID = fork ();        if (PID < 0) {            perror ("Fork Error:");            Exit (1);        } else if (PID = = 0) {///child process//sleep 3s guarantees that the following parent process exits, so that the current child process's father is the init process sleep (3);p rintf ("I am the second child process.pid:%d\tppid :%d\n ", Getpid (), Getppid ()); exit (0);} else if (PID >0) {//parent process exits            printf ("First procee is exited.\n");            Exit (0);        }    } else if (PID > 0) {//parent process//parent process exits the first child process, reclaims its resource if (Waitpid (PID, NULL, 0)! = pid) {perror ("Waitepid error:"); exit (1);} Exit (0);}    return 0;}

Execution Result:


All source code Download: http://download.csdn.net/download/lianghe_work/8999129


Linux system Programming--special process zombie process

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.