Why fork () two times to avoid a zombie process?

Source: Internet
Author: User

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

    • The parent process calls a function such as waitpid () to receive the child process exit status.
    • The parent process ends first, and the child process is automatically managed to the init process (PID = 1).

Now consider the case where the child process ends before the parent process :

    • If the parent process does not process the child process exit state, the child process remains in the zombie process 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, it will cause the parent process to go to sleep after calling Waitpid (), and only Waitpid () that the child process ends the parent process will be returned. If there is a child process ending, but the parent process has not yet performed to Waitpid (), then the child process will also be in the zombie process state.

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

The parent process once fork () produces a child process and then immediately executes the WAITPID (child process PID, NULL, 0) to wait for the child process to end, and then the child process fork () generates the grandson process and then immediately exit (0). This way the process terminates successfully (the parent process simply takes the child process to the corpse, does not need the return value of the child process), and then the parent process resumes execution. The grandson process is then forwarded to the INIT process for hosting because it loses its parent process (that is, the child process of the parent process). So the parent process has no inheritance relationship with the grandson process, their parent process is the init,init process at the end of its child process will automatically corpse, so there will not be a zombie process.

Why fork () two times to avoid a zombie process?

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.