#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
  pid_t pid;

  pid = fork();

  if (pid < 0) {
    fprintf(stderr,"Error: can't fork a process\n");
    perror("fork()");
    exit(1);
  } else if (pid) { // I am the parent
    int status;

    // wait for the child
    if (waitpid(pid,&status,0) == -1) {
      perror("waitpid()");
    } else {
      // print its exit code
      fprintf(stdout,"I am the parent. My child exited with code %d\n",
         WEXITSTATUS(status));
    }
  } else {
    fprintf(stdout,"I am the child and my pid is %d\n",getpid());
    sleep(5);
    exit(42);
  }
  
  exit(0);
}

