/* getitimer.c * * Neal Nelson 2009.11.04 * * Set and read the interval timer for real time. * */ #include #include main() { struct itimerval itval; /* Initialize the real itimer to two seconds */ itval.it_interval.tv_sec = 1; itval.it_interval.tv_usec = 999999; itval.it_value.tv_sec = 1; /* why do I need to set value? */ itval.it_value.tv_usec = 999999; /* works only if I do */ setitimer(ITIMER_REAL, &itval, NULL); /* read and display the real timer before sleep */ if (getitimer(ITIMER_REAL, &itval) == 0 ) { printf("Real Timer interval: %ld seconds, %ld microseconds\n", itval.it_interval.tv_sec, itval.it_interval.tv_usec); printf(" Real Timer value: %ld seconds, %ld microseconds\n", itval.it_value.tv_sec, itval.it_value.tv_usec); } /*--------------------------------------------------------------------------*/ printf("\nSleeping for one second\n\n"); sleep(1); /*--------------------------------------------------------------------------*/ /* read and display the real timer after sleep */ if (getitimer(ITIMER_REAL, &itval) == 0 ) { printf("Real Timer interval: %ld seconds, %ld microseconds\n", itval.it_interval.tv_sec, itval.it_interval.tv_usec); printf(" Real Timer value: %ld seconds, %ld microseconds\n", itval.it_value.tv_sec, itval.it_value.tv_usec); } }