TL;DR; Is it always necessary or recommended or is there any advantage on copying the select readfds fd_set over a "working" fd_set? What happens when using writefds and exceptfds?
This is my first question so apologies if this has been answered already but I tried to search as much as I could.
I am learning how to code with Unix C sockets API (specifically on GNU/Linux), and I have found many ways to deal with sockets using select() for sync I/O multiplexing.
Until now I only used the readfds fd_set of the select() function, many tutorials omits the other 2 sets, however the example of "man select_tut" gives me hints of ways to also use writefds, therefore I am experimenting with this set as well.
Beej's Guide example server (http://beej.us/guide/bgnet/examples/selectserver.c) shows the usage of a "master" fd_set.
However for instance, "man select_tut" only uses the 3 canonical fd_sets.
Imho, as long as you FD_ZERO the sets before select(), it will add the active descriptors on each set... so I don't see much point on keeping a second copy of the fd_sets out of the "select loop".
Thoughts?
Imho, as long as you FD_ZERO the sets before select(), it will add the active descriptors on each set...
That's not true.
When you call select
you need to specify exactly the descriptors you are interested in. After it returns, it partially clears the sets, i.e. only the descriptors that were "interesting" on that iteration are still set. That's why you need to reinitialize the sets.
The standard page spells it clearly:
Upon successful completion, the
pselect()
orselect()
function shall modify the objects pointed to by the readfds, writefds, and errorfds arguments to indicate which file descriptors are ready for reading, ready for writing, or have an error condition pending, respectively, and shall return the total number of ready descriptors in all the output sets.