Think Like an OS§

Authors: Shrey Patel and Jay Patel, Coconut Labs
Status: In progress. Structure locked; chapters land in batches. Every factual claim web-verified at authoring time; sources cited inline.
What this is: A hands-on book that teaches the fundamental building blocks of operating systems, from the hardware contract through kernel internals to shipping a distro, through the lens of what Coconut OS is actually building. One place for the concepts, the skills, and the prerequisites.


§1 Who this book is for§

Every abstraction you use daily is a fiction the kernel maintains for you: a file, a socket, a thread, a stretch of memory that looks like it belongs to you alone. The fictions are good ones. They are not free. This book takes them apart in runnable pieces and puts them back together.

Two destinations bring people here, and the book is built for both.

You are heading toward kernel or systems work. You want to open mm/memory.c and read it instead of bouncing off it, send a patch a maintainer accepts, and know a subsystem well enough to change it. Your spine is Ch 6 to 9 (boot, syscalls, processes, scheduling), then Part III for memory, then Part V, where Coconut OS's own kernel work lives. Part IX is the workflow you will be in every day.

You are heading toward low-latency work: trading systems, market data, anything judged on its worst response rather than its average. Your question is not what the machine does. It is when. Every stall you will chase is one of the mechanisms in here, and each one has a chapter that takes it apart:

None of those six are exotic. They are ordinary machinery that most working engineers were never shown, which is why timing reads as luck until you have read them. This is not a trading book: no strategy, no microstructure, nothing about markets. It is the layer underneath, taught until you can name the mechanism behind a number instead of guessing at it. Some of the chapters named above are written and some are still outlines; §3 marks which.

Where to start: Chapter 0, and do its lab rather than reading it. It ends with a Linux 6.12 kernel you compiled yourself, booting your own PID 1 in QEMU on your own machine. If you have never built a kernel, build that one before you read further. Every chapter after it lands differently.

Those two destinations are about where you are going. The three tracks below are about what you are bringing. Every chapter unfolds complex machinery into longer-but-simpler prose, and no prior kernel or CPU knowledge is assumed by the text itself. The tracks differ only in pace and which bridge sections you skip.

TrackReaderPath
A. Bridge (the curated track, §4 below)BS+MS CS; has taken compiler design + microprocessor/microcontroller coursework; no Linux-kernel internals yetFast bridge notes through Part I, then linear. Compiler background pays off directly in Ch 7, 24, 30.
B. Full pathAnyone in the CS industry (web dev, data, SRE, mobile) with no CPU or kernel exposureChapter 0 first, then all of Part F (the prerequisite layer), then linear. Read every "Unfolded" section; do every lab.
C. ReferenceEngineers already working on Coconut OS subsystemsJump per-chapter via §3; each chapter is self-contained with a source map.

§2 How every chapter works§

Fixed template, so you always know where you are:

  1. The problem. What breaks without this mechanism. Every OS structure is a solution; we start from the pain.
  2. Unfolded. The core idea in longer-but-simpler prose. No jargon before it is defined. This is the section that makes the book readable by someone who has never seen a page table.
  3. The real thing in Linux. Actual file paths, structs, and code paths in the 6.12 tree Coconut OS forks.
  4. Coconut tie-in. What Coconut OS changes, replaces, or adds here, cross-referenced to the spec (04-HLD, 05-LLD section numbers).
  5. Lab. Runnable, QEMU-based where possible, on a macOS or Linux host. Red-then-green: you predict, you run, you compare.
  6. Bridge notes. The "if you know compilers/microcontrollers, here is the mapping" callouts for Track A.
  7. Depth gauge. Each section is marked [fundamental], [working], or [advanced], so you can bail out of a deep dive without losing the thread. An [advanced] section is one you have permission to skip on the first pass.
  8. Sources. The URLs actually consulted and verified at authoring time.

§3 Full structure§

Part 0   Orientation & the lab bench
Part F   Core fundamentals            (the prerequisite layer: bits → programs → memory → C → assembly → data structures → concurrency)
Part I   The machine beneath          (hardware contract)
Part II  The kernel's core loop       (boot, syscalls, processes, scheduling)
Part III Memory                       (virtual, physical, page cache)
Part IV  Persistence                  (VFS, filesystems, block, io_uring)
Part V   Identity & capability        (creds, namespaces, LSM, caps, audit)  ← Coconut heart
Part VI  Communication                (IPC, networking)
Part VII The userspace contract       (ELF, libc, init, packaging)
Part VIII The interaction surface     (DRM/KMS, Wayland, compositors)
Part IX  Building, testing, shipping  (kbuild, KUnit, QEMU gates, fork maintenance)
Part X   The frontier                 (Rust-for-Linux, eBPF, KVM, agents-as-primitives)

Part 0: Orientation & the lab bench§

ChTitleOne-linerStatus
0Think Like an OSWhat an OS actually is (multiplexer, abstraction machine, protection referee); the Coconut OS north star; lab bench setup: QEMU, Docker cross-build, kernel tree navigationdone, fact-checked

Part F: Core fundamentals (the prerequisite layer)§

Everything the rest of the book assumes, in one place. Track B reads all of it after Chapter 0. Track A treats it as an optional refresher, with F.5 to F.7 recommended: intrusive kernel data structures and concurrency vocabulary are new even to most MS grads.

ChTitleOne-linerStatus
F.0The Unix survival kitShell fluency, filesystem hierarchy, permissions, processes from the user side, ssh, building software from source. The lab prerequisitedone, fact-checked
F.1How a computer computesBits, binary/hex, two's complement, gates to ALU, clocks, the von Neumann fetch-decode-execute loopdone, fact-checked
F.2Anatomy of a programSource → compiler → assembler → linker → executable; registers, PC/SP, stack frames, calling conventions at machine leveldone, fact-checked
F.3Memory from first principlesThe address space as a byte array; endianness; stack vs heap; pointers as addresses; layout and alignmentdone, fact-checked
F.4The C you needThe kernel-dialect working subset: pointers, structs, bit operations, function pointers, macros, goto error handling, container_of, undefined behaviordone, fact-checked
F.5Reading assembly without fearx86_64 + ARM64 reading fluency: prologues, calls, loops, syscall sites. Compiler Explorer as the labdone, fact-checked
F.6Data structures kernels loveIntrusive linked lists, hash tables, red-black trees, xarray, bitmaps, ring buffers, per-CPU data, and why kernels prefer intrusive formsdone, fact-checked
F.7Concurrency, the mental modelThreads, data races, atomicity, critical sections, locks. The userspace mental model Part II's kernel concurrency builds ondone, fact-checked

Part I: The machine beneath§

ChTitleOne-linerStatus
1The CPU's contractPrivilege: x86_64 rings + long mode, ARM64 exception levels; the instructions only kernels may run; what "kernel mode" physically meansdone, fact-checked
2Interrupts & exceptionsThe event-driven heart: vectors, IDT/GIC, faults vs traps vs aborts, nested interrupts, softirqs previewdone, fact-checked
3Memory hardwareMMU, 4/5-level page tables, TLB, caches and coherence. Why virtual memory exists and what it costsdone, fact-checked
4TimeTimers, clocksources, TSC/arch timers, APIC/GIC timers, tick vs ticklessplanned
5I/O at the wirePCIe, MMIO vs port I/O, DMA, IOMMU, device discoveryplanned

Part II: The kernel's core loop§

ChTitleOne-linerStatus
6BootPower-on → UEFI → shim/bootloader → kernel decompression → init → PID 1; Secure Boot trust chaindone, fact-checked
7The syscall boundaryThe user/kernel crossing instruction by instruction; ABI; vDSO; strace; how a new syscall is added (Coconut's 472 to 479 family)done, fact-checked
8Processes & threadstask_struct, fork/clone/exec, process lifecycle, credentials intro, zombies and reapingdone, fact-checked
9SchedulingTimer tick to context switch; CFS → EEVDF; RT classes; cgroup weighting; where SCHED_AGENT slots indone, fact-checked
10Kernel concurrencySpinlocks, mutexes, RCU, atomics, memory ordering, lockdep: the reasons kernel code is hardplanned

Part III: Memory§

ChTitleOne-linerStatus
11Virtual memorymm_struct, VMAs, mmap, page faults, demand paging, COWplanned
12Physical memoryBuddy allocator, slab/slub, vmalloc, reclaim, OOM killerplanned
13Page cache & tiersThe unified page cache; readahead; writeback; NUMA + tiered memory (Coconut mm tier-aware hooks)planned

Part IV: Persistence§

ChTitleOne-linerStatus
14VFSEverything is a file: dentries, inodes, mounts, the syscall-to-driver pathplanned
15Real filesystemsext4 journaling vs btrfs COW; overlayfs; what agentfs needsplanned
16Block & io_uringbio layer, schedulers, io_uring's shared-ring modelplanned

Part V: Identity & capability (the Coconut heart)§

ChTitleOne-linerStatus
17Credentials & DACuids, struct cred, POSIX capabilities, setuid, and why "root" is a design failure we inheritedplanned
18Namespaces & cgroupsContainers deconstructed into their eight namespaces + resource controllersplanned
19LSM & seccompThe hook architecture under SELinux/AppArmor; seccomp filters; where security/coconut/ attachesplanned
20Capability-based securityObject capabilities, seL4, Capsicum, Genode: the theory Coconut's cap tokens implement. v1 shim vs v2 replacementplanned
21AuditKernel audit today; append-only logs, hash chains, tamper evidence; kernel/audit/coconut designplanned

Part VI: Communication§

ChTitleOne-linerStatus
22IPCPipes, unix sockets, shared memory, futex, signals, D-Bus, binder: the menu, and when each one winsplanned
23Networkingsk_buff to socket API; netfilter; netlink (how userspace configures the kernel)planned

Part VII: The userspace contract§

ChTitleOne-linerStatus
24ELF, linking, loadingFrom compiler output to running process. Your compiler coursework meets execveplanned
25libc & the runtimeglibc/musl, syscall wrappers, TLS, the vDSO fast path, why static vs dynamic mattersplanned
26init & service managementPID 1 duties; systemd vs s6 vs dinit; the coconutd design (RC-3: s6-derived greenfield in Rust)planned
27Packaging & atomic updatesrpm, ostree, rpm-ostree; A/B updates + rollback; reproducible builds; coconutpkgplanned

Part VIII: The interaction surface§

ChTitleOne-linerStatus
28Pixels to glassDRM/KMS, GEM buffers, where each GPU driver actually stands, NVIDIA blob + DKMS realityplanned
29Wayland & compositorsThe protocol, Smithay's architecture, XWayland compat, agent-aware window managementplanned

Part IX: Building, testing, shipping§

ChTitleOne-linerStatus
30kbuild & cross-compilationkconfig, defconfigs, cross toolchains, building on a macOS host via Dockerplanned
31Kernel testingKUnit, kselftest, LTP; QEMU boot gates; designing CI that cannot silently pass (the 14-day silent-gap lesson)planned
32Debugging & observabilityprintk, ftrace, perf, eBPF tracing, kgdb, crash/kdump; KASAN/KCSAN/lockdep as truth machinesplanned
33Maintaining a forkPatch hygiene, review flow, LKML etiquette, the every-2-years upstream merge eventplanned

Part X: The frontier§

ChTitleOne-linerStatus
34Rust in the kernelRust-for-Linux status, unsafe boundaries, pin-init, why Coconut writes new subsystems in Rust where it canplanned
35eBPFThe in-kernel VM: verifier, maps, program types. The other "safe kernel extension" storyplanned
36VirtualizationKVM internals, virtio, how QEMU actually runs your lab kernelsplanned
37Agents as kernel primitivesThe Coconut thesis end-to-end: agent_* syscalls, SCHED_AGENT, cap enforcement, audit-everything. What "think like an OS" means for the agentic eraplanned

Appendices§

AppTitleStatus
Ax86_64 ↔ ARM64 cheat sheetplanned
BKernel source-tree map (where everything lives)planned
CLab solutionsplanned
DGlossaryplanned
EAnnotated reading list (books, papers, LWN, lectures)planned

§4 Track A: the curated course (your track)§

What you already have and what it buys you:

Your courseworkDirect payoffWhere
Microprocessor/microcontrollerYou know registers, interrupts, memory-mapped I/O on small cores. Part I is a bridge, not an intro: the delta is protection (rings/ELs), virtual memory hardware, and multi-core coherence, none of which an 8051/AVR/Cortex-M ever showed youCh 1 to 5, fast
Compiler designYou know codegen, linking concepts, calling conventions. This makes Ch 7 (ABI), Ch 24 (ELF/loading), Ch 30 (toolchains) fast lanesCh 7, 24, 30
MS CS courseworkOS-course theory (you likely saw scheduling/VM at whiteboard level). The book's job is to replace whiteboard understanding with source-level + lab-verified understandingeverywhere

Recommended order and pacing (assumes roughly 5 to 8 hours a week; a "session" is one sitting with the lab done):

PhaseChaptersSessionsGoal
0. Refresh (optional)F.5, F.6, F.72Assembly reading fluency, intrusive kernel data structures, concurrency vocabulary. Skim the rest of Part F only if rusty
1. Re-ground0, 1, 2, 34Bridge micro-scale hardware knowledge to server-class x86_64/ARM64 with protection + paging
2. The spine6, 7, 8, 95 to 6Boot a kernel you built; trace a syscall end-to-end; this is the minimum "I think like an OS" bar
3. Memory truth10, 11, 12, 135Concurrency and VM: the two hardest ideas. Do not rush 10
4. The Coconut core17, 18, 19, 20, 216Everything in Part V. This is the subject matter of our kernel work
5. Round out14, 15, 16, 22, 23, 24, 25, 26, 278Persistence, IPC, userspace contract
6. Ship it30, 31, 32, 334The dev workflow you will actually live in
7. Frontier28, 29, 34, 35, 36, 376Graphics, Rust, eBPF, KVM, and the thesis chapter last

Skip rules for Track A: skip nothing in Part V or Part IX; skim "Unfolded" sections where the depth gauge says [fundamental] and you already hold the idea. The labs are the non-skippable part.

§5 Fact discipline§

§6 File layout§

book/
├── 00-INDEX.md                    this file
├── 01-SYLLABUS.md                 section-level outline of every chapter, written and planned
├── part-0/ch00-think-like-an-os.md
├── part-f/chF0-unix-survival.md … chF7-concurrency-mental-model.md
├── part-1/ch01-cpu-contract.md
├── part-1/ch02-interrupts-exceptions.md
├── part-1/ch03-memory-hardware.md
├── part-2/ch06-boot.md
├── part-2/ch07-syscall-boundary.md
├── part-2/ch08-processes-threads.md
├── part-2/ch09-scheduling.md
└── ...                            batches land here as authored

The directory doubles as a docsify site. Serve it with any static file server and read it with the sidebar and search instead of scrolling raw Markdown:

bash
python3 -m http.server 8000 --directory book/
# then open http://localhost:8000/

Verified on this tree at authoring time; the docsify assets load from a CDN, so that view wants a network connection. Reading the .md files directly needs nothing.