When you're using select()
, you are trying to check the status of a set of file descriptors. The possible range of file descriptors you're interested in ranges from a low of 0 (standard input) to some maximum value (the highest file descriptor you have open that you're interested in checking the status of). You have to tell select()
how big the list of file descriptors is because the total number can be 'vast' (32767, for example). In that case, it takes time for the kernel to process the descriptors, plus you may not have initialized the fd_set
up to that number of entries. FD_SETSIZE
also figures in the equation, but sometimes you can change that value.
So, if you want to monitor file descriptors 24-31, you'd set nfds
to 32, and ensure that you use FD_ZERO()
to zero the whole fd_set
and FD_SET()
to set entries 24-31. Note, too, that select()
modifies the input parameters, so you have to use FD_ISSET()
to test after the select()
returns, and in general you have to redo the initialization (or copy a saved value) of fd_set
before calling select()
again.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…