| 1 |
/*********************************************************************** |
| 2 |
* Code listing from "Advanced Linux Programming," by CodeSourcery LLC * |
| 3 |
* Copyright (C) 2001 by New Riders Publishing * |
| 4 |
* See COPYRIGHT for license information. * |
| 5 |
***********************************************************************/ |
| 6 |
|
| 7 |
#include <signal.h> |
| 8 |
#include <stdio.h> |
| 9 |
#include <string.h> |
| 10 |
#include <sys/time.h> |
| 11 |
|
| 12 |
void timer_handler (int signum) |
| 13 |
{ |
| 14 |
static int count = 0; |
| 15 |
printf ("timer expired %d times\n", ++count); |
| 16 |
} |
| 17 |
|
| 18 |
int main () |
| 19 |
{ |
| 20 |
struct sigaction sa; |
| 21 |
struct itimerval timer; |
| 22 |
|
| 23 |
/* Install timer_handler as the signal handler for SIGVTALRM. */ |
| 24 |
memset (&sa, 0, sizeof (sa)); |
| 25 |
sa.sa_handler = &timer_handler; |
| 26 |
sigaction (SIGVTALRM, &sa, NULL); |
| 27 |
|
| 28 |
/* Configure the timer to expire after 250 msec... */ |
| 29 |
timer.it_value.tv_sec = 0; |
| 30 |
timer.it_value.tv_usec = 250000; |
| 31 |
/* ... and every 250 msec after that. */ |
| 32 |
timer.it_interval.tv_sec = 0; |
| 33 |
timer.it_interval.tv_usec = 250000; |
| 34 |
/* Start a virtual timer. It counts down whenever this process is |
| 35 |
executing. */ |
| 36 |
setitimer (ITIMER_VIRTUAL, &timer, NULL); |
| 37 |
|
| 38 |
/* Do busy work. */ |
| 39 |
while (1); |
| 40 |
} |