Waitpid and SIGCHLD signal recovery zombie Process

Source: Internet
Author: User
Tags terminates

For multiple processes, the parent process generally needs to track the exit status of the child process. Therefore, when the child process ends running, the kernel does not immediately release the table entries for the process table of the session. The information query that satisfies the parent process for subsequent child process exits (postmortem autopsy), of course, if the parent process is still running. After the child process finishes, the parent process reads its exit state, we call the child process in a zombie state (user space has been freed, it cannot be dispatched).

The two system call functions are described first:

#include <sys/types.h> #include <sys/wait.h>pid_t wait (int *status);//wait is a blocking function that waits for the child process to be recycled. Returns 1 if no child process is returned. idpid_t waitpid (pid_t pid, int *status, int options) that successfully returned a subprocess, or status is an incoming parameter and an outgoing parameter, which saves the process exit value by an incoming address. Here is a 32-bit part of the int type that is used to hold the process exit value, partly to hold the signal value. The blocking of the wait function is obviously not what the server expects, and the Waitpid function solves the problem <pre name= "code" class= "CPP" > First parameter:
< 1 reclaim any child processes within the specified process group
-1 Reclaim any child processes
0 Recycle and current call waitpid all child processes of a group
> 0 Reclaim a child process with the specified ID

Options
Wnohang No child process end, immediate return (non-blocking)
wuntraced If a child process is sigchld due to a stopped production, WAITPID returns immediately
wcontinued If a child process sigchld due to Sigcont wake-up, WAITPID returns immediately
Get status
wifexited (status) child process Normal exit terminates, returns true
Wexitstatus (status) Returns the child process normal exit value
Wifsignaled (status) Sub-process is signaled and returns true
Wtermsig (status) returns the signal value of the terminating child process
wifstopped (status) child process is stopped and returns true
Wstopsig (status) returns the signal value of the stop child process
The wifcontinued (status) sub-process transitions from a stopped state to a ready state, returning a true
#include <stdio.h> #include <sys/types.h> #include <sys/wait.h> #include <unistd.h>int main () { pid_t pid=fork (); if (pid<0) {perror ("fork"); return 0;} if (pid==0) {printf ("Child process pid:%d \ n", Getpid ()); "Sleep"; return 12;} Else{int status=0;printf ("parent process pid:%d \ n", Getpid ());p rintf ("Wait pid:%d \ n", Wait (&status)); Wait is the blocked printf ("Process exit value:%d \ n", Wexitstatus (status)), the exit value of the//print process, printf ("Signal value:%d \ n", Wtermsig ( status);//print signal value kill-9 kill the child process will be played 9}return 0;}

There are currently two ways to recycle the zombie process 1.wait blocking mode 2.waitpid polling mode. Both of these methods will cause the parent process to be unable to do anything else.

Next we will talk about how to use SIGCHLD to handle zombie processes

The default processing of the SIGCHLD condition is to ignore

1. When the child process terminates

2. When the child process receives the sigstop signal stops

3. The child process is in a stopped state and is awakened after receiving Sigcont

#include <stdio.h> #include <errno.h> #include <signal.h> #include <sys/types.h> #include < sys/wait.h> #include <unistd.h>void sig_child (int num) {int status;pid_t pid;while (pid=waitpid ( -1,& Status,wnohang)) >0)//Reclaim subprocess non-blocking mode {if (wifexited (status)) {printf ("Child%d Exit%d\n", PID, Wexitstat US (status));} else if (wifsignaled (status)) {printf ("Child%d Cancel signal%d\n", PID, Wtermsig (status);}}}    int main () {pid_t pid;int i=0;for (i=0;i<4;i++) {if (Pid=fork ()) ==0)//If the child process exits the loop directly, the parent process fork out four child processes {break; }else if (pid<0) {perror ("fork Error"); return 0;}} if (pid==0) {printf ("Child process pid:%d \ n", Getpid ()); sleep; return 10;}  Else{struct sigaction sigact; sigact.sa_handler=sig_child;//Signal Response function sigact.sa_flags=0;//Select the first type of function (based on struct sigaction struct) sigemptyset (&  Sigact.sa_mask);//0,sigchld default is ignored Sigaction (sigchld,&sigact,null); Register the signal while (1) {printf ("Parent ID is:%d \ n", Getpid ()); sleep (1);}} return 0;}
Also open a terminal input kill-9 2229, you can see that the process ID 2229 is killed by the signal.



Copyright NOTICE: Welcome to reprint, if there are deficiencies, please treatise.

Waitpid and SIGCHLD signal recovery 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.