Overview
An Operating System (OS) is the software that sits between your hardware and your applications. It manages the CPU, memory, storage, and devices so that programs can run without needing to control hardware directly. Understanding how operating systems work helps you write more efficient code, debug tricky performance issues, and design better systems.
What an Operating System Does
Section titled “What an Operating System Does”The OS provides three core services:
- Resource Management - allocating CPU time, memory, and I/O devices fairly across programs
- Abstraction - hiding the complexity of hardware behind simple interfaces like files, processes, and sockets
- Protection - keeping programs isolated from each other and from the kernel
Processes
Section titled “Processes”A process is a running instance of a program. It is the fundamental unit of execution in an OS.
- Process vs Program - a program is static code on disk; a process is that code running in memory
- Process States: New -> Ready -> Running -> Waiting -> Terminated
- Process Control Block (PCB) - the data structure the OS keeps for each process (PID, state, registers, memory maps)
- Context Switching - saving and restoring the state of a process when the CPU switches between them
- Process Creation -
fork()creates a child process that is a copy of the parent;exec()replaces the process image - Process Termination - normal exit, error exit, killed by OS or another process
- Zombie and Orphan Processes - what happens when a child exits before the parent collects its status
Threads and Concurrency
Section titled “Threads and Concurrency”Threads are lighter-weight units of execution that share the same memory space within a process.
- Thread vs Process - threads share code, data, and heap; each has its own stack and registers
- User-level vs Kernel-level threads - where threads are managed and the trade-offs
- Multithreading models: Many-to-One, One-to-One, Many-to-Many
- Concurrency problems:
- Race conditions - two threads reading and writing the same data at the same time
- Critical section - code that must not be executed by more than one thread at a time
- Synchronisation tools:
- Mutex (mutual exclusion lock) - only one thread can hold it at a time
- Semaphore - a counter that controls access to a shared resource
- Monitor - a high-level synchronisation construct combining a mutex and condition variables
- Condition variables - letting threads wait for a condition to become true
- Deadlock - four conditions, prevention, avoidance (Banker’s Algorithm), detection, and recovery
- Thread safety - designing code that works correctly when called from multiple threads
CPU Scheduling
Section titled “CPU Scheduling”The CPU scheduler decides which process gets to run next. Good scheduling maximises utilisation and minimises wait time.
- Scheduling criteria: CPU utilisation, throughput, turnaround time, waiting time, response time
- Preemptive vs Non-preemptive scheduling - can the OS interrupt a running process or not
- Scheduling algorithms:
- FCFS (First Come First Served) - simple but can lead to the convoy effect
- SJF (Shortest Job First) - optimal average waiting time but requires knowing burst times
- SRTF (Shortest Remaining Time First) - preemptive version of SJF
- Round Robin - each process gets a fixed time quantum in rotation; good for interactive systems
- Priority Scheduling - higher priority processes run first; can cause starvation
- Multilevel Queue - separate queues for different types of processes (foreground, background)
- Multilevel Feedback Queue - processes can move between queues based on behaviour
Memory Management
Section titled “Memory Management”Memory management controls how RAM is allocated and used by processes.
- Logical vs Physical Address - what the process sees vs the actual RAM location
- Memory Management Unit (MMU) - hardware that translates logical to physical addresses
- Contiguous Allocation - giving each process a single block of contiguous memory
- Fixed partitioning and variable partitioning
- Internal and external fragmentation
- Paging - dividing memory into fixed-size pages; eliminates external fragmentation
- Page table - maps logical pages to physical frames
- Multi-level page tables - for large address spaces
- TLB (Translation Lookaside Buffer) - a fast cache for page table entries
- Segmentation - dividing memory into logical segments (code, stack, heap)
- Paging vs Segmentation - trade-offs and when each is used
- Memory allocation algorithms: First Fit, Best Fit, Worst Fit
Virtual Memory
Section titled “Virtual Memory”Virtual memory gives processes the illusion of having more memory than physically exists by using disk as an overflow.
- Demand Paging - only load pages into RAM when they are actually needed
- Page Fault - what happens when a process accesses a page that is not in RAM
- Page Replacement Algorithms - deciding which page to evict when RAM is full:
- FIFO - evict the oldest page
- LRU (Least Recently Used) - evict the page that was last accessed longest ago
- Optimal - evict the page that will not be needed for the longest time in the future (theoretical best)
- Clock Algorithm - an efficient approximation of LRU
- Thrashing - when the system spends more time swapping pages than running processes; caused by too many processes competing for too little RAM
- Working Set Model - keeping only the pages a process is actively using in RAM
- Copy-on-Write (COW) - parent and child share pages until one writes, then a copy is made
File Systems
Section titled “File Systems”The file system organises data on storage devices and presents it as files and directories.
- File abstraction - name, type, size, permissions, timestamps, and data
- File operations: create, read, write, seek, delete, append
- Directory structure: flat, hierarchical, acyclic graph, and general graph
- File system implementation:
- Inodes - data structures that store file metadata (Linux/Unix)
- Allocation methods: contiguous, linked (FAT), indexed (inodes)
- Free space management: bitmap, linked list, grouping
- Common file systems: FAT32, NTFS, ext4, APFS, Btrfs, ZFS
- Journaling - recording changes before applying them to recover from crashes
- Mounting - attaching a file system to a directory in the global hierarchy
- Virtual File System (VFS) - an abstraction layer that lets the OS support multiple file systems
- Links: hard links (point to the same inode) and symbolic links (point to a path)
I/O Management
Section titled “I/O Management”The OS must efficiently handle input and output from many different devices.
- I/O hardware: controllers, ports, registers, and buses
- Polling vs Interrupts - the CPU checks continuously vs the device notifies the CPU when ready
- Direct Memory Access (DMA) - the device transfers data to RAM without CPU involvement
- Device drivers - software that translates OS commands into device-specific instructions
- Buffering - storing data in memory temporarily to smooth out speed differences between devices and the CPU
- Spooling - queuing output for a slow device (e.g., a printer) so the CPU can continue other work
- I/O scheduling: Disk scheduling algorithms (FCFS, SSTF, SCAN, C-SCAN, LOOK) for minimising seek time
Inter-Process Communication (IPC)
Section titled “Inter-Process Communication (IPC)”Processes often need to share data or coordinate with each other. IPC provides the mechanisms to do this.
- Shared Memory - two processes map the same region of RAM; the fastest form of IPC
- Message Passing - processes send and receive messages through the OS; simpler but slower
- Pipes - unidirectional data channels; anonymous pipes for related processes, named pipes (FIFOs) for unrelated ones
- Signals - asynchronous notifications sent to a process (e.g., SIGKILL, SIGTERM)
- Sockets - IPC over a network or locally; the same interface used for network communication
- Message Queues - a queue managed by the OS for passing messages between processes
System Calls
Section titled “System Calls”System calls are the interface between user programs and the OS kernel.
- User Mode vs Kernel Mode - the privilege levels that protect the OS from buggy or malicious programs
- System call interface - how a user program requests an OS service (traps / software interrupts)
- Categories of system calls:
- Process control:
fork,exec,exit,wait - File management:
open,read,write,close,stat - Device management:
ioctl,read,write - Information maintenance:
getpid,alarm,sleep - Communication:
pipe,socket,send,recv
- Process control:
- System call overhead - why system calls are more expensive than regular function calls
- Library wrappers - how standard library functions like
printfandmallocwrap system calls
Deadlocks
Section titled “Deadlocks”A deadlock occurs when a set of processes are each waiting for a resource held by another, and none can proceed.
- Four necessary conditions (Coffman conditions):
- Mutual Exclusion - only one process can use a resource at a time
- Hold and Wait - a process holds a resource and waits for another
- No Preemption - resources cannot be forcibly taken from a process
- Circular Wait - a cycle exists in the resource-wait graph
- Prevention - eliminate one of the four conditions (e.g., force processes to request all resources at once)
- Avoidance - use the Banker’s Algorithm to only grant requests that leave the system in a safe state
- Detection and Recovery - allow deadlocks to occur, detect them, and recover by terminating or preempting processes
- Deadlock vs Livelock vs Starvation - understanding the differences between these concurrency problems
Virtualisation and Containers
Section titled “Virtualisation and Containers”Modern infrastructure relies heavily on virtualisation.
- Hypervisors - software that creates and manages virtual machines (Type 1: bare-metal, Type 2: hosted)
- Virtual Machines (VMs) - full OS instances running on virtualised hardware
- Containers - lightweight alternatives to VMs; share the host OS kernel but isolate processes with namespaces and cgroups
- Namespaces - isolating process IDs, network, file system, and users between containers
- cgroups - limiting and accounting for CPU, memory, and I/O usage of process groups
- Docker and container runtimes - how containers are built, run, and managed in practice
Operating systems are complex, but they follow consistent principles. Once you understand how processes, memory, and the file system work, the rest of OS theory clicks into place naturally.