简体   繁体   中英

how this program creates zombie process?

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;
}

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM