Operating Systems Internals: Kernels, Virtual Memory, Concurrency, and I/O
An operating system (OS) kernel is the privileged supervisor software running in hardware ring 0 / EL1 that arbitrates raw physical CPUs, DRAM pages, NVMe flash drives, network interfaces, and accelerator PCIe bars among competing user-space processes. Mastering virtual memory paging, memory-mapped files (`mmap`), asynchronous I/O (`epoll` and `io_uring`), concurrency primitives, and Linux namespace/cgroup isolation explains everything from container orchestration to how 70 GB AI model weights load in milliseconds.
Key Highlights
- Hardware privilege rings isolate untrusted user-space processes from kernel memory; processes request privileged I/O or allocation via controlled system calls (`syscall`).
- Virtual memory + CPU Memory Management Units (MMUs) and Translation Lookaside Buffers (TLBs) give every process an isolated 64-bit address space backed by 4 KB or 2 MB/1 GB HugePages.
- Memory-mapping (`mmap`) safetensors or database files maps disk blocks directly into virtual page tables on demand via the OS page cache—eliminating duplicate user-space buffer copies.
- Linux Containers (Docker/Kubernetes Pods) are NOT lightweight virtual machines; they are ordinary host processes isolated by Kernel Namespaces (visibility) and Cgroups v2 (CPU/RAM/PID quotas).
Virtual Memory, HugePages, Page Faults, and `mmap` Model Loading
Every memory pointer dereferenced by a Python, Rust, or C++ program is a virtual address translated by the CPU’s MMU into a physical DRAM frame. Because walking a four-level radix page table in RAM on every instruction would cripple performance, hardware caches hot translations inside the Translation Lookaside Buffer (TLB). When databases or LLM inference servers allocate 64 GB of RAM across 16 million tiny 4 KB pages, TLB misses surge; enabling 2 MB Transparent or Explicit HugePages slashes page-table entries by 512×.
Understanding minor versus major page faults also demystifies fast AI model startup: calling `mmap()` on a 30 GB `.safetensors` file returns almost instantaneously by establishing virtual page-table mappings, streaming NVMe sectors directly into OS page-cache frames without double-copying through user-space `read()` buffers.
| OS Mechanism | What It Controls | Performance / Reliability Tip |
|---|---|---|
| Processes vs Threads | Isolated page tables vs shared address space | Threads share heap cheaply (~microsecond switch) but require race-free synchronization |
| 2 MB / 1 GB HugePages | MMU TLB translation granularity | Cuts TLB misses 5%–15% on PostgreSQL, Redis, JVM & high-RAM AI servers |
| epoll / kqueue / io_uring | Non-blocking event-driven network & disk I/O | Handles 100k+ concurrent sockets on a handful of event-loop threads without thread bloat |
| Cgroups v2 & OOM Killer | Hard container memory & CPU throttling | Set container memory requests/limits with headroom for OS page cache & CUDA context |
Concurrency Synchronization, Context Switches, and eBPF Observability
Spawning 20,000 OS kernel threads wastes gigabytes of stack memory and thrashes CPU caches during preemptive context switches; modern runtimes (Tokio in Rust, Go goroutines, Node/Python asyncio) multiplex tens of thousands of lightweight tasks onto `N = CPU_cores` OS worker threads. Meanwhile, Linux eBPF allows engineers to attach sandboxed, kernel-verified bytecode probes to network sockets, schedulers, and syscalls for sub-percent-overhead production profiling and security monitoring without risky kernel modules.
- Eliminate deadlocks deterministically by enforcing a strict global lock-acquisition ordering or preferring message-passing channels and immutable snapshots.
- Never block an async event-loop thread with synchronous CPU hashing or blocking file I/O—offload CPU work to dedicated worker pools.
- Use Firecracker microVMs or gVisor (rather than plain shared-kernel containers) whenever executing untrusted multi-tenant AI-generated code.
FAQs
Why did the Linux OOM Killer terminate my AI container even though Python weights were smaller than the RAM limit?
Cgroups v2 tracks total resident memory—including CUDA driver contexts (~1–2 GB), PyTorch allocator fragmentation, forked dataloader copy-on-write pages, and dirty page-cache buffers—not just model tensor sizes.
What is the difference between a Mutex and a Spinlock?
A mutex puts a waiting thread to sleep via the OS scheduler (freeing the CPU core for other work when waits exceed a few microseconds), whereas a spinlock busy-polls in a tight atomic loop—useful only inside ultra-short sub-microsecond kernel critical sections.
References & Sources
- Arpaci-Dusseau, Remzi H.; Arpaci-Dusseau, Andrea C. — Operating Systems: Three Easy Pieces (OSTEP), Arpaci-Dusseau Books.
- Gregg, Brendan — Systems Performance: Enterprise and the Cloud (2nd ed.), Addison-Wesley.
- Kerrisk, Michael — The Linux Programming Interface, No Starch Press.