NOFILE, getdtablesize()

Chris Torek chris at umcp-cs.UUCP
Sun Nov 2 10:01:24 AEST 1986


In article <679 at copper.UUCP> stevesu at copper.UUCP writes:
>This change [more file descriptors] particularly affects callers
>of select().  The mask arguments are now pointers to arrays of
>integers, not pointers to single 32-bit integers.  (Berkeley really
>lucked out on this one; they were able to maintain backwards-
>compatibility with 4.1bsd,

I guess you mean `with 4.2BSD'.  It was hardly luck, having been
planned in advance.

>... Anyway, programs must in general dynamically allocate the arrays
>they'll hand to select, using getdtablesize() to determine how big
>the array needs to be.

That is not really necessary, although it will not hurt.

>	#include "select.h"
>
>	int *mask = (int *)malloc(NFDwords(getdtablesize()) * sizeof(int));
>
>	bzero((int *)mask, NFDwords(getdtablesize()) * sizeof(int));
>
>	Setbit(0, mask);
>
>	select(getdtablesize(), mask, (int *)NULL, (int *)NULL,
>						(struct timeval *)NULL);
>
>	if(Getbit(0, mask)) ...

4.3BSD already has types and macros for this:

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

	fd_set mask;
	int cc;

	FD_ZERO(&mask);
	FD_SET(0, &mask);
	cc = select(1, &mask, (fd_set *)0, (fd_set *)0, (struct timeval *)0);
	if (cc < 0) ...
	if (FD_ISSET(0, &mask)) ...

The first argument to select should be one more than the maximum
file descriptor set in any of the masks.  (Using larger values is
legal, but makes the kernel work harder.)  See the revised IPC
primer; it should be somewhere in /usr/doc.

Unfortunately, this is limited to FD_SETSIZE descriptors, normally
256, which makes binary compatibility somewhat difficult if anyone
raises NOFILE beyond this.  It might be nice if instead there were
library routines to allocate the masks based on getdtablesize().

Also, note that getdtablesize() is a system call, and therefore
expensive.  If you are going to use it often, put it into a variable.
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7690)
UUCP:	seismo!umcp-cs!chris
CSNet:	chris at umcp-cs		ARPA:	chris at mimsy.umd.edu



More information about the Comp.unix.wizards mailing list