4

Consider this example -

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>

int main()
{
  pid_t pid = fork();
  if (pid > 0)
  {
    printf("Child pid is %d\n", (int)pid);
    sleep(10);
    system("ps -ef | grep defunct | grep -v grep");
  }
  return 0;
}

In this example, the child process remains a zombie until the parent process terminates. How did this zombie process get cleaned up without being reaped by any process ?

$ ./a.out 
Child pid is 32029
32029 32028  0 05:40 pts/0    00:00:00 [a.out] <defunct>

$ ps -p 32029 
  PID TTY          TIME CMD

2 Answers 2

2

When the parent dies, children are inherited by init (pid=1) which will wait on them and clean the process table.

2

The point of a zombie process is to leave a placeholder in the kernel data structures to hold performance data from the process.

When the parent process calls wait(2), that performance data is collected by the parent and the kernel releases the data structure.

If the parent exits before the data is reaped, the performance data would be, where appropriate, merged into the parent's own performance data, which would then by reaped by the parent's parent.

In addition, a child process whose parent exits is inherited by process 1 (usually called "init"), which could then collect any remaining data with wait(2), allowing the kernel to release the data structure.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .