#include <sys/types.h>
#include <sys/wait.h>
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include "Lib.c"
    
#define NUM_PROCESSES 500 
/*#define DEBUG */

int main(){

int    i;
int    tmppid;
myTime mytime;
int    child_pid[NUM_PROCESSES];

       /* Start Timer[0] */
       BeginTiming(0);

       for (i=0;i < NUM_PROCESSES; i++){
      	if ((tmppid=fork()) < 0)
      	{
        	perror ("Fork failed");
	        exit(errno);
      	}
#ifdef DEBUG
	printf("\n i = %d", i);
#endif

	if (!tmppid){ /* In children process */
#ifdef DEBUG
	     printf(" getpid() = %d", getpid());
#endif
	     return 0;
        }
	/* In parent process */
	else 
	     child_pid[i] = tmppid;
       }

#ifdef DEBUG
      printf("\n Fork successed!");
#endif


      for(i = 0; i < NUM_PROCESSES; i++)
         waitpid (child_pid[i], NULL, 0);
      

      /* Stop Timer[0] */
      mytime = EndTiming(0);

      printf("\n Running time accounted as follows: ");
      printf("\n   o System Level running time of thread creation is %lf", mytime.stime);
      printf("\n   o User Level running time of thread creation is %lf", mytime.utime);
      printf("\n\nmain() reporting that all %d processes have terminated\n\n", NUM_PROCESSES);
    }
    
