wrong.c

 // Program to measure the difference between volatile and not
 // written by Kevin P. Dankwardt [email protected]
 // 3 March 2005
 
 #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; // 10 ms
   val.it_value.tv_sec = 0;
   val.it_value.tv_usec = 10000; // 10 ms
   setitimer(ITIMER_VIRTUAL, &val, NULL);
   param.sched_priority = 99;
   if (sched_setscheduler(0, SCHED_RR, &param) ==-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); // so optimizers doesn't throw away the loop
   printf("total = %d\n",total);
 }
             
 

Generated on Thu Mar 3 16:57:46 2005 by doxygen 1.3.4 (edited by K. Dankwardt)