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

int main ()
{
  int status;
  pid_t pid;

  pid = fork ();

  /* check for an error */
  if (pid == -1) {
    fprintf (stderr, "Fork failed.\n");
    exit (-1);
  }

  /* see if we're the parent or the child */
  if (pid == 0) {
    printf ("Hello from the child.\n");
    exit (7);
  }

  /* parent continues */
  printf ("Hello from the parent.\n");
  pid = wait (&status);

  if (pid == -1) {
    fprintf (stderr, "Wait failed.\n");
    exit (-1);
  }

  status = WEXITSTATUS(status);
  printf ("Child exited with error code %d.\n", status);

  exit (0);
}


/* HERE'S THE OUTPUT:

[downey@rocky fork]$ fork
Hello from the parent.
Hello from the child.
Child exited with error code 7.
*/
