CSE Roadmap

Core tier · about 5 hours · 7 parts

Part 3 of 7 · 2 min read

Thread per connection

In plain words: In the first design, the main thread only accepts. For each client it starts a new thread that runs serve, and goes back to waiting:

for (;;) {
    int fd = accept(ls, NULL, NULL);
    pthread_t t;
    pthread_create(&t, NULL, per_conn, (void *)(long)fd);
    pthread_detach(t);          /* nobody will join it (page 2) */
}

The client program opens C connections at once, each sending 10 requests one after another. A thread in the client handles each, so a run with 32 clients is 32 threads talking to the server. The test checks every reply, and reported 0 wrong replies in all runs below.

$ ./srv 0 &                      (0 means: thread per connection)
$ ./cli 8 10
clients 8, requests each 10: total 0.20 s, ... wrong replies 0
$ ./cli 32 10
clients 32, requests each 10: total 0.20 s, ... wrong replies 0

Both runs took 0.20 seconds. Ten requests of 20 ms each is 200 ms, and every client waited at the same time, so the number of clients did not matter. A sleeping thread uses no processor, and two processors are plenty. All 32 clients were served at once.

Strengths: the code is as simple as single-client code, each client is independent, and a slow client delays nobody else. Weaknesses: each client costs a thread, with its stack (8 MiB of address space by default here, ulimit -s printed 8192) and a kernel record. A busy server with thousands of clients can run into the limits of section 4. There is also no bound at all: a flood of connections gives a flood of threads.

Thread per connection

Thread per connection is a server design in which the program creates a new thread for each accepted connection and lets that thread serve the client until the connection closes.

Example: the first server above creates one detached thread for each accept, so 32 clients cause 32 threads to run serve together.

Check yourself: 1. Why did 32 clients take the same time as 8? 2. What does pthread_detach do here (page 2)? 3. Name one weakness of the design.