[comp.unix.questions] Sleep time interval

martin@macadam.mqcs.mq.oz.au (Martin Foord) (11/06/90)

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 ?

					Martin.

gwyn@smoke.brl.mil (Doug Gwyn) (11/06/90)

In article <725@macuni.mqcc.mq.oz> martin@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 );
	}

lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) (11/07/90)

In article <14347@smoke.brl.mil> gwyn@smoke.brl.mil (Doug Gwyn) writes:
: In article <725@macuni.mqcc.mq.oz> martin@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.

The second thing you need to be aware of is that it doesn't necessarily
specify the MINIMUM delay either.  If you say sleep(1), some implementations
will sleep till the top of the next second, which could be a very small
fraction of a second away.  So sleep(n) sleeps a minimum of n-1 seconds
on such machines.  In terms of portability, sleep(1) is virtually
worthless (except in a statistical sense) without knowing when in the
second it's going to sleep.

Machines on which alarm() is implemented using setitimer() do not appear
to have this problem.  It's possible it's been fixed elsewhere too--some
of my databanks haven't been updated in quite some time.  I could even
be getting confused with other OS's.  Caveat Programmor.

Larry Wall
lwall@jpl-devvax.jpl.nasa.gov

prc@erbe.se (Robert Claeson) (11/08/90)

In a recent article martin@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 ?

In general, yes. But Xenix and System V Release 3.2-4.0 has the "nap()"
function, which allow sub-second sleeps. Some systems has a "usleep()"
function for the same purpose. System V Release 3.0 and 3.1 can be
tricked into doing this with the "poll()" system call, and BSD systems
can be fooled into doing this using the poll-like "select()" system call.

Came to think of it, nap() and usleep() are probably implemented as system
calls on most systems.

-- 
Robert Claeson                  |Reasonable mailers: rclaeson@erbe.se
ERBE DATA AB                    |      Dumb mailers: rclaeson%erbe.se@sunet.se
Jakobsberg, Sweden              |  Perverse mailers: rclaeson%erbe.se@encore.com
Any opinions expressed herein definitely belongs to me and not to my employer.

root@tndsyd.oz (Berny Goodheart) (11/08/90)

Or:
	UNIX Curses Explained --- Berny Goodheart -- ISBN 0 13 931957 3
	Prentice Hall -- Just published.