Operating Systems β Complete Guide for GATE & Interviews
Before you start: basic programming concepts (what a program is, what memory is) are assumed β no prior OS-specific knowledge is needed.
Operating Systems is one of the highest-weightage subjects in GATE CSE and a core topic in every software engineering interview. This covers everything from processes to memory management to file systems.
Why This Exists (The Hook)
Your laptop runs dozens of programs at once, on a CPU that can genuinely execute only one instruction stream per core at any given instant β yet nothing feels like it's waiting in line. The OS exists to maintain that illusion convincingly: deciding which process gets the CPU next (scheduling), pretending every process has its own private memory when RAM is actually shared (virtual memory), and mediating access to shared resources like disks and network so programs don't corrupt each other's data. Every concept in this guide is really answering one question from a different angle: how does the OS make limited, shared hardware look like unlimited, private hardware to every program running on it?
Analogy β Think of an OS like an air traffic controller for a single busy runway, not a series of separate lanes. Only one plane can physically land or take off at any given instant (the CPU can only run one process's instructions right now), but the controller (the scheduler) switches between planes so fast and so fairly that from the ground, dozens of flights appear to be handled simultaneously. Round Robin scheduling is a controller giving each plane a fixed, short slot before moving to the next; priority scheduling is letting an emergency landing jump the queue β same runway, different rules for who goes next.
Try it (2 minutes) β Reason through why increasing the number of page frames can sometimes INCREASE page faults under FIFO replacement (Belady's Anomaly), without looking anything up: FIFO replaces whichever page has been in memory longest, regardless of whether it's about to be used again. If a program's access pattern happens to align badly with "oldest first," why wouldn't simply adding more memory (more frames) guarantee fewer faults β and what does that tell you about why LRU (which tracks actual usage recency, not just arrival order) doesn't suffer from this same anomaly?
What is an OS?
An OS is the software layer between hardware and user applications. It manages:
β’CPU time β which process runs when
β’Memory β who gets which RAM address
β’I/O devices β disk, network, keyboard
β’Files β how data is stored and accessed
β’Security β who can do what
User Applications
βββββββββββββββββ
System Calls (API to OS)
βββββββββββββββββ
OS Kernel
βββββββββββββββββ
Hardware (CPU, RAM, Disk, Network)
Processes & Threads
Process States
β
Ready
Waiting for CPU, in ready queue
β
Running
Currently executing on CPU
β
Waiting
Blocked for I/O or event
β
Terminated
Execution complete
New β Ready β Running β Waiting β Terminated
β β
Preempted I/O done
New: Process being created
Ready: Waiting for CPU (in ready queue)
Running: Currently executing on CPU
Waiting: Waiting for I/O or event (blocked)
Terminated: Execution complete
PCB (Process Control Block) contains:
PID, state, program counter, CPU registers,
memory limits, open files, I/O status
Process Scheduling
Scheduling criteria:
CPU Utilization: Keep CPU busy (maximize)
Throughput: Processes/second (maximize)
Turnaround Time: Submission β completion (minimize)
Waiting Time: Time in ready queue (minimize)
Response Time: First response time (minimize)
Key formulas:
Turnaround = Completion - Arrival
Waiting = Turnaround - Burst
Response = First CPU - Arrival
Scheduling Algorithms
1. FCFS (First Come First Serve)
Non-preemptive, simple, convoy effect
Example: P1(24ms) P2(3ms) P3(3ms) all arrive at 0
Order: P1βP2βP3
Waiting: P1=0, P2=24, P3=27 Avg=17ms β POOR
2. SJF (Shortest Job First) β Non-preemptive
Optimal average waiting time, needs future burst knowledge
Same example: P2(3)βP3(3)βP1(24)
Waiting: P2=0, P3=3, P1=6 Avg=3ms β OPTIMAL
3. SRTF (Shortest Remaining Time First) β Preemptive SJF
Preempts current process if new shorter job arrives
Starvation possible for long processes
4. Round Robin (RR) β Preemptive, time quantum q
Each process gets q ms on CPU, then back to queue
Context switch overhead: smaller q = more overhead
q too large = degenerates to FCFS
q = 20ms typical for interactive systems
5. Priority Scheduling
Lower number = higher priority (or vice versa)
Problem: starvation of low-priority processes
Solution: Aging β gradually increase priority of waiting processes
6. MLFQ (Multi-Level Feedback Queue)
Multiple queues with different priorities
Process moves to lower priority if uses full quantum
Process moves up if waits too long (aging)
Used by: Windows, Unix/Linux
Threads
Thread vs Process:
Process: independent, own memory space, expensive to create/switch
Thread: within process, shared memory, lightweight
Types:
User-level threads: managed by user library, kernel sees 1 thread
Kernel-level threads: OS manages, expensive but parallel on multi-core
POSIX Threads (pthreads):
pthread_create()
pthread_join()
pthread_mutex_lock/unlock()
Memory Management
Logical vs Physical Address
Logical (virtual) address: Generated by CPU during execution
Physical address: Actual RAM location
MMU (Memory Management Unit) translates logical β physical
Relocation register: physical = logical + base register
Paging
Page: Fixed-size logical memory block (typically 4KB)
Frame: Fixed-size physical memory block (same size as page)
Page Table: Logical page β physical frame mapping
Each process has its own page table
Page table entry contains: frame number + valid bit + dirty bit
Address translation:
Logical address = page number + offset
Physical address = frame number + offset (offset unchanged)
Multi-level paging: For large address space, page table is too large
2-level: page directory β page table β frame
Used by: x86 (32-bit uses 2-level, 64-bit uses 4-level)
TLB (Translation Lookaside Buffer):
Cache for page table entries
Hit: O(1) translation, Miss: O(n) page table walk
Hit ratio h, TLB time t, Memory time m:
EAT = h(t+m) + (1-h)(t+2m)
Page Replacement Algorithms
Need a page replacement when all frames full and new page needed
FIFO: Replace oldest page
Belady's Anomaly: More frames can cause MORE page faults
OPT (Optimal): Replace page not used for longest time
Not implementable (needs future knowledge)
Used as benchmark
LRU (Least Recently Used): Replace least recently used
Closest approximation to OPT
Implementation: counters or stack
No Belady's anomaly
Clock Algorithm (Second Chance): Approximates LRU
Use bit per page, circular scan
If use bit=1, clear it and move on
If use bit=0, replace it
Thrashing: Process spends more time paging than executing
Cause: Too many processes, not enough frames
Solution: Working Set Model, reduce degree of multiprogramming
Segmentation
Variable-sized logical segments (code, data, stack, heap)
Segment table: base + limit for each segment
Segment + offset β physical address (if offset < limit)
External fragmentation problem (unlike paging)
Deadlock
4 necessary conditions (ALL must hold for deadlock):
1. Mutual Exclusion: At least one non-sharable resource
2. Hold and Wait: Process holds resources while waiting for more
3. No Preemption: Resources can't be forcibly taken
4. Circular Wait: P1 waits for P2, P2 waits for P3, P3 waits for P1
Resource Allocation Graph (RAG):
Process node (circle) β Resource node (square): request edge
Resource node (square) β Process node (circle): assignment edge
Cycle in RAG = deadlock (if single instance per resource)
Cycle β deadlock if multiple instances (need to check fully)
Deadlock Handling:
Prevention: Negate one of the 4 conditions (restrictive)
β No Hold & Wait: Request all resources at start
β Allow Preemption: Take resources from waiting processes
β No Circular Wait: Order resources, request in order
Avoidance: Banker's Algorithm
Safe state: all processes can complete in some order
Unsafe state: may lead to deadlock (not guaranteed)
Algorithm checks if granting request keeps system safe
Detection: Allow deadlock, detect using RAG reduction
Recovery: Kill process or preempt resources
GATE favourite: Banker's Algorithm examples
n processes, m resource types
Need matrix, Available vector, Allocation matrix
Find safe sequence
File Systems
File attributes: name, type, size, location, owner, timestamps
Directory structures:
Single-level: all files in one directory
Two-level: one per user
Tree: hierarchical (most common)
DAG: allows sharing (links)
File Allocation Methods:
Contiguous: fast access, external fragmentation
Linked: no fragmentation, slow random access
Indexed: index block, supports random access
FAT (File Allocation Table): Linked with in-memory table
Unix inode:
Inode = 12 direct + 1 single indirect + 1 double + 1 triple indirect
Single indirect = 1024 block pointers (with 1KB blocks)
Double indirect = 1024Β² block pointers
Synchronization
Race condition: Multiple processes access shared data concurrently
Critical section: Code that accesses shared resource
Requirements for solution:
1. Mutual exclusion: Only one process in CS at a time
2. Progress: If no process in CS, one wanting to enter should
3. Bounded waiting: Bound on how long a process waits
Peterson's Solution (2 processes):
flag[i] = true; turn = j;
while(flag[j] && turn == j); // wait
// critical section
flag[i] = false;
Semaphore:
wait(S): S--; if S<0, block
signal(S): S++; if Sβ€0, wake one
Binary semaphore (mutex): 0 or 1
Counting semaphore: any non-negative integer
Classic problems:
Producer-Consumer: buffer, mutex + empty + full semaphores
Readers-Writers: prefer readers or writers
Dining Philosophers: prevent deadlock in resource sharing
Monitor: High-level synchronization construct (Java synchronized)
Mutual exclusion built-in
Condition variables: wait() and signal()
I/O and Disk Scheduling
Disk scheduling (for hard drives, less relevant for SSDs):
FCFS: Serve in order of requests
SSTF (Shortest Seek Time First): Go to nearest cylinder
Starvation possible for far requests
SCAN (Elevator): Move in one direction, reverse at end
C-SCAN: Move one direction only, jump back (no serve on return)
C-LOOK: Like C-SCAN but only go to last request each direction
GATE often gives: track requests and asks for total head movement