Linux programming calls fork () two times to avoid zombie processes

Source: Internet
Author: User
Tags exit sleep linux

When we only fork () once, there are parent processes and child processes. There are two ways to avoid creating a zombie process:

The parent process invokes functions such as Waitpid () to receive the child process exit status.

The parent process ends first, and the child process is automatically hosted to the init process (PID = 1).

Currently, consider the case where the child process ended before the parent process:

If the parent process does not handle the child process exit state, the child process remains in a zombie state until the parent process exits.

If the parent process calls Waitpid (), which uses blocking calls to ensure that the child process ends before the parent process finishes, it will cause the parent process to go to sleep after calling Waitpid (), and only the Waitpid () of the parent process will be returned by the child process. If there is a child process end, but the parent process has not yet been executed to Waitpid (), then the subprocess will also be in a zombie state during this period.

From this, you can see that the parent process has a parent-child relationship, and the child process has the opportunity to become a zombie process unless it is guaranteed that the parent process finishes before the child process or that the parent process executes waitpid () before the child process ends. So how do you make it easier for the parent process to create a subprocess that doesn't become a zombie process? This will take two times fork ().

The parent process once fork () produces a subprocess and then immediately executes the waitpid (subprocess pid, NULL, 0) to wait for the child process to finish, then the Subprocess Fork () after the child process then immediately exit (0). This process terminates successfully (the parent process only takes the child process to the corpse, does not need the return value of the subprocess), and the parent process continues to execute. At this time the grandson process is transferred to the INIT process hosting because it loses its parent process (that is, the parent process's child process). So the parent process has no inheritance relationship with grandson process, their parent process is the init,init process at the end of its child process will automatically corpse, so will not have the zombie process.

 #include <stdio.h> #include <sys/wait.h> #include <sys/types.h> #include <unistd.h&    
         
Gt       
           
    int main (void) {pid_t pid;       
        if ((PID = fork ()) < 0) {fprintf (stderr, "fork error!/n");       
    Exit (-1);        
            else if (PID = = 0)//* * */{if (PID = fork ()) < 0) {       
            fprintf (stderr, "Fork error!/n");       
        Exit (-1); else if (PID > 0) exit (0); /* Parent from second fork = =/* We ' re the second child;    
         Our parent becomes init as soon * as we real parent calls exit () in the statement above.    
         * Here's where we ' d continue executing, knowing that's when * we've done, init'll reap our status.       
        * * Sleep (2); printf ("Second Child, Parent PID =%d/n ", Getppid ());       
    Exit (0); } if (Waitpid (PID, NULL, 0)!= pid)//wait-for-child/{fprintf (s       
        Tderr, "Waitpid error!/n");       
    Exit (-1); }/* We ' re the parent (the original process);    
     We continue executing, * knowing that we ' re not the parent of the second child.       
* * EXIT (0); }

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.