Operating Systems Crash Course#

Table of Contents#

  1. 1. What is an Operating System?
  2. 2. Kernel vs User Mode & Syscalls
  3. 3. Processes & Process Control Block
  4. 4. Threads & Concurrency
  5. 5. CPU Scheduling
  6. 6. Inter-Process Communication (IPC)
  7. 7. Synchronization & Locks
  8. 8. Deadlocks & Prevention
  9. 9. Memory Management & Paging
  10. 10. Virtual Memory & Page Faults
  11. 11. File Systems & Inodes
  12. 12. Quick Reference & Cheat Sheet

1. What is an Operating System?#

An Operating System (OS) is software that acts as an intermediary between computer hardware and user applications. Without an OS, every program would have to write its own disk drivers, manage raw RAM addresses directly, and schedule its own hardware access without safety checks.

The primary responsibilities of an OS break down into two roles:

  1. Resource Manager: Coordinates the allocation of CPU time, memory space, network bandwidth, and I/O devices among competing programs.
  2. Extended Machine (Abstraction Layer): Replaces complex, low-level hardware details (disk sectors, memory voltages, CPU interrupts) with clean abstractions like files, sockets, processes, and virtual memory.

Mental Model: Think of the OS as the head referee in a busy sports arena. Players (applications) cannot run onto the field whenever they please or claim ownership of the stadium seats (RAM). The referee enforces time slices, boundaries, and prevents players from colliding or sabotaging each other.

2. Kernel vs User Mode & Syscalls#

To prevent malicious or buggy code from crashing the hardware or snooping on other programs, modern CPUs provide hardware-enforced execution rings:

  1. User Mode (Ring 3): Applications run with restricted privileges. They cannot directly access hardware, modify page tables, or disable interrupts.
  2. Kernel Mode (Ring 0): The core of the OS runs with unrestricted hardware access. It can execute privileged CPU instructions and read/write any memory address.

When user applications need hardware services (such as reading a file from SSD or sending a packet over the network), they must transition into kernel space using a System Call (syscall) via a software interrupt or CPU instruction like syscall (x86-64) or svc (ARM).

// User Program makes a request
ssize_t bytes_read = read(fd, buffer, sizeof(buffer));

// 1. Trap / Syscall instruction triggers CPU mode switch (Ring 3 -> Ring 0)
// 2. Kernel looks up Syscall table (e.g., sys_read)
// 3. Kernel verifies memory pointers and reads data from device driver
// 4. Kernel copies data to user buffer and returns to User Mode (Ring 0 -> Ring 3)

3. Processes & Process Control Block#

A process is an active, running instance of a program. While a program is passive code sitting on disk, a process is an active execution context containing:

  1. Code Segment (Text): The compiled machine instructions.
  2. Data & BSS Segments: Global and static variables.
  3. Heap: Dynamically allocated memory at runtime (e.g. malloc, new).
  4. Stack: Local variables, function call frames, and return addresses.

The kernel tracks every process using a Process Control Block (PCB), which preserves:

4. Threads & Concurrency#

A thread is the smallest unit of execution scheduled by the OS. A single process can contain multiple threads that share:

However, each thread maintains its own private:

Context Switch Overhead: Switching between two threads of the same process is much faster than switching between two distinct processes because the OS does not have to flush or swap the virtual memory page tables (TLB).

5. CPU Scheduling#

The CPU scheduler selects which ready process or thread executes on an available CPU core. Key scheduling strategies include:

  1. First-Come, First-Served (FCFS): Simple queue; suffers from the "convoy effect" where short tasks wait behind huge CPU-bound tasks.
  2. Shortest Job First (SJF): Minimizes average waiting time; requires predicting burst durations.
  3. Round Robin (RR): Preemptive scheduling using a fixed time quantum (slice). Ensures fair sharing and low response times for interactive applications.
  4. Multi-Level Feedback Queue (MLFQ): Adaptive priorities; I/O-bound jobs stay at high priority for quick response, while CPU-bound compute jobs sink to lower priority with longer quanta.

6. Inter-Process Communication (IPC)#

Because processes run in isolated address spaces, they cannot read each other's memory directly. The OS provides IPC mechanisms:

  1. Pipes & FIFOs: Unidirectional byte streams between related processes.
  2. Shared Memory: Multiple processes map the exact same physical memory frames into their address space. The fastest IPC mechanism since it avoids kernel copy buffers.
  3. Message Queues: Discrete structured messages delivered asynchronously.
  4. Sockets: Bidirectional stream/datagram communication across process boundaries or over a network.

7. Synchronization & Locks#

When multiple threads concurrently read and write shared data without coordination, a race condition occurs, leading to corrupted data.

A Critical Section is a block of code accessing shared resources that must execute atomically. OS primitives used to enforce mutual exclusion include:

8. Deadlocks & Prevention#

A deadlock is a state where two or more processes are permanently blocked because each is holding a resource the other needs. All 4 Coffman Conditions must hold simultaneously for a deadlock to occur:

  1. Mutual Exclusion: At least one resource cannot be shared.
  2. Hold and Wait: A process holds resources while requesting additional ones.
  3. No Preemption: Resources cannot be forcibly taken from a process.
  4. Circular Wait: A closed chain of processes exists where each process waits for a resource held by the next.

Prevention Tip: The simplest way to prevent deadlocks in software is Lock Ordering: always acquire multiple locks in the exact same predefined global order across every thread.

9. Memory Management & Paging#

Programs do not interact with physical RAM addresses. Instead, the CPU's Memory Management Unit (MMU) translates Virtual Addresses to Physical Addresses using Paging.

10. Virtual Memory & Page Faults#

Virtual memory allows a computer to run programs that exceed the size of physical RAM by storing inactive pages on disk (swap/pagefile).

  1. When a program accesses a virtual page that is not currently loaded in physical RAM, the MMU triggers a hardware trap called a Page Fault.
  2. The kernel intercepts the trap, locates the requested page in swap space or executable file on disk.
  3. The OS finds a free physical frame (or evicts an existing page using algorithms like LRU or Clock).
  4. The page is read from disk into RAM, the page table entry is updated, and the CPU instruction is retried.

11. File Systems & Inodes#

A file system structures raw disk blocks into files and directories. In Unix/Linux file systems (e.g. ext4):

12. Quick Reference & Cheat Sheet#

Core Concepts at a Glance

  • Kernel: Core program running in Ring 0 with hardware control.
  • Process: Isolated address space with text, data, heap, stack, and PCB.
  • Thread: Lightweight execution unit within a process sharing heap and file descriptors.
  • Context Switch: Saving and restoring CPU registers between tasks.
  • TLB: Hardware cache for virtual-to-physical address translation.
  • Page Fault: Trap triggered when accessing an unmapped or swapped page.
  • Deadlock: Four conditions (Mutual Exclusion, Hold & Wait, No Preemption, Circular Wait).

Next Steps: Dive deeper into Linux CFS, futexes, and copy-on-write with the OS Detailed Course, or explore practical command-line and systems skills in the Linux Crash Course. View the full catalog at All OS Courses.