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 ...
- 02-28-2011 #1Just Joined!
- Join Date
- Dec 2010
- Posts
- 34
[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
But if i set "long long freq_nanosecs = 1" in that also the output is only once from the timer. It should be repeatedCode:#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; } }
- 03-01-2011 #2Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,974
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!
- 03-02-2011 #3Just Joined!
- Join Date
- Dec 2010
- Posts
- 34



