Part 10 · 1 chapters · ~8 min
Capstone: A Shell, an Allocator, an HTTP Server
Three small programs that exercise the whole course: a shell with pipes, redirection and signals; a malloc replacement with size classes; and a single-threaded HTTP/1.1 server with keep-alive, all with acceptance scripts and sanitiser-clean builds.
11
Three programs, one standard
code
// shell: connect two stages with a pipe
int p[2]; pipe(p);
if (fork() == 0) { dup2(p[1], 1); close(p[0]); close(p[1]); execvp(left[0], left); _exit(127); }
if (fork() == 0) { dup2(p[0], 0); close(p[0]); close(p[1]); execvp(right[0], right); _exit(127); }
close(p[0]); close(p[1]); while (wait(NULL) > 0) {} // close unused ends or the reader never sees EOF
# acceptance
./mysh -c 'printf "a\nb\nc\n" | grep b' → b
./mysh -c 'ls /nope 2>&1 | wc -l' → 1
LD_PRELOAD=./libmymalloc.so python3 -c 'print(sum(range(10**6)))' → 499999500000
wrk -t2 -c100 -d10s http://localhost:8080/index.html → record requests/sec vs a Node static serverCAPSTONE: THREE PROGRAMS
each exercises a different half of the course
swipe the figure sideways, or tap expand for full screen
1/4
the shell
Parse a command line with pipes and redirections, fork and exec each stage, connect them with pipe and dup2, wait for the pipeline, and handle Ctrl-C with signals.
processes and descriptorsls | grep c > out.txt