/* itimerV.c * * Neal Nelson 2009.11.04 * * Set the virtual interval timer to 0.1 and then repeatedly accumulate its * value * from the sighandler handler on each 0.1 sec interrupt for a count * of 10. Display the accumulation at each interrupt. * * Doesn't work for some reason. No interrupts happen. Maybe because I'm * not accumulating any virtual time! Give some real work to do! * * */ #include #include /* for pause() */ #include #include #include #include /* Prototypes must occur before sig handlers are referenced */ static void itimer_handler(int); unsigned long fib(unsigned int n); /* Initialize the virtual time accumulator */ static long virt_count = 0; main() { int i; struct itimerval virt; /* Register the itimer_handler to catch SIGALRM, SIGVTALRM, SIGPROF */ if (signal(SIGVTALRM, itimer_handler) == SIG_ERR) printf("Parent: Unable to create handler for SIGVTALRM\n"); /* Initialize the virt itimer to one tenth of a second */ virt.it_interval.tv_sec = 0; virt.it_interval.tv_usec = 99999; virt.it_value.tv_sec = 0; virt.it_value.tv_usec = 99999; setitimer(ITIMER_VIRTUAL, &virt, NULL); for (i = 0; i < 10; i++) pause(); /* catch 10 interrupts */ printf("Done.\n"); } static void itimer_handler(int signo) { switch(signo) { printf("%d\n",signo); case SIGVTALRM: virt_count++; printf("Parent: received SIGVTALRM signal, virt_count: %ld\n",virt_count); break; default: break; /* should never get here */ } return; }