/* itimer.c * * Neal Nelson 2009.11.04 * * Set the real 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. * * */ #include #include /* for pause() */ #include #include #include /* Prototypes must occur before sig handlers are referenced */ static void itimer_handler(int); /* Initialize the real time accumulator */ static long real_count = 0; main() { int i; struct itimerval real; /* Register the itimer_handler to catch SIGALRM, SIGVTALRM, SIGPROF */ if (signal(SIGALRM, itimer_handler) == SIG_ERR) printf("Parent: Unable to create handler for SIGALRM\n"); /* Initialize the real itimers to one tenth of a second */ real.it_interval.tv_sec = 0; real.it_interval.tv_usec = 99999; real.it_value.tv_sec = 0; real.it_value.tv_usec = 99999; setitimer(ITIMER_REAL, &real, NULL); for (i = 0; i < 10; i++) pause(); /* catch 10 interrupts */ printf("Done.\n"); } static void itimer_handler(int signo) { switch(signo) { case SIGALRM: real_count++; printf("Parent: received SIGALRM signal, real_count: %ld\n",real_count); break; default: break; /* should never get here */ } return; }