Background
We know that when a process fork out of a child process, there is no child process to recycle, then the state after the child process has finished running becomes a zombie process.
We can use wait and waitpid to reclaim the child process and prevent the zombie process from appearing.
But wait and waitpid, either running in a blocking fashion or running in polling mode, are taking up CPU resources very much.
This article describes how a parent process recycles a child process through an asynchronous operation!
Principle
A SIGCHLD signal is sent to the parent process after each child process has finished running. By default, the parent process ignores the signal. As a result, we only need to capture the sigchld signal, and we can recycle the sub-process asynchronously.
Core Code 1
void WaitChild1 (int sig) {int status = 0;if (Waitpid ( -1,&status,wnohang) >0) {printf ("The child exit status is%d\n", Wexitstatus (status));} else{printf ("failure!\n");}}
Therefore, we recycle all the sub-processes that generate SIGCHLD signals through waitpid.
It's like a simple look!!!
Yes, it's that simple!
However, the code above has a bug, and if there are many child processes exiting at the same time, the parent process will be sent a signal.
And we know that due to the constraints of the bitmap, the conventional signal appears multiple times, only one time. Therefore, if there is a child process at the same time, the above code must not be all the child processes are recycled!
So, we need to strengthen the code!!!!
Upgrade Core Code
void waitChild2 (int sig) {int status = 0;pid_t PID = 0;while (pid = Waitpid ( -1,&status,wnohang)) >0) {printf ("%d ' s ex It status is%d\n ", Pid,wexitstatus (status));}}
As the above code, the change is actually very simple, just change the IF to the while!
Full version Code
File Description: wait.c//Author: High minor//creation time: June 27, 2017 Tuesday 20:45 16 sec.//Development environment: Kali linux/g++ v6.3.0////////////////////////////////////#include <stdio.h> #include <unistd.h> #include <sys/ types.h> #include <sys/wait.h> #include <stdlib.h>void waitChild1 (int sig) {int status = 0;if (Waitpid (-1, &status,wnohang) {>0) {printf ("The child exit status is%d\n", Wexitstatus (status));} else{printf ("failure!\n");}} void waitChild2 (int sig) {int status = 0;pid_t PID = 0;while (pid = Waitpid ( -1,&status,wnohang)) >0) {printf ("%d ' s ex It status is%d\n ", Pid,wexitstatus (status));}} int main () {signal (SIGCHLD,WAITCHILD2); int pid = fork (); if (PID = = 0) {//childprintf ("I am child,my pid is%d\n", getpid ()); E XIT (1);} PID = fork (), if (PID = = 0) {//childprintf ("I am child,my pid is%d\n", getpid ()); exit (1);} PID = fork (), if (PID = = 0) {//childprintf ("I am child,my pid is%d\n", getpid ()); exit (1);} PID = fork (), if (PID = = 0) {//childprintf ("I am child,my pid is%d\n", getpid ()); exit(1);} while (1) {printf ("I am father,my pid is%d\n", getpid ()); sleep (5);} return 0;}
Asynchronous recycling of sub-processes under Linux