C Network programming, select()
Dear all,
I have a somehow trivial question about network programming in c. I was reading yesterday the man pages of select() Code:
int select(int nfds, fd_set *readfds, fd_set *writefds, According to the man page Code:
nfds is the highest-numbered file descriptor in any of the three sets, plus 1. I am trying to understand two things 1) Why the highest number there is needed? Before using select one has few file descriptors (actually) that want to track. Would not make sense to pass directly those instead of the maximum number. What one probably wants to do is to ask from kernel "Please look my file descriptors X,Y,Z". How kernel handles that when it know the maximum number of those? 2) Why +1 is needed? Would not be more convenient just to give the max number and for the reason (which I do not also understand) the function then to add +1 to the provided number? I would like to than k you in advance for your help B.R Alex |
The file-descriptors are stored in the three bit-vectors (readfds, writefds, exceptfds), the first parameter only informs the kernel how long it has to check to bit-vector.
For example, if you only want to check the standard input (handle==0), then set nfds to 1. |
As far as I could tell from looking it up, it's a legacy feature that short-circuits some work.
I guess having select() implemen a MAX(int *fds) on the inputs is too much work? Sure, maybe it does save a bit of work. But I think the signature could be reworked into something a little more sensible. |
Quote:
I'm an old-school developer, so I still use select(); it just add a simple line of code to determine the max file descriptor. Something like: Code:
int maxfd = -1; |
Quote:
|
All times are GMT -5. The time now is 05:34 PM. |