1
int select(int nfds, 
       fd_set *readfds, 
       fd_set *writefds, 
       fd_set *exceptfds, 
       struct timeval *timeout);

Given fd_set is 1024 in length. What if the fd we want to monitor is 2048? What does select do when this happens? Will it happen at all?

Brian
  • 807
  • 3
  • 8
  • 16

1 Answers1

2

According to the specification, if nfds is larger than FD_SETSIZE, select() will return -1 and set errno to EINVAL.

In some implementations (including on Linux systems), it will instead write to bits outside the fd_set structure, potentially corrupting memory in your application. (The intent is to allow applications to use larger fd_set structures, but the effect is often to cause applications to crash as soon as they try to use more than 1024 file descriptors.)

To work with more than 1024 file descriptors, you will need to use an API other than select(). These APIs are often system-specific; one platform-independent option to consider is the libev library, which provides a set of useful abstractions for highly concurrent applications.