I have a client which adds sockets fd's to FD_SET, and later in the code i want to use select() mechanism on this FD_SET i have. If the "master" fd_set contains no items at all, what is the return value of select()? What will is my "fdmax" parameter ? 0?
I need to handle such case, where my set is actually empty.. I just wonder if its possible to handle it implicitly, without special counter + if { }
It will work just fine.
The manual page for Linux states:
Some code calls select() with all three sets empty, nfds zero, and a non-NULL timeout as a fairly portable way to sleep with subsecond precision.
So, there's nothing strange with using empty sets. Sets can be empty, that's a part of their definition. And yes, you must pass 0, since you're supposed to pass one more than the largest descriptor.
I suggest defining your API to parties that need to add descriptors like so:
int add_fds(FD_SET *set);
and have them return 1 + (the largest descriptor added), or 0 if none was added as per above.
The return value will likely be 0, the manual page says:
On success, select() and pselect() return the number of file descriptors contained in the three returned descriptor sets (that is, the total number of bits that are set in readfds, writefds, exceptfds) which may be zero if the timeout expires before anything interesting happens.
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