Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is a server an infinite loop running as a background process?

Tags:

c

theory

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!

like image 444
Tony Avatar asked Apr 28 '10 12:04

Tony


3 Answers

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.

like image 103
Joachim Sauer Avatar answered Nov 16 '22 03:11

Joachim Sauer


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 :)

like image 24
Tim Post Avatar answered Nov 16 '22 02:11

Tim Post


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.

like image 39
dlamotte Avatar answered Nov 16 '22 02:11

dlamotte