imara learning library ← all courses
Computer Science · Systems · 4 weeks

Operating systems, from the metal up

What actually happens between your code and the hardware. Four weeks on the three jobs every OS does — sharing the CPU, sharing memory, and storing things that survive a power cut — ending where it matters to you: how virtual machines and containers really work.

4
weeks: CPU, concurrency, memory, storage
4
interactive simulators to run
3
illusions the OS maintains for you
∞
bugs this will help you finally understand
Week 01foundations & processes

What an operating system is

Strip away the jargon and an OS has one job: stand between your programs and the hardware, and make a messy, singular, physical machine look like a tidy set of private, abstract resources to everyone who asks. It's part referee, part illusionist.

The two roles

Everything the OS does falls into one of two buckets:

The whole course is really three deep dives into that illusionist role: making one CPU look like many (Week 1–2), making limited memory look private and infinite (Week 3), and making volatile hardware look like permanent, named storage (Week 4).

Kernel vs user space — the great divide

The most important boundary in any computer. Code runs in one of two modes:

This divide is enforced by the CPU itself, not by politeness. It's why one crashing app doesn't take the whole machine down, and why malware can't (easily) read another program's secrets. The caging is the feature.

The system call — crossing the divide

But a caged program still needs to do real things: read a file, send a packet, start another program. It does this by asking the kernel through a system call — a controlled doorway between user and kernel mode.

The mechanism

A system call is a deliberate, hardware-assisted "trap": the program sets up its request, executes a special instruction, and the CPU jumps into kernel mode at a fixed, trusted entry point. The kernel checks the request, does the privileged work, and hands control back. It's the airlock between the cage and the machine room — the only sanctioned way through.

The process: a program in motion

A program is a dead thing — a file on disk. A process is that program brought to life and running: it has memory, open files, a slice of CPU time, and a current position in its own execution. The process is the OS's fundamental unit of "a running thing."

What a process is made of

The OS tracks each process in a bookkeeping record (the process control block): its unique ID, its current state, the contents of the CPU registers when it last paused, its memory layout, and its open files. That register snapshot is the key to the magic trick we'll see next — it's a complete "bookmark" of a running program.

The three states

At any instant a process is in one of three states, and it dances between them constantly:

Click through the transitions — this little state machine is the heartbeat of every computer you've ever used:

Process state machine · trigger the events
A process spends its life bouncing Ready → Running → (I/O) Blocked → Ready. "Preempt" is the scheduler forcibly taking the CPU back — the trick that lets many programs share one core.

Creating processes: fork and exec

On Unix-like systems (Linux, macOS, Android under the hood) new processes are born a surprising way: a process clones itself with fork(), producing a near-identical child, which then replaces its own program with exec(). Clone, then transform.

spawn.c — the classic pattern
pid_t pid = fork();        // clone this process in two
if (pid == 0) {
    // child: become a different program
    execlp("ls", "ls", "-l", NULL);
} else {
    // parent: wait for the child to finish
    wait(NULL);
}

Every process descends from another, forming a tree rooted at the very first process the kernel starts at boot. It's turtles all the way down to init.

Week 02sharing the CPU

Concurrency: many things at once on one brain

Your laptop runs hundreds of processes on a handful of cores. It does this with a magic trick (time-sharing) and a hard problem (keeping things correct when they overlap). Week 2 is both halves.

The illusion: context switching

A single CPU core does exactly one thing at a time. The appearance of many simultaneous programs comes from switching between them fast — thousands of times a second, faster than you can perceive. Each switch is a context switch:

The bookmark trick

To pause process A and run process B, the OS saves all of A's CPU registers (its exact bookmark) into A's control block, loads B's saved registers, and resumes. When A's turn comes again, it continues as if nothing happened — it has no idea it was ever paused. The register snapshot is the running program's soul; save and restore it, and you can juggle hundreds.

Switching isn't free — it costs a little time and throws away warm caches — so the OS balances switching often (responsive) against switching rarely (efficient).

Threads: lightweight concurrency

A thread is a line of execution within a process. One process can have many threads, and crucially they share the same memory. That makes threads cheap to create and great for parallel work — and simultaneously the source of the hardest bugs in computing, because shared memory means they can step on each other.

The scheduler: who runs next?

When the CPU comes free, the scheduler picks which ready process runs. Different algorithms optimise for different things:

AlgorithmRuleStrength / weakness
FIFO / FCFSFirst to arrive runs first, to completionSimple; but one long job stalls everyone ("convoy effect")
SJFShortest job firstBest average wait; but needs to know job lengths, can starve long jobs
Round RobinEach gets a fixed time slice, then rotatesFair and responsive; more switching overhead
MLFQMultiple priority queues, jobs demoted as they use CPUWhat real systems use — learns who's interactive vs batch

Run the same four jobs through different schedulers and watch the trade-off in the timeline and the average wait:

CPU scheduler · same jobs, different policies
Four jobs (P1–P4) with different arrival and burst times. Watch FIFO let a long job hog the CPU, SJF minimise average wait, and Round Robin interleave everyone fairly.

The hard part: getting concurrency right

When two threads touch the same data at the same time, results depend on the exact interleaving of their steps — which is unpredictable. This is a race condition, and it's the bug that only appears in production, at 2am, once a week.

The race

Imagine two threads each doing balance = balance + 100 on a shared account. That one line is secretly three steps: read balance, add 100, write it back. Interleave them badly:

A lost update
start: balance = 500
Thread A reads 500 · Thread B reads 500
A computes 600, writes 600 · B computes 600, writes 600
end: balance = 600 — but two deposits of 100 happened. ₹100 vanished.

The code looks correct. It is correct, run alone. It's only wrong when it overlaps — which is exactly what makes these bugs so maddening.

The fix: mutual exclusion

The overlapping region (the "critical section") must be made indivisible — only one thread inside at a time. The tool is a lock (mutex):

deposit.c — guarded by a lock
lock(&account_mutex);      // wait until it's mine alone
balance = balance + 100;   // critical section — safe now
unlock(&account_mutex);    // let the next thread in

Other tools build on this idea: semaphores (count a limited resource — "5 free slots"), and condition variables ("sleep until this becomes true"). But they all serve the same goal: tame the interleaving.

Deadlock: the cure that kills

Locks solve races but introduce a new hazard. Thread A holds lock 1 and wants lock 2; thread B holds lock 2 and wants lock 1. Both wait forever. That's deadlock, and it needs four conditions all at once — break any one and you're safe:

The practical fix

The easiest condition to break is circular wait: always acquire locks in a fixed global order. If every thread grabs lock 1 before lock 2, the cycle can never form. A simple convention beats clever detection.

Week 03sharing memory

Virtual memory: a private, roomy illusion

Every program believes it owns a huge, private, contiguous block of memory starting at address zero. That belief is a lie the OS and hardware maintain beautifully — and it's what stops programs corrupting each other while squeezing them all into limited physical RAM.

The problem it solves

Physical memory is one finite array of bytes, shared by everything. Without help, program A could read or clobber program B's data, programs would fight over the same addresses, and nothing would fit once RAM filled. Virtual memory fixes all three at once.

Virtual vs physical addresses

The trick: programs only ever use virtual addresses. Every memory access is translated, in hardware, to a real physical address on the fly — by a dedicated chip called the MMU (memory management unit). Program A's address 0 and program B's address 0 map to different physical locations, so they're invisible to each other. The illusion of a private machine, enforced per access.

Paging: the mechanism

Rather than translate every byte, memory is chopped into fixed-size pages (typically 4 KB). Physical memory is chopped into equal frames. A per-process page table records which physical frame holds each virtual page. Translation splits an address in two:

Slide a virtual address and watch it get translated, exactly as the MMU does billions of times a second:

Address translation · virtual → physical (4 KB pages)
Page number = address ÷ 4096 (the page table turns it into a frame). Offset = address mod 4096 (carried across untouched). Same offset, different frame — that's the whole idea.
Why it's fast: the TLB

Looking up the page table on every access would be ruinously slow. So the MMU keeps a small cache of recent translations — the TLB (translation lookaside buffer). Hit the TLB and translation is nearly free; miss it and you pay for the full lookup. Most programs hit ~99% of the time, which is why virtual memory costs almost nothing in practice.

Demand paging & the page fault

Here's where the "infinite memory" illusion comes from. Not every page needs to be in RAM — rarely used pages can sit on disk. When a program touches a page that isn't currently in RAM, the MMU raises a page fault: the OS pauses the program, fetches the page from disk into a free frame, updates the page table, and resumes. The program never knows it happened — it just ran a little slower that once.

Page replacement: what to evict

When RAM is full and a new page must come in, something has to go. Which page you evict matters enormously — evict one you'll need immediately and you'll just fault it back in (a disaster called thrashing). The classic policies:

Feed the same sequence of page accesses to both and count the faults — LRU usually wins:

Page replacement · FIFO vs LRU
Top row: the stream of pages the program asks for. Grid: what's in each RAM frame over time. Red = a page fault (had to fetch from disk). Fewer reds is better.
Week 04storage, I/O & the cloud

Persistence: things that survive the power cut

CPU and memory are fast and forgetful — pull the plug and they're blank. The last job of the OS is to manage slow, permanent storage and give it a clean face: files and directories. This week also lands where your working life does — virtual machines and containers.

Talking to devices: interrupts

Disks, networks, and keyboards are glacially slow next to the CPU. The OS could sit and repeatedly ask "done yet? done yet?" (polling — wasteful), or it could issue the request, go do other work, and let the device interrupt it when finished. Interrupts are how a computer stays productive while waiting on the physical world — the CPU fires off a disk read and schedules other processes until the "I'm done" signal arrives.

And DMA

Moving a big file byte-by-byte through the CPU would waste it entirely. Direct memory access lets a device copy data straight into RAM on its own, interrupting the CPU only once at the end. The CPU delegates the grunt work and stays free for real computation.

The file abstraction

A disk is really just a huge numbered array of fixed-size blocks. The OS hides that behind the file: a named, arbitrarily-sized, growable sequence of bytes, organised in a tree of directories. You say "append to report.txt"; the OS figures out which physical blocks that means, allocating new ones as needed.

Inodes: the file's identity card

On Unix-style file systems, each file has an inode — a record holding everything about the file except its name: its size, permissions, timestamps, and the list of disk blocks holding its data. The name lives in the directory, which just maps names to inode numbers. A neat consequence: the same file can have two names (hard links) because two directory entries can point at one inode.

Crash consistency: journaling

What if the power dies mid-write? You could be left with a half-updated file system — a corrupted mess. Modern file systems prevent this with a journal: before making a change, they first write down their intent in a dedicated log. After a crash, the OS replays the journal to finish or discard half-done operations. It's the write-ahead log idea — the same trick databases use to never lose a committed transaction.

You've seen this pattern before

Write-the-intent-first, then act, then confirm — journaling for file systems, write-ahead logs for databases, idempotent retries for payments. The same reliability pattern recurs at every layer of a system, including the ledgers in a lending platform.

Where it all meets the cloud: VMs & containers

Every concept so far — the kernel boundary, virtual memory, the process, the file system — comes together in the two technologies your infrastructure runs on. Understanding the difference is understanding your whole deployment stack.

Virtual machines — virtualize the hardware

A VM takes the OS's illusionist trick up a level: a hypervisor makes one physical machine present itself as several complete virtual machines, each running its own full OS, kernel and all, believing it owns real hardware. Strong isolation (each has its own kernel), but heavy — you're running many complete operating systems, each with its own memory and boot time.

Containers — virtualize the OS

Containers are lighter and cleverer. They share the host's single kernel but use two kernel features to make each container feel like it has the machine to itself:

 Virtual machineContainer
VirtualizesThe hardwareThe operating system
KernelOne per VMShared host kernel
IsolationVery strongStrong, but shared-kernel
Start timeSeconds to minutesMilliseconds
OverheadHeavy (full OS each)Light (just your process)
The one-sentence version

A VM is a whole computer pretending to exist; a container is a process wearing a convincing disguise. VMs isolate by duplicating the OS; containers isolate by namespacing a shared one — which is why a container boots in milliseconds and a VM doesn't.

You've seen the whole machine

From the kernel boundary and the system-call airlock, through processes and the context-switch bookmark, the scheduler and the locks that tame concurrency, virtual memory and the page fault, down to files, inodes, journaling, and the VMs and containers your platform runs on — you now have the mental model of what happens between your code and the silicon. Every performance mystery, every weird production bug, every infrastructure choice makes more sense from here.