You are misunderstanding the select(2) man page (and see also select_tut(2)). BTW, you'll better use poll(2) in new code, like this; since select
is limiting (by the size of fd_set
) the highest file descriptor that it can use (think of the C10K problem).
The select
man page defines a file descriptor of your process as ready ...
... if it is possible to perform a corresponding I/O operation (e.g., read(2) without blocking, or a sufficiently small write(2)).
That is, if a file descriptor is such that you could successfully read(2) at least one byte from it, it is a ready file descriptor (for read
s). Most of the time, you'll be able to read several bytes from it. A file descriptor from which you cannot read
, because a read(2) attempt would block, is not ready so could be called a blocked file descriptor.
There are several cases of a file descriptor being blocked (that is, not ready). The usual one is some network socket(7) or some pipe(7) (or some terminal, see pty(7) and the TTY demystified and be aware of the line discipline) not having any more input. That is why poll
(or select
, epoll(7), etc...) is needed to code event loops (in particular in widget toolkits for GUI, in Web servers, etc.), since you want to avoid busy polling.
For further explanation, read a good Linux programming book, such as the ALP.
You could also read Operating Systems: Three Easy Pieces to get more knowledge about OSes.
Be aware that stdio(3) is implementing buffered IO (above read(2) and write(2)). See also fflush(3) and setvbuf(3).
See also proc(5) to query programmatically the state of processes (most of them being idle since blocked in IO operations or event loops). Or use ps(1), top(1) (both are using /proc/
) in commands.
At last, look into the source code of many free software projects (e.g. web servers, GUI toolkits, etc.). You can find many examples of event loops using select
or poll
.