Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return value of select() with empty sets

Tags:

c

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 { }

like image 679
buddy123 Avatar asked Jan 31 '26 00:01

buddy123


1 Answers

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.

like image 91
unwind Avatar answered Feb 01 '26 13:02

unwind



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!