[net.unix-wizards] Use of 4.2 select

jim@artecon.UUCP (Jim Wang) (01/17/86)

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@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. */

wls@astrovax.UUCP (William L. Sebok) (01/20/86)

In article <109@artecon.UUCP> jim@artecon.UUCP (Jim Wang) writes:
>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:

It's what is used for sub-second sleeps here.
-- 
Bill Sebok			Princeton University, Astrophysics
{allegra,akgua,cbosgd,decvax,ihnp4,noao,philabs,princeton,vax135}!astrovax!wls