user5346812
user5346812

Reputation:

Confused on "zombie processes" in c programming

Question: How can I determine which one produces a "zombie process"

// Case 1

while(fork())

    ; 

exit(0);


// Case 2

while(!fork())

     ;

exit(0);

I know that a "zombie" is - when a process terminates and still consumes resources. (Or at least I think that's what it is)

I think that Case 1 is the case in which will produce a zombie process because it will return -1 on an error, and while(-1) = true so it will just keep forking? I'm not really sure. Any insight would be great,

BTW: This code is being run on a Linux environment in the c programming language

Thanks in advance

Upvotes: 1

Views: 1612

Answers (1)

fluter
fluter

Reputation: 13836

A zombie process is a child process that terminates, but has not been waited for by the parent. Child processes are typically created by fork:

int pid = fork();
if (pid < 0) {
     // fork failed
} else if (pid == 0) {
    // this is the child process
}
// this is the parent

fork returns 0 to the child process and a positive pid to the parent. There are two cases in terms of their termination:

  1. child exit before parent, then the child becomes a "zombie" process until the parent calls the wait family functions to get the child's exit status.

  2. parent exit before child, then the child will be re-parented to the init process, the init process will call wait upon the child exits. This works because the parent will receive SIGCHLD signal when its child exits, and it can call wait in the signal handler. So in this case, no zombie will be created.

Also, note the posix defines no zombies are created if the SIGCHLD is ignored by the parent:

POSIX.1-2001 specifies that if the disposition of SIGCHLD is set to SIG_IGN or the SA_NOCLDWAIT flag is set for SIGCHLD (see sigaction(2)), then children that terminate do not become zombies and a call to wait() or waitpid() will block until all children have terminated, and then fail with errno set to ECHILD. (The original POSIX standard left the behavior of setting SIGCHLD to SIG_IGN unspecified. Note that even though the default disposition of SIGCHLD is "ignore", explicitly setting the disposition to SIG_IGN results in different treatment of zombie process children.)

For the two case in the OP:

// Case 1
while(fork())  // same as while(fork() != 0)
    ;

exit(0);


// Case 2
while(!fork()) // same as while(fork() == 0)
    ;

exit(0);

The 1st code keeps forking in the parent, no matter it succeeds or not, and the resulted children will exit immediately since the return value will be 0. Since the parent is hung in the while loop no matter the fork succeeds or failed(fork only returns 0 for children), all the children will become zombie.

For the 2nd case, the parent exit immediately when fork returns, but the child will keep forking, and this child will again do the same, that is, it will exit immediately, and the child it created will keep forking. In this case, since the parent exit, all its children will reparented to init process, as a result, no zombies will be created.

Upvotes: 3

Related Questions