how to solve this multiple linux timer with single signal handler

OP (Firma: Student) #3552830
/*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

Antwort schreiben

Eine eigene Frage? Dieser Thread ist über 12 Jahre alt. Stell sie in einem eigenen Thread – dort lesen mehr Leute mit, und sie wird öfter und schneller beantwortet.

Neuen Thread starten

Eine Lösung, Ergänzung oder Rückmeldung zu diesem Thread? Dann antworte hier.

Formatierung
  • [c]C-Code[/c]
  • [code]Code in anderen Sprachen, ASCII-Zeichnungen[/code]
  • [math]Formel in LaTeX-Syntax[/math]
  • [[Titel]] – Link zu einem Artikel
  • Verweis auf einen anderen Beitrag: Rechtsklick auf dessen Beitrags-ID (#8078628), „Adresse kopieren“, in den Text einfügen

Mehr zur Formatierung

Längeren Sourcecode als Anhang, nicht in den Text. JPG nur für Fotos und Scans; Zeichnungen und Screenshots als PNG oder GIF. Siehe Bildformate.

Schon registriert? Anmelden

Wird nicht angezeigt. Wir schicken dir einen Link; sobald du damit bestätigst, erscheint dein Beitrag.

Mit dem Abschicken legst du einen kostenlosen Account an und erkennst die Nutzungsbedingungen und die Datenschutzerklärung an.