/*
 * A simple process example.  Forks lots of children and
 * waits for them to exit.
 */

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define CHILDREN 5

int main()
{
  int child;
  int childnum;
  int status;
  int pause;
  int exitval;

  srand(time(NULL));

  for (childnum = 0; childnum < CHILDREN; ++childnum) {
    pause = rand() % 10;
    exitval = rand() % 2;

    if (!fork()) {
      sleep(pause);
      printf("Hi, I'm child %d\n", childnum);
      exit(exitval);
    }
  } // for
  for (childnum = 0; childnum < CHILDREN; ++childnum) {
    child = wait(&status);
    if (status) {
      printf("Child %d failed with status %d\n", child, status);
    }
  } // for   
  printf("All my children are done.\n");
  exit(0);
} // main()
 
