Three ways to avoid zombie processes

Source: Internet
Author: User
Tags signal handler sleep

The parent process of the zombie process to recycle, the parent process every time to query whether the child process is finished and recycled, call wait () or Waitpid (), notify the kernel to release the zombie process

/* Let the parent process of the zombie process recycle, and the parent process queries the child process to see if it ends and recycles at intervals,
calls wait () or waitpid (), notifies the kernel to release the zombie process
*/

#include <stdio.h >
#include <unistd.h>
#include <sys/types.h>
#include <stdlib.h>
#include <sys/wait.h>
int main (void)
{
	pid_t pid=fork ();
	int n=0;
	char *s=null;
	if (pid<0) {
		perror ("fork creat");
	} else if (pid>0) {//parent process
		n=1;
		S= "This is parent process";
	} else{//pid=0//Sub-process
		n=10;
		S= "This was child process";
	}
	for (int i=0;i<n;++i) {
		printf ("%s\n", s);
		if (pid>0) {	
			printf ("I am waiting for you, my child\n");
			Wait (NULL);	
		}
		Sleep (1);
	}
	printf ("%s bye\n", s);
	return 0;
}
Run results

Xfliu@ubuntu: Avoid the zombie process $./BIN/1 This
is the parent process I am waiting for your, my child this is the child process
t He is a child process, this is a child process, which is a child process, this is a child process, this is a child
proce The SS this is the child process, which is the child process, which is the child process.
Child process bye The is
parent process bye

Two
Uses signal SIGCHLD to notify processing and calls the wait function in the signal handler

/*
use signal sigchld to notify processing and call the wait function in the signal handler
*/

#include <stdio.h>
#include <unistd.h>
# include<sys/types.h>
#include <stdlib.h>
#include <sys/wait.h>
#include < signal.h>
void signal_wait (int isig)
{
	wait (NULL);
	printf ("Child is cleaned\n");
}
int main (void)
{
	pid_t pid=fork ();
	char *s=null;
	if (signal (sigchld,signal_wait) ==sig_err) {
		perror ("Install signal_wait");
	}
	if (pid<0) {
		perror ("fork creat");
	} else if (pid>0) {//Parent process
		sleep (1);
		S= "This is parent process";
	} else{//pid=0//subprocess s= "This is a child process
		";
	}
	printf ("%s bye\n", s);
	return 0;
}

Results

This is the child process bye child is cleaned the is
parent
processes bye Xfliu@ubuntu: Avoid zombie progress $ 


Third, let the zombie process into the orphan process, from the init recovery, is to let the father died first

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdlib.h >
#include <sys/wait.h>
int main (void)
{
	pid_t pid=fork ();
	int n=0;
	char *s=null;
	if (pid<0) {
		perror ("fork creat");
	} else if (pid>0) {//parent process
		n=1;
		S= "This is parent process";
	} else{//pid=0//Sub-process
		n=10;
		S= "This was child process";
	}
	for (int i=0;i<n;++i) {
		printf ("%s\n", s);
		/*
		if (pid>0) {	
			printf ("I am waiting for you, my child\n");
			Wait (NULL);	
		}
		Sleep (1);
		*
	/} printf ("%s bye\n", s);
	return 0;
}

Operation Result:

Xfliu@ubuntu: Avoid zombie processes $./BIN/2 This is the parent process, which is the
parent process Bye This
is the child process, this
IS-child process, which is a child process that is a child process that is a child process, which is a child
process< C8/>this is a child process, this is a child process, which is a child process, this is a child process, this is
Chi LD Process bye

About wait and waitpid after the study

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.