System calls

Programs cannot touch hardware; they ask through one door

user mode: may calculate, may not touch hardwareBrowserDatabaseYour programkernel mode: every instruction allowed✗ writes to the disk directly: the CPU refusessystem call: the one doorKernel: the only code allowed to touch the hardwarekeeps programs apart · shares the cores between them · checks every request for a file or the networkProcessor coresMemoryDiskNetworkLinux has about 450 system calls (read, write, open …). Each crossing costs 30 – 150 nanoseconds, and every request is checked on the way in.

The kernel is the only code the processor lets near the hardware. Everything else asks it, through a system call.

System calls · Going deeper · 1 of 3

Kernel and user-space — the layered stack the OS maintains Process A Process B Process C Process D browser editor database shell user space · ring 3 syscall syscall syscall syscall Kernel ring 0 Scheduler Virtual memory VFS Net stack IPC · signals Page cache Block layer Drivers picks next task page tables · TLB files · inodes TCP · UDP pipes · futex read-ahead · dirty elevator · queues device-specific Hardware CPU cores RAM · MMU Disk · SSD · NIC timers · IRQs programs reach the kernel through one narrow door — the syscall — and the kernel reaches hardware through drivers

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

A syscall — privilege transition from ring 3 to ring 0 and back User process · ring 3 mov rax, 0 ; sys_read · syscall syscall — atomic transition Kernel · ring 0 1. save user regs switch to kernel stack 2. dispatch via sys_call_table[rax] 3. run handler e.g. vfs_read() cost (cold): ≈100–500 cycles base + TLB / cache pollution cost (hot): ≈50–150 cycles · vDSO maps cheap reads (gettimeofday, clock_gettime) into user space and skips the boundary sysret — back to ring 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

No OS versus OS-mediated sharing No OS — programs collide OS mediates every access Program A Program B overlapping memory · racing for the CPU one bug corrupts the other Program A Program B Kernel — mediates Hardware syscall syscall isolation, fairness, sharing — three jobs the kernel performs on every cycle

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

Process 1its own private memoryShared by its threadsprogram code · heap memory · open filesThread 1its own stackThread 2its own stackThread 3its own stackthreads can read and write each other’s dataProcess 2its own private memoryShared by its threadsprogram code · heap memory · open filesThread 1its own stack✗ crashits own stackThread 3its own stacka crash takes down this process onlythe wall: neither process can see the other’s memoryTime to create one (bar length to scale)a thread10 – 30 µs: cheap, shares memory, can corrupt ita process100 – 500 µs: slower, talks only through explicit channels, survives its neighbours

A process owns private memory, so a crash stays local. A thread is one line of execution inside it, sharing that memory.

Processes and threads · Going deeper · 1 of 2

Process as isolation unit; threads as concurrency units inside it Process 1 PID 4271 text (code) data + bss (globals) heap (malloc) file table · sockets · creds Threads (each its own stack + registers) T1 T2 T3 stack PC · regs SP · TLS stack PC · regs SP · TLS stack PC · regs SP · TLS Process 2 PID 4302 text · data · heap separate VAS separate FD table separate creds T1 single-threaded no shared memory

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

A thread moves through five kernel-tracked states across its lifetime Created Ready Running Blocked Terminated task struct built on a runqueue on a CPU waiting on I/O · lock zombie until reaped admit schedule preempt · slice end syscall blocks (read · futex) I/O ready · signal · lock free exit() · fatal signal a context switch is any transition that ends Running

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

0 ms8 ms16 ms24 ms32 ms40 ms48 msthe one coreABCABCACACBAthread Athread Bblocked: waiting for the disk, uses no corethread CEach slice is 1 – 10 ms, too short to notice, so all three seem to run at once.Who goes next?The thread that has had the least core time so far. Higher priority counts time more slowly.What a switch costs1 – 10 µs to save one thread’s registers and load the next.The larger cost is hidden: the new thread starts with a cold cache. Switch too often and warm-up eats the work.

The scheduler hands out the core in short slices. Running “at once” is an illusion made of fast turns.

Scheduling · Going deeper

CFS picks the leftmost runnable task in a per-CPU red-black tree 142 98 186 62 115 160 210 31 leftmost — picked next vruntime (ms) smaller = less CPU time consumed tree walks in O(log n) — a microsecond even with thousands of tasks

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

coreABC?????B: read() from diskthree threads, one core

Thread B calls read() and its data is on a slow disk. What does thread B do to the core while it waits?

Virtual memory

Every process gets its own private map of memory

What process A seesWhat process B seesReal memory (frames of 4 KB)0 kernel1 A’s heap2 B’s heap3 A’s data4 B’s data5 program codeaddress 0x4000heapaddress 0x4000heapaddress 0x2000dataaddress 0x2000dataaddress 0x1000program codeaddress 0x1000program codesame address …… different memoryone copy of shared code serves bothEach process has its own translation table. The processor translates every single memory access through it.A cannot even name B’s memory. Each process gets 256 TB of addresses, almost all of them pointing at nothing until used.

Virtual memory: a program’s addresses are not real locations. A per-process table translates each one, which is what keeps processes apart.

Virtual memory · Going deeper · 1 of 2

Per-process virtual address space mapped to physical frames via the MMU Process A — VAS Physical RAM text (code) data + bss heap → (grows up) unmapped (huge gap) ← stack MMU + page table VPN → PFN TLB caches recent translations frame 0 frame 4 frame 6 frame 8 (shared lib) frame 11 filled physical frames are mapped; the dashed VAS region is unmapped — touching it traps

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

x86-64 four-level page-table walk PML4 idx PDPT idx PD idx PT idx page offset 9 bits 9 bits 9 bits 9 bits 12 bits 48-bit virtual address PML4 PDPT PD PT 4 KB frame 512 entries 512 entries 512 entries 512 entries the data CR3 holds the physical address of the PML4 — a context switch reloads CR3 4 dependent loads on a TLB miss · the TLB exists because this is expensive

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

1 Program readsan address it waspromised2 Nothing behind itthe processor stops:a page fault3 Kernel suppliesa frame of realmemory4 Instruction retriesthe program nevernoticesWhere the page comes from decides the cost (bar length to scale)a fresh empty pageunder 10 µsa private copy of a shared pageunder 10 µsread back from the disk1 – 10 ms: about a thousand times longer. A program doing this all the time has run out of memory.Memory is a promise, kept only when you touch it.A program can open a 1 GB file and pay only for the pages it reads. Starting a copy of a 50 MB process copies nothinguntil one side writes. What a program really uses is its working set, not what it asked for.

A page fault is the kernel filling in memory at the last moment. From memory it takes microseconds; from disk, a thousand times longer.

Demand paging · Going deeper

Page fault flow — translation miss, kernel resolves backing, instruction retries User load/store mov rax, [0x7f…] MMU walk PTE bit: present = 0 Trap → page-fault handler do_page_fault(addr, err) Where does the page come from? anonymous file-backed CoW (after fork) swap first heap touch map a fresh zero page cost: under 10 µs mmap region read from disk · cache cost: 1–10 ms cold write to RO shared page copy + remap RW cost: under 10 µs paged out read from swap cost: 1–10 ms install PTE · flush local TLB return from trap, retry instruction load completes — instruction is none the wiser

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

Finding a file: every “/” is one lookup/homeusernotes.txtinodesize · owner · rightswhere the blocks arethe bytesSaving a file: “written” does not mean “on disk”Your programcalls write()Page cachein memorywrite() returns OK herelater, when the kernel choosesDisksurvives power losspower cut here: the data is gone, although write() said OKfsync(): do not return until it is on the disk50 – 500 µs on a fast SSD, 1 – 10 ms on a hard disk, every timeDatabases call it forevery commit, and batchcommits to afford it.

A file is named bytes that persist. But a write lands in memory first; only fsync guarantees it survived.

File systems and fsync · Going deeper · 1 of 2

Path resolution: /home/user/file.txt walks dentries to inodes to data blocks /home/user/file.txt / (root) /home /home/user file.txt inode 2 dir inode 128 dir inode 4012 dir inode 9001 regular file inode 9001 mode: 0644 uid/gid: 1000/1000 size: 16384 bytes atime · mtime · ctime direct[0..11] → indirect → double-indirect → triple-indirect → block 412 block 413 block 414 block 415 indirect block (pointers) block N block N+1 …

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

Five common filesystems on the same VFS interface, compared on four axes filesystem on-disk shape crash safety snapshots / CoW checksums ext4 XFS ZFS Btrfs APFS block groups · extents B+tree · dynamic inodes B-tree of block ptrs · CoW B-tree · CoW B-tree · CoW · clones journaled metadata delayed alloc · journal CoW (no journal needed) CoW (no journal needed) CoW (no journal needed) no no yes — cheap yes — cheap yes — file clones metadata only metadata only data + metadata data + metadata metadata only a program reads bytes from any of these the same way — VFS hides every difference behind read(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

Programwrite() → OKPage cachememoryDiskpower fails 1 second after OK

A program calls write(), gets OK, and one second later the power fails. Is the data on the disk?

Interrupts and asynchronous I/O

Devices are slow, so the kernel never lets a core wait

One thread per connectioneach thread sleeps until its own data arrives10,000 connections = 10,000 threads, 1 – 8 MB of stack eachOne thread watches them allthe kernel reports only the connections that are readyOne threadhandles 3100,000+ connections per thread (epoll, io_uring)Why waiting must be cheap: how long things take, counted in processor ticksmemory miss100 ns300 ticksSSD read50 – 100 µs200,000 tickshard disk seek10 ms30 million ticksacross a continent80 ms round trip240 million ticksA core that waits is wasted, so the kernel parks the waiting thread and runs another. Never loop asking “ready yet?”.

An SSD read costs 200,000 processor ticks. Good I/O design means one thread can serve thousands of slow connections.

Interrupts and asynchronous I/O · Going deeper

Three I/O models — blocking, multiplexed, async — on a common timeline Blocking Multiplexed (epoll) Async (io_uring) read() thread blocked · ≈30 ms disk seek return 1 syscall · 1 op epoll_wait read fd 4 read fd 7 epoll_wait read fd 9 ≈3 syscalls · N ops submit 32 ops reap 32 kernel + device process the queue · CPU free for other work 2 syscalls · N ops syscall count drops as the API moves work from per-op to per-batch

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

Virtual machinesContainersApp + its librariesApp + its librariesA whole guest kernelA whole guest kernelHypervisor: pretends to be hardwareHardwarestarts in seconds · gigabytes eachwalls enforced by the hardwareApp + libsApp + libsApp + libsOne shared kernelnamespaces: what each container can seecgroups: how much each may useHardwarestarts in milliseconds · near full speedone kernel bug can cross between containersA container is not a small machine. It is an ordinary process that the kernel shows a restricted view:its own process list, its own “/”, its own network, a cap on processor and memory.The app’s whole environment travels with it as an image, so what ran on a laptop runs the same in production.For code you do not trust, wrap the container in a tiny virtual machine (Firecracker, Kata).

A container is a normal process the kernel shows less to. A virtual machine carries a whole second kernel.

Containers · Going deeper · 1 of 2

Containers vs VMs — same kernel, isolated views Containers Virtual machines App A App B App C PID ns MNT ns NET ns cgroup PID ns MNT ns NET ns cgroup PID ns MNT ns NET ns cgroup Host kernel (one) namespaces · cgroups · seccomp Hardware App A App B App C guest libs guest kernel guest init virtio drivers guest libs guest kernel guest init virtio drivers guest libs guest kernel guest init virtio drivers Hypervisor (KVM · ESXi · Hyper-V) Hardware Start: ≈50 ms Overhead: 0 — same kernel Density: 100s–1000s/host Isolation: kernel-level Start: ≈10–30 s Overhead: ≈5–15% Density: 10s/host Isolation: hardware-level

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 OCI image as a stack of read-only layers plus a writable overlay writable layer (overlayfs upper) app + config — your image runtime libs (npm modules, pip wheels) language runtime (node, python, jvm) base OS userspace (debian, alpine) tmpfs sha256:9c… sha256:4a… sha256:7e… sha256:1f… manifest.json layers: [ sha256:1f…, sha256:7e…, sha256:4a… ] reads fall through the stack to the first layer that has the file writes land in the upper layer · deletes are recorded as whiteouts two containers off the same image share their lower layers byte-for-byte

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

Option AOption Bcodecodeone shared kernelhardwarecodecodekernelkernelhypervisorhardware

You must run code uploaded by strangers. Which is the safer choice, and why?

Summary

One read() uses the whole operating system

One line of code: read(file, buffer, 4096) — what happens, on a time line to scale0 µs20 µs40 µs60 µs80 µsyour threadthe coreparked: waiting for the SSD, about 80 µsruns other threads meanwhile1 through the door: a system call, 0.1 µs2 find the file; is the page already in memory? no3 ask the SSD and park this thread5 wake, copy 4 KB to the program, return4 the scheduler gives the core to someone elseNearly all of the time is waiting, and the operating system fills the wait with other work.That is the whole job: one door, private memory, shared cores, slow devices hidden behind a simple call.Read the same page again and it comes from memory in about a microsecond.

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.

Summary · Going deeper

Lifecycle of one read(fd, buf, 4096) — from user code through scheduler, VFS, page cache, and SSD user mode kernel device · scheduler read(fd, buf, 4096) ≈0 ns · user code libc trampoline · syscall ≈10 ns kernel entry · save regs · ring 3 to 0 ≈50 ns VFS dispatch · find inode ≈100 ns page-cache lookup → miss submit to block-device queue · ≈1 µs SSD does the read task blocks · scheduler runs other work on this core ≈100 µs IRQ wakes task · copy to user buf ≈100 ns sysret · back to ring 3 ≈10 ns total ≈100 µs · syscall + VFS + scheduling overhead is <1% · the SSD wait is everything else

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.

Open in a new tab