Learning SequenceVol. 18 · Part XVIII
0 / 0

Trails / Learning Sequence

LS·197

Phase 1 — CS Foundations

The first phase of the learning sequence: programming, data structures and algorithms, discrete maths, computer architecture, operating systems and networking, in that order and why; an entry check, step-by-step exit checks mapped to Volume 1, the phase projects (C systems programs, a shell, an HTTP server, data structures, networking tools) with milestones and definitions of done, common traps, a time budget, and a lab that builds a minimal HTTP server in C.

18 topics · ~8 min read · 16-question quiz
Updated Suggest a correction

Start here: Phase 1 in one page

Every later phase rests on this one. When a Spring service stalls, a Kubernetes pod is throttled or a model server runs out of memory, the explanation is almost always in Phase 1 material: processes and threads, memory and caches, system calls, sockets, data structures and their costs. Engineers who skip it can follow recipes; engineers who have it can debug what the recipe didn't cover.

The sequence and why it's in this order

Text
Programming → data structures and algorithms → discrete maths → computer architecture → operating systems → networking
  • Programming first, because everything else is learnt by writing programs.
  • Data structures and algorithms next, because they're the vocabulary of performance: "this is O(n²)", "use a hash map".
  • Discrete maths supplies the proofs and counting behind algorithms, hashing, probability and cryptography.
  • Computer architecture explains what the hardware actually does: caches, pipelines, memory.
  • Operating systems build on architecture: processes, scheduling, virtual memory, files.
  • Networking builds on operating systems: sockets are file descriptors, servers are processes.

What you'll be able to do at the end

Write C and one higher-level language comfortably; choose data structures by their costs; explain what happens from a keypress to a pixel and from curl to a response; and build small systems programs: a shell, an HTTP server, a network tool.

A taste of the phase project

The lab built a 40-line HTTP server in C. Served one connection at a time, it answered about 29,000 requests per second to one client, yet a second client got no response at all while the first stayed connected. Forking a process per connection fixed that and reached about 118,000 requests per second with 50 clients. Python's built-in server, by comparison, managed 23 requests per second per connection, held back by a 40 ms network stall caused by how it writes responses. Each of those numbers is explained by Phase 1 material: blocking system calls, processes, and TCP's Nagle and delayed-ACK algorithms.

How deep to go in Phase 1

Volume 17 defines six mastery levels; Phase 1's exit targets are:

Topic Exit level What that means here
programming (C + one high-level language) L2–L3 write, test and debug programs; explain memory and calls
data structures and algorithms L3 implement core structures; reason about costs
discrete maths L2 use logic, sets, counting and probability in proofs and estimates
computer architecture L2–L3 explain caches, memory and instruction execution
operating systems L3 processes, threads, scheduling, virtual memory, files, system calls
networking L3 TCP/IP, DNS, HTTP, sockets, latency

L4 and beyond come later, when you debug real systems in Phases 6–9.

The sequence, step by step

Step Learn Read Exit check
1 variables, functions, memory, pointers, testing; C plus Python or Java Vol. 1, Start Here, ch. 2 write a 200-line program with tests, no tutorial
2 arrays, lists, hash maps, trees, heaps, graphs; complexity; sorting and searching Vol. 1, ch. 3–4 implement a hash map and a heap; predict their costs and measure them
3 logic, sets, functions, counting, probability, proof by induction Vol. 1, ch. 5 prove a loop invariant; estimate a collision probability
4 number representation, CPU, caches, memory hierarchy Vol. 1, ch. 6–7 explain why traversing a 2D array row by row beats column by column, and measure it
5 processes, threads, scheduling, virtual memory, file systems, system calls Vol. 1, ch. 8 write a shell with pipes and job control
6 TCP/IP, DNS, HTTP, TLS basics, sockets Vol. 1, ch. 9 write an HTTP server and a network diagnostic tool
7 compilers and runtimes (overview) Vol. 1, ch. 10 explain what happens between source code and a running process

Entry check

You're ready to start if you can write a small program in any language (loops, functions, reading a file) and use a terminal (navigate directories, run programs, read errors). If not, begin with Volume 1's Start Here chapter, which assumes nothing.

The phase projects

Volume 1's projects chapter (chapter 11) gives full specifications; here is what each must prove for this phase.

Project Proves Done when
C systems programs (e.g. wc, grep-lite, a file copier) memory, I/O, error handling correct on edge cases (empty files, huge files, binary data); no leaks under Valgrind or ASan
a shell processes, fork/exec, pipes, signals, job control runs pipelines like ls \| grep x \| wc -l, handles Ctrl-C correctly, no zombie processes
an HTTP server sockets, concurrency, parsing serves static files to many concurrent clients; survives malformed requests; load-tested with numbers
data structures library DSA, testing, benchmarking hash map, heap, balanced tree and graph algorithms with tests and measured costs
networking tools protocols a ping- or traceroute-like tool, or a DNS resolver, explained with packet captures

Common traps

  • Reading instead of building. Phase 1 knowledge sticks only when programs force it to.
  • Skipping C. Higher-level languages hide memory, system calls and costs; a few weeks of C pays off for years.
  • Treating big-O as the whole story. Measure: constant factors and memory layout often decide real performance.
  • Going too deep too early. L3 is the target; writing an operating system can wait.

Exit criteria

  • [ ] I've built every phase project, and each has tests and a README with measurements.
  • [ ] I can explain, without notes, what happens when I run curl https://example.com, from DNS to TCP to TLS to HTTP and back.
  • [ ] I can explain processes vs threads, virtual memory and what a system call is.
  • [ ] I can choose a data structure for a problem and justify it with costs.
  • [ ] I scored at least 80% on 40 mixed questions from Volume 1's quizzes, twice, a week apart.

Time budget

Volume 1 is about 156,000 words, roughly 11 hours of reading. With exercises, labs and review (a planning rule of about four hours of study per hour of reading), that's about 45 hours of study plus 4–6 weeks of part-time project work: about 3 months at 8 hours a week.

Lab: an HTTP server in C, and why it behaves as it does

Setup. gcc with -O2 on the lab machine (2 CPUs); a 40-line C server (socket, bind, listen, accept, recv, send; HTTP/1.1 keep-alive; TCP_NODELAY) in two modes: iterative (serve one connection until it closes, then accept the next) and fork (a child process per connection); Python's http.server (single-threaded and ThreadingHTTPServer) for comparison; wrk for load, 5 s per run.

Experiment 1: throughput and latency

Server Clients Avg latency Requests/s
C, iterative 1 41 µs 29,243
C, iterative 50 33 µs* 30,198
C, fork per connection 1 37 µs 29,292
C, fork per connection 50 280 µs 117,954
Python, threads 1 43.6 ms 22.7
Python, threads 50 86 ms 836
Python, single-threaded 50 43.6 ms 22.7

*The iterative server's numbers with 50 clients come from the one connection it was serving; the other 49 were waiting (Experiment 2).

Experiment 2: the iterative server and a second client

Text
conn A: HTTP/1.1 200 OK
conn B: no response after 3.0 s (TimeoutError) while A stays open
after A closes, conn B: HTTP/1.1 200 OK in 0.000 s

The iterative loop stays inside serve() reading from connection A; with keep-alive, A never closes, so the process never returns to accept(). Connection B was accepted by the kernel (it sat in the listen backlog) but nobody read it. This is the core problem every server design solves: processes (fork), threads, or an event loop (epoll) that watches many sockets at once.

What explains each number

  • Fork scaled because each connection got its own process, and with 2 CPUs two could run at once; per-request latency rose (280 µs) as 50 processes shared 2 CPUs.
  • Python's 43.6 ms per request is not Python's speed. BaseHTTPRequestHandler writes the headers and the body in separate send calls; with Nagle's algorithm on, the second small write waits for the client's ACK of the first, and the client's delayed ACK waits up to ~40 ms hoping to piggyback on data. The C server set TCP_NODELAY and sent one buffer. (Volume 12's lab hit the same stall; the fix is the same: one write, or TCP_NODELAY.)
  • Neither server parses HTTP properly (the C one assumes a request arrives in one recv). A real Phase 1 project must handle partial reads, pipelining and malformed input, which is where most of the work and learning is.

Try it yourself

  1. Add an epoll event-loop mode to the C server and compare it with fork at 50 and 500 clients.
  2. Remove TCP_NODELAY from the C server and send headers and body in two send calls; measure the latency change.
  3. Send a request split across two TCP segments (printf 'GET / HTTP/1.1\r\n'; sleep 1; printf '\r\n') and fix the parser.
  4. Capture both servers' traffic with tcpdump and find the 40 ms gap.

Revision sheet: Phase 1, CS foundations

Key ideas

  • Sequence: programming → DSA → discrete maths → architecture → operating systems → networking; each step uses the previous one.
  • Exit at L2–L3: build and explain, not yet debug production systems.
  • Projects prove the phase: C programs, a shell, an HTTP server, a data-structures library, networking tools.
  • Build before reading further; measure before believing big-O alone.

Numbers to remember

  • Lab: C fork-per-connection server ~118,000 req/s with 50 clients on 2 CPUs; iterative server starved the second client.
  • Lab: Python http.server 43.6 ms per request on keep-alive: Nagle + delayed ACK (~40 ms).
  • Budget: ~45 study hours + 4–6 project weeks ≈ 3 months at 8 h/week.

Pitfalls

  • Reading without building.
  • Skipping C and system calls.
  • Ignoring constant factors and memory layout.
  • Assuming one recv returns one whole request.

Glossary

  • System call: a request from a program to the operating system kernel.
  • File descriptor: the kernel handle for an open file, pipe or socket.
  • fork/exec: create a process / replace its program.
  • Nagle's algorithm: coalescing small TCP writes until earlier data is acknowledged.
  • Event loop: one thread watching many sockets and handling whichever is ready.