int main() { int n = 10; srand(time(0) ^ getpid()); while(n-- > 0) { random_length_task(); } return 0; }
int main(int argc, char *argv[]){ int n = 10; if( argc == 2 ){ char* endptr = NULL; n = strtol(argv[1], &endptr, 0); if( endptr == argv[1] ){ /* first char of argument is not a number */ perror("argument is not a number\n"); } if( n == 0 ){ perror("argument is 0\n"); } } int total_round = n; srand(time(0) ^ getpid()); struct timeval start_time; gettimeofday(&start_time, NULL); while( n-- > 0 ){ // printf("Round %d\n", total_round - n); random_length_task(); struct timeval task_finish, rest_of_time; gettimeofday(&task_finish, NULL); struct timeval expected_time = {start_time.tv_sec, start_time.tv_usec + 1000*100*(total_round - n)}; expected_time.tv_sec += expected_time.tv_usec / 1000000; // 10^-6 expected_time.tv_usec %= 1000000; // 10^-6 timersub(&expected_time, &task_finish, &rest_of_time); struct timespec sleep_time; sleep_time.tv_sec = rest_of_time.tv_sec; sleep_time.tv_nsec = rest_of_time.tv_usec * 1000; nanosleep(&sleep_time, NULL); // printf("start time %ld.%06ld\n", start_time.tv_sec, start_time.tv_usec); // printf("task finish time %ld.%06ld\n", task_finish.tv_sec, task_finish.tv_usec); // printf("expected time %ld.%06ld\n", expected_time.tv_sec, expected_time.tv_usec); // printf("rest of time %ld.%06ld\n", rest_of_time.tv_sec, rest_of_time.tv_usec); // printf("\n"); } return 0; }
int main(int argc, char ** argv) { srand(time(0) ^ getpid()); int n = atoi(argv[1]); struct timeval start_time; gettimeofday(&start_time, NULL); while(n--) { int usec = get_us_elapsed_from(start_time); usec %= EXPECTED_SLOT_USEC; usleep(EXPECTED_SLOT_USEC - usec); random_length_task(); } return 0; }
int main(int argc,char * argv[]) { if( argc != 2) { printf("Usage: ./hw1 [times]\n"); exit(-1); } struct timeval begin; struct timeval end; int n = atoi(argv[1]); srand(time(0) ^ getpid()); while(n-- > 0) { gettimeofday(&begin, NULL); long int endtime=begin.tv_sec*1000000+begin.tv_usec+100000; random_length_task(); while(1) { gettimeofday(&end, NULL); if(end.tv_sec*1000000+end.tv_usec >= endtime) break; } } return 0; }
int main(int argc, char* argv[]) { int n = 10; struct timeval origin; struct timeval current; if(argc == 2){ n = atoi(argv[1]); } srand(time(0)); while(n--){ gettimeofday(&origin, NULL); random_length_task(); gettimeofday(¤t, NULL); while((1000000 + current.tv_usec - origin.tv_usec) % 1000000 < DELAY_TIME){ gettimeofday(¤t, NULL); } } return 0; }