Thread: Timers in c

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    192

    Timers in c

    I wanted to know if theres any timers in C linux programming and if anyone could give an example. So I don't want something like sleep(). Because I want the timer in an if statement since the statement will be in an infinite while loop. That onces the timer goes off then the program will process the code in the if statement. Is there anything like that in C linux.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So look in <time.h> for the standard stuff, and maybe unistd.h if you want something Linux specific, for getting the current time. (Presumably you'd get the time when you start the clock and then use something like difftime to get the current reading of the clock.)

  3. #3
    Registered User
    Join Date
    Aug 2009
    Posts
    192
    So I looked it up and It doesnt really seem to get any data types towards teh milisecond acting as a stop watch kind of. Is the only method have two timers and getting the differences until it matches up with the desired time?

  4. #4
    Registered User
    Join Date
    Aug 2009
    Posts
    198
    Code:
    man gettimeofday
    man ftime
    Last edited by MTK; 09-09-2009 at 11:54 AM.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by kiros88 View Post
    So I looked it up and It doesnt really seem to get any data types towards teh milisecond acting as a stop watch kind of. Is the only method have two timers and getting the differences until it matches up with the desired time?
    Are you thinking of something like sleep(in seconds), or delay(in milliseconds) ?

  6. #6
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    You can use the alarm() or ualarm() function to trigger an event after the specified number of seconds / microseconds have elapsed. Trap the SIGALRM signal by writing a signal handler and put the if-else statement inside it.

  7. #7
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    . . . then there is always nanosleep()

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by kiros88 View Post
    Is the only method have two timers and getting the differences until it matches up with the desired time?
    That is why they call it "low level programming"

    Since you are using linux, you can use Glib. Take a look here:

    Timers

    (Ignore the fact that this is presented as part of the GNOME documentation, it does not require or depend on GNOME)

    Then take a look here

    The Main Event Loop

    and search for "g_timeout_add". This allows you to set a callback to occur at a certain time, IF you are using the Glib "main event loop".

    Which is a lot to swallow all at once, but Glib has TONS of functionality build into it -- complex datatypes like linked lists and hash tables, "g" threads, IO channels, on and on, it's a kind of whahoo! shopping spree library and if you intend to program for linux worth the time.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  9. #9
    Registered User
    Join Date
    Aug 2009
    Posts
    192
    I don't want to sleep or delay the program because i want the rest of the code to be runnning since im planning to just use the timer for and if else statement. I was looking for alarm() but what header file uses it

  10. #10
    Registered User
    Join Date
    Aug 2009
    Posts
    198
    So you want it like a stopwatch?

    Why not just save the start time and trigger when the difference is right?

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    If you want to know how alarm() works, type the command "man alarm".
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  12. #12
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    alarm() is limited to an integer number of seconds for the delay. These days it is more common to use setitimer() instead which allows for sub-second delays.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  13. #13
    Registered User
    Join Date
    Aug 2009
    Posts
    192
    Does anyone have a really simple example of setitimer I'm looking online and I found one but its kinda of confusing to understand
    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;
    }
    I dont really understand what calls the exit signal

  14. #14
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Perhaps start off with the simpler alarm() interface before moving to setitimer(). If you want to set a timer in milliseconds then consider using the microseconds analog of alarm() ie ualarm().

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Efficient timer mechanism for many timers
    By ShwangShwing in forum C Programming
    Replies: 9
    Last Post: 07-28-2009, 11:52 AM
  2. Threads and Timers
    By scioner in forum C Programming
    Replies: 8
    Last Post: 03-22-2008, 07:56 AM
  3. Timers, Timers, Timers!
    By Stan100 in forum Game Programming
    Replies: 9
    Last Post: 01-24-2003, 04:45 PM
  4. Timers
    By Mox in forum Windows Programming
    Replies: 2
    Last Post: 11-09-2001, 04:34 AM