Thread: multiple timers problem

  1. #1
    Registered User
    Join Date
    Mar 2015
    Posts
    2

    multiple timers problem

    Hello all.

    The issue is that I am trying to run a code which can trigger different timers at the same time. That is, when 3 is typed by the user, the firstimer is triggered. If before expiration another 3 is introduced, the second timer will be launched, and the difference between both triggers will be shown. Under this circumstance, timer_gettime only works once. I tried all the things I could imagine but still, it_value_tv_sec and _nsec return zero after the first expiration.

    -Theoretically, and following the manuals, once the timer is restarted the gettime function should work as it does the first time.
    -I have also tried to removed the timer and start it again, but in this case the timers are still being triggered, but the gettime outputs(even the first time) are nonsense.
    Here is the code I am testing:

    [CODE]#include<stdio.h>
    Code:
    #include<signal.h>
    #include<time.h>
    
    
    timer_t firstTimerID;
    timer_t secondTimerID;
    timer_t thirdTimerID;
    timer_t fourthTimerID;
    int z=0;
    int a=0;
    int t_block3=4;
    float delay_blocks;
    
    struct itimerspec it;
    struct itimerspec its;
    
    staticvoid timerHandler(int sig,siginfo_t*si,void*uc )
    {
        timer_t*tidp;
    
        tidp = si->si_value.sival_ptr;
    
        if(*tidp == firstTimerID ){
            printf("First timer\n");
        }
        elseif(*tidp == secondTimerID ){
            printf("Second timer\n");
        }
        elseif(*tidp == thirdTimerID ){
            printf("Third timer\n");
        }
        elseif(*tidp == fourthTimerID ){
            printf("Fourth timer\n");
        }
    
        z--;
        printf("%d", z);
    
    }
    
    staticint makeTimer(timer_t*timerID,int time)
    {
        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;//Action when singal is triggered
        sigemptyset(&sa.sa_mask);
        if(sigaction(sigNo,&sa, NULL)==-1){
            perror("sigaction");
        }
    
        // Set and enable alarm 
        te.sigev_notify = SIGEV_SIGNAL;//Gnerate alarm upon expiration
        te.sigev_signo = sigNo;//SIGALRM
        te.sigev_value.sival_ptr = timerID;//Timer ID
        //Create a per_process timer using the timer ID
        timer_create(CLOCK_REALTIME,&te, timerID);
    
        //Interval for starting again 
        its.it_interval.tv_sec =0;
        its.it_interval.tv_nsec =0;
        //Timer time
        its.it_value.tv_sec = time;
        its.it_value.tv_nsec =0;
        //Arm/disarmer a per process time
        timer_settime(*timerID,0,&its, NULL);
    
        return1;
    }
    
    int main(){
    
    while(1){
        printf("Enter the number of the block\n");
        if(scanf(" %d",&a)==1){
    
        switch(a){
        case3: printf("Block number three, belt will proceed to stop in 12 seconds\n");
                if(z==0){                  
                makeTimer(&firstTimerID, t_block3);
                }elseif(z==1){
                    makeTimer(&secondTimerID,4);
                    timer_gettime(&firstTimerID,&it);
                    delay_blocks=t_block3-(it.it_value.tv_sec+(it.it_value.tv_nsec*0.000000001));
                    printf("Difference between the first and the second timer = %f\n", delay_blocks);
    
                }elseif(z==2){
                makeTimer(&thirdTimerID,4);
                }elseif(z==3){
                makeTimer(&fourthTimerID,4);
                }
                z++;
                break;
    
        case2: printf("Block number two, belt will proceed to stop in 10 seconds\n");
                        //sleep(1);
                        break;
    
        case4: printf("Block number four, belt won't stop\n");
                        //sleep(1);
                        break;
        default:printf("Wrong lecture\n");
    
        }
        }
    
    }
     }


    Thank you in advance!

  2. #2
    Registered User
    Join Date
    Mar 2015
    Posts
    2
    Finally got it. The problem is that the timer_create must be used only once. Afterwards, to armed it or disarmed just using timer_settime. If not, the signals work anyway, but the timer_gettime is unstable.

    Thanks.


    Quote Originally Posted by landondonovan View Post
    Hello all.

    The issue is that I am trying to run a code which can trigger different timers at the same time. That is, when 3 is typed by the user, the firstimer is triggered. If before expiration another 3 is introduced, the second timer will be launched, and the difference between both triggers will be shown. Under this circumstance, timer_gettime only works once. I tried all the things I could imagine but still, it_value_tv_sec and _nsec return zero after the first expiration.

    -Theoretically, and following the manuals, once the timer is restarted the gettime function should work as it does the first time.
    -I have also tried to removed the timer and start it again, but in this case the timers are still being triggered, but the gettime outputs(even the first time) are nonsense.
    Here is the code I am testing:

    [CODE]#include<stdio.h>
    Code:
    #include<signal.h>
    #include<time.h>
    
    
    timer_t firstTimerID;
    timer_t secondTimerID;
    timer_t thirdTimerID;
    timer_t fourthTimerID;
    int z=0;
    int a=0;
    int t_block3=4;
    float delay_blocks;
    
    struct itimerspec it;
    struct itimerspec its;
    
    staticvoid timerHandler(int sig,siginfo_t*si,void*uc )
    {
        timer_t*tidp;
    
        tidp = si->si_value.sival_ptr;
    
        if(*tidp == firstTimerID ){
            printf("First timer\n");
        }
        elseif(*tidp == secondTimerID ){
            printf("Second timer\n");
        }
        elseif(*tidp == thirdTimerID ){
            printf("Third timer\n");
        }
        elseif(*tidp == fourthTimerID ){
            printf("Fourth timer\n");
        }
    
        z--;
        printf("%d", z);
    
    }
    
    staticint makeTimer(timer_t*timerID,int time)
    {
        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;//Action when singal is triggered
        sigemptyset(&sa.sa_mask);
        if(sigaction(sigNo,&sa, NULL)==-1){
            perror("sigaction");
        }
    
        // Set and enable alarm 
        te.sigev_notify = SIGEV_SIGNAL;//Gnerate alarm upon expiration
        te.sigev_signo = sigNo;//SIGALRM
        te.sigev_value.sival_ptr = timerID;//Timer ID
        //Create a per_process timer using the timer ID
        timer_create(CLOCK_REALTIME,&te, timerID);
    
        //Interval for starting again 
        its.it_interval.tv_sec =0;
        its.it_interval.tv_nsec =0;
        //Timer time
        its.it_value.tv_sec = time;
        its.it_value.tv_nsec =0;
        //Arm/disarmer a per process time
        timer_settime(*timerID,0,&its, NULL);
    
        return1;
    }
    
    int main(){
    
    while(1){
        printf("Enter the number of the block\n");
        if(scanf(" %d",&a)==1){
    
        switch(a){
        case3: printf("Block number three, belt will proceed to stop in 12 seconds\n");
                if(z==0){                  
                makeTimer(&firstTimerID, t_block3);
                }elseif(z==1){
                    makeTimer(&secondTimerID,4);
                    timer_gettime(&firstTimerID,&it);
                    delay_blocks=t_block3-(it.it_value.tv_sec+(it.it_value.tv_nsec*0.000000001));
                    printf("Difference between the first and the second timer = %f\n", delay_blocks);
    
                }elseif(z==2){
                makeTimer(&thirdTimerID,4);
                }elseif(z==3){
                makeTimer(&fourthTimerID,4);
                }
                z++;
                break;
    
        case2: printf("Block number two, belt will proceed to stop in 10 seconds\n");
                        //sleep(1);
                        break;
    
        case4: printf("Block number four, belt won't stop\n");
                        //sleep(1);
                        break;
        default:printf("Wrong lecture\n");
    
        }
        }
    
    }
     }


    Thank you in advance!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with C# timers and using objects
    By BatchProgrammer in forum C# Programming
    Replies: 5
    Last Post: 05-13-2013, 07:44 PM
  2. Problem with translations and timers?
    By Jake.c in forum Game Programming
    Replies: 1
    Last Post: 09-19-2009, 11:32 AM
  3. Multiple timers on single window
    By csonx_p in forum Windows Programming
    Replies: 16
    Last Post: 06-25-2008, 09:06 PM
  4. System.Timers.Timer problem
    By Rune Hunter in forum C# Programming
    Replies: 2
    Last Post: 01-29-2008, 02:29 PM
  5. Multiple Timers
    By Devil Panther in forum Windows Programming
    Replies: 2
    Last Post: 08-09-2003, 09:26 AM

Tags for this Thread