If I have a file descriptor with an integer value above 1024, can select() still work on it?

199 Views Asked by At

The man page for select() states:

WARNING: select() can monitor only file descriptors numbers that are less than FD_SETSIZE (1024)—an unreasonably low limit for many modern applications.

This is very vague, I understand that select can only monitor 1024 file descriptors, but what if I want it to monitor a single file descriptor, a descriptor with an integer value greater than 1024.

For context, I am writing a program that has a timeout on a file descriptor of 15 seconds, and this file descriptor may have an integer value greater than 1024. From what I heard a few months ago, select() seems like a prime choice for this task.

I will only be monitoring one file descriptor, say it is called int fd and fd == 65533. Can select watch this single descriptor 65533?

1

There are 1 best solutions below

0
zwol On

The manpage might give you the impression that 1024 is a hard upper limit for select, but that's not true. Some C libraries let you #define FD_SETSIZE yourself, before including sys/select.h, and thus enlarge fd_set. Even if you don't have one of those C libraries, nothing stops you from defining your own, larger fd_set-like data type. The kernel does not know how big the C library defined fd_set to be: instead it trusts the nfds argument to tell it how many bits there are.

However, if you only have one fd to monitor, you should definitely use poll() instead of select(), because it's both simpler to use, and more efficient, for small numbers of fds. And if you have many fds to monitor, epoll or kqueue or similar will be more efficient than either poll or select. So it usually isn't worth bothering to work around the size of fd_set.