/**
 * File: zombie.c
 * ------------------
 * Novelty program to illustrate how zombies work.
 * Created by Roz.
 */

#include <stdio.h>        // for printf
#include <unistd.h>       // for fork, getpid, getppid
#include "exit-utils.h"   // for our own exitIf

static const int kForkFailed = 1;

int main(int argc, char *argv[]) {
  printf("Currently running process: %d\n", getpid());
  
  pid_t pid = fork();
  exitIf(pid == -1, kForkFailed, stderr, "fork function failed.\n");
  
  if (pid == 0) { // in child
  	printf("Child process (pid=%d) exiting.\n", getpid());
    exit(0); // terminate child
  } else {
  	// in parent, which doesn't reap its child,
  	// so while it runs for a minute, the child 
  	// is in a zombie state once the child exits.
  	// The child will be adopted by init when
  	// the parent process ends.
  	
  	sleep(60);
  	printf("Parent process done.\n");
  }

  return 0;
}