Delay for a fraction of a second in C

Dave Jones djones at megatest.UUCP
Fri Nov 4 08:26:13 AEST 1988


>From article <2804 at ingr.UUCP>, by crossgl at ingr.UUCP (Gordon Cross):
> In article <1145 at orion.cf.uci.edu>, echarne at orion.cf.uci.edu (Eli B. Charne) writes:
>> 
>> The Unix sleep command will only go in increments of one second.  I was
>> wondering if someone new of a nice little routine I could use, or has
>> written one that will let me pause (in the Unix operating system) for
>> a fraction of a second.  If it could go to 100th of a second, that would
>> be great!
> 
> You are not going to be able to do this without coding up some kind of
> "delay loop" yourself.  Of course that solution is definitely NOT portable in
> any way since the loop would have to be fine tuned to your machines execution
> speed.  Remember that in the UNIX kernal, scheduled wakeups occur at fixed
> one second intervals.  Thus, unless you are using a non standard kernal, sleeps
> of only whole numbers of seconds are possible!
> 
> 
> Gordon Cross
> Intergraph Corp.  Huntsville, AL




Mr. Cross, perhaps it is time that *you* should wake up.
You are dreaming up answers.  :-)

Pour yourself a cup of strong coffee, then try the following:

#include <sys/time.h>

delay(seconds, micro_seconds)
  long seconds;
  long micro_seconds;
{
  struct timeval timev;
  timev.tv_sec = seconds;
  timev.tv_usec = micro_seconds;
  select(0,0,0,0, &timev);
}

Some unixes may name the include-file <time.h> and the structure
"struct time_val", but it comes to the same thing.  Read the 
man-pages on select(), setitimer(), and signal().

Also, please don't code tight spin-loops for delays or for polling.
Particularly if you are on a time-shared system.



		Best regards,

		Dave J.



More information about the Comp.lang.c mailing list