// Aaron Linville
// aaron@linville.org
// http://www.linville.org

// fork.cpp - Identify Parents and Children

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

using namespace std;

int main(int, char *[]) {  
  int stat_loc, pid; 

  if((pid = fork()) == -1) { 
    cerr << "Unable to fork!" << endl;
  } else { 
    if(pid == 0) { 
      cout << "I am the child! My PID is " << getpid() << endl;
    } else { 
      cout << "I am the parent! My PID is " << getpid() << endl;
      wait(&stat_loc); // Wait for child to exit
    } 
  } 

  return 0;
} 

