clinuxzombie-process

what's the relationship between exit(0) and zombie process


I found that it can't create a zombie process when I remove exit(0); from the child part. Can you tell me why?

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>

int main() {
      if(!fork()) {
        printf("child pid=%d\n", getpid());
        exit(0);
      }

      sleep(20);

      printf("parent pid=%d\n",getpid());
      exit(0);
}

Solution

  • A zombie process is a dead child process that the parent process hasn’t checked on. In the original code, the child ends 20 seconds earlier than the parent, so it’s a zombie for 20 seconds. If you remove the first exit(0), they both stay alive for 20 seconds because in the child, control passes right out the bottom of the if block unless something stops it.

    Thus, if you remove the child's exit() then not only is it unlikely to go zombie for an observable amount of time, but you should see it print a "parent pid" message in addition to its "child pid" message.