OS Notes
Comprehensive OS interview questions covering processes, threads, scheduling, memory management, deadlocks, synchronization, and system design — with detailed answers for technical interviews.
Introduction
Operating Systems questions appear in virtually every systems-level technical interview. Whether you are interviewing for a software engineering role at a tech company, a DevOps position, or a systems programming job, you need solid OS fundamentals. This page covers the most commonly asked questions across all OS topics, with answers that demonstrate both conceptual understanding and practical knowledge.
Process and Thread Questions
Q1: What is the difference between a process and a thread?
Answer: A process is an independent execution unit with its own address space, code, data, and system resources. A thread is a lightweight execution unit within a process that shares the process's address space and resources but has its own stack, program counter, and register set.
Key differences:
- Process creation is expensive (fork copies address space); thread creation is cheap
- Processes communicate via IPC (pipes, sockets, shared memory); threads communicate by reading/writing shared variables directly
- A crash in one process does not affect others; a crash in one thread can bring down the entire process
- Context switching between processes is expensive (TLB flush, cache invalidation); between threads of the same process it is cheaper
Q2: What happens when you call fork()?
Answer: fork() creates a new child process that is nearly identical to the parent. The child gets a copy of the parent's address space, file descriptors, and register state. After fork(), both parent and child continue execution from the same point — the return value distinguishes them (parent gets child's PID, child gets 0).
Modern systems use Copy-on-Write (COW): physical pages are not actually copied at fork time. Both processes share the same physical pages marked read-only. Only when one process writes to a page does the OS create a private copy. This makes fork() fast even for processes with large address spaces.
Q3: Explain the process state transitions.
Answer:
- New → Ready: Process created and admitted to ready queue
- Ready → Running: Scheduler dispatches process to CPU
- Running → Waiting: Process issues I/O request or waits on event
- Waiting → Ready: I/O completes or event occurs
- Running → Ready: Preemption (time quantum expired or higher priority arrives)
- Running → Terminated: Process completes execution or is killed
Memory Management Questions
Q4: What is virtual memory and why is it needed?
Answer: Virtual memory gives each process the illusion of a large, contiguous address space, regardless of how much physical RAM is available. It works by keeping only actively used pages in RAM and storing the rest on disk (swap space).
Benefits:
- Processes can be larger than physical memory
- Multiple large processes can run simultaneously
- Memory protection — each process has its own address space
- Simplified memory allocation — no need for contiguous physical memory
- Shared libraries use physical memory only once despite being mapped into multiple processes
Q5: What is a page fault? Walk through what happens.
Answer: A page fault occurs when a process accesses a page that is not currently in physical memory.
Steps:
- CPU generates a logical address
- MMU looks up page table — finds the valid bit is 0 (page not in RAM)
- MMU raises a page fault trap (interrupt to OS)
- OS checks if the access is legal (within process's address space)
- OS finds the page on disk (swap space or file system)
- OS selects a free frame (or uses page replacement if memory is full)
- OS initiates disk I/O to load the page into the selected frame
- When I/O completes, OS updates the page table (valid bit = 1, frame number set)
- OS restarts the instruction that caused the fault
The process does not "know" a page fault happened — it appears as if the page was always there.
Deadlock Questions
Q6: What are the four necessary conditions for deadlock?
Answer: All four must hold simultaneously for deadlock to occur:
- Mutual Exclusion: At least one resource is non-shareable (only one process can use it at a time)
- Hold and Wait: A process holds at least one resource while waiting for additional resources held by other processes
- No Preemption: Resources cannot be forcibly taken — they must be voluntarily released
- Circular Wait: A circular chain exists where each process waits for a resource held by the next process in the chain
Breaking ANY one condition prevents deadlock.
Q7: How does the Banker's Algorithm work?
Answer: The Banker's Algorithm is a deadlock avoidance strategy. Before granting a resource request, the OS simulates the allocation and checks if the resulting state is "safe."
A state is safe if there exists at least one sequence in which all processes can complete (each process can eventually obtain its maximum needed resources, execute, and release them).
Algorithm:
- When a process requests resources, pretend to grant the request
- Calculate Need = Max - Allocation for all processes
- Find a process whose Need ≤ Available (can complete)
- Pretend that process finishes — add its Allocation to Available
- Repeat until all processes can finish (safe) or no process can proceed (unsafe)
- If safe, grant the request; if unsafe, make the process wait
Synchronization Questions
Q8: What is a race condition? Give an example.
Answer: A race condition occurs when multiple processes/threads access shared data concurrently and the outcome depends on the specific execution order.
Example: Two threads incrementing a shared counter:
// Thread A // Thread B
load counter (value: 5) load counter (value: 5)
increment (value: 6) increment (value: 6)
store counter (value: 6) store counter (value: 6)Expected result: 7. Actual result: 6. One increment is lost because both threads read the old value before either writes the new one.
Solution: Use a mutex/lock around the increment operation to ensure atomicity.
Q9: Explain the difference between a mutex and a semaphore.
Answer:
- Mutex (Mutual Exclusion): A binary lock with ownership. Only the thread that acquired the mutex can release it. Used for protecting critical sections. Think of it as a key — you must have the key to unlock the door.
- Semaphore: A signaling mechanism with a counter. Any thread can signal (increment) — not just the one that waited. Used for resource counting and signaling between threads. Think of it as a parking lot counter — shows how many spots are available.
Key distinction: Mutex = ownership + locking. Semaphore = signaling + counting. Using a binary semaphore as a mutex is possible but loses the ownership property (dangerous — any thread can accidentally signal).
Q10: How would you solve the Producer-Consumer problem?
Answer:
semaphore mutex = 1; // protects buffer access
semaphore empty = N; // counts empty slots (initially N)
semaphore full = 0; // counts full slots (initially 0)
// Producer
while (true) {
item = produce_item();
wait(empty); // wait for empty slot
wait(mutex); // enter critical section
add_to_buffer(item);
signal(mutex); // leave critical section
signal(full); // signal that buffer has item
}
// Consumer
while (true) {
wait(full); // wait for available item
wait(mutex); // enter critical section
item = remove_from_buffer();
signal(mutex); // leave critical section
signal(empty); // signal that slot is free
consume_item(item);
}Important: The order of wait operations matters. If you swap wait(empty) and wait(mutex) in the producer, deadlock is possible when the buffer is full.
System Design Questions
Q11: How does a context switch work internally?
Answer: When the OS decides to switch from Process A to Process B:
- Save Process A's registers (PC, SP, general-purpose) to its PCB
- Save Process A's memory state (page table base register)
- Update Process A's state from "Running" to "Ready" or "Waiting"
- Select Process B from the ready queue (scheduling decision)
- Load Process B's memory state (change page table base → TLB flush)
- Load Process B's registers from its PCB
- Jump to Process B's saved program counter
Overhead: Typically 1-10 microseconds. Includes direct costs (register save/restore) and indirect costs (TLB misses, cache cold start).
Q12: What is the difference between user mode and kernel mode?
Answer: The CPU operates in two privilege levels:
- User mode: Restricted. Cannot execute privileged instructions (I/O, interrupt control, memory management), cannot access kernel memory. Normal applications run here.
- Kernel mode: Unrestricted. Can execute any instruction, access all memory, control hardware. OS kernel runs here.
Transition from user to kernel mode happens via system calls (software interrupt), hardware interrupts, or exceptions. The mode bit in the CPU status register tracks the current mode. This separation prevents buggy or malicious user programs from crashing the entire system.
Tips for OS Interviews
- Use analogies — make abstract concepts tangible for the interviewer
- Mention trade-offs — every OS design decision has trade-offs; showing awareness impresses
- Draw diagrams — process states, address translation, resource allocation graphs
- Know Linux — interviewers assume Linux; know fork, exec, wait, pthread_create
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Operating System Interview Questions — Operating Systems.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Operating Systems topic.
Search Terms
operating-systems, operating systems, operating, systems, interview, preparation, system, questions
Related Operating Systems Topics