Programs cannot touch hardware; they ask through one door
The kernel is the only code the processor lets near the hardware. Everything else asks it, through a system call.
Last lesson built one machine. Now hundreds of programs want it at once. Each program runs in user mode: it may calculate, but it may not touch hardware. If a program tries to write to the disk directly, the processor itself refuses. This is enforced in silicon, not by good manners. There is exactly one way through: a system call. It switches the processor into kernel mode and jumps to a fixed entry point. The kernel is the only code allowed to touch the hardware, so it can keep programs apart and check every request. Each crossing costs thirty to a hundred and fifty nanoseconds.
System calls · Going deeper · 1 of 3
The stack the kernel maintains
Process, address space, file, socket, container: every abstraction here keeps one illusion alive, that each program has the machine to itself. On x86 user code runs in ring 3 and the kernel in ring 0; on Arm, EL0 and EL1.
System calls · Going deeper · 2 of 3
What a system call does
One instruction saves the program counter, raises the privilege level and jumps to the kernel’s entry point. The kernel looks the number up in a table, validates every pointer, runs the handler and returns. After the Spectre and Meltdown fixes, call-heavy workloads lost 5 to 30%. The cure for call overhead is batching: one buffered write instead of 4,096 single-byte ones.
System calls · Going deeper · 3 of 3
With and without an operating system
With one program, bare hardware is fine; embedded systems still work that way. With two, whichever wrote last wins. Isolation is not perfect: caches and branch predictors are physically shared, which Spectre, Meltdown and Rowhammer exploit.
Processes and threads
A process is a set of walls; threads are the workers inside
A process owns private memory, so a crash stays local. A thread is one line of execution inside it, sharing that memory.
The kernel uses two different units. A process is a running program with its own private memory and its own open files. Inside it, threads do the work. They share the memory, and each has only its own stack. Between processes there is a wall. Neither can see the other’s memory. So when a thread crashes, it takes down its own process, and nothing else. The price is in the bars. A thread takes ten to thirty microseconds to create. A process takes ten times longer. Threads are cheap and share everything, which also means they can corrupt each other’s data. Processes are slower and safer.
Processes and threads · Going deeper · 1 of 2
Isolation unit and concurrency unit
Processes talk only when both sides opt in: pipes, sockets, shared memory. Each thread stack is 1 to 8 MB on Linux. fork takes 100 to 500 µs because the kernel must clone page tables.
Processes and threads · Going deeper · 2 of 2
The five states of a thread
Created, ready, running, blocked, terminated. Most transitions are involuntary: the timer preempts, a blocking call parks, an I/O completion wakes. Signals interrupt a thread asynchronously, and only a short list of functions is safe inside a signal handler.
Scheduling
One core runs many threads by switching every few milliseconds
The scheduler hands out the core in short slices. Running “at once” is an illusion made of fast turns.
There are nearly always more threads than cores. So the kernel cuts the core’s time into slices, here four milliseconds each, and deals them out. From each thread’s point of view it just runs, with gaps too short to notice. Slices last one to ten milliseconds. When thread B asks for the disk, it is blocked. A blocked thread uses no core at all, so A and C share the time until B’s data arrives. Who goes next? The thread that has had the least core time so far. A switch itself costs a few microseconds. The hidden cost is larger: the new thread starts with a cold cache.
Scheduling · Going deeper
The Linux scheduler, precisely
CFS keeps a red-black tree per core, ordered by virtual runtime: CPU time weighted by priority. It always runs the leftmost thread. Insert and pick are O(log n), about a microsecond with thousands of threads. Moving a thread to another core costs 10 to 100 µs of cache warm-up, so the balancer prefers the same socket.
Check yourself
Thread B calls read() and its data is on a slow disk. What does thread B do to the core while it waits?
A blocked thread is taken off the run queue. It uses no core time until its data is ready.
Virtual memory
Every process gets its own private map of memory
Virtual memory: a program’s addresses are not real locations. A per-process table translates each one, which is what keeps processes apart.
How does the wall between processes actually work? Process A and process B both use address four thousand hex. But each process has its own translation table, and the two tables send that address to different frames of real memory. Same address, different memory. The same goes for their data. A has no address that leads to B’s frames, so it cannot even name them. And where sharing is safe, it is free. Both run the same program code, so both tables point at one copy. The processor does this translation on every single memory access.
Virtual memory · Going deeper · 1 of 2
The address space, to scale
Pages are 4 KB, with optional 2 MB and 1 GB huge pages. The 48-bit space is 256 TB and almost entirely unmapped: code at the bottom, heap growing up, stack growing down.
Virtual memory · Going deeper · 2 of 2
Walking the table
The table is a four-level tree with 9 address bits per level and a 12-bit offset. A walk is four dependent memory reads, 50 to 500 cycles. The TLB caches recent translations, but its 64 to 512 entries cover at most 2 MB; huge pages are the standard fix for large working sets.
Demand paging
Memory is a promise, kept only when a program touches it
A page fault is the kernel filling in memory at the last moment. From memory it takes microseconds; from disk, a thousand times longer.
The kernel hands out addresses freely, and real memory late. A program reads an address it was promised. There is nothing behind it yet, so the processor stops with a page fault. The kernel finds a frame of real memory and maps it in. The instruction runs again, and the program never notices. If the page is a fresh one, or a private copy of a shared one, this takes under ten microseconds: a sliver you can barely see. If the page has to be read back from disk, it takes one to ten milliseconds, a thousand times longer. A program doing that constantly has run out of memory.
Demand paging · Going deeper
Faults, fork and mapped files
fork copies page tables, not pages: both sides share frames marked read-only, and a page is copied only when one writes (copy-on-write). mmap points the page table at the kernel’s own cached file pages, so a multi-gigabyte file maps in microseconds and loads lazily.
File systems and fsync
A file is a name that leads to blocks, and “saved” means fsync
A file is named bytes that persist. But a write lands in memory first; only fsync guarantees it survived.
A file is the kernel’s promise of bytes you can find again by name. To open a path, the kernel looks up each name in turn, one lookup per slash, until it reaches an inode: the record of the file’s size, owner, and where its blocks are. Now the surprise. When your program calls write, the bytes go into the page cache, in memory, and write returns OK. They reach the disk later, when the kernel chooses. If the power fails in between, the data is gone, although write said OK. The only guarantee is fsync, which does not return until the data is on the disk. It is slow, which is why databases batch their commits.
File systems and fsync · Going deeper · 1 of 2
Names, inodes and blocks
A directory is itself a file whose data is a list of names and inode numbers. An ext4 inode is 256 bytes. Large files use extents, runs of contiguous blocks stored as start and length, to keep metadata small.
File systems and fsync · Going deeper · 2 of 2
One interface, many filesystems
ext4, XFS, ZFS, Btrfs and APFS all sit behind the same read and write calls. They survive a crash in one of two ways: a journal that logs each change before applying it, or copy-on-write that never overwrites live data. ZFS and Btrfs add checksums that catch silent corruption.
Check yourself
A program calls write(), gets OK, and one second later the power fails. Is the data on the disk?
write() returns once the bytes are in the page cache. Only fsync() waits for the disk.
Interrupts and asynchronous I/O
Devices are slow, so the kernel never lets a core wait
An SSD read costs 200,000 processor ticks. Good I/O design means one thread can serve thousands of slow connections.
Next to a processor, every device is slow. The simple design gives each connection its own thread, which sleeps until its data arrives. Ten thousand connections means ten thousand threads, each holding megabytes of stack. The better design uses one thread. It asks the kernel which connections are ready, handles just those, and asks again. One thread can watch a hundred thousand connections. Here is why it matters. A memory miss costs three hundred ticks. An SSD read costs two hundred thousand. A hard disk seek, thirty million. A round trip across a continent, two hundred and forty million. A core that sits waiting is wasted.
Interrupts and asynchronous I/O · Going deeper
Three generations of I/O
Blocking calls are simple but need a thread each. epoll returns only ready descriptors, so its cost is O(ready), not O(total). io_uring shares a submit queue and a completion queue with the kernel, so one call handles dozens of operations: 10 to 100 times less call overhead.
Containers
A container is a restricted view of one kernel, not a second machine
A container is a normal process the kernel shows less to. A virtual machine carries a whole second kernel.
How do you run someone else’s software safely on your machine? A virtual machine gives each app a whole guest kernel, on a hypervisor that pretends to be hardware. The walls are enforced by the hardware, but each one takes seconds to start and gigabytes of space. A container shares the one real kernel. Namespaces limit what each container can see. Cgroups limit how much it may use. It starts in milliseconds and runs at nearly full speed. The risk is that shared kernel: one kernel bug can cross between containers. So a container is not a small machine. It is an ordinary process with a restricted view. Its environment travels with it as an image.
Containers · Going deeper · 1 of 2
Namespaces and cgroups
PID, mount, network, user and other namespaces each isolate one kind of kernel state; inside, the container’s first process is PID 1 and root can be an unprivileged user outside. cgroups v2 cap CPU, memory, I/O and process counts. seccomp filters the system calls a container may make.
Containers · Going deeper · 2 of 2
An image is a stack of layers
An OCI image is read-only layers named by their SHA-256 hash, plus a writable layer on top that is thrown away on exit. Containers that share a base layer share its bytes on disk and in memory: pull 200 MB once, run a hundred containers.
Check yourself
You must run code uploaded by strangers. Which is the safer choice, and why?
Containers share the kernel, so a kernel flaw is a way out. Separate kernels behind a hypervisor give hardware-enforced walls.
Summary
One read() uses the whole operating system
A system call, a file lookup, a parked thread, a busy core and a copy: every idea in this lesson, in 84 microseconds. Next: the network.
Put it all together with one line of code. The read call goes through the door: a system call, a tenth of a microsecond. The kernel finds the file and checks whether that page is already in memory. It is not. So it asks the SSD and parks your thread, for about eighty microseconds. Meanwhile the scheduler gives the core to other threads. Nothing is wasted. When the data arrives, your thread wakes, the kernel copies four kilobytes into your program’s memory, and the call returns. Nearly all of the time was waiting, and the operating system filled it with other work. Next lesson: what happens when the data lives on another machine.
Summary · Going deeper
The same call, layer by layer
read crosses the system-call boundary, the virtual filesystem, the page cache, the scheduler and the device driver. The contract that keeps it portable is POSIX; Linux’s system-call interface has been stable for decades, which is why a container image built years ago still runs.
Lesson complete
You finished “Orchestration”
Answer the questions along the way to see your score here.