#include /* for gettimeofday() */ #include /* for gettimeofday() */ #include /* for printf() */ #include /* for wait() */ #include /* for wait() */ /* * NO_OF_ITERATIONS e o numero de vezes que se repete o loop existente * em cada processo. Se este valor for maior, a media calculada sofrera * menor desvio. */ #define NO_OF_ITERATIONS 500 /* * NO_OF_CHILDREN e o numero de filhos a serem criados, cada qual responsavel * pela medida do desvio. Quanto mais filhos, maior sera o desvio. */ #define NO_OF_CHILDREN 3 /* * MICRO_PER_SECOND define o numero de microsegundos em um segundo */ #define MICRO_PER_SECOND 1000000 int main( int argc, char *argv[]) { struct timeval start_time; struct timeval stop_time; /* * Outras variaveis importantes */ float drift; int count; int child_no; int SLEEP_TIME; if(argc!=2) { printf("\nParametros invalidos!\n\n"); exit(1); } child_no=atoi(argv[0]); SLEEP_TIME=atoi(argv[1]); gettimeofday( &start_time, NULL ); /* * Este loop ocasiona a dormencia do filho, de acordo com * SLEEP_TIME, tantas vezes quanto NO_OF_ITERATIONS */ for( count = 0; count < NO_OF_ITERATIONS; count++ ) { usleep(SLEEP_TIME); } /* * Paraobter o tempo final */ gettimeofday( &stop_time, NULL ); /* * Calcula-se o desvio */ drift = (float)(stop_time.tv_sec - start_time.tv_sec); drift += (stop_time.tv_usec - start_time.tv_usec)/(float)MICRO_PER_SECOND; /* * Exibe os resultados */ printf("Filho #%d -- desvio total: %.6f -- desvio medio: %.6f\n", child_no, drift - NO_OF_ITERATIONS*SLEEP_TIME/MICRO_PER_SECOND, (drift - NO_OF_ITERATIONS*SLEEP_TIME/MICRO_PER_SECOND)/NO_OF_ITERATIONS); }