Foundations to Frontier
Tests
C. Programming, Tools and HPC › C4 Linux and Systems

Processes, Memory and I/O

When a training run is slow, killed or stuck, the answer is in how the operating system manages processes, memory and I/O. This topic covers processes and threads (fork/exec, states, signals, exit codes such as 137 from the out-of-memory killer), observing them (ps, top/htop, /proc), virtual memory (pages, page tables, the TLB, page faults, RSS versus virtual size, the page cache, swap, cgroup limits), and I/O (file descriptors, buffering, fsync, latency versus bandwidth versus IOPS, sequential versus random access). In the exercises you parse /proc/<pid>/stat and /proc/meminfo correctly, compute CPU use, translate x86-64 virtual addresses through a four-level page table and decode exit statuses; then you compare page-replacement policies (FIFO, LRU, CLOCK, optimal) on Belady's string, looping scans and Zipf workloads.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

A desk that is too small

Imagine working at a small desk with a huge archive in the basement. Only a few folders fit on the desk; when you need another, something must go back downstairs. A computer’s fast memory is that desk, and the disk is the basement. The operating system decides what stays on the desk, shares the computer among many running programs (processes), and moves data to and from disk. Good decisions keep everything fast; bad ones make the machine crawl.

Page replacement: who gets evicted?
policy:
ref123412512345
frame 0111444555555
frame 1·22211111333
frame 2··3332222244
FIFO with 3 frames: 9 page faults out of 12 references
faults1 fr2 fr3 fr4 fr5 fr
FIFO12129105
LRU12121085
CLOCK12129105
OPT129765
Orange counts in the summary rise when a frame is added (Belady's anomaly, possible for FIFO and CLOCK but never for LRU or OPT). OPT evicts the page used farthest in the future: unbeatable but needs to know the future, so it serves as a yardstick. Real kernels approximate LRU with reference bits (CLOCK and Linux's active/inactive lists).
Why engineers care

Flight computers and robot controllers run many processes with hard memory budgets; a training job on a cluster is killed the moment it exceeds its memory limit. Reading top, interpreting an exit code of 137 and knowing why a “free memory: 2%” report is usually harmless save hours of debugging.

📝 Quick check (Level 0)
0/3 answered
  1. 1. A process is…
  2. 2. When fast memory is full, the OS must…
  3. 3. In the demo with Belady's string, giving FIFO one more frame…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

Watching processes

ps aux --sort=-%mem | head           # biggest memory users
top / htop                           # live view: %CPU, RES (resident memory), state
pgrep -af python                     # find processes by name
kill 4242                            # polite stop (SIGTERM); kill -9 4242 = SIGKILL, cannot be caught
nice -n 10 python train.py &         # lower priority, run in the background
free -h; df -h; iostat -x 1          # memory, disk space, disk activity

Process states: R running or runnable, S sleeping (waiting for something), D uninterruptible I/O wait (often a slow file system), Z zombie (finished, parent hasn’t collected it), T stopped. A %CPU of 400 means four cores busy.

Exit codes

0 is success; 1 to 125 are the program’s own errors; 128 + n means “killed by signal n”: 130 = Ctrl-C (SIGINT), 137 = SIGKILL (very often the out-of-memory killer or a scheduler’s limit), 139 = SIGSEGV (invalid memory access), 143 = SIGTERM.

📝 Quick check (Level 1)
0/3 answered
  1. 1. A job exits with status 137. The first suspect is…
  2. 2. %CPU = 400 in top means…
  3. 3. Which signal can a program not catch?

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 35 minUndergraduate students and practising engineers.

Processes and threads

fork() duplicates a process (cheaply, with copy-on-write pages), exec() replaces its program; the shell runs every command this way. Threads share one address space; processes do not, so multiprocessing in Python copies or shares data explicitly (C5). Everything the kernel knows about a process is visible under /proc/<pid>/: stat (state, CPU ticks, memory), status (human-readable), fd/ (open files), maps (memory regions).

Virtual memory

Each process sees its own virtual address space. Memory is managed in pages (4 KiB; also 2 MiB huge pages). On x86-64 a 48-bit address is split 9 + 9 + 9 + 9 + 12 bits: four page-table levels and an offset; the TLB caches recent translations. Touching an unmapped page causes a page fault: minor if the data is already in RAM (first touch, shared library), major if it must be read from disk. RSS is what is resident in RAM; virtual size (VSZ) includes reserved-but-untouched memory, so a 30 GB VSZ is normal for a GPU process.

Linux uses otherwise idle RAM as page cache for files, so “free” memory is small on a healthy machine; MemAvailable estimates what programs can still get. When memory truly runs out, the system swaps (slow) and finally the OOM killer kills a process; under cgroups (Docker, Slurm) the limit is per job.

🐍 Exercise 1: reading the kernel's view
Parse /proc/<pid>/stat robustly (the command name may contain spaces and parentheses), compute CPU percent between two samples, parse /proc/meminfo and compare 'free' with 'available', split x86-64 virtual addresses into page-table indices and translate them through a nested page table (raising PageFault when unmapped), find all descendants of a process, and decode exit statuses above 128 into signals.
📝 Quick check (Level 2)
0/3 answered
  1. 1. The page offset of a 4 KiB page has how many bits?
  2. 2. A 'major' page fault means…
  3. 3. free shows 2% free but MemAvailable is 70%. The machine…

Level 3: Advanced

★★★★☆ Advanced⏱ 30 minGraduate level, with full derivations and proofs.

Page replacement

When RAM is full, the kernel evicts pages. The ideal (OPT, Belady) evicts the page used farthest in the future; real systems approximate LRU with reference bits (CLOCK, Linux’s active and inactive lists). Two classic facts: FIFO can suffer Belady’s anomaly (more memory, more faults), and LRU collapses on loops slightly larger than memory (every access misses). Large sequential scans can flush a hot working set; posix_fadvise(DONTNEED), direct I/O or Linux’s two-list design limit the damage.

I/O performance

Three numbers describe storage: latency (time per operation: about 0.1 ms for NVMe, 5–10 ms for a hard disk seek), IOPS (operations per second) and bandwidth (bytes per second). Small random reads are latency-bound; large sequential reads are bandwidth-bound. Writes go to the page cache first and reach the disk later; fsync forces durability (essential for checkpoints you rely on, and slow if called too often). Measure with iostat, fio, or simply time a read loop; tune with larger requests, fewer files (C4.1), and asynchronous prefetching (C3.6’s tf.data, PyTorch’s DataLoader workers).

📝 Quick check (Level 3)
0/3 answered
  1. 1. A loop over 100 pages with 99 frames under LRU gives a hit rate of…
  2. 2. Which policy can show Belady's anomaly?
  3. 3. Random 4 KiB reads on an HDD are slow because of…

Level 4: Expert

★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.

The kernel’s view, measured

Exercise 1. A stat line for a process named python train (v2).py parses correctly only when split at the last ). 780 user and 60 system clock ticks in 2 s of wall time (100 ticks per second) are 420% CPU. A machine with 64 GB, 2.5% “free” and 70% available is only 30% used. The virtual address 0x7f3a1c2b3d4e splits into indices 254, 232, 225, 179 and offset 0xd4e; mapping its page to frame 0x1234 translates it to 0x1234d4e, and the next page faults. Exit 137 decodes to SIGKILL.

Exercise 2: page-replacement policies.

Workload FIFO LRU CLOCK OPT
Belady’s string, faults with 3 / 4 frames 9 / 10 10 / 8 9 / 10 7 / 6
loop of 100 pages, 99 frames: hit rate 0% 0% 0% 98%
Zipf (1000 pages, s = 1), 10 frames: hit rate 0.182 0.210 0.196 0.432
Zipf, 50 frames 0.406 0.462 0.447 0.659
Zipf, 200 frames 0.644 0.694 0.683 0.836
hot set of 50 + 400-page scans, 100 frames 0.679 0.679 0.679 0.711

On skewed (Zipf) workloads CLOCK recovers most of LRU’s advantage over FIFO at a fraction of the bookkeeping, and OPT shows the remaining headroom. On the scan workload every realistic policy loses the hot set after each scan (50 extra misses per cycle); OPT keeps it.

🐍 Exercise 2 (research): page-replacement policies
Implement FIFO, LRU, CLOCK (second chance) and OPT (Belady's, with next-use precomputation and a heap). Reproduce Belady's anomaly, show LRU's collapse on a loop one page larger than memory, compare hit rates on a Zipf workload for three memory sizes, and measure the damage of sequential scans to a hot working set.

Processes, memory and I/O in AI, robotics and aerospace

  • Training jobs: exit 137 and dmesg OOM messages; memory limits in Slurm and Docker are cgroup limits.
  • Data loading: page cache warm-up between epochs, DataLoader worker processes (fork and copy-on-write), memory-mapped datasets (np.memmap, HDF5).
  • Flight and robot software: real-time Linux (PREEMPT_RT), locked memory (mlockall) to avoid page faults in control loops, process isolation between subsystems.
  • Telemetry archives: checkpoint durability with fsync; sequential append-only logs for throughput.

References

  1. R. H. Arpaci-Dusseau, A. C. Arpaci-Dusseau (2023). Operating Systems: Three Easy Pieces, v1.10 (ostep.org).
  2. L. A. Belady, R. A. Nelson, G. S. Shedler (1969). “An anomaly in space-time characteristics of certain programs running in a paging machine.” Communications of the ACM, 12(6).
  3. R. L. Mattson, J. Gecsei, D. R. Slutz, I. L. Traiger (1970). “Evaluation techniques for storage hierarchies.” IBM Systems Journal, 9(2).
  4. Linux kernel documentation: The /proc filesystem (proc(5)), Memory management (docs.kernel.org).
  5. B. Gregg (2020). Systems Performance, 2nd ed. Addison-Wesley.