/* getitimerV.c * * Neal Nelson 2009.11.04 * * Set and read the virtual timer. * It doesn't appear to work properly on my laptop. * */ #include #include /* prototypes */ unsigned long fib(unsigned int n); main() { struct itimerval itval; unsigned long thefib; /* Initialize the virtual itimer to ten seconds */ itval.it_interval.tv_sec = 9; itval.it_interval.tv_usec = 999999; itval.it_value.tv_sec = 9; /* why do I need to set value? */ itval.it_value.tv_usec = 999999; /* works only if I do */ setitimer(ITIMER_VIRTUAL, &itval, NULL); /* read and display the virtual timer before sleep */ if (getitimer(ITIMER_VIRTUAL, &itval) == 0 ) { printf("Virtual Timer interval: %ld seconds, %ld microseconds\n", itval.it_interval.tv_sec, itval.it_interval.tv_usec); printf(" Virtual Timer value: %ld seconds, %ld microseconds\n", itval.it_value.tv_sec, itval.it_value.tv_usec); } /*--------------------------------------------------------------------------*/ printf("\nBusy computuing fib(40)\n"); thefib = fib(40); /*--------------------------------------------------------------------------*/ /* read and display the virtual timer after busy loop */ printf("thefib %ld\n", thefib); if (getitimer(ITIMER_VIRTUAL, &itval) == 0 ) { printf("Virtual Timer interval: %ld seconds, %ld microseconds\n", itval.it_interval.tv_sec, itval.it_interval.tv_usec); printf(" Virtual Timer value: %ld seconds, %ld microseconds\n", itval.it_value.tv_sec, itval.it_value.tv_usec); } } unsigned long fib(unsigned int n) { if (n == 0) return 0; else if (n == 1 || n == 2) return 1; else return (fib(n-1) + fib(n-2)); }