select() is a relatively expensive operation. Well, it is never described as such in operating system courses, but it is in fact, specially when you design programs that handle a lot of file descriptors. Let’s see why.
Most servers work in the following way. When a new client connects and a session starts, the server establishes a connection though a socket. The server must then periodically check that socket and see if the client has sent any new data and, in that case, the server will probably process it, do some work and return an answer to the client.
What’s the problem with this? The problem happens when a lot of connections are handled by the server. Hundreds of sockets must be checked (polled) and this consumes a lot of time. Even when the poll()ing operation is short, the interchange of information between user and kernel space and the syscall becomes a problem when it is multiplied by a long list of connections.
The preferred solution is then the use of select(). With this scheme, you invoke the function with the list of descriptors you are interested in, and the kernel tells you whether a one of them is ready, whether or not you’ve done anything with that file descriptor since the last time the kernel told you about it.
But select() have also some problems with the descriptors list. This list must be built every time the functions is invoked, and it must also be checked after the function returns in order to see which sockets are ready. I don’t want to go into too many details, but we must know that the kernel is also quite inefficient when checking descriptors, as it goes along the list several times, performs copies, etc.
So, this is the moment when libevent comes to the rescue. Libevent is an abstraction over other operating system specific solutions (kqueue, epoll, etc) that gives good scalability when the number of descriptors grow. “libevent is meant to replace the event loop found in event driven network servers. An application just needs to call event_dispatch() and then add or remove events dynamically without having to change the event loop.”
I will be using libevent during the next month or so…



0 Responses to “The Problem with select()”