#include #include #include #include #include #include #include #define DEFAULT_LOOP 10 static int do_loop = 1; void alarm_handler(int unused) { do_loop = 0; } int main(int argc, char *argv[]) { struct timeval tv, last, diff; unsigned int timeout; if (argc == 2) { timeout = strtoul(argv[1], NULL, 10); } else if (argc == 1) { timeout = DEFAULT_LOOP; } else { fprintf(stderr, "Usage: %s [seconds]\n", argv[0]); return 1; } signal(SIGALRM, &alarm_handler); if (alarm(timeout) != 0) fprintf(stderr, "Warning, alarm(2) already set!\n"); if (gettimeofday(&last, NULL) != 0) { perror("initial gettimeofday() failed:"); return 1; } while(do_loop) { if (gettimeofday(&tv, NULL) != 0) { perror("loop gettimeofday() failed:"); return 1; } timersub(&tv, &last, &diff); //printf("Diff of timers is %ld\n", diff.tv_usec); #if 0 if ((i + 1) % 1000000 == 0) { printf("."); fflush(stdout); } #endif if (diff.tv_sec > 0) { fprintf(stderr, "Error: subsequent calls to gettimeofday() differed by %ld seconds\n", diff.tv_sec); return 1; } memcpy(&last, &tv, sizeof(last)); } printf("\ncompleted successfully\n"); return 0; }