Operating Systems
Processes, memory, scheduling, deadlocks
Operating Systems (OS) are system software that manages computer hardware and software resources, providing common services for computer programs. Core topics include process management, memory management, file systems, I/O management, and CPU scheduling. Understanding these concepts is essential for system programming, performance optimisation, and technical interviews.
Complexity / Key Facts
Key Concepts
Process vs Thread
A process is an independent program in execution with its own address space, resources, and PCB (Process Control Block). A thread is a lightweight unit of execution within a process, sharing the same address space and resources but having its own stack, registers, and program counter. Processes are isolated; threads within the same process communicate easily but can cause race conditions. Context switching between threads is faster than between processes.
CPU Scheduling Algorithms
First-Come-First-Served (FCFS): Non-preemptive, simple, may cause convoy effect. Shortest Job First (SJF): Optimal for minimum waiting time, requires prediction of burst times. Priority Scheduling: Can be preemptive or non-preemptive, may suffer from starvation. Round Robin (RR): Time quantum-based, fair, good for time-sharing systems. Multilevel Queue: Processes permanently assigned to queues. Multilevel Feedback Queue: Allows processes to move between queues based on behaviour.
Memory Management
Contiguous Allocation: Memory divided into partitions (fixed or variable size). External fragmentation occurs in variable partitioning. Paging: Divides logical memory into fixed-size pages and physical memory into frames. Eliminates external fragmentation, may have internal fragmentation. Segmentation: Divides memory into variable-sized segments based on program structure (code, data, stack). Virtual Memory: Allows execution of processes not entirely in memory using demand paging. Page replacement algorithms: FIFO, Optimal, LRU, LRU Approximation (Clock), Second Chance.
Deadlock
Deadlock occurs when processes wait indefinitely for resources held by each other. Four necessary conditions: Mutual Exclusion (resources cannot be shared), Hold and Wait (process holds resources while waiting for more), No Preemption (resources cannot be forcibly taken), Circular Wait (circular chain of processes waiting). Deadlock Handling: Prevention (break one condition), Avoidance (Banker's Algorithm), Detection and Recovery, Ignorance (Ostrich Algorithm). Banker's Algorithm checks if system is in safe state before granting resource requests.
Synchronisation Mechanisms
Race Condition: Multiple processes access shared data concurrently with outcome dependent on execution order. Critical Section: Code segment accessing shared resources; requires mutual exclusion. Solutions: Peterson's Solution (software), Mutex Locks (binary semaphore), Semaphores (counting and binary), Monitors (high-level abstraction). Classical Problems: Producer-Consumer (Bounded Buffer), Readers-Writers, Dining Philosophers. Deadlock can occur in Dining Philosophers if all pick up left fork simultaneously.
File System & I/O
File Allocation Methods: Contiguous (fast access, external fragmentation), Linked (no fragmentation, slow random access), Indexed (direct access, pointer overhead). Directory Structure: Single-level, Two-level, Tree-structured, Acyclic Graph (shared files), General Graph (allows cycles, requires garbage collection). Disk Scheduling: FCFS, SSTF (shortest seek time first), SCAN (elevator), C-SCAN, LOOK, C-LOOK. I/O Techniques: Programmed I/O, Interrupt-Driven I/O, DMA (Direct Memory Access) for large transfers without CPU intervention.
Tips
- For CPU scheduling problems, always draw a Gantt chart to visualise process execution order and calculate completion times accurately.
- Remember the key differences: Paging uses fixed-size blocks (eliminates external fragmentation), Segmentation uses variable-size blocks (matches program structure).
- In deadlock questions, check for ALL four necessary conditions. Breaking any one condition prevents deadlock.
- For page replacement algorithms, track frames visually. LRU approximates Optimal; FIFO suffers from Belady's anomaly.
- Banker's Algorithm: Always calculate 'Need' matrix first, then find a process that can execute with current Available resources.
- For disk scheduling, SCAN (elevator) and C-SCAN provide more uniform wait times than SSTF which may cause starvation.
Practice with questions
Real placement-style technical questions.