Forum: PC Hard- und Software how to solve this multiple linux timer with single signal handler


von Hemanth V. (Firma: Student) (hemanth)


Lesenswert?

/*The handler checks that the value stored in sival_ptr matches a given 
timerID variable. The sival_ptr is the same as the one we set in 
makeTimer(), though here it lives in a different structure. Obviously, 
it got copied from there to here on the way to this signal handler. The 
point is that the timerID is what is used to determine which timer just 
went off and determine what to do next */

static void timerHandler( int sig, siginfo_t *si, void *uc )
{
    timer_t *tidp;

    tidp = si->si_value.sival_ptr;

    if ( *tidp == firstTimerID )

        TASK1(Task2ms_Raster);
   else if ( *tidp == secondTimerID )
       TASK2(Task10ms_Raster);
    else if ( *tidp == thirdTimerID )
        TASK3(Task100ms_Raster);
}

/* The function takes a pointer to a timer_t variable that will be 
filled with the timer ID created by timer_create(). This pointer is also 
saved in the sival_ptr variable right before calling timer_create(). In 
this function notice that we always use the SIGRTMIN signal, so 
expiration of any timer causes this signal to be raised. The signal 
handler I've written for that signal is timerHandler. */

 static int makeTimer( char *name, timer_t *timerID, int expireMS, int 
intervalMS )
{
     //sigset_t mask;
    struct sigevent         te;
    struct itimerspec       its;
    struct sigaction        sa;
    int                     sigNo = SIGRTMIN;

    /* Set up signal handler. */
    sa.sa_flags = SA_SIGINFO;
    sa.sa_sigaction = timerHandler;
    sigemptyset(&sa.sa_mask);
    if (sigaction(sigNo, &sa, NULL) == -1)
    {
        perror("sigaction");
    }

    /* Set and enable alarm */
    te.sigev_notify = SIGEV_SIGNAL;
    te.sigev_signo = sigNo;
    te.sigev_value.sival_ptr = timerID;
    timer_create(CLOCK_REALTIME, &te, timerID);

    its.it_interval.tv_sec = 0;
    its.it_interval.tv_nsec = intervalMS * 1000000;
    its.it_value.tv_sec = 0;
    its.it_value.tv_nsec = expireMS * 1000000;
    timer_settime(*timerID, 0, &its, NULL);


    return 1;
}

int main()
 {

     makeTimer("First Timer", &firstTimerID, 2, 2);   //2ms

   makeTimer("Second Timer", &secondTimerID, 10, 10);    //10ms
   makeTimer("Third Timer", &thirdTimerID, 100, 100);  //100ms

 }
I am using single handler to call the task for every 2ms, 10ms and 
100ms. I am using multiple timers with single signal handler. How to 
identify which timer just went. In debug mode : the last debug control 
session is : static void timerHandler( int sig, siginfo_t *si, void *uc 
){ . Could anyone help me how to solve this ?  you will get more info 
here : http://www.graphics-muse.org/wp/?p=868

: Bearbeitet durch User
Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.