#include <unistd.h>
#include <sys/wait.h>
#include <iostream>

using std::cerr;
using std::cout;
using std::endl;

int main(int argc, char **argv) {
  cout << "[parent] about to fork.." << endl;
  sleep(8);

  pid_t cpid = fork();
  if (cpid == 0) {
    // child
    cout << "[child] I'm alive!" << endl;
    sleep(10);
    cout << "[child] exiting...becoming a zombie." << endl;
    return EXIT_SUCCESS;
  } else if (cpid < 0) {
    // parent -- fork failed
    cerr << "[parent] fork failed :(" << endl;
    return EXIT_FAILURE;
  }

  // parent -- fork succeeded
  int stat_loc;
  cout << "[parent] Child process is running!" << endl;
  sleep(20);

  cout << "[parent] Waiting for child process to finish..." << endl;
  waitpid(cpid, &stat_loc, 0);

  cout << "[parent] Child process is done." << endl;
  sleep(8);
  return EXIT_SUCCESS;
}