Asynchronous recycling of sub-processes under Linux

Source: Internet
Author: User
Tags kali linux

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

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.