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

int main() {
  printf("Printed before fork.\n");

  pid_t result =  fork();
  if (result > 0) {
    /* this will print immediately */
    printf("ppp -- I am the parent. My pid is %d. Child's pid is %d\n",
           getpid(), result);

    int status;
    int child = wait(&status);

    /* this prints only after child finishes */
    printf("ppp -- Child %d has terminated.\n", child);
    if (WIFEXITED(status))
      printf("Exit code %d.\n", WEXITSTATUS(status));
  }
  else if (result == 0) {
    /* delay so we can see that parent waits */
    sleep(3);
    printf("ccc -- I am the child. My pid is %d. My parent's pid is %d.\n",
           getpid(), getppid());
    exit(62);
  }
  else
    printf("This should not print. If so, fork failed.\n");
  
  return 0;
}



/***********************************************************************/
#if 0

Printed before fork.
ppp -- I am the parent. My pid is 32729. Child's pid is 32730

< child sleeps, and parent waits >

ccc -- I am the child. My pid is 32730. My parent's pid is 32729.
ppp -- Child 32730 has terminated.
Exit code 62.


#endif
/***********************************************************************/
