1

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?

Wallace
  • 561
  • 2
  • 21
  • 54
  • 2
    IMHO, if you have that many sockets, you really should consider epoll rather than select, it scales way better than select. – Nim Jan 08 '14 at 13:39
  • You could use some [event loop](http://en.wikipedia.org/wiki/Event_loop) library like [libevent](http://libevent.org/) or [libev](http://libev.schmorp.de/‎) – Basile Starynkevitch Jan 08 '14 at 14:17
  • possible duplicate of [Increasing limit of FD\_SETSIZE and select](http://stackoverflow.com/questions/7976388/increasing-limit-of-fd-setsize-and-select) – mpromonet Jan 17 '15 at 13:39
  • In my case, it gave back the wrong "ready to read" fd, and my (single-threaded program ended up blocked indefinitely on a read() – GL2014 Mar 04 '20 at 20:05

1 Answers1

3

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.

  • I don't think this answer is particularly relevant to the question... the OP wants to know how to handle *more than* 1024 sockets... – Nim Jan 08 '14 at 13:40
  • @Nim the questions are all "what will happen if I do this", not "what should I do instead" - I chose to answer what was asked instead of skipping ahead to an anticipated followup question. –  Jan 08 '14 at 13:44