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 ?

  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
    Sep 2005
    Posts
    18

    Re: How to use timer in Unix ?

    Thx a lot!

  5. #5
    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?

  6. #6
    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.

  7. #7
    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)

  8. #8
    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)();

  9. #9
    Join Date
    Sep 2007
    Posts
    1

    Re: How to use timer in Unix ?

    How can I use this kind of timer to run a file every few minutes/seconds ?
    I tried crontab but seems like is not working right (even though it was supposed to).
    I want to run a file.pl using perl every 5 minutes or so. How should I change this timer to what I need ?

  10. #10
    Join Date
    Apr 2008
    Posts
    1

    Re: How to use timer in Unix ?

    This informmation was very useful thanks for this post

  11. #11
    Join Date
    Apr 2008
    Posts
    26

    Re: How to use timer in Unix ?

    Quote Originally Posted by Corne
    How can I use this kind of timer to run a file every few minutes/seconds ?
    I tried crontab but seems like is not working right (even though it was supposed to).
    I want to run a file.pl using perl every 5 minutes or so. How should I change this timer to what I need ?
    1) Put "sleep 300" in the perl file and run it infinitely
    2) Use shell script and use that

    These two are easier than C program. C++ - Never reinvent the wheel

  12. #12
    Join Date
    Dec 2006
    Posts
    10

    Re: How to use timer in Unix ?

    Hi nkhambal ,
    actually i have tried to run the 1st code u posted, it works fine.
    but i get these warnings:

    s1.c: In function ‘alarm_wakeup’:
    s1.c:35: warning: incompatible implicit declaration of built-in function ‘exit’

    how can i get rid of them?

    (i am working with Ubuntu8.04)

    thanks
    Last edited by raedbenz; July 12th, 2008 at 07:40 AM. Reason: new results

  13. #13
    Join Date
    Jul 2009
    Posts
    1

    Post Re: How to use timer in Unix ?

    hello togehter,

    I also looking for a timer for my program. The example from "nkhambal" works fine. But is it possible to recognize a method from a class as signal handler instead a function??

    I have a solution like this, but i would prefer a normal method as signal handler!

    Code:
    .h:
    
    class CommandProcessor
    
    {
    
    public: 
    	// implements singleton pattern
    	static CommandProcessor& getInstance();		
    
    
    	void timerExpired();
    };
    
    .cpp:
    
    //normal functions:
    
    void sigHandler(int i)
    
    {	
        CommandProcessor::getInstance().timerExpired();
    }
    
    	
    
    void configureTimer()
    
    {
    
    	struct itimerval timer;
    
    
    	timer.it_interval.tv_sec = 0;
      	timer.it_interval.tv_usec = 0;
      	timer.it_value.tv_sec = 0; ;//INTERVAL; /* set timer for "INTERVAL (10) seconds */
      	timer.it_value.tv_usec = 1;
      	setitimer(ITIMER_REAL, &timer,0);
    	
    	signal(SIGALRM, sigHandler);
    
    }
    
    // method from commandProcessor class
    void CommandProcessor::timerExpired()
    {
       //....
    }


    Thx for help,
    Cheers Bisk

  14. #14
    Join Date
    Apr 2013
    Posts
    1

    Re: How to use timer in Unix ?

    Hi Raedbenz,

    I realize this question was posed in 2008 and it is 2013 already, but the answer to your question is very simple. You probably already have it figured out..

    Add the line:

    Code:
    #include <stdlib.h>

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center