Four Fundamental OS Concepts
Source: UC Berkeley CS162, Fall 2020/2021 - Prof. John Kubiatowicz, Lecture 2
This lecture lays the foundation for the whole course. It starts from a single question - what does an operating system actually do? - and then introduces the four ideas everything else is built on: the thread, the address space, the process, and dual-mode operation. Get these four straight and the rest of the course (scheduling, memory, concurrency) is variations on them.
Taming complexity: the OS as an abstraction layer
Hardware is a pile of complexity - many cores, memory hierarchies, dozens of devices, each with its own quirks. An operating system sits between the hardware and your program and offers clean, easy-to-use abstractions of that messy physical machine. It plays three roles at once: a referee that manages sharing and protection between programs, an illusionist that makes each program feel like it has the machine to itself, and glue that provides common services (storage, windowing, networking).

What a program actually is
To see what the OS has to virtualize, recall from architecture what running a program means: the processor repeats a fetch/decode/execute cycle - fetch the instruction at the program counter, decode it, execute it (reading and writing registers and memory), advance the PC, and repeat. The complete state of a running computation at any instant is just its registers (including the PC and stack pointer) plus the memory it can touch.

Concept 1: the thread
The OS gives each running activity the illusion of its own processor. In reality one core is time-multiplexed among many, but from the programmer's view each thread has a dedicated (virtual) CPU with its own registers.

A thread is a single, sequential stream of execution - one PC threading its way through the code. The magic that maintains the illusion is the context switch: the OS saves the current thread's registers into memory, loads another thread's saved registers, and jumps to its PC. The saved state lives in a per-thread structure (a thread/process control block).


- A thread is one sequential execution context: a PC plus the registers and stack that go with it.
- Its complete state can be saved and restored, so a thread can be paused and resumed transparently.
- The OS keeps that state in a control block in kernel memory and multiplexes many threads onto the real CPU(s) via context switches.
Concept 2: the address space
The second concept answers "what memory can a thread touch?" An address space is the set of memory addresses a program may reference, plus the state associated with them. Each program is written as if it owns a big, private range of memory.

- 32-bit processor -
2^32addresses, about 4 billion bytes (4 GiB). - 64-bit processor -
2^64addresses, about 18 quintillion bytes - far more than any real machine has RAM for, which is the whole point: the address space is virtual.
For this to be safe, the hardware must stop one program's addresses from reaching another's memory. The simplest scheme is base and bound: the hardware adds a per-program base to every address and checks it against a bound, so a program can only touch its own region and cannot see the OS or other programs.

Real machines generalise this to paged virtual memory: the address space is split into fixed-size pages, and a per-process page table maps each virtual page to a physical frame (or to disk). This gives each process its own private, relocatable address space without contiguous physical memory.

Concept 3: the process
Put a thread and an address space together and add the OS state that protects them, and you get a process - the OS's unit of protection and resource ownership. A process is one address space plus one or more threads running inside it, plus everything the OS tracks on its behalf (open files, and so on).


Why bother with the process boundary at all? Protection.

- A process = one address space + one or more threads + the OS state (open files, etc.) associated with them.
- It is the OS's unit of protection and isolation: each process sees only its own memory.
- A single process can run multiple threads, which share the address space but each keep their own registers and stack.
Protection is a property of the address-space boundary, i.e. the process. Two threads in the same process share all of memory by design, so one can freely stomp on another's data. That is exactly why concurrency inside a process is dangerous and needs synchronization - a topic the next unit is entirely about.
Concept 4: dual-mode operation
The final concept is what makes the other three enforceable. The CPU runs in one of (at least) two modes: user mode, where dangerous operations are forbidden, and kernel mode (supervisor/privileged mode), where the OS runs with full access to hardware. Only carefully controlled transitions move between them.

- The CPU has a hardware mode bit: user (restricted) vs kernel (full privilege).
- Privileged operations (changing the page table, disabling interrupts, touching devices) are only allowed in kernel mode.
- The only entries to the kernel are a system call, an interrupt, or an exception - each an "unprogrammed control transfer" to a fixed, OS-controlled address.
The transition into kernel mode does not let user code pick where execution lands. Each entry (syscall/interrupt/exception) vectors to an address the OS chose in advance. If a program could set the target itself, it would run arbitrary code with full privilege - so the "unprogrammed control transfer" is deliberately out of the program's hands.
Tying it together
With all four concepts in place, loading a program is straightforward: the OS sets up an address space (here via base and bound), copies in the code and data, initialises the stack, records the process's saved registers, and then switches into it in user mode. Everything after that is the same four ideas working together.

Recap
- The OS is a referee, illusionist, and glue that turns messy hardware into clean abstractions.
- A thread is a single execution context (PC + registers + stack) that the OS can save, restore, and multiplex onto real CPUs via context switches.
- An address space is the private set of memory a program can touch, enforced by hardware (base/bound, then paged virtual memory).
- A process is an address space plus its threads plus OS state - the unit of protection and isolation.
- Dual-mode operation (user vs kernel) plus controlled transitions is what makes that protection enforceable.
- Next: how these are exposed to programs, starting with the process API -
fork,exec,wait,exit.