Core CS · 22 questions
Operating Systems interview questions
Processes and threads, scheduling, synchronisation, deadlocks and memory: the OS questions interviews and campus tests ask most, with simulators for the numerical ones.
Basics
What is the kernel, and what are user mode and kernel mode?
The kernel is the core of the OS: it manages the CPU, memory, devices and files, and it runs with full access to the hardware. Programs run in user mode, where they can't touch hardware or other programs' memory directly. To do anything privileged (read a file, open a socket) a program makes a system call, which switches the CPU to kernel mode, does the work, and switches back.
What is a system call? Give examples.
The way a program asks the kernel to do something it isn't allowed to do itself. It traps into kernel mode, the kernel checks the request and does it, and control returns to the program.
Examples: fork, exec, exit, wait (processes); open, read, write, close (files); socket, send (network); mmap (memory). Library functions like printf call write underneath.
Monolithic kernel vs microkernel?
A monolithic kernel runs everything (scheduling, memory, file systems, drivers, networking) in kernel space as one program: fast, because parts call each other directly, but one buggy driver can crash everything. Linux is monolithic (with loadable modules).
A microkernel keeps only the essentials (scheduling, memory, messaging) in the kernel and runs drivers and file systems as user-space servers: more robust, slower because of message passing. Examples: MINIX, QNX, seL4.
Processes and threads
Process vs thread?
A process is a running program with its own memory space. A thread is a path of execution inside a process; threads of the same process share its memory (code, heap, open files) but each has its own stack and registers.
- Threads are cheaper to create and switch between.
- Threads talk through shared memory; processes need IPC (pipes, sockets…).
- A crashing thread can take down its whole process; a crashing process doesn't harm others.
What are the states of a process?
New → Ready (waiting for the CPU) → Running → Terminated. A running process that waits for I/O goes to Waiting (blocked), then back to Ready when the I/O finishes. A running process that is preempted (its time slice ends) goes back to Ready.
What is a PCB, and what happens in a context switch?
The process control block is the kernel's record of a process: its id, state, program counter, registers, memory information (page table), open files and scheduling details.
A context switch saves the running process's registers and program counter into its PCB and loads the next process's from its PCB. It's pure overhead, and switching between processes also costs cache and TLB misses, which is why threads switch more cheaply.
What does fork() return, and how many times does this print?
fork() creates a copy of the calling process. It returns 0 in the child, the child's PID in the parent, and -1 if it fails.
fork(); fork(); printf("hi\n"); prints 4 times: each fork doubles the number of processes, so n forks give 2n processes.
What are zombie and orphan processes?
A zombie has finished, but its parent hasn't called wait() to read its exit status, so its entry stays in the process table. An orphan is still running, but its parent has exited; it's adopted by init/systemd (PID 1), which reaps it when it ends.
How do processes communicate (IPC)?
- Pipes: a one-way byte stream, usually parent to child (
ls | grep). Named pipes (FIFOs) work between unrelated processes. - Message queues: send and receive whole messages through the kernel.
- Shared memory: the fastest; both map the same memory, but they must synchronise (semaphores).
- Sockets: between processes on the same machine or across a network.
- Signals: tiny notifications (
SIGKILL,SIGTERM).
CPU scheduling
Preemptive vs non-preemptive scheduling?
Non-preemptive: once a process gets the CPU, it keeps it until it finishes or blocks (FCFS, SJF). Preemptive: the OS can take the CPU away, at a timer interrupt or when a more important process arrives (Round Robin, SRTF, preemptive priority). Every modern general-purpose OS is preemptive, so one process can't freeze the machine.
Work any scheduling question on the CPU scheduling simulator →
What is starvation, and how does aging fix it?
Starvation is when a process waits forever because others keep getting chosen first: a long job under SJF, or a low-priority process under priority scheduling. Aging slowly raises the priority of a process the longer it waits, so eventually it runs.
What does a scheduler try to optimise?
High CPU utilisation and throughput (jobs finished per unit of time); low turnaround time (CT − AT), waiting time (TAT − BT) and response time (first run − AT). They pull against each other: Round Robin gives good response time; SJF and SRTF give the lowest average waiting time.
Synchronisation and deadlocks
What is a race condition, and what is a critical section?
A race condition is when the result depends on the timing of threads. Two threads doing count++ (read, add, write) can both read 5 and both write 6, losing an update. The code that touches shared data is the critical section; a correct solution needs:
- Mutual exclusion: one at a time inside.
- Progress: if no one is inside, someone waiting gets in.
- Bounded waiting: no one waits forever.
Mutex vs semaphore?
A mutex is a lock with an owner: the thread that locks it must unlock it; it protects one critical section. A semaphore is a counter with wait() (P, decrement, block at 0) and signal() (V, increment). A counting semaphore allows N at once (a pool of 5 connections); a binary semaphore (0/1) looks like a mutex but has no owner, so any thread can signal it, which makes it good for one thread telling another "go".
Solve producer–consumer with semaphores.
A buffer of size N, with three semaphores: empty = N (free slots), full = 0 (filled slots) and mutex = 1 (buffer access).
Producer: wait(empty); wait(mutex); add the item signal(mutex); signal(full);
Consumer: wait(full); wait(mutex); remove the item signal(mutex); signal(empty);
What are the four conditions for deadlock?
All four must hold at once (the Coffman conditions):
- Mutual exclusion: a resource is used by one process at a time.
- Hold and wait: a process holds one resource while waiting for another.
- No preemption: resources can't be taken away.
- Circular wait: P1 waits for P2, which waits for … Pn, which waits for P1.
Break any one and deadlock is impossible: for example, always take locks in the same global order to stop circular wait.
How do operating systems handle deadlocks?
- Prevention: make one of the four conditions impossible (lock ordering, take everything at once).
- Avoidance: grant a request only if the system stays in a safe state: the Banker's algorithm.
- Detection and recovery: let it happen, find cycles in the wait-for graph, then kill or roll back a process.
- Ignore it (the ostrich approach): what Linux and Windows do for most resources, because deadlocks are rare and the alternatives cost too much.
Memory
Paging vs segmentation?
Paging splits memory into fixed-size pages (often 4 KB) that map to frames anywhere in physical memory. There's no external fragmentation, but there is a little internal fragmentation in the last page.
Segmentation splits a program into variable-size logical parts (code, stack, heap). It matches how programmers think, but leaves external fragmentation. Modern systems use paging, x86-64 almost entirely.
What is virtual memory, and what happens on a page fault?
Each process sees its own large address space; only the pages in use need to be in RAM, the rest live on disk. Demand paging loads a page only when it's first touched.
On a page fault: the MMU finds the page isn't in RAM and traps to the OS; the OS finds the page on disk, picks a free frame (or evicts a page using a replacement algorithm), reads the page in, updates the page table, and restarts the instruction.
What is thrashing?
When processes don't have enough frames for the pages they're actively using, they page-fault constantly; the CPU spends its time waiting for disk instead of working, so utilisation collapses. The OS reacts by giving processes their working set (the pages they've used recently) or by suspending some processes, not by starting more.
What is a TLB?
The translation lookaside buffer is a small, fast cache of recent page-table entries (virtual page → physical frame) inside the CPU. A TLB hit translates an address without reading the page table from memory; a miss costs extra memory reads. Effective access time = hit ratio × (TLB + memory) + miss ratio × (TLB + 2 × memory), for a one-level page table.
Internal vs external fragmentation?
Internal: wasted space inside an allocated block, because blocks come in fixed sizes (a 4 KB page holding 1 KB of data). External: enough free memory in total, but in pieces too small for the request; compaction or paging solves it.
A few cards a day, back just before you'd forget them.