All posts

Operating System Interview Questions: 25 Questions with Answers on Processes, Memory and Deadlock

Operating System Interview Questions: 25 Questions with Answers on Processes, Memory and Deadlock

The interviewer asks the difference between a process and a thread. You answer cleanly: a process has its own memory, threads share it. "Good. Then why does Chrome run each tab as a separate process?" You know the definition. Turning it into a reason someone would pay extra memory for is what gets tested.

The short answer

Operating system interview questions for freshers and backend roles come from five areas: processes and threads, CPU scheduling, synchronisation, deadlock and memory management, plus file systems and some Linux. Interviewers want why each mechanism exists and what breaks without it, and many hand you a small numerical problem: a scheduling chart, a Banker's safe sequence or a page replacement string. Below are 25 operating system interview questions with answers, and the follow-up you are likely to hear next.

Campus rounds usually pair OS with DBMS and networks; see the DBMS interview questions and the computer networks interview questions.

Processes and threads

1. What is the difference between a process and a thread?

A process is a running program with its own address space, isolated from other processes by the OS. A thread is a unit of execution inside a process. Threads share the process's code, heap and open files but each has its own stack, registers and program counter. Threads are cheaper to create and switch. The price is isolation: one thread that corrupts memory or crashes takes the whole process down.

Likely follow-up: "So why a process per Chrome tab?" A crashed or compromised tab cannot touch another tab's memory, and each renderer can be sandboxed. Chrome trades memory for safety.

2. What are the states of a process, and what is a PCB?

New, Ready (waiting for the CPU), Running, Waiting (blocked on I/O or an event) and Terminated. Waiting returns to Ready, never straight to Running. The Process Control Block is the kernel's record of a process: pid, state, saved program counter and registers, priority, page table pointer, open files and accounting data.

3. What happens during a context switch, and why is it expensive?

The kernel saves the running process's registers into its PCB, picks the next process, restores its state and switches the address space. Much of the cost is indirect: cold CPU caches and possibly a flushed TLB.

4. What are user mode and kernel mode, and what is a system call?

Applications run in user mode, where privileged instructions and direct hardware access are forbidden; the kernel runs in kernel mode. A system call such as read or fork is a trap that switches the CPU to kernel mode at a fixed entry point, where the kernel validates arguments, does the work and returns. That fixed entry is the safeguard: a program cannot jump into arbitrary kernel code.

Likely follow-up: "Is a system call a context switch?" No, it is a mode switch within the same process. A context switch follows only if the call blocks or the kernel decides to run another process before returning.

5. What do fork() and exec() do? What are zombie and orphan processes?

fork() creates a copy of the calling process, returning 0 in the child, the child's pid in the parent and -1 on failure; copy-on-write shares pages until one side writes. exec() replaces the program image, keeping the pid and open file descriptors (except those marked close-on-exec). A shell forks, execs in the child and waits. A zombie has exited but its parent has not called wait() to collect its status. An orphan's parent died first; init (pid 1) or a subreaper adopts and reaps it.

CPU scheduling

6. What is the difference between preemptive and non-preemptive scheduling?

Non-preemptive: a process keeps the CPU until it blocks or finishes. Preemptive: the OS can take it away, on a timer interrupt or when a higher-priority process becomes ready. Preemption gives responsiveness but can interrupt shared data mid-update.

7. Compare FCFS and SJF with an example.

Three processes arrive at time 0 with bursts P1 = 24, P2 = 3, P3 = 3. FCFS in that order gives waiting times 0, 24 and 27, average 17: the convoy effect, short jobs stuck behind a long one. SJF runs P2, P3, P1: waiting 0, 3 and 6, average 3. SJF minimises average waiting time when jobs arrive together; its preemptive form is Shortest Remaining Time First. But burst lengths are unknown, so the OS predicts them with an exponential average of past bursts.

Likely follow-up: "SJF's other weakness?" Starvation: a stream of short jobs can postpone a long one forever.

8. How do round robin and priority scheduling work?

Round robin gives each process a time quantum, then sends it to the back of the queue. With quantum 4 above: P1 runs 0–4, P2 4–7, P3 7–10, P1 10–30. Waiting times are 6, 4 and 7, average about 5.67. Too large a quantum becomes FCFS; too small wastes the CPU on switches. Priority scheduling runs the highest-priority ready process; SJF is priority scheduling by predicted burst. Low priorities can starve, fixed by aging: raising priority the longer a process waits.

Synchronisation and IPC

9. What are the ways processes communicate (IPC)?

Pipes (one-way, usually between related processes), named pipes (FIFOs), message queues, shared memory, sockets (also across machines) and signals (notifications, not data). Shared memory is fastest because after setup no data passes through the kernel, but the processes must synchronise themselves.

Likely follow-up: "How does ls | grep txt work?" The shell creates a pipe, forks twice, wires one child's stdout and the other's stdin to it with dup2, and each child execs.

10. What is a race condition, and what is a critical section?

A race condition is when the result depends on thread timing. count++ is load, add, store: two threads can both load 5, both store 6, and lose an increment. The critical section is the code touching shared data. A correct solution provides mutual exclusion, progress (if nobody is inside, a waiting thread can enter) and bounded waiting (nobody waits forever). Locks are built on atomic instructions such as test-and-set and compare-and-swap.

Likely follow-up: "Does volatile fix it?" No. Java's volatile gives visibility, not atomic ++; C's gives no thread guarantee. Use a lock or an atomic type.

11. What is the difference between a mutex and a semaphore?

A mutex is a lock with an owner: only the thread that locked it unlocks it. A semaphore is a counter with atomic wait (decrement, block below zero) and signal (increment, wake a waiter), and any thread can signal. A counting semaphore set to N guards N identical resources, such as 10 database connections; a binary semaphore suits signalling between threads. In producer-consumer, wait on empty before taking the mutex; reversed, a producer holding the mutex sleeps on a full buffer and the consumer cannot get in.

Likely follow-up: "What is priority inversion?" A high-priority task waits on a lock held by a low-priority one, which a medium-priority task preempts. Priority inheritance fixes it; the Mars Pathfinder rover hit this in 1997.

Deadlock and starvation

12. What is a deadlock, and what are the four Coffman conditions?

A set of processes each waiting for a resource another holds, so none proceeds. All four conditions are needed: mutual exclusion, hold and wait, no preemption and circular wait. Prevention breaks one. Ordering resources and always acquiring them in that order breaks circular wait, and is what real code does: lock account 101 before 205, whatever the transfer's direction.

13. How does deadlock avoidance work? Explain Banker's algorithm.

Avoidance grants a request only if the resulting state is safe: some order exists in which every process can get its maximum need and finish. Banker's keeps Available, Max, Allocation and Need (Max minus Allocation), pretends to grant each request and searches for a safe sequence. Example: 12 tape drives; P0 holds 5 of max 10, P1 2 of 4, P2 2 of 9, so 3 are free. P1 finishes (5 free), then P0 (10 free), then P2: safe. Give P2 one more drive and only 2 are free; after P1, 4 are free, but P0 needs 5 and P2 needs 6. Unsafe, so the request waits.

Likely follow-up: "Is unsafe the same as deadlocked?" No, deadlock is possible, not certain. Banker's is rare in real operating systems because processes seldom declare maximum needs.

14. How are deadlocks detected and recovered from?

With one instance per resource, a cycle in the wait-for graph means deadlock. With several instances, a cycle is necessary but not sufficient, so a detection algorithm like Banker's safety check runs on current requests. Recovery aborts processes until the cycle breaks, or preempts resources and rolls a victim back, as databases do.

15. What is the difference between starvation and deadlock?

In deadlock, every process in the cycle is stuck. In starvation, one process waits indefinitely while others progress: a long job under SJF, a low-priority process, a writer behind endless readers. Every deadlocked process starves; not every starving one is deadlocked. The fix is fairness: aging and FIFO lock queues.

Memory management

16. What is the difference between paging and segmentation?

Paging splits the address space into fixed-size pages (commonly 4 KB) and memory into frames of the same size, mapped by a page table: no external fragmentation, a little internal. Segmentation uses variable-sized logical units (code, stack, heap) with base and limit, which matches the programmer's view and makes per-segment protection easy, but causes external fragmentation.

17. What is virtual memory, and how is an address translated?

Each process gets a private address space that the MMU maps to physical frames, loaded on demand. That gives isolation, programs larger than free RAM, shared library pages and copy-on-write fork. With 4 KB pages, the low 12 bits are the offset; on 32-bit, the top 20 are the page number, which indexes the page table to find the frame.

Likely follow-up: "Why multi-level page tables?" A flat 32-bit table has 2^20 entries; at 4 bytes each that is 4 MB per process, mostly unused. Levels allocate only what is used; x86-64 uses four, or five on newer chips.

18. What is a page fault, and what happens when one occurs?

A trap raised when a process touches a page not in memory. If the address is invalid, the process gets a segmentation fault (SIGSEGV). Otherwise the kernel finds a free frame, evicting a page and writing it back if modified, reads the page in while the process blocks, updates the page table and restarts the instruction. A minor fault finds the page already in memory; a major fault reads from disk and can cost milliseconds.

19. What is a TLB, and why does it matter?

Every memory access would otherwise need a page table read first. The Translation Lookaside Buffer caches recent translations in the MMU. With 100 ns memory, a single-level table and an 80% hit rate, ignoring TLB lookup time, the effective access time is 0.8 × 100 + 0.2 × 200 = 120 ns.

Likely follow-up: "And on a context switch?" Entries belong to the old address space, so they are flushed, unless tagged with an address-space id (PCID on x86).

20. Explain FIFO, LRU and optimal page replacement, and Belady's anomaly.

With 3 frames and the string 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 1, 2, 0, 1, 7, 0, 1: FIFO (evict oldest) gives 15 faults, LRU (evict least recently used) 12, Optimal (evict the page used furthest ahead) 9. Optimal needs the future, so it is only a benchmark; real systems approximate LRU with the clock algorithm and a reference bit. Belady's anomaly is more frames giving more faults: FIFO on 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5 gives 9 faults with 3 frames and 10 with 4. LRU and Optimal are stack algorithms, so they never show it.

21. What is thrashing, and how do you fix it?

Processes spend more time paging than executing because their actively used pages do not fit in memory. CPU use collapses, and a scheduler that admits more processes makes it worse. The working set model runs only as many processes as their recent working sets fit; page-fault-frequency control gives frames to processes faulting often and takes them from others.

22. What is the difference between internal and external fragmentation?

Internal: wasted space inside an allocated block, such as 4,097 bytes taking two 4 KB pages. External: enough total free memory, but no contiguous hole big enough. First-fit, best-fit and worst-fit pick a hole; best-fit leaves tiny unusable ones. Compaction fixes external fragmentation at a cost; paging removes it entirely while keeping some internal.

File systems and Linux basics

23. What is an inode, and how do hard and soft links differ?

An inode stores a file's metadata: type, permissions, owner, size, timestamps, link count and data block pointers. Not its name: a directory maps names to inode numbers. A hard link is another name for the same inode; data survives until the link count is zero and no process has it open, and it cannot cross file systems. A soft link is a file holding a path; it can cross file systems but breaks if the target moves.

Likely follow-up: "Disk full, but df -h shows space?" Inodes may be exhausted by tiny files; check df -i.

24. How do Linux file permissions work?

Read, write and execute bits for owner, group and others; r = 4, w = 2, x = 1, so chmod 755 is rwxr-xr-x. On a directory, r lists names, w creates and deletes entries, and x lets you enter it. A umask of 022 makes new files 644 and directories 755. Setuid runs a program as its owner (passwd uses it); the sticky bit on /tmp stops users deleting each other's files.

25. How do you manage processes and signals in Linux?

ps aux lists processes, top shows them live and kill sends a signal. SIGTERM (15, the default) asks a process to exit and can be caught for cleanup; SIGKILL (9) and SIGSTOP cannot be caught or ignored. Ctrl+C sends SIGINT, Ctrl+Z SIGTSTP, and SIGCHLD tells a parent a child exited.

Likely follow-up: "Why not always kill -9?" The process cannot flush buffers or remove lock files, so try SIGTERM first. And it cannot kill a zombie, which is already dead; its parent must reap it.

How to practise these

Work questions 7, 8, 13 and 20 on paper until they are quick. Then, on Linux, leave a forked child unreaped to see a Z in ps, delete the target of a soft link, and trap SIGTERM in a script. When an interviewer goes deeper than you have read, see how to handle a question you don't know, and for the full plan, the interview preparation guide for freshers.

These are explanation questions, so practise saying the answers out loud. A backend practice round on 99interview with Sia, our AI interviewer, asks one question at a time, which you answer by speaking or typing against a clock, and scores each answer with a written write-up. There is no code editor, which suits concept questions like these. For a person, a live Zoom interview with a working engineer rates you on technical depth, problem solving, communication, behaviour and code quality, with written feedback.