Domanda

As far as I know, select only supports no more than 1024 sockets. But a process can own 65535 sockets which means most of the socket numbers are bigger than 1024, so I have three questions:

Q1. What will happen if passing socket numbers bigger than 1024 to FD_SET()?
Q2. What will happen if passing fd_set whose socket numbers are all bigger than 1024 to select()?
Q3. On Linux Fedora with kernel 2.6.8, x86 64bit, will exceptions be thrown in Q1 and Q2?

È stato utile?

Soluzione

An fd_set is an array of bits, only manipulated with FD_* macros because C doesn't have a "bit" type. (The type is officially opaque, and could be implemented a different way - in fact winsock does implement it differently - but all unix-like OSes use the array of bits.)

So this code:

fd_set my_fds;
....
FD_SET(1024, &my_fds);

has the same problem as this code:

char my_fds[1024];
....
my_fds[1024] = 1;

assuming FD_SETSIZE is 1024.

You will be overwriting whatever comes after the fd_set in memory, causing a segfault if you're lucky, more subtle errors if you're not.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top