Part 7 of 7 · 2 min read
Exercises
Easy
Easy 1: Predict the waves (20 minutes)
A pool server has 6 workers. 30 clients connect, each needing 200 ms. Predict the total time.
Done when: 5 waves of 0.2 s, about 1.0 s.
Hint
Waves = 30 divided by 6 = 5.
Easy 2: Compute throughput (20 minutes)
A test sends 450 requests and finishes in 0.9 s. Give the throughput.
Done when: 500 requests per second.
Hint
Throughput = requests divided by time.
Easy 3: Read the limits (20 minutes)
Run ulimit -n, ulimit -u and cat /proc/sys/kernel/threads-max. Write what each limits.
Done when: three values from your machine with one sentence each.
Hint
ulimit -n is open files per process.
Medium
Medium 1: Build and test (45 minutes)
Write the server (thread per connection) and the client, and run 8 and 32 clients with 10 requests each.
Done when: 0 wrong replies and both total times near 0.20 s.
Hint
SO_REUSEADDR lets the server restart at once.
Medium 2: Add the pool (45 minutes)
Add the pool mode with a queue. Run it with pools of 4, 8 and 16 and 32 clients. Compare with the wave formula.
Done when: a table of three measured times next to three predictions within 5 per cent.
Hint
Use the wave count rounded up.
Medium 3: Count the threads (30 minutes)
While 100 clients are connected (use more requests per client), read Threads: from /proc/<pid>/status for each design.
Done when: about 101 threads for thread per connection and 9 for a pool of 8.
Hint
Threads: line in /proc/<pid>/status.
Hard
Hard 1: Requests in the queue (90 minutes)
Change the pool so that workers serve one request, not a whole connection. Use poll in the main thread to read the requests, and queue (socket, request) pairs. Repeat the 4-worker, 32-client test.
Done when: the total time falls well below 1.6 s and every reply is right.
Hint
Put the socket and the number in one struct in the queue.
Hard 2: Break it (60 minutes)
Make the client send 5,000 connections quickly to the thread-per-connection server, with ulimit -n lowered to 256 in the server's shell. Record what fails and the error number.
Done when: a report naming the failing call (accept, error EMFILE) and a fix (check, back off, close idle connections).
Hint
accept fails with EMFILE when the process has no free descriptor.
Hard 3: Shut down cleanly (90 minutes)
Add a SIGINT handler (setting a flag) so that the server stops accepting, lets workers finish their clients, joins them and prints the number of requests served.
Done when: a run that ends with the printed count equal to the number of requests the client sent.
Hint
Use sigaction without SA_RESTART, so that accept returns when interrupted.