I have a legacy code which is doing this:
select(nFD + 1, &tReadFds, NULL, NULL, &timer);
.............
if (FD_ISSET(nFD, &tReadFds))
n = read(nFD,len,x);
is the read gonna read the whole receive buffer(nFD), assuming ‘len’ and ‘x’ are big enough.
I think SELECT here is acting as just a way of blocking till data becomes available in recv buffer.
In a nutshell,
selectis a function that you can call without blocking (i.e. it returns immediately), and upon return it will tell you a list of file descriptors on which you can callread(orwrite) without blocking.Such a function is crucial if you want to provide a persistent service while processing I/O with only a single thread: You cannot afford to do nothing while you are waiting for I/O, and so you need a deterministic method to ensure that you can do non-blocking I/O.
Edit. Here’s an example of a typical single-threaded
select-server, in pseudo-code:Such a server never has to be idle, and the expensive processing function can take up almost all the available computing time (it just has to make sure to return when it needs more data). I think
selecteven allows for a context switch, so this will play nice in a multi-process environment.