Delay for a fraction of a second in C

Bill Davidson billd at celerity.UUCP
Tue Nov 8 12:37:04 AEST 1988


Well, it seems that I have to flame myself:

In article <191 at celerity.UUCP> billd at celerity.UUCP (Bill Davidson) writes:
>In article <1145 at orion.cf.uci.edu>, echarne at orion.cf.uci.edu (Eli B. Charne) writes:
>> 
.....
>> a fraction of a second.  If it could go to 100th of a second, that would
>> be great!
>
.....
>given the correct intervals because in UNIX, you may be waiting to get
>swapped in.  I should probably test it on our VAX but I'm too lazy.
>*	FSLEEP.C - Fine sleep.  This function allows you to sleep for
>*	smaller intervals of time than 1 second.
.....
>*	Note that the manual page incorrectly identifies the timer
>*	intervals as being setable to microseconds.  It really meant
>*	milleseconds (either that, or whoever set up setitimer on our
>*	machine made a mistake).  When I first tried it (naively
>*	believing the manual) I used 500000 for usecs thinking it
>*	would be half a second instead of 500 seconds which is what
>*	it was.

A kernel type engineer here informed me that this was fixed a LONG time ago
and that it now works in microseconds on our machine.  Of course, this means
that my program is wrong as well.  This is the part that needs to be changed:

>    if ( usecs >= 1000L ){			/* fix the interval */
>	secs += usecs / 1000L;
>	usecs %= 1000L;
>    }

All of the 1000L's should be changed to 1000000L's to reflect the microseconds.
I should have assumed that other people's setitimer's worked correctly when
I posted anyway.  If anyone picked up my code, then they should make this
change and it *should* work (but as I said before, I don't personally
guarantee anything).  I suppose this really belongs in comp.unix.questions
but this is where the previous things were posted.  There doesn't seem to
be *any* portable way to write a delay function with delays < 1 second.
On UNIX, ANY delay function is going to have an accuracy problem.  Setitimer
could have a smallest interval of 1/100 sec and there would probably be no
functional difference.

As long as I flaming myself, I'll also flame the guy who suggested spinning
delay loops:
	ACK! GAG! BARF! :-(

	--Bill Davidson

-- 
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
....!{ucsd|sdcsvax}!celerity!billd



More information about the Comp.lang.c mailing list