Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

implement time delay in c

I don't know exactly how to word a search for this.. so I haven't had any luck finding anything.. :S

I need to implement a time delay in C.

for example I want to do some stuff, then wait say 1 minute, then continue on doing stuff.

Did that make sense? Can anyone help me out?

like image 238
developer Avatar asked Oct 14 '10 05:10

developer


People also ask

What is time delay in C programming?

Delay in C: delay function is used to suspend execution of a program for a particular time. Declaration: void delay(unsigned int); Here unsigned int is the number of milliseconds (remember 1 second = 1000 milliseconds). To use delay function in your program you should include the "dos.

How do you implement a delay function?

To delay a function call, use setTimeout() function. functionname − The function name for the function to be executed. milliseconds − The number of milliseconds. arg1, arg2, arg3 − These are the arguments passed to the function.

How is delay calculated in embedded C?

The first method is simply using Loop program function in which Delay() function is made or by providing for(); delay loop in Embedded C programming. You can define your own value of delay and how long you want to display. For example- for(i=0;i<"any decimal value";i++); this is the delay for loop used in embedded C.

How do you implement delay in assembly?

One classic way to make a delay is to use nested decrement loops. Every time the inner loop counts down to 0, then the next decrements, and so on. It's a bit tedious to adjust the timing, and interrupts will mess with the process, but it works.


2 Answers

In standard C (C99), you can use time() to do this, something like:

#include <time.h> : void waitFor (unsigned int secs) {     unsigned int retTime = time(0) + secs;   // Get finishing time.     while (time(0) < retTime);               // Loop until it arrives. } 

By the way, this assumes time() returns a 1-second resolution value. I don't think that's mandated by the standard so you may have to adjust for it.


In order to clarify, this is the only way I'm aware of to do this with ISO C99 (and the question is tagged with nothing more than "C" which usually means portable solutions are desirable although, of course, vendor-specific solutions may still be given).

By all means, if you're on a platform that provides a more efficient way, use it. As several comments have indicated, there may be specific problems with a tight loop like this, with regard to CPU usage and battery life.

Any decent time-slicing OS would be able to drop the dynamic priority of a task that continuously uses its full time slice but the battery power may be more problematic.

However C specifies nothing about the OS details in a hosted environment, and this answer is for ISO C and ISO C alone (so no use of sleep, select, Win32 API calls or anything like that).

And keep in mind that POSIX sleep can be interrupted by signals. If you are going to go down that path, you need to do something like:

int finishing = 0; // set finishing in signal handler                     // if you want to really stop.  void sleepWrapper (unsigned int secs) {     unsigned int left = secs;     while ((left > 0) && (!finishing)) // Don't continue if signal has         left = sleep (left);           //   indicated exit needed. } 
like image 154
paxdiablo Avatar answered Sep 29 '22 18:09

paxdiablo


Here is how you can do it on most desktop systems:

#ifdef _WIN32     #include <windows.h> #else     #include <unistd.h> #endif  void wait( int seconds ) {   // Pretty crossplatform, both ALL POSIX compliant systems AND Windows     #ifdef _WIN32         Sleep( 1000 * seconds );     #else         sleep( seconds );     #endif }  int main( int argc, char **argv) {     int running = 3;     while( running )     {   // do something         --running;         wait( 3 );     }     return 0; // OK } 

Here is how you can do it on a microcomputer / processor w/o timer:

int wait_loop0 = 10000; int wait_loop1 = 6000;  // for microprocessor without timer, if it has a timer refer to vendor documentation and use it instead. void wait( int seconds ) {   // this function needs to be finetuned for the specific microprocessor     int i, j, k;     for(i = 0; i < seconds; i++)     {         for(j = 0; j < wait_loop0; j++)         {             for(k = 0; k < wait_loop1; k++)             {   // waste function, volatile makes sure it is not being optimized out by compiler                 int volatile t = 120 * j * i + k;                 t = t + 5;             }         }     } }  int main( int argc, char **argv) {     int running = 3;     while( running )     {   // do something         --running;         wait( 3 );     }     return 0; // OK } 

The waitloop variables must be fine tuned, those did work pretty close for my computer, but the frequency scale thing makes it very imprecise for a modern desktop system; So don't use there unless you're bare to the metal and not doing such stuff.

like image 44
Frank Avatar answered Sep 29 '22 19:09

Frank