What you'll learn
Quick Answer
Operating system interviews focus on processes and threads and how they differ, deadlock and its four necessary conditions, CPU scheduling algorithms, memory management including paging and virtual memory, and synchronisation with mutexes and semaphores. The questions asked most often are process versus thread, the deadlock conditions, and the difference between a mutex and a semaphore.
Processes and Threads
What is the difference between a process and a thread? Near-guaranteed, so answer it structurally.
A process is a program in execution with its own memory space. A thread is a unit of execution within a process, sharing that process's memory with its sibling threads.
PROCESS THREAD
Own address space Shares the process's memory
Expensive to create Cheap to create
Isolated — a crash is contained A crash can take down the process
IPC needed to communicate Communicates via shared memory
Context switch is costly Context switch is cheaperThe practical consequence worth adding: because threads share memory, they can corrupt each other's data, which is why synchronisation exists. Processes cannot, which is why browsers isolate tabs into separate processes.
What is a context switch? Saving the state of one process or thread and restoring another's so the CPU can switch between them. It has real cost — registers, program counter, memory maps — which is why excessive switching hurts performance.
What are the process states? New, ready, running, waiting or blocked, and terminated. A process moves to waiting when it needs I/O, and back to ready when that completes.
What is a zombie process? One that has finished but whose parent has not read its exit status, so its entry remains in the process table. An orphan is one whose parent died first; it is adopted by the init process.
Deadlock
What is a deadlock? Two or more processes each waiting for a resource the other holds, so none can proceed.
What are the four necessary conditions? All four must hold simultaneously — memorise them, because the follow-up is always "how do you prevent it?" and the answer is to break one.
- Mutual exclusion — at least one resource is non-shareable.
- Hold and wait — a process holds one resource while waiting for another.
- No preemption — resources cannot be forcibly taken away.
- Circular wait — a cycle exists in the chain of waiting.
How do you handle deadlock? Four strategies:
Prevention — break one condition by design. The most practical is eliminating circular wait by requiring all processes to acquire resources in a fixed global order.
Avoidance — allocate only when the system stays in a safe state. The Banker's algorithm does this, but requires knowing maximum resource needs in advance, which is rarely realistic.
Detection and recovery — allow deadlock, detect cycles in the wait-for graph, then abort or roll back a process. This is what databases do.
Ignore it — the ostrich algorithm. General-purpose operating systems largely do this, because deadlocks are rare and the prevention cost is high.
Deadlock vs starvation: in deadlock nobody progresses; in starvation a process is perpetually deprived while others proceed, usually because of priority scheduling. Ageing — gradually raising the priority of waiting processes — fixes starvation.
CPU Scheduling
Why schedule at all? To decide which ready process runs next, balancing CPU utilisation, throughput, turnaround time, waiting time and response time.
The algorithms to know:
- FCFS — first come, first served. Simple, non-preemptive, and suffers the convoy effect: one long process delays everything behind it.
- SJF — shortest job first. Optimal for average waiting time, but requires knowing burst times in advance and can starve long processes.
- SRTF — the preemptive version of SJF.
- Round Robin — each process gets a fixed time quantum. Fair and responsive, which is why interactive systems use it. Quantum size matters: too large degenerates to FCFS, too small wastes time on context switches.
- Priority scheduling — highest priority first. Can starve low-priority processes; ageing is the fix.
- Multilevel queue — separate queues for different process types, each with its own algorithm.
Preemptive vs non-preemptive? Preemptive scheduling can interrupt a running process; non-preemptive waits for it to yield or finish. Preemption gives better responsiveness at the cost of more context switches.
Be ready to compute average waiting and turnaround time for a small table of processes — this is a standard written question, and the arithmetic is where marks are lost rather than the concept.
Memory Management
What is virtual memory? An abstraction giving each process the illusion of a large contiguous address space, with parts stored on disk and brought into RAM on demand. It allows programs larger than physical memory and isolates processes from each other.
What is paging? Dividing memory into fixed-size pages, mapped to frames in physical memory by a page table. It eliminates external fragmentation, though internal fragmentation remains within the last page.
Paging vs segmentation? Paging uses fixed-size blocks invisible to the programmer. Segmentation uses variable-size logical divisions — code, stack, heap — that reflect program structure. Paging causes internal fragmentation; segmentation causes external.
What is a page fault? A reference to a page not currently in physical memory. The OS fetches it from disk, possibly evicting another page. Frequent faults cause thrashing, where the system spends more time swapping than executing.
Page replacement algorithms: FIFO, which is simple but suffers Belady's anomaly where more frames can mean more faults; LRU, which replaces the least recently used and approximates optimal well; and Optimal, which replaces the page not needed for longest — impossible in practice but used as a benchmark.
What is a TLB? A cache of recent page-table translations, avoiding a memory access to look up the mapping on every reference.
Internal vs external fragmentation? Internal is wasted space inside an allocated block; external is free memory split into pieces too small to use.
Synchronisation and IPC
What is a race condition? Two threads accessing shared data concurrently, where the result depends on timing. The classic example is two threads incrementing a counter — read, add, write is not atomic, so one update can be lost.
What is a critical section? The part of code accessing shared resources, which must not be executed by more than one thread at a time.
Mutex vs semaphore? A frequent question, so be precise.
A mutex allows one thread at a time and has ownership — the thread that locks it must be the one to unlock it. A semaphore is a counter allowing up to N concurrent accesses and has no ownership, so any thread can signal it. A binary semaphore resembles a mutex but lacks the ownership rule.
Use a mutex for mutual exclusion over one resource; use a counting semaphore to manage a pool of N identical resources.
What is a spinlock? A lock where the waiting thread loops rather than sleeping. Efficient when the wait is very short, wasteful otherwise, since it burns CPU.
What are the classic synchronisation problems? Producer-consumer with a bounded buffer, readers-writers where many can read but writes are exclusive, and dining philosophers, which illustrates deadlock and starvation. Knowing what each demonstrates is usually enough.
IPC mechanisms: pipes, message queues, shared memory (fastest, but needs synchronisation) and sockets (which work across machines).
