Linux epoll vs non-blocking sockets

Started by
12 comments, last by hplus0603 17 years, 2 months ago
Quote:On Winsock newsgroups experts say that this is not true. It can still block.


If select() returns a socket as readable, then recv() will not block on the next read, guaranteed.

This is not true for certain Windows-specific versions, like the infamous WSAAsyncSelect(). Stay away from AsyncSelect().

enum Bool { True, False, FileNotFound };
Advertisement
When 'select' says readable, that means it *was* readable at the time 'select' said it was. It has *never* provided a future guarantee of readability. *No* status reporting system call provides future guarantees.
You can't compare recv() and epoll they exist for different purposes. epoll is an efficient facility for determining which of a large number of file descriptors has stuff waiting to be read/ written / errors etc.

Of course epoll is vastly preferable to 5000 recv()s if almost all of them return EWOULDBLOCK (e.g. there is no data waiting and they're non-blocking).

But if you know that there are data to read, you may as well read them.

epoll is a Linux-specific facility (although some other Unixes have similar ones) which enables you to add a FD to watch, just once, and continue to watch it until you tell it to stop (or you close it).

Then each call to epoll_wait will wait for any of the things the epoll FD is currently configured to wait for, which could be a great many. This saves the setup work required for select().

Moreover, you can get an array back telling you exactly what has happened on which watched FDs.

Mark
Quote:When 'select' says readable, that means it *was* readable at the time 'select' said it was. It has *never* provided a future guarantee of readability. *No* status reporting system call provides future guarantees.


Not true. Once data is queued for the incoming socket, then that file descriptor WILL be readable, until you close that socket. If select() returns a given socket as readable, it WILL NOT BLOCK the next time you call recv(), unless you take explicit action on that socket in the meanwhile.

There may be other kinds of file descriptors (not sockets) where this is not true. However, the interaction between select/recv() is defined to be exactly this, for good reason. Also note that I'm talking about recv(), not read().
enum Bool { True, False, FileNotFound };

This topic is closed to new replies.

Advertisement