Results 1 to 2 of 2
Hi All,
I have a small piece of code, where i have used a POSIX timer: timer_create( ). The timer is created using the signal approach (SIGEV_SIGNAL) - as SIGEV_THREAD ...
- 04-27-2011 #1Just Joined!
- Join Date
- Dec 2010
- Posts
- 34
URGENT!!! Problem with signals, timer (SIGEV_SIGNAL)
Hi All,
I have a small piece of code, where i have used a POSIX timer: timer_create( ). The timer is created using the signal approach (SIGEV_SIGNAL) - as SIGEV_THREAD not supported on our platform. When the timer expires, it generates a signal, SIGUSR1, to notify of it's expiration, and there is a corresponding handler, to catch this signal, inside this handler (in actual program, not shown on code) i have a flag which sets, once the signal given by timer is caught.
Uptill this, everything is fine: Problem is, suppose if the test program also generates same signal as the timer (SIGUSR1 in this case), then the same flag is set, not by timer. So there is no way to discriminate, wether the signal received at signal handler is that of a timer, or any other test program.
Can you help me, to resolve this issue?
Thanks in advance.
Please note that: This code is actually part of a tool, which is used by a test program. The test program can generate any signal including SIGUSR1, and we can't restrict the user not to use SIGUSR (He can use any signal)Code:#include <stdlib.h> #include <time.h> #include <stdio.h> #include <signal.h> #include <errno.h> #include <string.h> void sig_handlerTimer1(int); time_t timerid; int main() { int i; static struct sigaction sa; static struct sigevent sevp; // argument to timer_create static struct itimerspec its; // argument to timer_gettime memset (&sevp, 0, sizeof (struct sigevent)); sevp.sigev_value.sival_ptr = &timerid; sevp.sigev_notify = SIGEV_SIGNAL; sevp.sigev_notify_attributes = NULL; sevp.sigev_signo = SIGUSR1; sevp.sigev_notify_function=sig_handlerTimer1; /* Setting timer interval */ its.it_interval.tv_sec = 0; its.it_interval.tv_nsec = 0; /* Setting timer expiration */ its.it_value.tv_sec = 2; // First expiry after 1 sec its.it_value.tv_nsec = 0; /* Setting the signal handlers before invoking timer*/ sa.sa_handler = sig_handlerTimer1; sa.sa_flags = 0; sigaction(SIGUSR1, &sa, NULL); if (timer_create(CLOCK_REALTIME, &sevp, &timerid) == -1) { fprintf(stderr, "LeakTracer (timer_trackStartTime): timer_create failed to create timer. " \ "Leak measurement will be for entire duration of the execution period:%s \n", strerror(errno)); return; } if (timer_settime(timerid, 0, &its, NULL) == -1) { fprintf(stderr, "LeakTracer (timer_trackStartTime): timer_settime failed to set the timer. " \ "Leak measurement will be for entire duration of execution period:%s \n", strerror(errno)); return; } for(i=0; i<10; i++) { printf("%d\n",i); if(i==3) { kill(getpid(), SIGUSR1); // SIGUSR1 also generated by test program which reaches same handler and sets flag (THIS IS UN-DESIRABLE) } sleep(1); } } void sig_handlerTimer1(int signum) { int flag = 1; printf("Caught signal: %d\n",signum); if (timer_delete(timerid) < 0) { fprintf(stderr, "timer deletion failed. " \ "This may result in some memory leaks (sig_handlerTimer1):%s \n", strerror(errno)); } }Last edited by kingsmasher1; 04-27-2011 at 06:40 AM.
- 04-27-2011 #2Just Joined!
- Join Date
- Dec 2010
- Posts
- 34
This way we can make sure that signal received is from timer.
Code:static void handler(int sig, siginfo_t *si, void *uc) { if(si->si_value.sival_ptr != &timerid) // To make sure signal is from timer { printf("Stray signal\n"); } else { flag =1; // set flag only when timer expires printf("Caught signal %d from timer\n", sig); } }


Reply With Quote