Question

How the below program works and create a Zombie process under linux?

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

int main ()
{
  pid_t child_pid;

  child_pid = fork ();
  if (child_pid > 0) {
    sleep (60);
  }
  else {
    exit (0);
  }
  return 0;
}
Was it helpful?

Solution

It creates children and doesn't wait (with one of the wait* system call) for them. And zombies are just that: children that the parents hasn't waited yet, the kernel has to maintain some information for them -- mainly the exit status -- in order to be able to return it to the parent.

OTHER TIPS

The setsid() command is missing.

Every *nix process produces an exit status that must be reaped. This is supposed to be reaped by the parent process using a wait() statement, if the child is supposed to terminate first.

The setsid() command switches the parent process to init when the parent terminates before the child process.

Root should be able to remove zombies from the process list using kill -9. Inexperienced programmers sometimes omit setsid(), which will hide bugs that produce errors that would otherwise clog the disk drive.

In days of old, the system administrator would use zombies to identify inexperienced programmers that need additional training to produce good code.

The exit status harvested by init is sent to syslog when the kernel terminates a program prematurely. That exit status is used to identify the nature of the bug that caused the early termination (error conditions not handled by the programmer).

Exit status reported in this way becomes part of the syslog or klog files, which are commonly used to debug code.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top