Search code examples
linuxsocketsselectnonblocking

Select with non-blocking reads


I'm having a hard time finding an answer for this online.

What should be expected when calling select for reading file and the socket is set to non-blocking on Linux ?

I don't find it trivial as select man page states:

Those listed in readfds will be watched to see if characters become available for reading (more precisely, to see if a read will not block; in particular, a file descriptor is also ready on end-of-file)

If the socket is set non-blocking, it should never block, should select return immediately? This sounds wrong... Is Hyde documentation wrong or just ignore this case?

Also, if select will indeed block, what should be it's return value? Reading the socket will not block but the socket has no data available for reading..

When I get home I'll write some code examples and edit this question, this could help a lot to anyone else googling this subject.


Solution

  • If you select() to read on a socket which has no data waiting to be read (nor any errors), it will block (until the select() timeout expires).