Foundations of ComputingDeeper

Kayavak / Foundations of Computing / CS·08 Operating Systems / Deeper

Deeper · Foundations of Computing

Operating Systems

The questions the chapter doesn't stop for: why this exists, the model to keep in your head, what changes when an assumption does, how sure we are of each claim, and what's still open. Read it after the chapter; back to the chapter.

Why does this exist?

The first computers ran one program at a time, loaded by hand, and sat idle while people prepared the next. Batch systems in the 1950s queued jobs so the machine never waited for an operator; then the machine waited for tapes and printers instead, and people waited hours for results. Time-sharing answered that: switch between many users' programs so fast that each feels it has the machine. MIT's CTSS, led by Fernando Corbató, demonstrated it in 1961; Multics (MIT, Bell Labs and General Electric, from 1965) tried to make it a dependable utility; Ken Thompson and Dennis Ritchie, after Bell Labs left Multics, built the much smaller Unix from 1969.

Sharing one machine safely needed new ideas, each still in your kernel. The Atlas computer at Manchester (1962) had the first virtual memory: each program sees its own address space, and pages move between fast and slow memory automatically. Dijkstra introduced semaphores in 1965 to coordinate concurrent processes. In 1971 Edward Coffman and colleagues set out the four conditions under which processes deadlock, which the chapter lists. Hardware support for privilege levels let the kernel stop one program from corrupting another or the system.

The mental model

Try it yourself first: write your own model in the Mental Model Explorer, then compare.

An operating system is a referee and a magician: it divides one machine among many programs, and it gives each one the illusion of having a whole, simpler machine to itself.

Question Answer
What is it? Privileged software that manages hardware and offers abstractions: processes, threads, virtual memory, files, sockets
Problem it solves Sharing hardware safely and efficiently among programs that don't trust or know about each other
Parts Kernel, scheduler, memory manager (page tables), filesystems, device drivers, system-call interface
How it works Programs run in user mode; anything privileged goes through a system call; timers interrupt them so the scheduler can switch
Assumptions The kernel is correct and trusted; hardware enforces the user/kernel boundary
When they change A kernel bug or hardware flaw breaks every isolation guarantee at once
Where it fails Shared state between threads (races, deadlock), illusions that leak (page faults, swapping, write before fsync)
Related ideas Virtual memory uses the MMU and TLB (ch07); I/O models underlie servers (ch09, The Backend); containers reuse these (Infrastructure)

What if?

What if there were no virtual memory?

Reason it through, then open

Every program would see real physical addresses. Two programs couldn't both be loaded at address 0x400000, so programs would have to be relocated when loaded; a stray pointer in one would overwrite another program or the kernel; and fork couldn't share memory copy-on-write. Embedded systems without an MMU live like this, which is why they usually run one trusted program.

What if the OS never interrupted a running program?

Reason it through, then open

That's cooperative multitasking: each program must give up the processor voluntarily. One infinite loop or slow computation freezes everything, including the mouse pointer. Classic Mac OS and 16-bit Windows worked this way, and users knew the frozen-screen result well. Preemptive scheduling with timer interrupts is what lets the OS take the CPU back. The same trade-off reappears inside programs: an event loop is cooperative, so a blocking call stalls every request it serves.

Where the boundary is

Aspect Process Thread
Memory Its own address space Shares its process's address space
Isolation A crash or bad pointer stays inside One bad write can corrupt every thread
Creation and switching Heavier: new page tables Lighter: new stack and registers
Communication Pipes, sockets, shared memory set up on purpose Just read and write the same variables (hence races)
Pick it when You want isolation (browsers' tabs, servers' workers) You want cheap sharing and accept the discipline it needs

Derive it

Why page tables have several levels.

  1. x86-64 uses 48-bit virtual addresses (in its common 4-level mode) and 4 KB pages. A 4 KB page covers 2¹² addresses, so there are 2⁴⁸ / 2¹² = 2³⁶ possible pages.
  2. A flat table with one 8-byte entry per page would need 2³⁶ × 8 bytes = 512 GB, per process. Impossible.
  3. Most of the address space is unused, so split the 36 page-number bits into four groups of 9. Each group indexes a table of 2⁹ = 512 entries × 8 bytes = 4 KB: exactly one page.
  4. Only the tables covering addresses actually in use need to exist. A small process needs a handful of 4 KB tables, not 512 GB.
  5. The price is four memory lookups per translation, which is why the TLB caches recent translations (chapter 7), and why large pages (2 MB, 1 GB), which skip levels, help programs with huge memory.

How sure are we?

Claim How it's known Where from
Deadlock needs all four Coffman conditions Theorem Coffman, Elphick and Shoshani, 1971
Four 9-bit levels cover 48-bit addresses with 4 KB tables Theorem The arithmetic above; Intel and AMD manuals
A context switch costs about 1–5 µs Measured Varies with hardware, mitigations and cache effects
write returning means data is in the page cache, not on disk Model POSIX semantics; drives and controllers add their own caches

Still open

Kernel design is still argued about: large monolithic kernels (Linux) versus microkernels (seL4, which has a formal proof of correctness), how much to move into user space, and how to schedule fairly and efficiently on chips with fast and slow cores mixed. Safe languages in kernels are new: Rust code has been merged into Linux since 2022.

What would change your mind? If a microkernel matched a monolithic kernel's performance on everyday workloads, the main argument for putting everything in one privileged program would fall away.

Recall

What problem did time-sharing solve?

Letting many people use one machine at once by switching between their programs quickly.

Why can't a page table be one flat array on x86-64?

2³⁶ pages × 8 bytes is 512 GB per process; multi-level tables only exist for addresses in use.

What are the four conditions for deadlock?

Mutual exclusion, hold-and-wait, no preemption, circular wait.

What's the main difference between a process and a thread?

Processes have separate address spaces; threads in one process share memory.

What happens with cooperative multitasking if one program loops forever?

Everything freezes, because nothing can take the processor back.

Reflect

Be honest with yourself; this stays in your browser. Anything you can't tick yet is what to revisit.