Sleep time interval

Doug Gwyn gwyn at smoke.brl.mil
Tue Nov 6 15:23:43 AEST 1990


In article <725 at macuni.mqcc.mq.oz> martin at macadam.mqcs.mq.oz.au (Martin Foord) writes:
>Is the smallest interval of sleep time the integer 1? Is there anyway of
>sleeping from a C program for a smaller period of time ?

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).  The
following code is adapted from my System V emulation support for 4BSD
and exploits the 4BSD select() system call.  On System V you might be
able to use poll() in a similar way.

/*
	_nap -- support routine for 4.2BSD system call emulations

	last edit:	29-Oct-1984	D A Gwyn
*/

extern int	_select();


int
_nap( 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 );
	}



More information about the Comp.unix.questions mailing list