Mihran Hovsepyan
Mihran Hovsepyan

Reputation: 11108

How to use POSIX select()

Shoud I make file descriptors non-blocking before using them in select()?

Upvotes: 3

Views: 2579

Answers (3)

hdante
hdante

Reputation: 8030

The goal of select is to block, so it will ignore the non blocking flag. However, as described in the bugs section in the Linux manual pages:

Under Linux, select() may report a socket file descriptor as "ready for reading", while nevertheless a subsequent read blocks. This could for example happen when data has arrived but upon examination has wrong checksum and is discarded. There may be other circumstances in which a file descriptor is spuriously reported as ready. Thus it may be safer to use O_NONBLOCK on sockets that should not block.

So, due to buggy behavior, you should set the file descriptors to non-blocking.

Upvotes: 0

jbruni
jbruni

Reputation: 1247

Select itself will block regardless of the blocking status of the descriptors it is used to monitor. If you don't want select to block, use a timeout of 0 (i.e. point to a timeval structure of zero, not a nil pointer).

Upvotes: 3

Erik
Erik

Reputation: 91320

Doesn't matter.

select tells you which sockets are readable/writable/closed/have state that you're interested in. Blocking/non-blocking affects how e.g. a recv or send call acts. These are independent of each other.

Upvotes: 6

Related Questions