[OS Dev] Fundamentals - 4 Essential Concepts

Concept 1: Thread of Control

Thread: A virtualized execution context that progresses through instruction cycles. It is executing when it is resident on the processor registers

Resident: Registers hold the root state of the thread:

A process is NOT resident when the processor is pointing at some other thread and the PC is not pointing at the next instruction from this thread.

Question: How is the illusion of multiple processor made?

We multiplex through running multiple virtual cores sequentially.

Question: Where is the thread?

When its running, its on the core. When its not running, its saved in a chunk of memory called the Thread Control Block (TCB).

Thread Control Block: Holds the contents of registers when a thread is not running. It is stored in memory

Concept 2: Address Space

Address Space: Set of accessible addreses + state associated with them. For a 32 bit processor, there are 232 addresses and 264 for a 64 bit processor (but maybe not are all useable, e.g. only 48 for x86)

There are a few parts to the address space:

Simple Multiprogramming

Question: Why is protection important?

First, the OS itself needs to be protected from user programs

The OS also needs to protect user programs from each other

Protection can be accomplished through Base and Bound which defines a section of address space upon which a program can run. Enforcement is done by hardware checks on each memory access rather then software.

Address Space Translation: This can be done with a Paged Virtual Address Space which breaks the entire virtual address space into equally sized chunks (pages) with a base for each.

The hardware will translate addresses using a page table.

Example
<Virtual Address> = <Page #> <Page Offset>

The <Page #> is fed into the Page Table, which gives you the <Frame Addr>. 

* Note the similarity of Frame Addr to "Base" in the Base and Bound concept.

Then, the <Page Offset> gives you the exact location within that page.

Concept 3: Process

Process: An address space with 1 or more threads. It is an execution environment with restricted rights. An application program executes as a process. Processes are protected from each other and OS is protected from them.

Threads encapsulate concurrency, and Address Spaces encapsulate protection

Processes also ensure reliability (memory isolation), security (can't see others), and to some degree, fairness.

Concept 4: Dual Mode Operation

Hardware provides at least 2 modes:

  1. Kernel Mode
  2. User Mode

Certain operations cannot be done on user mode (e.g. change page table pointer, disable interrupts). There are also certain transitions between user mode and kernel mode (e.g. system calls, interrupts, exceptions).

There are 3 types of user -> kernel mode transfers:

  1. Syscall: process requests a system service, e.g. exit
  2. Interrupt: external asynchronous event triggers a context switch, e.g. timer or I/O device
  3. Trap or Exception: internal synchronous event in process which can trigger context switch, e.g. protection violation (segmentation fault), divide by zero...