Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
}
like image 995
TAKCHI CHAN Avatar asked Nov 17 '25 23:11

TAKCHI CHAN


1 Answers

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.

like image 137
2 revs, 2 users 60%John Bollinger Avatar answered Nov 20 '25 13:11

2 revs, 2 users 60%John Bollinger



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!