Q:

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' 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?

A:

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

