The Operating Systems Picture Book
browser editor game server system calls Kernel processesscheduler memorymanager filesystems driversnet · devices Hardware: CPU · RAM · disk · NIC three jobs: share, protect, abstract
PLATE 0 — The OS lets many programs share one machine without trampling each other.
A picture book for grown-ups · with commands

Operating Systems, Under the Hood

Nine spreads on what the kernel does: processes, threads, scheduling, memory, file systems, I/O, and how programs ask it for things. Commands for Linux, with notes for Windows.

Say it like a pro

"The kernel schedules processes onto cores, gives each its own virtual address space, and mediates files and devices through system calls."

Use the tabs or the ← → keys.

User mode · restricted your code libcread() trap (syscall) Kernel mode · full power check permissions talk to disk copy to buffer the CPU enforces the split: user code can't touch hardware or other processes' memory directly
PLATE 1 — Programs ask. The kernel decides.
Chapter 1 · The kernel & system calls

The only door to the hardware

user vs kernel mode
CPU privilege levels; a crash in user mode kills one process, a kernel bug can take the machine down
system call
a controlled entry into the kernel: open, read, write, fork, execve, mmap, socket
monolithic vs microkernel
Linux keeps drivers and file systems inside the kernel; microkernels move them to user space for isolation
everything is a file
files, pipes, sockets and devices share the same read/write interface
$ strace -c ls /etc > /dev/null           # count the syscalls a command makes
$ strace -e openat,read cat /etc/hostname
$ uname -a ; cat /proc/version
$ ls /proc/$$/                           # live kernel view of this shell (a pseudo-filesystem)
$ lsmod | head                           # loaded kernel modules (drivers)
PS> Get-Process | Sort CPU -Desc | Select -First 5     # Windows view
// the smallest "program": write(1, "hi\n", 3) then exit
#include <unistd.h>
int main(void) { write(1, "hi\n", 3); return 0; }     // gcc hi.c -o hi && strace ./hi
Say it like a pro

"Every file, socket and process operation is a system call across the user-kernel boundary."

stack ↓ free heap ↑ data (globals) code (text) one process's address space new ready running waiting exit running → ready (preempted) · running → waiting (I/O) → ready fork() then exec()fork copies the process · exec replaces its program · the shell does this for every command
PLATE 2 — A process is a running program with its own memory, files and state.
Chapter 2 · Processes

A program in motion

PID / PPID
process ID and its parent's; systemd is PID 1
PCB
process control block: registers, state, page tables, open files kept by the kernel
context switch
save one process's registers, load another's; not free, so don't overdo it
zombie / orphan
finished but unreaped / parent died (adopted by init)
IPC
pipes, sockets, shared memory, signals, message queues
$ ps -eo pid,ppid,stat,%cpu,%mem,cmd --sort=-%cpu | head
$ pstree -p $$                           # the process tree under your shell
$ cat /proc/$$/status | grep -E 'State|VmRSS|Threads'
$ ls -l /proc/$$/fd                     # open file descriptors (0 stdin, 1 stdout, 2 stderr)
$ kill -STOP $PID ; kill -CONT $PID       # pause / resume
import os
pid = os.fork()
if pid == 0:                          # child
    os.execvp("echo", ["echo", "hello from the child"])
else:                                 # parent
    os.waitpid(pid, 0)                # reap it, so no zombie
    print("child finished")
Say it like a pro

"The shell forks, the child execs the program, and the parent waits. Each process has its own address space."

One process shared: heap · code · open files T1 stack T2 stack T3 stack Race condition counter = 5 T1 reads 5 · T2 reads 5 T1 writes 6 · T2 writes 6 expected 7. got 6. Fixes and their dangers mutex / lock: one thread at a time in the critical section semaphore, condition variable, atomic ops, message passing instead of sharing Deadlock needs all four mutual exclusion · hold-and-wait · no preemption · circular wait break one: lock in a fixed order, use timeouts, or avoid nested locks
PLATE 3 — Threads share memory. That makes them fast and makes bugs subtle.
Chapter 3 · Threads & synchronization

Shared memory needs rules

import threading
counter = 0
lock = threading.Lock()

def work():
    global counter
    for _ in range(100_000):
        with lock:              # remove the lock and the total will usually be wrong
            counter += 1

ts = [threading.Thread(target=work) for _ in range(4)]
[t.start() for t in ts]; [t.join() for t in ts]
print(counter)                  # 400000
ModelGood for
processesisolation, CPU-bound work in Python (bypasses the GIL)
threadsshared state, I/O-bound work
async / event loopthousands of idle connections on one thread
producer–consumer
a bounded queue with locks; the classic pattern
starvation / priority inversion
a thread never gets the lock or CPU / a low-priority holder blocks a high-priority waiter
$ ps -eLf | head ; top -H -p $PID        # threads of a process
$ nproc ; taskset -c 0,1 ./job             # pin to specific cores
Say it like a pro

"Protect the critical section with a lock, keep lock order consistent, and prefer message passing where I can."

Round robin · time slice 2 ms A B C A B C A time → (each box = one slice, a context switch between) What a scheduler optimizes (it can't have everything) throughput · turnaround time · response time · fairness · CPU utilization Common algorithms FCFS · SJF (shortest job first, optimal average wait, needs prediction) round robin (fair, interactive) · priority (with aging to avoid starvation) multilevel feedback queue: reward short, interactive jobs Linux today CFS / EEVDF: track "virtual runtime", always run the task that has had the least nice values (−20…19) bias that · real-time classes: SCHED_FIFO, SCHED_RR
PLATE 4 — Time-slicing makes one CPU feel like many.
Chapter 4 · CPU scheduling

Who runs next

# compare average waiting time for FCFS vs SJF, bursts in ms (all arrive at 0)
bursts = {"A": 8, "B": 2, "C": 4}

def avg_wait(order):
    t, waits = 0, []
    for p in order:
        waits.append(t); t += bursts[p]
    return sum(waits) / len(waits)

print("FCFS A,B,C :", avg_wait(["A","B","C"]))                       # 6.0
print("SJF  B,C,A :", avg_wait(sorted(bursts, key=bursts.get)))      # 2.67
$ nice -n 10 ./batch-job             # be polite: lower priority
$ renice -n 5 -p $PID
$ chrt -p $PID                        # scheduling policy of a process
$ uptime ; cat /proc/loadavg          # load average: runnable + waiting tasks (compare to core count)
$ vmstat 1 5 ; pidstat 1 3            # run queue (r), context switches (cs)
$ perf sched record -- sleep 5 && perf sched latency
preemption
the kernel can take the CPU away (timer interrupt)
I/O-bound vs CPU-bound
mostly waiting vs mostly computing; schedulers favor the former for responsiveness
cgroups
cap CPU shares for groups; how containers get limits
Say it like a pro

"Load average is above the core count, so tasks are queueing, and I'll check whether it's CPU or I/O wait."

virtual addrpage # | offset TLBfast cache page tablepage → frame physical RAM frame not present → page fault → OS loads from disk Why virtual memory? isolation: each process sees its own private address space flexibility: a program can be bigger than RAM; pages swap to disk sharing: libraries mapped once, shared; copy-on-write makes fork cheap When RAM runs out evict least-recently-used pages · thrashing = constant swapping, everything crawls Linux OOM killer ends a process to save the system
PLATE 5 — Every address a program uses is fake. The hardware and OS translate it.
Chapter 5 · Virtual memory

Pages, tables, and faults

page
fixed-size block, usually 4 KiB; the unit of mapping and swapping
page table
per-process map from virtual pages to physical frames (multi-level, to save space)
TLB
cache of translations; a miss costs several memory reads
minor / major fault
page is in RAM but unmapped / must be read from disk (slow)
mmap
map a file straight into the address space
OOM killer
chooses a victim by "badness" when memory is exhausted
$ getconf PAGESIZE
$ free -h ; cat /proc/meminfo | head -8
$ vmstat 1 5                            # si/so = swap in/out; non-zero for long = trouble
$ cat /proc/$$/maps | head                # this process's address-space map
$ /usr/bin/time -v ls 2>&1 | grep -E 'faults|Maximum resident'
$ sudo dmesg | grep -i 'out of memory'    # OOM killer events
$ swapon --show ; sysctl vm.swappiness
ReplacementIdea
FIFOevict the oldest (can behave oddly: Bélády's anomaly)
LRUevict the least recently used (good, approximated in practice)
Clockcheap LRU approximation with a reference bit
Say it like a pro

"The process was OOM-killed. RSS climbed with no swap, so it's a leak or a limit that's too tight."

directory"notes.txt" → 1204 inode 1204owner · mode · sizetimes · block ptrs data blocks Ideas that explain surprising behaviour the file's name lives in the directory, not the inode: rename and hard links are cheap deleting = removing a name; data goes when the link count and open handles hit 0 "disk full" can mean out of blocks or out of inodes Crash safety journaling (ext4, NTFS): write the intent to a log first, then the data copy-on-write (btrfs, ZFS, APFS): never overwrite, so snapshots are cheap fsync() is what actually forces data to stable storage
PLATE 6 — Names point to inodes. Inodes point to data.
Chapter 6 · File systems

Names, inodes, and blocks

$ ls -li notes.txt                    # -i shows the inode number
$ stat notes.txt                      # inode, size, blocks, links, times
$ ln notes.txt hard.txt ; ln -s notes.txt soft.txt
$ ls -li notes.txt hard.txt soft.txt    # hard: same inode · soft: its own inode, holds a path
$ df -h ; df -i                       # space used vs inodes used
$ lsblk -f ; findmnt                  # filesystems and where they're mounted
$ du -xh --max-depth=1 / 2>/dev/null | sort -h | tail
$ sudo debugfs -R "stat <1204>" /dev/sda1     # peek inside ext4 (read-only is safest)
$ lsof +L1                            # deleted files still held open (space not freed)
$ mount | grep -E 'ext4|xfs|btrfs|tmpfs'
File systemKnown for
ext4Linux default, journaling, mature
XFSbig files, parallel I/O
btrfs / ZFSsnapshots, checksums, copy-on-write
NTFS / APFSWindows / macOS defaults
tmpfs / procfsRAM-backed / kernel info as files
Say it like a pro

"It's out of inodes, not space, so I'll find the directory with millions of tiny files."

CPU driverin kernel device 1 request 2 interrupt when done DMA: device ↔ RAM directly Three ways to wait for I/O polling: keep asking (wastes CPU) · interrupts: be told · DMA: don't even move the bytes Page cache the kernel caches file data in spare RAM; "free" memory is mostly this cache
PLATE 7 — Devices are slow. The OS overlaps waiting with other work.
Chapter 7 · I/O, interrupts & drivers

Talking to slow hardware

blocking vs non-blocking
sleep until ready / return immediately with "not yet"
epoll / kqueue / io_uring
watch thousands of file descriptors efficiently; how web servers handle many connections
block vs char device
/dev/sda (random access blocks) vs /dev/tty (stream)
I/O wait
CPU idle because processes wait on disk or network
$ cat /proc/interrupts | head ; cat /proc/devices | head
$ iostat -xz 1 3                       # %util, await per disk (sysstat package)
$ iotop -oPa                          # which process is doing I/O
$ dd if=/dev/zero of=/tmp/t bs=1M count=512 oflag=direct status=progress
$ sync ; echo 3 | sudo tee /proc/sys/vm/drop_caches    # for benchmarking only
$ udevadm info /dev/sda ; dmesg | tail
$ echo hello | tee /dev/null ; cat /dev/urandom | head -c 16 | xxd
import select, socket
srv = socket.socket(); srv.bind(("127.0.0.1", 9000)); srv.listen(); srv.setblocking(False)
ep = select.epoll(); ep.register(srv.fileno(), select.EPOLLIN)
# loop: ep.poll() → returns only the sockets that are ready; one thread, many clients
Say it like a pro

"High iowait with a saturated disk: I'll find the heavy reader with iotop and check the page cache hit rate."

users, groups, file permissions (DAC) capabilities: split root into small powers namespaces + cgroups = containers seccomp / SELinux / AppArmor (MAC) hypervisor: virtual machines stronger isolation, more overhead → a container shares the host kernel; a VM brings its own
PLATE 8 — Layer the walls. No single one is enough.
Chapter 8 · Protection & isolation

Keeping programs in their lane

ASLR / NX
randomize addresses / mark data non-executable; they make memory-corruption bugs harder to exploit
setuid
runs with the file owner's rights; powerful and risky (passwd, sudo)
capabilities
e.g. CAP_NET_BIND_SERVICE to bind port 80 without being root
namespaces
separate views of PIDs, network, mounts, users; cgroups cap CPU/memory
MAC
SELinux/AppArmor policies that even root must obey
$ id ; groups ; ls -l /usr/bin/passwd           # note the 's' (setuid) bit
$ find / -perm -4000 -type f 2>/dev/null        # audit setuid programs
$ getcap -r / 2>/dev/null ; cat /proc/sys/kernel/randomize_va_space
$ lsns ; ls -l /proc/$$/ns                    # namespaces in use
$ sudo unshare --pid --fork --mount-proc bash  # a shell in a new PID namespace: ps shows PID 1!
$ systemd-cgls ; cat /sys/fs/cgroup/memory.max
$ getenforce ; aa-status                      # SELinux / AppArmor state
$ sudo setcap cap_net_bind_service=+ep ./myserver
Say it like a pro

"Run the service as a non-root user with just the needed capability, in its own namespaces with cgroup limits."

UEFI GRUB kernel initramfs systemd login "It's slow." Check in this order (USE method) CPU utilization, saturation (run queue), errors → top, uptime, mpstat Memory free, swapping, OOM kills → free, vmstat, dmesg Disk %util, await, queue length → iostat -x, iotop Network throughput, drops, retransmits → ss -s, sar -n DEV Then go deeper strace (what syscalls?) · perf top (which function?) · lsof (which files?) journalctl -b -p err (what did the system complain about?) change one thing at a time · record the before and after
PLATE 9 — Know how it starts, and check each resource in turn when it misbehaves.
Chapter 9 · Booting & diagnosing

From power button to prompt, and from "slow" to "why"

  1. Firmware (UEFI) tests hardware and finds a bootable disk.
  2. Bootloader (GRUB) loads the kernel and an initial RAM disk.
  3. Kernel detects hardware, mounts the root filesystem, starts PID 1.
  4. systemd starts services in dependency order, then the login prompt.
$ systemd-analyze ; systemd-analyze blame | head    # what slowed the boot
$ journalctl -b -p warning                        # this boot's warnings and errors
$ dmesg -T | head -50 ; cat /proc/cmdline         # kernel messages and boot parameters
$ top -bn1 | head -15 ; mpstat -P ALL 1 3 ; sar -u 1 3
$ sudo perf top ; sudo strace -fp $PID -c         # deeper, when you know the suspect
$ sudo lsof -p $PID | head ; ss -tulpn
PS> Get-Counter '\Processor(_Total)\% Processor Time','\Memory\Available MBytes' -SampleInterval 2 -MaxSamples 3
PS> msinfo32 ; resmon ; perfmon                    # Windows tools
practise
read the kernel docs, build a tiny shell (fork/exec/pipe) in C or Python, then try xv6 or OSTEP's projects
read next
"Operating Systems: Three Easy Pieces" (free online) · "The Linux Programming Interface"
Say it like a pro

"I went resource by resource (CPU, memory, disk, network), found disk saturation, and traced the process doing it."