6 Server models

In general, a server process has to accept and handle a number of simultaneous connections from client processes. This makes a server program somewhat more interesting than a client program.

There are two main ways to write a server program. A one-process-per-connection server uses fork to start a new copy of itself every time a connection is accepted. This method means that each child process only needs to handle a single connection. This method is used by apache2.

A single-process-single-thread server handle things in a more interesting way. Make it much more interesting.

Although a single-process server can use non-blocking sockets and just keep trying until the intended read or write operations are done, this method is not particularly efficient, as it has to busy-wait for something to read or there is room to write.

As a result, most single-process servers utilize the select (see man 2 select) call, instead. In this programming model, sockets are not non-blocking. However, the server process knows exactly which connection FDs have bytes to read from or write to.

This way, the blocking read and write calls do not actually block. For a read call, if there is at least one byte available, the call returns. For a write call, if at least one byte can be written, the call does not block. In both cases, the actual number of bytes read or written becomes the return value.

The select call itself is a blocking call. It does not return until at least one monitored FD has bytes to read from or write to. Consequently, the server process goes to sleep when there is nothing to process.