简体   繁体   中英

How to create Zombie Process?

I was writing programs to create zombie process (for purpose of learning).

int main(int argc, char *argv[]) {

  int i = ::fork();

  if(i == 0) sleep(30);
  else printf("process %d/%d\n", getpid(), i); 

  return 0;

}

The above code call fork without waitpid on the child process. However, after launching this code I use ps aux | grep 'Z' ps aux | grep 'Z' trying to find the zombie process. I didn't see anything. The child process appears in the process list and soon after 30 seconds ( sleep ) it disappeared and I find nothing with status 'Z' in the process list. Does this code actually create a zombie process?

int main(int argc, char *argv[])
{
    int i = fork();

    if(i == 0)
    {
        exit(0); /* we let the child die as fast as possible */
    } else {
        printf("process %d/%d\n", getpid(), i);
        sleep(30); /* during these 30 sec, the child is a zombie, because it is dead, but not reaped with waitpid yet. Use ps command during this to see it in the process list */
    }
    /* when we do not reap the child before we exit, it will either be removed by OS or reaped by init as it is reparented */
    return 0;
}

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