Find the answer to your Linux question:
Results 1 to 3 of 3
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

    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,


        #include <stdlib.h>
        #include <unistd.h>
        #include <stdio.h>
        #include <signal.h>
        #include <time.h>
        #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);
          signal(sig, handler);
        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)
         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)
         int main() {
           timer ob;
           for(int i=0; i<20; i++) {
           if(flag) {
             cout << "Timer called" << endl;
           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
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    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

    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.

  4. $spacer_open

Posting Permissions

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