Operating Systems Interview Questions
The most asked OS interview questions on processes, threads, scheduling, deadlocks, paging and virtual memory, with answers.
Operating systems interview questions
OS questions appear in campus placements, backend interviews and systems roles. Answer with a definition and a practical example.
1. What is an operating system?
Software that manages hardware resources and provides services to programs: process scheduling, memory management, file systems, device I/O, security and networking.
2. What is the difference between a process and a thread?
A process is a running program with its own address space. Threads run inside a process and share its memory and open files, but each has its own stack and registers. Threads are cheaper to create and communicate faster, but a bug in one thread can crash the whole process.
3. What is a system call?
A controlled entry point for user programs to request kernel services such as read, write, fork or socket. The CPU switches from user mode to kernel mode to execute it.
4. What is a context switch?
Saving the state (registers, program counter) of the running thread and loading another's so the CPU can run it. It enables multitasking but costs time, especially between processes.
5. What are the process states?
New, ready, running, waiting (blocked) and terminated. The scheduler moves processes from ready to running; I/O moves them to waiting.
6. Explain FCFS, SJF and Round Robin scheduling.
FCFS runs jobs in arrival order (convoy effect). SJF runs the shortest job next and minimises average waiting time but can starve long jobs. Round Robin gives each job a time quantum in turn, improving response time for interactive systems.
7. What is starvation and how is it prevented?
A process waits indefinitely because others keep getting priority. Aging gradually raises the priority of waiting processes.
8. What is a race condition?
When the outcome depends on the timing of concurrent threads accessing shared data, e.g. two threads both reading a balance and writing back, losing one update.
9. Mutex vs semaphore?
A mutex allows one thread into a critical section and is owned by the thread that locked it. A semaphore holds a count of permits and can allow N threads at once — useful for limiting concurrency such as connection pools.
10. What are the four conditions for deadlock?
Mutual exclusion, hold and wait, no preemption and circular wait. All four must hold; breaking any one prevents deadlock.
11. How do you prevent deadlocks in practice?
Acquire locks in a consistent global order, use timeouts, keep critical sections small, and retry transactions when the database reports a deadlock.
12. What is virtual memory?
An abstraction giving each process a private address space, mapped to physical memory through page tables. It provides isolation, lets programs use more memory than physically present (via swap), and enables lazy loading and copy-on-write.
13. What is paging?
Dividing virtual memory into fixed-size pages and physical memory into frames of the same size, with page tables mapping one to the other. It eliminates external fragmentation.
14. What is a page fault?
An exception raised when a program accesses a page not currently in RAM. The OS loads it from disk, possibly evicting another page, then resumes the program.
15. What is thrashing?
When the system spends more time swapping pages than executing, because the working sets of running processes don't fit in RAM. Fixes: reduce load, add memory, or improve locality.
16. Compare FIFO, LRU and optimal page replacement.
FIFO evicts the oldest loaded page and can show Belady's anomaly. LRU evicts the least recently used page and works well in practice. Optimal evicts the page used furthest in the future — the theoretical best, used as a benchmark.
17. What is Belady's anomaly?
With FIFO replacement, increasing the number of frames can increase page faults, e.g. reference string 1,2,3,4,1,2,5,1,2,3,4,5 has 9 faults with 3 frames but 10 with 4.
18. Stack vs heap?
The stack holds function frames and local variables, is fast and automatically managed, but small. The heap holds dynamically allocated objects, is larger, and is managed manually or by a garbage collector.
19. What is an inode?
A data structure in Unix file systems storing a file's metadata and data block pointers — everything except its name. Directories map names to inode numbers.
20. Hard link vs symbolic link?
A hard link is another directory entry for the same inode; the data remains until all hard links are removed. A symbolic link is a file containing a path; it breaks if the target is deleted.
21. Why do databases call fsync?
Because write() usually only reaches the OS page cache; fsync() forces data to durable storage so committed transactions survive power loss.
22. Blocking vs non-blocking I/O?
Blocking calls put the thread to sleep until the operation completes. Non-blocking I/O with event notification (epoll, kqueue, IOCP) lets a single thread manage thousands of connections, as in Nginx, Node.js and asyncio.
Practice tip
Draw a Gantt chart for FCFS, SJF and Round Robin on a small workload and compute waiting times by hand, then check your answers with the simulator in the CPU Scheduling lesson. Interviewers love seeing you calculate, not just define.