Can Unix sleep in terms of mili/micro?

Tom Weinstein tomw at orac.esd.sgi.com
Tue Sep 11 00:38:44 AEST 1990


In article <11899 at chaph.usc.edu>, tli at phakt.usc.edu (Tony Li) writes:
> In article <24437 at adm.BRL.MIL> TAYBENGH%NUSDISCS.BITNET at cunyvm.cuny.edu writes:
>     
>             Can Unix sleep in terms of mili or mirco second? I am aware that
>     sleep() can only sleep in terms of second. Please specify the Unix Dialect
>     when u reply. Thanks.
>     
> BSD derived systems provide usleep(3) which does what you want.  

Yuck.  Ick.  Ptui.

usleep is really pretty icky because it uses itimers, and the system
call overhead (4 of them!) can seriously destroy the accuracy of your
timing.  A much better idea is to use something like this:

{
   static struct timeval tv;

   tv.tv_sec = usecs / 1000000;
   tv.tv_usec = usecs % 1000000;

   select(0, 0, 0, 0, &tv);
}

This uses only one system call instead of the 4 of usleep, and it
doesn't use itimers so it won't screw up programs that use them for
other things.

--
Tom Weinstein
Silicon Graphics, Inc., Entry Systems Division, Window Systems
tomw at orac.esd.sgi.com
Any opinions expressed above are mine, not sgi's.



More information about the Comp.unix.internals mailing list