Use of 4.2 select(2) call for sub-second sleeps

Jim Wang jim at artecon.UUCP
Fri Jan 17 08:44:07 AEST 1986


Is there any reason why the select(2) system call on 4.2bsd
systems can't be used for a quick and dirty (QND) sub-second
sleep?  Granted that it is not portable to non-Berkeley, but in
cases where that is not a consideration, it has a few advantages:

1)	Does not require setting up a timer (You don't have to
	remember all those nested structures).

2)	You don't have to worry about taking over the SIGALRM
	signal.  You also needn't set up a dummy routine that
	does nothing except provide a place to point to.

3)	Most importantly, you don't have to worry about critical
	races where the timer expires before the signal catcher
	is set up.  These are a distinct possibility for
	sub-second intervals.

Not knowing how select() works internally, I can't determine if
a tremendous amount of overhead is required.  I have used it in
a number of applications (see below for example) without apparent
detrimental effects, but I've wondered if perhaps I'm missing
something.


Jim Wang
Artecon, Inc.
(jim at seismo, seismo!kobold!artecon!jim)
-----------------------------------------------------

#include <stdio.h>
#include <sys/time.h>

static
void
nap(seconds, microseconds)
int	seconds, microseconds;
{
	struct	timeval	timeout;

	timeout.tv_sec = seconds;
	timeout.tv_usec = microseconds;

	(void) select(1, 0, 0, 0, &timeout);
}

/* That's all there is to it. */



More information about the Comp.unix.wizards mailing list