Advertisement
Guest User

Untitled

a guest
Sep 25th, 2017
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.39 KB | None | 0 0
  1. #include <sys/time.h>
  2. #include <signal.h>
  3. #include <unistd.h>
  4. #include <stdio.h>
  5. #include <time.h>
  6. #include <errno.h>
  7. #define LOOP_LIMIT  1E2
  8. volatile int sigcount=0;
  9. void catcher( int sig ) {
  10.     struct itimerval value;
  11.     int which = ITIMER_REAL;
  12. //    printf( "Signal catcher called for signal %d\n", sig );
  13.     sigcount++;
  14.     if( sigcount > 1 ) {
  15.         /*          * Disable the real time interval timer          */
  16.         getitimer( which, &value );
  17.         value.it_value.tv_sec = 0;
  18.         value.it_value.tv_usec = 0;
  19.     //    setitimer( which, &value, NULL );
  20.     }
  21. }
  22. int main( int argc, char *argv[] ) {
  23.     int result = 0;
  24.     struct itimerval value, ovalue, pvalue;
  25.     int which = ITIMER_REAL;
  26.     struct sigaction sact;
  27.     volatile double count;
  28.     time_t t;
  29.     sigemptyset( &sact.sa_mask );
  30.     sact.sa_flags = 0;
  31.     sact.sa_handler = catcher;
  32.     sigaction( SIGALRM, &sact, NULL );
  33.     getitimer( which, &pvalue );
  34.     /*    * Set a real time interval timer to repeat every 200 milliseconds          */
  35.     value.it_interval.tv_sec = 0;        /* Zero seconds */
  36.     value.it_interval.tv_usec = 1;  /* Two hundred milliseconds */
  37.     value.it_value.tv_sec = 0;           /* Zero seconds */
  38.     value.it_value.tv_usec = 5;     /* Five hundred milliseconds */
  39.     result = setitimer( which, &value, &ovalue );
  40.     /* *      * The interval timer value returned by setitimer() should be
  41.  *           * identical to the timer value returned by getitimer().               */
  42.     if( ovalue.it_interval.tv_sec != pvalue.it_interval.tv_sec  ||
  43.         ovalue.it_interval.tv_usec != pvalue.it_interval.tv_usec ||
  44.         ovalue.it_value.tv_sec != pvalue.it_value.tv_sec ||
  45.         ovalue.it_value.tv_usec != pvalue.it_value.tv_usec ) {
  46.         printf( "Real time interval timer mismatch, test Failed\n" );
  47.         result = -1;
  48.     }
  49.     time( &t );
  50.     printf( "Before loop, time is %s", ctime(&t) );
  51. //    for( count=0; ((count<LOOP_LIMIT) && (sigcount<2)); count++ );
  52. //    printf("LOOP_LIMIT=%ld\n",LOOP_LIMIT);
  53.  
  54.     for( count=0; ((count<LOOP_LIMIT)); count++ );
  55.     time( &t );
  56.     printf( "After loop, time is %s\n", ctime(&t) );
  57.     if( sigcount == 0 )
  58.         printf( "The signal catcher never gained control\n" );
  59.     else
  60.         printf( "The signal catcher gained control\n" );
  61.     printf( "The value of count is %.0f\n", count );
  62.     return( result );
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement