Thread: init adopts zombie process?

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    48

    init adopts zombie process?

    Hi
    I tried to create a zombie process with the following program:
    Code:
    int main(void)
    {
            pid_t pid;
            int status;
    
            if ((pid = fork()) < 0)
                    perror("fork error");
            else if (pid == 0){ /* child process*/
                    exit(0);
            }
            printf("child process ID: %d\n", pid);
            sleep(10);
    
            return 0;
    }
    I can observe the "Z" state with the ps command, but this zombie process (the child process) only exists in the duration from its termination to its parent termination. I don't wait() the child process in the parent, so why doesn't the zombie process exist after the parent terminates?

    In <apue2>,
    But what happens if the parent terminates before the child? The answer is
    that the init process becomes the parent process of any process whose
    parent terminates. We say that the process has been inherited by init. What
    normally happens is that whenever a process terminates, the kernel goes
    through all active processes to see whether the terminating process is the
    parent of any process that still exists.
    My understanding is this: by the time the parent terminates, if there are child processes already terminated and still running, init will adopt the running ones, not the already terminated ones. (don't the "active" and "still exists" in apue2 mean this?) So a zombie child process won't be adopted by init. In my case, by the time the parent terminates, the child is not "active" and won't be adopted by init.

    Besides, the child process in my program disappears immediately after the parent terminates. As I described, I don't think this is done by init, then who did?

  2. #2
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    All processes, whether running or zombied, are adopted by init when their parent process terminates. init, additionally, will reap any zombie processes.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    48
    Thank you. I got a fully understanding now.

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by password636 View Post
    My understanding is this: by the time the parent terminates, if there are child processes already terminated and still running, init will adopt the running ones, not the already terminated ones. (don't the "active" and "still exists" in apue2 mean this?) So a zombie child process won't be adopted by init. In my case, by the time the parent terminates, the child is not "active" and won't be adopted by init.
    Why would it behave that way? That would cause zombies to stack up on the system, which is incorrect.

    init adopts ALL processes whose parents have died, whether those processes are still running or not.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #5
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    As always, there is another way to prevent the zombies. . .

    throw this into your code inside your main control loop (in the parent). . . this is the zombie garbage collection. Leaving zombies while your parent is running is considered to be "bad".
    Code:
    waitpid(-1, NULL, WNOHANG);
    Andy

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. create a child process that creates a child process
    By cus in forum Linux Programming
    Replies: 9
    Last Post: 01-13-2009, 02:14 PM
  2. Replies: 3
    Last Post: 10-15-2008, 09:24 AM
  3. Problem with forking a process
    By Unitedroad in forum C Programming
    Replies: 10
    Last Post: 10-04-2007, 01:43 AM
  4. process programming
    By St0rM-MaN in forum Linux Programming
    Replies: 2
    Last Post: 09-15-2007, 07:53 AM
  5. Process sending file descriptors to another process
    By Yasir_Malik in forum C Programming
    Replies: 4
    Last Post: 04-07-2005, 07:36 PM