0% revised

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
ProcessThread
MemorySeparate address spaceShared address space
CreationHeavy (fork)Light
CommunicationIPC (pipes, sockets)Shared memory directly
Crash impactIsolatedCan crash entire process
PCB contains: PID, state, PC, registers, memory limits, open files, scheduling info.
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
AlgorithmPreemptive?Starvation?Metric
FCFSNoNoSimple but convoy effect
SJFNoYes (long jobs)Optimal avg waiting time
SRTFYesYesPreemptive SJF
PriorityBothYes (low priority)Fix with aging
Round RobinYesNoFair; quantum matters
Gantt example (RR, q=2): P1(0-2), P2(2-4), P3(4-6), P1(6-7)
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).
Inter-Process Communication
OS
โ–พ
Quick Recall
Pipes (unidirectional), shared memory (fast, sync needed), message passing (safe)
MethodTypeNotes
PipeUnidirectional streamParent-child; named pipes for unrelated
Shared MemoryFastestNeeds explicit synchronization
Message Passingsend()/receive()No shared state; easier to reason about
SocketsNetwork-capableTCP/UDP between processes
SignalsAsync notificationLimited 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.
MutexSemaphore
TypeBinary lockCounting (0 to N)
OwnershipLock owner unlocksAny thread can signal
UseExclusive accessResource pool / signaling
Classic problems:
  • 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):
  1. Mutual Exclusion โ€” resource not shareable
  2. Hold & Wait โ€” holding one, waiting for another
  3. No Preemption โ€” can't forcibly take resource
  4. Circular Wait โ€” circular chain of waiting
Handling:
StrategyMethod
PreventionBreak one condition (e.g., order resources โ†’ no circular wait)
AvoidanceBanker's algorithm โ€” grant only if safe state
DetectionWait-for graph; detect cycle โ†’ abort one process
RecoveryKill process or preempt resources
Banker's algo intuition: Before granting, simulate: if system can still reach a state where all processes finish โ†’ safe. Otherwise deny.
Memory Management ๐ŸŽฏ
OS
โ–พ
Quick Recall
Contiguous โ†’ fragmentation; Paging โ†’ fixed frames; Segmentation โ†’ logical divisions
MethodUnitFragmentation
ContiguousVariable partitionsExternal
PagingFixed-size pages/framesInternal (last page)
SegmentationVariable logical segmentsExternal
Paging: Logical address = [page number | offset]. Page table maps page โ†’ frame. TLB caches recent translations.
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:
AlgorithmStrategyBelady's?
FIFOReplace oldest pageYes (anomaly possible)
LRUReplace least recently usedNo (stack algorithm)
OptimalReplace page not used for longestNo (theoretical best)
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.
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:
  • 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
AlgorithmStrategyTotal head movement
FCFSServe in order640
SSTFNearest request first236 (starvation possible)
SCAN (elevator)Move one direction, serve all, reverseModerate, no starvation
C-SCANSCAN but jump to start after reaching endMore uniform wait times
File Systems
OS
โ–พ
Quick Recall
Directory = tree of files; inode stores metadata; allocation: contiguous, linked, indexed
File allocation:
MethodProsCons
ContiguousFast sequential + random accessExternal fragmentation, hard to grow
LinkedNo fragmentation, easy growNo random access, pointer overhead
IndexedRandom access, no fragmentationIndex block overhead
Directory structures: Single-level โ†’ Two-level โ†’ Tree โ†’ Acyclic graph.
Inode: Stores file metadata (size, permissions, timestamps, pointers to data blocks).