//the code also can be a simple sample to implement timer
#include <stdio.h>
#include <signal.h>
#include <sched.h>
#include <sys/time.h>
#ifndef VOLATILE
#define VOLATILE
#endif
VOLATILE int total = 0;
void handle(int signo)
{
static int count = 0;
/* printf("timer expired %d times\n", ++count); */
total++;
printf("Total = %d\n", total);
}
int main()
{
int i,x;
struct itimerval val;
struct sigaction sa;
memset(&sa, 0, sizeof(sa));
sa.sa_handler = handle;
sigaction(SIGVTALRM, &sa, NULL);
val.it_interval.tv_sec = 0;
val.it_interval.tv_usec = 10000;
val.it_value.tv_sec = 0;
val.it_value.tv_usec = 10000;
setitimer(ITIMER_VIRTUAL, &val, NULL);
while(total < 500)
{
for(i = 0; i < 100; i++)
{
x = x + i;
}
}
printf("X = %d\n", x);
printf("Total = %d\n", total);
}