#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)
{
if (signo == SIGALRM)
{
printf("Total = %d\n",total);
exit(0);
}
total++;
}
int main ()
{
VOLATILE unsigned x=0;
VOLATILE int i,j;
struct sched_param param;
struct itimerval val;
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);
param.sched_priority = 99;
if (sched_setscheduler(0, SCHED_RR, ¶m) ==-1) {
perror("setting priority");
exit(1);
}
#define BIGNUM (1000)
alarm(10);
signal(SIGALRM,handle);
signal(SIGVTALRM,handle);
while (total < 100 )
{
for (i=0; i<BIGNUM; i++)
{
for (j=0; j<BIGNUM; j++)
x = x+j ;
}
}
printf("x = %u\n",x);
printf("total = %d\n",total);
}