1. Practice: Create 10 Zombie processes in an orphan process

Source: Internet
Author: User

A zombie process is a process that terminates before the parent process has a chance to collect its exit status with wait or Waitpid.

The child is called the zombie process because the process ended, but still exists in the process table.

the memory and resources allocated to it after the child process exits are freed, but it retains a record in the kernel process table that the kernel retains until the parent process reclaims the child process's exit state.

when the process entries are too long in the process table, they affect performance, causing the creation process to fail, system crashes, and so on.

The fork or EXEC function creates a new process to collect the exiting state of a new process to prevent zombie processes (zombie process), the parent process should call Waitpid wait for the child process to terminate, or the parent process exits, and the zombie process is destroyed. in Code

when i = n there are n zombie processes, and their parent process IP is PID because the PID for the child process has been executed at the beginning of the exit (0) So even in the fork loop, the child process will not create a new subprocess.

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ stat.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>

int main (void)
{
    pid_t pid = fork ();

    if (PID = = 0)
    {   
    int i = 0;
    for (i = 0; i<10; i++)   
    {   
    pid_t pid = fork ();
    if (!pid)
    {   

        exit (0);

    }   
    }   
    Pause ();
    }   
    else
            exit (0);
    
    return exit_success;
}


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.