Operating Systems Cheatsheet

Quick reference for OS concepts — scheduling algorithms, memory, and deadlock conditions.

ProcessesThreadsMemoryScheduling
NotesCheatsheet

Processes vs Threads

AspectProcessThread
MemorySeparate address spaceShared heap, own stack
CreationExpensive (fork)Cheap
CommunicationIPC (pipes, sockets)Shared memory (needs mutex)
Crash isolationCrash doesn't affect siblingsOne crash kills whole process

Process States

New → Ready → Running → Terminated
              ↑    ↓
              Waiting (Blocked) ← I/O / sleep / event

// New:      being created
// Ready:    in run queue, waiting for CPU
// Running:  executing on CPU right now
// Waiting:  blocked on I/O, lock, or event — CPU freed
// Terminated: finished or killed

CPU Scheduling Algorithms

AlgorithmPreemptive?Key propertyWeakness
FCFSNoArrival orderConvoy effect
SJFNo (SRTF = yes)Optimal avg wait timeStarvation, burst time unknown
Round RobinYesFair; good response timeHigh overhead if quantum too small
PriorityBoth variantsImportant tasks firstStarvation (fix: aging)
MLFQYesAdapts to behaviourComplex to tune; real OS standard

Memory — Virtual Memory & Paging

Virtual address → [Page Table] → Physical address (frame)

Page size: typically 4 KB
Page fault: page not in RAM → OS loads it from disk (swap)

TLB (Translation Lookaside Buffer): hardware cache of recent VA→PA
  translations; makes most address lookups O(1)

// Page Replacement (eviction when RAM full):
FIFO       — evict oldest loaded page; Bélády's anomaly possible
LRU        — evict least recently used; good in practice
Clock      — approximates LRU using reference bits; used in real OS
Optimal    — theoretical best; evict page used furthest in future

Synchronisation Primitives

// Mutex — one thread at a time in critical section
mutex.lock()
// critical section
counter++
mutex.unlock()

// Semaphore(n) — allows up to n threads concurrently
sem = Semaphore(10)   // e.g. DB connection pool
sem.wait()            // acquire (block if 0)
// use resource
sem.signal()          // release (wake waiter)

// Race condition: two threads read→increment→write same counter
// Without lock: both read 0, both write 1 → lost update

Deadlock — 4 Conditions (ALL must hold)

ConditionMeaningBreak it by…
Mutual ExclusionResource not shareableMake resource shareable (e.g. read locks)
Hold & WaitHolds resource, waits for moreRequest all resources at once
No PreemptionResources released voluntarily onlyAllow OS to forcibly reclaim
Circular WaitP1 waits for P2, P2 waits for P1Always acquire locks in the same order

File System Concepts

ConceptDescription
inodeFile metadata (permissions, size, timestamps, data block pointers); not the filename
Hard linkAnother directory entry → same inode; deleting one doesn't remove data
Soft linkFile containing path to another file; breaks if target is deleted
JournalingLog writes before executing; enables recovery after crash (ext4, NTFS)

Key Rules

  • Process = isolation. Thread = shared memory + cheap creation, but needs synchronisation.
  • Always acquire locks in a consistent global order to prevent circular wait (deadlock).
  • Page fault = not in RAM, must load from disk — expensive (~ms). TLB miss = must walk page table (~100ns).
  • LRU is the gold standard for page replacement; Clock algorithm is the practical approximation.
  • MLFQ is the basis of most real OS schedulers (Linux CFS is a variant).