/* forkwait.c (emx+gcc) */

/* 1276 */

#include <stdio.h>
#include <unistd.h>
#include <process.h>

int main (void)
{
  if (fork () == 0)
    {
      printf ("Child: pid = %d\n", getpid ());
      if (fork () == 0)
        {
          printf ("Grandchild: pid = %d\n", getpid ());
          puts ("Sleeping for 5 seconds...");
          sleep (5);
          printf ("Grandchild terminating.\n");
        }
      else
        printf ("Child terminating.\n");
    }
  else
    {
      int pid, st;

      while ((pid = wait (&st)) != -1)
        printf ("Process %d terminated\n", pid);
    }
  return 0;
}
