tags:

views:

179

answers:

3

Is a server essentially a background process running an infinite loop listening on a port? For example:

while(1){
   command = read(127.0.0.1:xxxx);
   if(command){
      execute(command);
   }
}

When I say server, I obviously am not referring to a physical server (computer). I am referring to a MySQL server, or Apache, etc.

Full disclosure - I haven't had time to poke through any source code. Actual code examples would be great!

+2  A: 

In a matter of speaking, yes. A server is simply something that "loops forever" and serves. However, typically you'll find that "daemons" do things like open STDOUT and STDERR onto file handles or /dev/null along with double forks among other things. Your code is a very simplistic "server" in a sense.

xyld
+5  A: 

That's more or less what server software generally does.

Usually it gets more complicated because the infinite loop "only" accepts the connection and each connection can often handle multiple "commands" (or whatever they are called in the used protocol), but the basic idea is roughly this.

Joachim Sauer
are you basically saying that my pseudo-code + threading is pretty much correct?
Tony
@Tony: yes. Of course pseudo-code implies that there are still many details to get right, but that's the idea.
Joachim Sauer
Not quite. What remains is _how_.
Tim Post
+3  A: 

There are three kinds of 'servers' - forking, threading and single threaded (non-blocking). All of them generally loop the way you show, the difference is what happens when there is something to be serviced.

A forking service is just that. For every request, fork() is invoked creating a new child process that handles the request, then exits (or remains alive, to handle subsequent requests, depending on the design).

A threading service is like a forking service, but instead of a whole new process, a new thread is created to serve the request. Like forks, sometimes threads stay around to handle subsequent requests. The difference in performance and footprint is simply the difference of threads vs forks. Depending on the memory usage that is not servicing a client (and prone to changing), its usually better to not clone the entire address space. The only added complexity here is synchronization.

A single process (aka single threaded) server will fork only once to daemonize. It will not spawn new threads, it will not spawn child processes. It will continue to poll() the socket to find out when the file descriptor is ready to receive data, or has data available to be processed. Data for each connection is kept in its own structure, identified by various states (writing, waiting for ACK, reading, closing, etc). This can be an extremely efficient design, if done properly. Instead of having multiple children or threads blocking while waiting to do work, you have a single process and event loop servicing requests as they are ready.

There are instances where single threaded services spawn multiple threads, however the additional threads aren't working on servicing incoming requests, one might (for instance) set up a local socket in a thread that allows an administrator to obtain a status of all connections.

A little googling for non blocking http server will yield some interesting hand rolled web servers written as code golf challenges.

In short, the difference is what happens once the endless loop is entered, not just the endless loop :)

Tim Post
great explanation.
Tony