LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Posix timers not behaving correctly (https://www.linuxquestions.org/questions/programming-9/posix-timers-not-behaving-correctly-590143/)

sean04 10-07-2007 07:54 PM

Posix timers not behaving correctly
 
Hi I don't understand how this isn't working correctly...I'm running Linux Kernel 2.6.9. What's happening is that as soon as I call the timer_settime function it immediately triggers the signal handler. No matter how big I set the time. Here's the code:



sigs.sa_handler = signal_handler_function;
sigemptyset(&sigs.sa_mask);
if (sigaction(SIGALRM, &sigs, (struct sigaction *) NULL) != 0)
{
exit(1);
}

if (timer_create(CLOCK_REALTIME,
(struct sigevent *) NULL, &timer_id ) != 0)
{
exit(1);
}

ItimerSpec.it_value.tv_sec = sec; //assume correct values
ItimerSpec.it_value.tv_nsec = nsec;//assume correct values
ItimerSpec.it_interval.tv_sec = 0;
ItimerSpec.it_interval.tv_nsec = 0;

if (timer_settime(timer_id,TIMER_ABSTIME,&ItimerSpec, NULL) != 0)
{
exit(1);
}

orgcandman 10-07-2007 11:34 PM

you might want to read the manpages.

The TIMER_ABSTIME flag is probably not what you want, unless you're getting seconds and nanoseconds in absolute time. In the above function, there's no indication that values you're getting _are_ correct, and the symptom you're seeing is characteristic of incorrect values.

I've got some timer code that works, and it looks like:

Code:

    timeout.it_value.tv_sec = 0;
    timeout.it_value.tv_nsec = 1000;

    timeout.it_interval.tv_sec = timeout.it_value.tv_sec;
    timeout.it_interval.tv_nsec = timeout.it_value.tv_nsec;

    status = timer_create(CLOCK_REALTIME, &evp, &m_nTimerIds[nTimerId]);

    sigaction(evp.sigev_signo, &sig_act, 0);

    if(status < 0)
    {
        fprintf(stderr, "ERROR: Cannot start timer for id: %d.\n", nTimerId);
        return status;
    }

    status = timer_settime(m_nTimerIds[nTimerId], 0, &timeout, NULL);
    if(status < 0)
    {
        perror("settimer failed");
        return -1;
    }

this is relative time based, rather than absolute time based (absolute time being prone to errors such as "The date is really tuesday, but you think its monday").


All times are GMT -5. The time now is 03:21 PM.