#include #include #include #include #include #define MICRO_FACTOR 1000000 #define MAX_INTERVAL 10 void do_nothing(int signum) { } int main(void) { struct sigaction action; struct itimerval period; struct timeval before_time, after_time; int delta; int interval; action.sa_handler = &do_nothing; action.sa_flags = 0; sigaction(SIGALRM, &action, NULL); for(;;) { interval = random() % MAX_INTERVAL; if(interval == 0) interval = 1; period.it_interval.tv_sec = period.it_value.tv_sec = interval; period.it_interval.tv_usec = period.it_value.tv_usec = 0; setitimer(ITIMER_REAL, &period, NULL); gettimeofday(&before_time, NULL); pause(); gettimeofday(&after_time, NULL); delta = (after_time.tv_sec - before_time.tv_sec) * MICRO_FACTOR + (after_time.tv_usec - before_time.tv_usec) - interval * MICRO_FACTOR; printf("%ld\n", delta); } }