How to use timer in Unix ?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14

Thread: How to use timer in Unix ?

Hybrid View

  1. #1
    Join Date
    Sep 2005
    Posts
    18

    How to use timer in Unix ?

    I want to start a timer to count a period of about 10 second, how should I implement the method in time.h ?

  2. #2
    Join Date
    Mar 2005
    Posts
    137

    Re: How to use timer in Unix ?

    You can use setitimer function on Unix to set a timer interval. After the specified timer expires it generates SIGALRM which can be captured using signal() function to call a signal handler function.

    setitimer takes argument of type "struct itimerval" found int sys/time.h and has following structure

    Code:
    struct itimerval {
                 struct timeval it_interval; /* next value */
                 struct timeval it_value;    /* current value */
    };
    struct timeval {
               long tv_sec;                /* seconds */
               long tv_usec;               /* microseconds */
    };

    A sample code would be

    Code:
    #include <stdio.h>
    #include <signal.h>
    #include <sys/time.h>
    
    #define INTERVAL 10
    
    void alarm_wakeup (int i)
    {
       signal(SIGALRM,alarm_wakeup);
       printf("%d sec up partner, Wakeup!!!\n",INTERVAL);
       exit(0);
    }
    
    int main ()
    {
      struct itimerval tout_val;
      
      tout_val.it_interval.tv_sec = 0;
      tout_val.it_interval.tv_usec = 0;
      tout_val.it_value.tv_sec = INTERVAL; /* set timer for "INTERVAL (10) seconds */
      tout_val.it_value.tv_usec = 0;
      setitimer(ITIMER_REAL, &tout_val,0);
    
      signal(SIGALRM,alarm_wakeup); /* set the Alarm signal capture */
      
      while (1)
      {
        ;
      }
      
      return 0;
    
    }
    Program output

    [root@olinux1 tools]# gcc -o sample sample.c
    [root@olinux1 tools]# ./sample
    10 sec up partner, Wakeup!!!
    [root@olinux1 tools]#

  3. #3
    Join Date
    Mar 2005
    Posts
    137

    Re: How to use timer in Unix ?

    another example with setitimer() to implement a repetitive timer. Signal handler for SIGALRM signal, resets the timer.

    Code:
    #include <stdio.h>
    #include <signal.h>
    #include <sys/time.h>
    
    #define INTERVAL 5
    
    int howmany = 0;
    
    void alarm_wakeup (int i)
    {
       struct itimerval tout_val;
    
       signal(SIGALRM,alarm_wakeup);
    
       howmany += INTERVAL;
       
       printf("\n%d sec up partner, Wakeup!!!\n",howmany);
       tout_val.it_interval.tv_sec = 0;
       tout_val.it_interval.tv_usec = 0;
       tout_val.it_value.tv_sec = INTERVAL; /* 10 seconds timer */
       tout_val.it_value.tv_usec = 0;
       
       setitimer(ITIMER_REAL, &tout_val,0);
       
    }
    
    void exit_func (int i)
    {
        signal(SIGINT,exit_func);
        printf("\nBye Bye!!!\n");
        exit(0);
    }
    
    int main ()
    {
      struct itimerval tout_val;
      
      tout_val.it_interval.tv_sec = 0;
      tout_val.it_interval.tv_usec = 0;
      tout_val.it_value.tv_sec = INTERVAL; /* 10 seconds timer */
      tout_val.it_value.tv_usec = 0;
      setitimer(ITIMER_REAL, &tout_val,0);
    
      signal(SIGALRM,alarm_wakeup); /* set the Alarm signal capture */
      signal(SIGINT,exit_func);
      
      while (1)
      {
        //printf("!");
      }
      
      return 0;
    
    }
    Program output

    [root@olinux1 tools]# ./sample

    5 sec up partner, Wakeup!!!

    10 sec up partner, Wakeup!!!

    15 sec up partner, Wakeup!!!

    20 sec up partner, Wakeup!!!

    ^C
    Bye Bye!!!
    [root@olinux1 tools]#

  4. #4
    Join Date
    Apr 2008
    Posts
    1

    Re: How to use timer in Unix ?

    This informmation was very useful thanks for this post

  5. #5
    Join Date
    Sep 2005
    Posts
    18

    Re: How to use timer in Unix ?

    Thx a lot!

  6. #6
    Join Date
    Sep 2005
    Posts
    18

    Re: How to use timer in Unix ?

    I want to know how to pass parameter to the function "alarm_wakup" from main?

  7. #7
    Join Date
    Mar 2005
    Posts
    137

    Re: How to use timer in Unix ?

    Well, unfortunately you can't. Work with global variables. They are bad programming choice. But you have a very little choice here.

  8. #8
    Join Date
    Sep 2005
    Posts
    18

    Re: How to use timer in Unix ?

    then what does the (int i) mean ?

    void alarm_wakeup (int i)

  9. #9
    Join Date
    Mar 2005
    Posts
    137

    Re: How to use timer in Unix ?

    Frankly, I do not really know for sure. But signal handler prototype varies from system to system. On my linux system it has prototype as

    Code:
     #include <signal.h>
    
      typedef void (*sighandler_t)(int);
    
      sighandler_t signal(int signum, sighandler_t handler);
    The function argument here to the signal handler function is usually the signal number that invoked the handler. Anyway as far I know, its unused.

    I have also seen signal handler prototyped as

    Code:
    void (*func)();

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center