Interview questions › Operating Systems
Operating Systems interview questions
1. What is an operating system?
Easy
System software that manages hardware and provides services to programs: process management, memory management, file systems, device I/O, security and a user interface. Examples: Windows, Linux, macOS, Android.
2. What is the kernel?
Medium
The core part of the OS that runs in privileged (kernel) mode and directly manages CPU, memory and devices. Monolithic kernels (Linux) run most services inside the kernel; microkernels keep only essentials and run the rest as user processes.
3. What is the difference between a process and a program?
Easy
A program is a passive file of instructions on disk. A process is an active instance of a program in execution with its own memory, registers, program counter and resources. One program can run as many processes.
4. What is a process vs a thread?
Easy
A process has its own address space; threads are lightweight units of execution inside a process that share its code, data, heap and files but have their own stack, registers and PC. Thread creation and switching are cheaper than process.
5. What is a PCB?
Easy
The Process Control Block stores everything the OS needs about a process: PID, state, program counter, registers, scheduling info, memory limits, open files. It is saved and restored during context switches.
6. What are the states of a process?
Easy
New → Ready → Running → (Waiting/Blocked for I/O) → Ready … → Terminated. The scheduler moves processes between Ready and Running; I/O moves them to Waiting.
7. What is a context switch?
Easy
Saving the state of the running process/thread and loading the state of the next one so the CPU can switch between them. It is overhead (no useful work) and is cheaper between threads of the same process.
8. What is multiprogramming, multitasking and multiprocessing?
Medium
Multiprogramming: several programs in memory so the CPU stays busy when one waits for I/O. Multitasking: time-sharing the CPU among tasks quickly so they seem simultaneous. Multiprocessing: multiple CPUs/cores running processes truly in parallel.
9. What is a system call?
Easy
The interface through which a user program requests a service from the kernel, e.g. fork(), exec(), read(), write(), open(). It switches the CPU from user mode to kernel mode.
10. What does fork() do?
Medium
fork() creates a new child process as a copy of the parent. It returns 0 in the child, the child's PID in the parent, and −1 on failure. Modern systems use copy-on-write so memory is copied only when modified.
11. What is a zombie process and an orphan process?
Medium
A zombie has finished executing but still has an entry in the process table because the parent hasn't called wait() to read its exit status. An orphan is a running process whose parent has terminated; it is adopted by init/systemd.
12. What is CPU scheduling? Name the criteria.
Easy
Deciding which ready process gets the CPU next. Criteria: maximise CPU utilisation and throughput; minimise turnaround time, waiting time and response time; ensure fairness.
13. Explain FCFS, SJF and Round Robin.
Easy
FCFS runs processes in arrival order (simple, convoy effect). SJF runs the shortest burst first (minimum average waiting time, can starve long jobs). Round Robin gives each process a fixed time quantum in turn (fair, good response time).
14. What is preemptive vs non-preemptive scheduling?
Easy
In preemptive scheduling the OS can take the CPU away from a running process (e.g. when a higher-priority process arrives or the time quantum ends). In non-preemptive scheduling a process keeps the CPU until it finishes or blocks.
15. What is starvation and aging?
Easy
Starvation: a low-priority process waits indefinitely because others keep getting the CPU. Aging gradually increases the priority of processes that wait a long time, so they eventually run.
16. What is the convoy effect?
Easy
In FCFS, short processes get stuck waiting behind one long CPU-bound process, increasing average waiting time and lowering device utilisation.
17. What is a race condition?
Medium
When the result depends on the timing/order in which threads access shared data, e.g. two threads doing count++ at the same time and losing an update. Prevented by synchronisation (locks, atomic operations).
18. What is a critical section and its requirements?
Medium
Code that accesses shared resources and must not be executed by more than one process at a time. A solution must satisfy mutual exclusion, progress and bounded waiting.
19. What is a mutex?
Medium
A mutual-exclusion lock: a thread must acquire it before entering a critical section and release it afterwards. Only the thread that locked it can unlock it.
20. What is a semaphore? Binary vs counting?
Medium
An integer variable accessed only through atomic wait() (P, decrement, block if < 0) and signal() (V, increment). A binary semaphore (0/1) works like a lock; a counting semaphore controls access to a pool of N identical resources.
21. Mutex vs semaphore?
Medium
A mutex is a locking mechanism with ownership — only the locker can unlock, used for mutual exclusion. A semaphore is a signalling mechanism without ownership — any thread can signal, and a counting semaphore allows N concurrent accesses.
22. Explain the producer–consumer problem.
Hard
Producers add items to a bounded buffer and consumers remove them; producers must wait when it is full and consumers when it is empty. Classic solution: a mutex for the buffer plus two counting semaphores, empty (free slots) and full (filled slots).
23. What is the dining philosophers problem?
Hard
Five philosophers share five forks; each needs both neighbouring forks to eat. If everyone picks the left fork first, they deadlock. Solutions: pick forks in a global order, allow at most four to sit, or use a waiter/monitor.
24. What is a deadlock? What are the necessary conditions?
Easy
A situation where processes wait forever for resources held by each other. Four conditions must hold together: mutual exclusion, hold and wait, no preemption and circular wait.
25. How can deadlocks be handled?
Medium
Prevention (break one of the four conditions), avoidance (Banker's algorithm keeps the system in a safe state), detection and recovery (find cycles, then kill/preempt), or ignoring them (ostrich approach).
26. What is the Banker's algorithm?
Hard
A deadlock-avoidance algorithm: before granting a request, it checks whether the system would still be in a safe state — i.e. there is an order in which every process can obtain its maximum need and finish. If not, the request waits.
27. Deadlock vs starvation?
Easy
In a deadlock processes are blocked forever waiting on each other in a cycle. In starvation a process is ready but is repeatedly passed over; the system keeps running for others.
28. What is paging?
Easy
A memory scheme that splits logical memory into fixed-size pages and physical memory into equal frames; a page table maps pages to frames. It removes external fragmentation and lets a process be non-contiguous.
29. What is segmentation?
Easy
Dividing memory into variable-sized logical segments (code, data, stack) according to the program's structure. It matches the programmer's view but suffers from external fragmentation.
30. Internal vs external fragmentation?
Easy
Internal: wasted space inside an allocated block that is larger than needed (fixed partitions, paging). External: total free memory is enough but it is split into non-contiguous holes (variable partitions, segmentation).
31. What is virtual memory?
Easy
A technique that lets processes use more memory than physically available by keeping only the needed pages in RAM and the rest on disk. Each process gets its own large, isolated address space.
32. What is demand paging and a page fault?
Medium
With demand paging a page is loaded only when it is first accessed. Accessing a page that is not in RAM triggers a page fault; the OS loads it from disk, updates the page table and restarts the instruction.
33. What is thrashing?
Medium
When processes have too few frames for their working sets, they page-fault constantly and the system spends more time swapping than executing. Fixed by reducing the degree of multiprogramming or using working-set/page-fault-frequency control.
34. Explain page replacement algorithms.
Medium
FIFO evicts the oldest page (can show Belady's anomaly). Optimal evicts the page used farthest in the future (best, but needs future knowledge). LRU evicts the least recently used page (good approximation of optimal).
35. What is Belady's anomaly?
Medium
The counter-intuitive situation where increasing the number of frames increases the number of page faults. It happens with FIFO, but not with stack algorithms like LRU and Optimal.
36. What is a TLB?
Medium
The Translation Lookaside Buffer is a small, fast hardware cache of recent page-table entries. On a TLB hit, address translation avoids an extra memory access to the page table.
37. What is a page table?
Medium
A per-process data structure that maps virtual page numbers to physical frame numbers, with extra bits like valid, dirty and protection. Large address spaces use multi-level or inverted page tables.
38. What is swapping?
Easy
Moving an entire process (or pages) from main memory to disk (swap space) and back later, to free RAM for other processes.
39. What is cache memory and locality of reference?
Easy
Cache is small, very fast memory between the CPU and RAM that holds recently used data. It works because of locality: temporal (recently used data is used again soon) and spatial (nearby data is used soon).
40. What is the difference between user mode and kernel mode?
Easy
In user mode programs run with restricted privileges and can't access hardware directly. In kernel mode the OS has full access to all instructions and memory. System calls and interrupts switch to kernel mode.
41. What is an interrupt?
Easy
A signal from hardware or software that makes the CPU pause the current work and run an interrupt handler (e.g. keyboard input, timer, I/O completion), then resume.
42. What is a file system? Name some.
Easy
The part of the OS that organises data on storage into files and directories and manages space, metadata and permissions. Examples: NTFS, FAT32, ext4, APFS.
43. What are the disk scheduling algorithms?
Medium
FCFS, SSTF (shortest seek time first), SCAN (elevator — move to one end then reverse), C-SCAN (circular, return to start), LOOK and C-LOOK (only go as far as the last request). They reduce seek time.
44. What is a spinlock?
Medium
A lock where a thread waiting for it keeps looping ("spinning") and checking instead of sleeping. Efficient for very short critical sections on multicore systems, wasteful otherwise.
45. What is a monitor?
Hard
A high-level synchronisation construct that bundles shared data with procedures and guarantees only one thread is active inside it at a time, with condition variables for waiting/signalling (e.g. Java synchronized + wait/notify).
46. What is the difference between a hard and soft real-time system?
Easy
In a hard real-time system missing a deadline is a failure (airbag, pacemaker). In a soft real-time system missing deadlines degrades quality but is tolerated (video streaming, gaming).
47. What is a daemon process?
Easy
A background process that runs without a controlling terminal, usually started at boot to provide services (e.g. sshd, cron, print spooler).
48. What is booting?
Easy
The process of starting a computer: firmware (BIOS/UEFI) runs POST, finds the boot device, loads the bootloader (e.g. GRUB), which loads the kernel into memory; the kernel then starts the first user process (init/systemd).
49. What is IPC? Name some mechanisms.
Medium
Inter-Process Communication lets processes exchange data: pipes, named pipes (FIFOs), message queues, shared memory, sockets and signals.
50. What is copy-on-write?
Hard
An optimisation where parent and child share the same memory pages after fork(); a page is actually copied only when one of them writes to it, saving time and memory.