/* gettimeofday.c -- Program that determines time required for a fork */
/*               If calls gettimeofday before and after a fork in the child */
/*               and prints the time interval between the two times in  */
/*               milliseconds.                                          */


#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <sys/time.h>
#define  MAXTIMES  100
#define  SLEEPTIME 2

int main(){
  struct timeval time[2];
  int pid;
  int lcv;
  int delta;

  for (lcv=0; lcv < MAXTIMES; lcv++) {
    if (gettimeofday(&(time[0]), NULL) != 0) {
      perror("gettimeofday");
      exit(1);}
    if((pid = fork()) < 0) { /* error */
      perror("fork");
      exit(1);
    }
    if (pid == 0) {   
    /* child */
      if (gettimeofday(&(time[1]), 0) != 0) {
	perror("gettimeofday");
	exit(1);}
      delta = (time[1].tv_sec - time[0].tv_sec)*1000000+(time[1].tv_usec - time[0].tv_usec);
      printf("DELTA = %d\n",  delta);
      exit(0);
    } 
    /* parent */
    sleep(SLEEPTIME);
  }
  return 0;
}
