Find the answer to your Linux question:
Results 1 to 3 of 3
Hi All, i have this C++ program. It has a simple for loop which prints numbers from 1 to 20. Between this, execution, the timer expires multiple times, and each ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Dec 2010
    Posts
    34

    Question [SOLVED] Help: C++ program not giving expected output ( timer )


    Hi All,
    i have this C++ program. It has a simple for loop which prints numbers from 1 to 20. Between this, execution, the timer expires multiple times, and each time it expires, it should print an output from signal handler.

    Unfortunately i am not getting this output. And it is just simply printing the numbers from 1 to 20. Can someone please help me?

    Thanks in advance,

    kingsmasher1


    Code:
        #include <stdlib.h>
        #include <unistd.h>
        #include <stdio.h>
        #include <signal.h>
        #include <time.h>
        #define CLOCKID CLOCK_REALTIME
        #define SIG SIGRTMIN
        #define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \
        } while (0)
        #include <iostream>
    
        using namespace std;
    
        static int flag=0;
        class timer{
    
        static void
        handler(int sig) {
          printf("Caught signal %d\n", sig);
          ::flag=1;
          signal(sig, handler);
        }
    
        public:
    
        void timer_func()
        {
        timer_t timerid;
        struct sigevent sev;
        struct itimerspec its;
        long long freq_nanosecs=1; // The timer frequency in nanosecs
        sigset_t mask;
        struct sigaction sa;
    
        /* Establish handler for timer signal */
    
        printf("Establishing handler for signal %d\n", SIG);
    
        sa.sa_flags = SA_RESETHAND;
        sa.sa_handler = handler;
    
    
         /* Create the timer */
    
         sev.sigev_notify = SIGEV_SIGNAL;
         sev.sigev_signo = SIG;
         sev.sigev_value.sival_ptr = &timerid;
    
         if (timer_create(CLOCKID, &sev, &timerid) == -1)
           errExit("timer_create");
    
         printf("timer ID is 0x%lx\n", (long) timerid);
    
         /* Start the timer */
    
         its.it_value.tv_sec = freq_nanosecs / 1000000000;
         its.it_value.tv_nsec = freq_nanosecs % 1000000000;
         its.it_interval.tv_sec = its.it_value.tv_sec;
         its.it_interval.tv_nsec = its.it_value.tv_nsec;
    
         if (timer_settime(timerid, 0, &its, NULL) == -1)
           errExit("timer_settime");
          }
         };
    
         int main() {
          
           timer ob;
           ob.timer_func();
    
           for(int i=0; i<20; i++) {
             sleep(1);
           if(flag) {
             cout << "Timer called" << endl;
             flag=0;
            }
           cout << "Printing i: " << i << endl;
          }
         }
    But if i set "long long freq_nanosecs = 1" in that also the output is only once from the timer. It should be repeated

  2. #2
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,558
    I don't see where you are calling the sigaction() function in order to enable the handler for the timer signal.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  3. #3
    Just Joined!
    Join Date
    Dec 2010
    Posts
    34

    Thumbs up

    Quote Originally Posted by Rubberman View Post
    I don't see where you are calling the sigaction() function in order to enable the handler for the timer signal.
    Thanks Rubberman, i just got it correct.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •