UNIX FAQ Version 2.1 92/12/04 -- Question 4.6

UNIX FAQ Version 2.1 92/12/04 -- Question 4.6

How do I sleep() in a C program for less than one second?

The first thing you need to be aware of is that all you can specify is a MINIMUM amount of delay; the actual delay will depend on scheduling issues such as system load, and could be arbitrarily large if you're unlucky. There is no standard library function that you can count on in all environments for "napping" (the usual name for short sleeps). Some environments supply a "usleep(n)" function which suspends execution for n microseconds. If your environment doesn't support usleep(), here are a couple of implementations for BSD and System V environments. The following code is adapted from Doug Gwyn's System V emulation support for 4BSD and exploits the 4BSD select() system call. Doug originally called it 'nap()'; you probably want to call it "usleep()"; /* usleep -- support routine for 4.2BSD system call emulations last edit: 29-Oct-1984 D A Gwyn */ extern int select(); int usleep( usec ) /* returns 0 if ok, else -1 */ long usec; /* delay in microseconds */ { static struct /* `timeval' */ { long tv_sec; /* seconds */ long tv_usec; /* microsecs */ } delay; /* _select() timeout */ delay.tv_sec = usec / 1000000L; delay.tv_usec = usec % 1000000L; return select( 0, (long *)0, (long *)0, (long *)0, &delay ); } On System V you might do it this way: /* subseconds sleeps for System V - or anything that has poll() Don Libes, 4/1/1991 The BSD analog to this function is defined in terms of microseconds while poll() is defined in terms of milliseconds. For compatibility, this function provides accuracy "over the long run" by truncating actual requests to milliseconds and accumulating microseconds across calls with the idea that you are probably calling it in a tight loop, and that over the long run, the error will even out. If you aren't calling it in a tight loop, then you almost certainly aren't making microsecond-resolution requests anyway, in which case you don't care about microseconds. And if you did, you wouldn't be using UNIX anyway because random system indigestion (i.e., scheduling) can make mincemeat out of any timing code. Returns 0 if successful timeout, -1 if unsuccessful. */ #include <poll.h> int usleep(usec) unsigned int usec; /* microseconds */ { static subtotal = 0; /* microseconds */ int msec; /* milliseconds */ /* 'foo' is only here because some versions of 5.3 have * a bug where the first argument to poll() is checked * for a valid memory address even if the second argument is 0. */ struct pollfd foo; subtotal += usec; /* if less then 1 msec request, do nothing but remember it */ if (subtotal < 1000) return(0); msec = subtotal/1000; subtotal = subtotal%1000; return poll(&foo,(unsigned long)0,msec); } Another possibility for nap()ing on System V, and probably other non-BSD Unices is Jon Zeeff's s5nap package, posted to comp.sources.misc, volume 4. It does require a installing a device driver, but works flawlessly once installed. (Its resolution is limited to the kernel HZ value, since it uses the kernel delay() routine.)