19th: Two classic ways to deal with zombie processes

Source: Internet
Author: User
Tags terminates

Objective

If the parent process does not end, and the child process terminates. The child process will always be a zombie process until the parent process calls the wait function to reclaim the child process or the parent process terminates.

This article will provide two ways to deal with this problem.

Method One: Parent process recovery method

The wait function blocks its callers until one of its child processes terminates. Therefore, the parent process can call the wait function to reclaim its zombie subprocess. In addition, the WAITPID function provides a more detailed function (which adds non-blocking and a specified wait function), and the reader is asked to check the relevant information.

Code implementation
1#include <unistd.h>2#include <sys/wait.h>3#include <stdio.h>4#include <stdlib.h>5 6 intMain ()7 {8     intpid;9     int*status;Ten  Oneprintf"%s\n","Start Parent Process"); A  -     if(PID = fork ()) <0) { -printf"%s\n","failed to create child process"); theExit1); -     } -     Else  -         if(PID = =0) { +printf"%s\n","Enter child process"); -Sleep4); +             //terminating a child process AExit0); at         } -     Else { -         //go to Parent process -         //Reclaim Zombie Child process - Wait (status); -printf"%s\n","Recycling Complete"); in     } -  toExit0); +}
Run Tests

Results analysis

The third line of "Recycle complete" is displayed after the program executes for four seconds. This means that although I blocked the child process for 4 seconds, the parent process does not terminate before the child process. Because it calls the wait function, it needs to wait for a subprocess to end and recycle it, otherwise it's stuck there.

Method Two: Init process recovery method

The above solution requires the parent process to wait for the child process, but in many cases this is not appropriate because the parent process may have other tasks to do and cannot be blocked here. Please understand the following two concepts before you can complete the process of reclaiming a child process without waiting for the parent:

1. If the parent process ends before the child process, the parent process of the child process is automatically changed to the init process.

2. If the child process of Init ends, the init process automatically reclaims the resources of its child processes instead of turning it into a zombie process.

Code implementation
1#include"apue.h"2#include <sys/wait.h>3 4 int5Mainvoid)6 {7 pid_t pid;8 9     if(PID = fork ()) <0) {//Create first child processTenErr_sys ("Fork Error"); One}Else if(PID = =0) {//Enter the first child process A         if(PID = fork ()) <0)//Create a second child process -Err_sys ("Fork Error"); -         Else if(PID >0)//Enter the first child process theExit0);//terminates the first child process -         //The second child process executes after sleep 2S, so the first child process is normally terminated first.  -Sleep2); -         //at this point, the first child process is definitely terminated, then its parent process automatically becomes init.  +printf"second child, parent PID =%d\n", Getppid ()); -Exit0); +     } A  at     //The parent process waits and reclaims the first child process -     if(Waitpid (PID, NULL,0) !=pid) -Err_sys ("waitpid Error"); -  -     //after the parent process has executed here, you can exit or perform other tasks.  -     //for the second child process just now, it inherits the resources of the parent process, and it is also recycled by the INIT process when it terminates . in     //does not become a zombie process.  -Exit0); to}
Description

1. Fork after the child process is created, the child process has a copy of the resource for the parent process instead of sharing the resource with it.

2. When a child process is terminated, it becomes a zombie process, not a system bug, but some of its information operating systems or users may later be used after the child process terminates.

19th: Two classic ways to deal with zombie processes

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.