Search code examples
cfile-descriptorposix-select

How would select() return anything other than -1, 0, or 1?


This is definitely just a dumb misunderstanding on my part, but the man page for select() states:

The timeout argument specifies the interval that select() should block waiting for a file descriptor to become ready. The call will block until either:

*a file descriptor becomes ready;

*the call is interrupted by a signal handler; or

*the timeout expires.

And furthermore, that

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. On error, -1 is returned, and errno is set to indicate the error; the file descriptor sets are unmodified, and timeout becomes undefined.

So my question is -- if it stops blocking as soon as a file descriptor is ready, would it not immediately return 1? And if no fds become ready, it returns 0, otherwise error and returns -1.

Obviously in practice it returns more than 1: the whole point is that you should be able to read/write multiple fds, right?


Solution

  • Due to the way modern preemptive multitasking works, multiple descriptors could become ready before your process is awoken and the select calls counts the descriptors.