Solved

How to create a timer in C that will execute some code when a particular amount of time has elapsed

Posted on 2004-09-19
8
326 Views
Last Modified: 2010-04-15
I was wondering if there is such a function in C that will allow me to enter a time and once that time has elapsed, then execute some code ??
I wish to do something like this

timer(4)
{
          executing = false;
}

once the 4 seconds is up I would like the executing flag to be set to false.
I don't know if this is possible but this is what I want to do.
0
Comment
Question by:Broken_Arrow
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12098149
you must implement a function like this:

timer(int seconds)
{
   time_t t;

    for (t= time(NULL); time(NULL)-t > 4000; );
}

Or if your are using windows environment you can use Sleep(4000);
0
 
LVL 22

Expert Comment

by:grg99
ID: 12098260
You can use signal() and alarm() or setitimer() to do this.  do a "man alarm"


0
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 200 total points
ID: 12098326
sorry, about my "timer" function, there is a mistake. I will perfect it:

timer(int seconds)
{
   time_t   t = time(NULL) + seconds;

   while (time(NULL)<t)  {
       /* do nothing */
   };
}

0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 

Author Comment

by:Broken_Arrow
ID: 12098341
I was just trying to figure out how it worked. Thanks I will give that a  go
0
 

Author Comment

by:Broken_Arrow
ID: 12098358
Thanks that works perfect
0
 
LVL 23

Expert Comment

by:Mysidia
ID: 12098379
Well, any timer facilities are part of external libraries specific to the environment and not part of the C language
itself, so technically no.

Do you want your program to stop executing and wait until the timer runs out and then
continue?

In that case jaime's suggestion of a busy loop would work, but it would also potentially waste a whole
lot of CPU time -- all your program has access to while possibly mpacting system wide performance
(depending on the environment).

Best to instead issue a sleep call, i.e. sleep(4);
on a unix system  #include <unistd.h>

sleep(4);
executing = false;

It sounds like you want this to happen in parallel however (while you're program is doing something
else)

in that case you want to setup a signal handler and use alarm() or setitimer as grg99 suggests

You may have a few other options:

 * Store your timers in a list and run the whole program in an event loop, for example
    time running out is just one kind of event that would resume execution to take an action

while ( !quitting )
{
      event = wait_for_event();
      quitting = react_to_event(event);
}

 * Or if your code is running inside an active loop and you want the timer to stop the loop by setting
    executing to false... have it periodically check

     while ( executing ) {
             check_time();
              ... next step in your computation ... }

  * Using multi-threaded programming could also achieve this... perhaps you could make a timer thread
     which would halt for at least 4 seconds and then set a shared executing variable to false

0
 

Author Comment

by:Broken_Arrow
ID: 12098543
Could anyone give me some help on setting up signal handler and using alarm(). I have just realised that the busy loop will not work in this case as I do need my program to be able to continue doing something else as Mysidia  pointed out.

0
 
LVL 23

Expert Comment

by:Mysidia
ID: 12098603
In unix... suppose you want to delay for 4 seconds and then send signal SIGALRM

#include <unistd.h>
#include <signal.h>

int timed_executing = true;

void alarm_timer(int signal_num)
{
      timed_executing = false;
}

signal(SIGALRM, alarm_timer);  /* <-- register the signal handler to be called when SIGALRM is received */
...

alarm(4);   /* <-- resets the timer to 4 seconds and starts it */

 /* code to run */

alarm(0);  /* <-- stops the timer without raising the signal */
signal(SIGALRM,  SIG_DFL);  /* <-- de-register the signal handler, setting SIGALRM back to Defaults */

0

Featured Post

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
My eclipse editor won't start ? 4 162
negation in C function 14 161
C Programming - If Statement 8 82
Resolve Dependency Issues 4 79
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question