Subject: Re: poll(2) oddity
To: David Laight <>
From: Jaromir Dolecek <>
List: tech-userlevel
Date: 07/08/2002 12:50:25
David Laight wrote:
> It is much more usual to set pfd[fd].fd = fd which simplifies
> the user side and is ok unless you have are polling for fd #1000
> (and no others).

This isn't portable of course - the fd is opaque int number,
kernel can return to you e.g. 18278932 even if you have only
descriptors 0, 1, 2 open. The fact that Unix returns consequent
descripts number is implementation detail. Also, using your
way, you always waste three slots (0, 1, 2).

> > Readable and doesn't waste kernel memory.
> Just user space code bloat....

No - during the poll, kernel allocates memory for array to hold
all the poll structs userland has passed via poll(2) call. Also,
the bigger the array the more time is spent copying structs from
userland to kernel, especially if you watch many descriptors and
call poll(2) frequently.

> > > This whole thing got me worried and i just tested the limits
> > > on FreeBSD, Solaris, and Linux.  The first two limit the size
> > > to the maximum number of open files, while Linux doesn't seem
> > > to have any limit.
> > 
> > Upping the limit to maximum number of open files would prolly be sensible.
> > However, I personally don't see a problem in keeping it the way it is
> > (and documenting the behaviour).
> The SuS says that EINVAL should be returned if the 'nfds' argument
> is greater than OPEN_MAX.

OPEN_MAX is fixed @ 64 (see <sys/syslimits.h>)
> This would be p->p_rlimit[RLIMIT_NOFILE].rlim_cur instead of
> p->p_fd->fd_nfiles.
> (There should be a check in setrlimit to ensure that
> p->p_rlimit[RLIMIT_NOFILE].rlim_cur > p->p_fd->fd_lastfile)

I don't think so. There isn't anything wrong in setting NOFILE
to lower value than current use; that would just prevent to open
any new files until some descriptors are closed.
> The limits.h page says the OPEN_MAX is a compile-time limit, and the
> run-time one can be found using sysconf().
> sysconf() says that the values it returns should be constant for the
> lifetime of a process.
> setrlimit(RLIMIT_NOFILE,...) allows a process to change the value
> - which breaks the sysconf() interface rules.
> Nice and consistent!

Hehe, indeed :)

Jaromir Dolecek <>
-=- We should be mindful of the potential goal, but as the tantric    -=-
-=- Buddhist masters say, ``You may notice during meditation that you -=-
-=- sometimes levitate or glow.   Do not let this distract you.''     -=-