I was wondering what the nfds
does, after reading different manuals, I end up with the only answer being it is the highest numbered file descriptor plus one. What is it exactly used for?
The select() function indicates which of the specified file descriptors is ready for reading, ready for writing, or has an error condition pending.
select() works by blocking until something happens on a file descriptor (aka a socket). What's 'something'? Data coming in or being able to write to a file descriptor -- you tell select() what you want to be woken up by.
The SELECT call tests each selected socket for activity and returns only those sockets that have completed. On return, if a socket's bit is raised, the socket is ready for reading data or for writing data, or an exceptional condition has occurred.
select command in Linux is used to create a numbered menu from which a user can select an option. If the user enters a valid option then it executes the set of command written in select block and then ask again to enter a number, if a wrong option is entered it does nothing.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With