RE: Question about select and poll system call

From: David Laight
Date: Sat Mar 18 2023 - 09:00:17 EST


> On Fri, Mar 17, 2023 at 2:15 AM Linus Torvalds
> <torvalds@xxxxxxxxxxxxxxxxxxxx> wrote:
> >
...
> > And back then, we had this:
> >
> > typedef unsigned long fd_set;
> >
> > which may seem a bit limiting today ("Only 32 bits??!?"), but to put
> > that in perspective, back then we also had this:
> >
> > #define NR_OPEN 20

That is the historic limit for SYSV (and probably BSD).
I suspect you just copied it.
Quite why it was 20 and not 16 or 32 I don't know.
But 20 open files was assumed to be 'plenty'!

The first SYSV kernel that supported fd >= 20 actually used
a linked list to hold the internal data (mostly a pointer).

So accessing a big fd number was O(fd).
Calling poll() O(numfd**2) and getting that many open sockets
in a network server process O(n**3).

Don't even think about how slowly a process trying to
use 2000 sockets was!

The 20 fd limit also made them a limited resource.
Demons couldn't really afford to dup() /dev/null onto 0, 1 and 2,
instead they'd just close the fd.
An accidental printf() that should have been sprintf() then
slowly fills the stdout buffer, when that eventually fills
the write to fd 1 has side effects that are rather difficult
to debug.
(Someone should have noticed that the tracing was incorrect)

David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)