I'm doing some socket programming using the POSIX interface. I'm trying to use the select() function to monitor when a nonblocking socket is ready to be read/write.

When using the select() function, I give it a set of read and write file descriptors. Specifically, I have a single socket fd that I want to check if it is both readable and writable.

The POSIX documentation states that the return value is a sum of all file descriptors featured in all the fd sets.

My early testing shows that I get a '2' back which is good (that means my socket is both readable and writable). However, what happens if the file descriptor doesn't simultaenous become both readable/writable? Say it becomes readable a microsecond before it becomes writable? What happens? Does it just return a 1?

  • It will return 1, you will do your processing, and when you loop back it will return another value. Apr 30, 2018 at 22:22
  • select() returns the number of file descriptors that are ready for reading or writing, not their sum. Also, you linked to a linux manual, not to the POSIX documentation.
    – Kusalananda
    May 2, 2018 at 6:59

1 Answer 1


select() will return when at least one of the file descriptors become ready for reading or writing, or when the timeout is reached, or when interrupted by a signal.

This means that you will have to test the file descriptor sets given to select() after determining that at least one file descriptor is ready for reading or writing (the sets are modified by select()).

Upon successful completion, the pselect() or select() 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. For each file descriptor less than nfds, the corresponding bit shall be set upon successful completion if it was set on input and the associated condition is true for that file descriptor.

(from the POSIX documentation on select())

If you need your file descriptor open for both reading and writing, you would have to loop over select() until both conditions are met.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.