Operating Systems
Processes, scheduling, memory, deadlocks
0 / 11 revised
Process vs Thread ๐ฏ
OS
โพ
Quick Recall
Process = isolated memory + PCB; Thread = shared memory within process, lighter
| Process | Thread | |
|---|---|---|
| Memory | Separate address space | Shared address space |
| Creation | Heavy (fork) | Light |
| Communication | IPC (pipes, sockets) | Shared memory directly |
| Crash impact | Isolated | Can crash entire process |
Process states: New โ Ready โ Running โ (Waiting) โ Terminated. Transitions via scheduler dispatch, I/O wait, interrupt.
CPU Scheduling ๐ฏ
OS
โพ
Quick Recall
FCFS=simple; SJF=optimal avg wait; RR=fair with quantum; SRTF=preemptive SJF
| Algorithm | Preemptive? | Starvation? | Metric |
|---|---|---|---|
| FCFS | No | No | Simple but convoy effect |
| SJF | No | Yes (long jobs) | Optimal avg waiting time |
| SRTF | Yes | Yes | Preemptive SJF |
| Priority | Both | Yes (low priority) | Fix with aging |
| Round Robin | Yes | No | Fair; quantum matters |
Avg Waiting = ฮฃ(start - arrival) / n
Avg Turnaround = ฮฃ(completion - arrival) / n
Context Switching
OS
โพ
Quick Recall
Save PCB of current process, load PCB of next โ pure overhead
What happens: Save registers/PC/state of current process to its PCB โ load saved state of next process from its PCB โ flush TLB/caches if needed.
Cost: Pure overhead (no useful work). Typically microseconds but adds up. Threads within same process are cheaper to switch (shared address space, no TLB flush).
Cost: Pure overhead (no useful work). Typically microseconds but adds up. Threads within same process are cheaper to switch (shared address space, no TLB flush).
Inter-Process Communication
OS
โพ
Quick Recall
Pipes (unidirectional), shared memory (fast, sync needed), message passing (safe)
| Method | Type | Notes |
|---|---|---|
| Pipe | Unidirectional stream | Parent-child; named pipes for unrelated |
| Shared Memory | Fastest | Needs explicit synchronization |
| Message Passing | send()/receive() | No shared state; easier to reason about |
| Sockets | Network-capable | TCP/UDP between processes |
| Signals | Async notification | Limited info (signal number only) |
Synchronization ๐ฏ
OS
โพ
Quick Recall
Critical section: mutex (1 thread) or semaphore (N threads); avoid race conditions
Race condition: Output depends on execution order of concurrent processes.
Critical section requirements: Mutual exclusion, Progress, Bounded waiting.
Classic problems:
Critical section requirements: Mutual exclusion, Progress, Bounded waiting.
| Mutex | Semaphore | |
|---|---|---|
| Type | Binary lock | Counting (0 to N) |
| Ownership | Lock owner unlocks | Any thread can signal |
| Use | Exclusive access | Resource pool / signaling |
- Producer-Consumer: Bounded buffer + empty/full semaphores + mutex
- Reader-Writer: Multiple readers OR one writer; use read_count + mutex
- Dining Philosophers: 5 forks, 5 philosophers; solutions: pick up both or limit to N-1 diners
Deadlock ๐ฏ
OS
โพ
Quick Recall
4 Coffman conditions (ALL must hold): Mutual exclusion, Hold & wait, No preemption, Circular wait
Coffman conditions (all 4 needed):
Banker's algo intuition: Before granting, simulate: if system can still reach a state where all processes finish โ safe. Otherwise deny.
- Mutual Exclusion โ resource not shareable
- Hold & Wait โ holding one, waiting for another
- No Preemption โ can't forcibly take resource
- Circular Wait โ circular chain of waiting
| Strategy | Method |
|---|---|
| Prevention | Break one condition (e.g., order resources โ no circular wait) |
| Avoidance | Banker's algorithm โ grant only if safe state |
| Detection | Wait-for graph; detect cycle โ abort one process |
| Recovery | Kill process or preempt resources |
Memory Management ๐ฏ
OS
โพ
Quick Recall
Contiguous โ fragmentation; Paging โ fixed frames; Segmentation โ logical divisions
| Method | Unit | Fragmentation |
|---|---|---|
| Contiguous | Variable partitions | External |
| Paging | Fixed-size pages/frames | Internal (last page) |
| Segmentation | Variable logical segments | External |
Segmentation: Divides by logical units (code, stack, heap). Each segment has base + limit.
Virtual Memory & Page Replacement ๐ฏ
OS
โพ
Quick Recall
Demand paging: load pages on fault; LRU โ optimal in practice; Belady's anomaly = more frames โ more faults (FIFO only)
Demand Paging: Load page into memory only when accessed. Page fault โ OS fetches from disk.
Page Replacement Algorithms:
Worked example (3 frames, FIFO):
Pages: 7 0 1 2 0 3 0 4
Faults: 7(F) 0(F) 1(F) 2(F,evict7) 0(H) 3(F,evict0) 0(F,evict1) 4(F,evict2) = 7 faults
Belady's Anomaly: FIFO with 4 frames can have MORE faults than 3 frames for certain sequences.
Page Replacement Algorithms:
| Algorithm | Strategy | Belady's? |
|---|---|---|
| FIFO | Replace oldest page | Yes (anomaly possible) |
| LRU | Replace least recently used | No (stack algorithm) |
| Optimal | Replace page not used for longest | No (theoretical best) |
Pages: 7 0 1 2 0 3 0 4
Faults: 7(F) 0(F) 1(F) 2(F,evict7) 0(H) 3(F,evict0) 0(F,evict1) 4(F,evict2) = 7 faults
Belady's Anomaly: FIFO with 4 frames can have MORE faults than 3 frames for certain sequences.
Thrashing
OS
โพ
Quick Recall
CPU busy page-faulting, not doing useful work; fix with working set model or reduce multiprogramming
Cause: Too many processes โ not enough frames each โ constant page faults โ disk I/O dominates โ CPU utilization drops โ OS adds more processes (making it worse).
Detection: High page-fault rate + low CPU utilization.
Solutions:
Detection: High page-fault rate + low CPU utilization.
Solutions:
- Working Set Model: allocate frames โฅ process's working set size
- Page Fault Frequency: if rate too high, allocate more frames; if too low, reclaim frames
- Reduce degree of multiprogramming (suspend processes)
Disk Scheduling
OS
โพ
Quick Recall
SSTF=greedy nearest; SCAN=elevator; C-SCAN=uniform wait
Given: Head at 53, queue: 98 183 37 122 14 124 65 67
| Algorithm | Strategy | Total head movement |
|---|---|---|
| FCFS | Serve in order | 640 |
| SSTF | Nearest request first | 236 (starvation possible) |
| SCAN (elevator) | Move one direction, serve all, reverse | Moderate, no starvation |
| C-SCAN | SCAN but jump to start after reaching end | More uniform wait times |
File Systems
OS
โพ
Quick Recall
Directory = tree of files; inode stores metadata; allocation: contiguous, linked, indexed
File allocation:
Directory structures: Single-level โ Two-level โ Tree โ Acyclic graph.
Inode: Stores file metadata (size, permissions, timestamps, pointers to data blocks).
| Method | Pros | Cons |
|---|---|---|
| Contiguous | Fast sequential + random access | External fragmentation, hard to grow |
| Linked | No fragmentation, easy grow | No random access, pointer overhead |
| Indexed | Random access, no fragmentation | Index block overhead |
Inode: Stores file metadata (size, permissions, timestamps, pointers to data blocks).