Ch. 21

Operating Systems interview questions & answers

What happens under your code: processes and threads, scheduling, virtual memory and page faults, locks and deadlocks, file descriptors and the page cache.

8 interview questions11 quiz questions25 notes
your progress0%

Notes in this chapter

Filter all notes →
read ✓Operating Systems · hard

Operating Systems: CPU Cache Locality

Measured on an M1: column-order matrix walks up to 30x slower, shuffled lists at 97 ns a node, and false sharing slowing two counters 4x.

~6 min readread →
read ✓Operating Systems · hard

Copy-on-Write Memory and fork

How fork shares pages until a write, measured on macOS: 16,517 pages copied on write, few on read, and Python copying pages just by reading.

~6 min readread →
read ✓Operating Systems · hard

Operating Systems: epoll and select

Why select stops at 1,024 descriptors and poll slows with every idle socket, how epoll and kqueue avoid both, and when edge-triggered mode bites.

~7 min readread →
read ✓Operating Systems · hard

Operating Systems: I/O Models

Blocking, non-blocking, multiplexed and completion I/O compared: a 1-second spin burned a full second of CPU, and 2,000 threads cost 70 MiB.

~6 min readread →
read ✓Operating Systems · hard

Operating Systems: Journaling and Durability

Why a crash mid-update corrupts a filesystem, how write-ahead journaling and replay fix it, ext4's modes, and what journaling won't do for you.

~7 min readread →
read ✓Operating Systems · mid

Condition Variables and Predicate-Based Waiting

Why a condition variable wait sits in a while loop under the lock, with Python runs of a stolen wakeup, a lost notify and a notify() deadlock.

~6 min readread →
read ✓Operating Systems · easy

Context Switching and Scheduling Overhead

What a context switch saves and loses, voluntary vs involuntary switches, a 3.5 µs pipe ping-pong on an M1, and the real cost of 512 threads.

~6 min readread →
read ✓Operating Systems · easy

CPU Scheduling and Response-Time Goals

FIFO, SJF, STCF and round robin simulated with real numbers, the turnaround vs response trade-off, and what an M1's efficiency cores add.

~7 min readread →
read ✓Operating Systems · mid

Deadlock Conditions and Lock Ordering

Reproduce a two-account deadlock in Python, map it to the four Coffman conditions, and fix it with lock ordering, timeouts or a different design.

~6 min readread →
read ✓Operating Systems · mid

File Descriptors and Resource Exhaustion

What a file descriptor points to, which limits apply, and a real EMFILE reproduced in a throwaway process, plus how to find the leak with lsof.

~6 min readread →
read ✓Operating Systems · hard

Filesystem Writes, Buffers and Durability

What write, fsync and F_FULLFSYNC really promise, measured on an M1 (2 µs, 25 µs, 3.8 ms), plus atomic file replacement and group commit.

~6 min readread →
read ✓Operating Systems · hard

Interrupts and Device Completion Notifications

From a NIC interrupt to the thread that reads the data: top halves, deferred work, and a polling vs blocking test showing the CPU-latency trade.

~7 min readread →
read ✓Operating Systems · hard

Operating-System Page Cache and I/O Measurements

Random 4 KiB reads took 189 µs cold and 1.6 µs warm on an M1. How the page cache works, how to benchmark around it, and when to bypass it.

~7 min readread →
read ✓Operating Systems · easy

Paging, Page Faults and Memory Access

Minor, major and invalid page faults, with real counts: 4,096 faults for 64 MiB on an M1, and a cold mmap read 34 times slower than warm.

~6 min readread →
read ✓Operating Systems · easy

Processes Versus Threads and Shared State

What a process owns, what its threads share, and what that costs: macOS runs of thread vs fork vs spawn, and a segfault that kills a process.

~7 min readread →
read ✓Operating Systems · mid

Producer-Consumer Queues and Backpressure

Unbounded, blocking and dropping queues measured in Python: how a bound turns overload into a policy, how to size it, and how to shut it down.

~7 min readread →
read ✓Operating Systems · mid

Race Conditions and Atomic State Transitions

Lost updates and check-then-act races, run in Node and Python: why a racy counter can pass tests, and how atomics, locks and O_EXCL fix it.

~6 min readread →
read ✓Operating Systems · easy

User Mode, Kernel Mode and Privileged Operations

How the CPU stops your code doing privileged work, what a system call costs, and why clock_gettime skips the kernel, with real M1 measurements.

~6 min readread →
read ✓Operating Systems · easy

Virtual Memory and Address-Space Isolation

Why two processes can hold different values at the same address, how page tables translate it, and why a 32 GiB mapping can cost almost nothing.

~6 min readread →
read ✓Operating Systems · mid

Working Sets and Memory Thrashing

Why throughput collapses when active memory exceeds RAM: in an LRU test, one frame fewer took faults from 2% to 100%. How to spot and fix it.

~7 min readread →

8 Operating Systems interview questions study by subtopic

8 questions
  1. 1.How does a process differ from a thread?mid

    A process is an execution environment with its own virtual address space and resources. Threads within a process share that address space but have their own stacks and execution state. Sharing memory makes communication convenient and introduces synchronization risks. Processes generally provide stronger isolation, while threads can be useful for concurrent work inside an application. The costs of creation and scheduling depend on the operating system and runtime.

    What interviewers listen for
    • Processes have separate address spaces
    • Threads share process memory
    • Threads have separate stacks
    • Sharing requires synchronization

    Likely follow-up: When would you choose worker processes over threads?

  2. 2.What causes deadlock and how can you prevent it?hard

    Deadlock occurs when participants wait indefinitely for resources held by one another. The classic conditions are mutual exclusion, holding resources while waiting, no forced preemption and circular wait. A consistent global lock acquisition order can break circular wait. Other approaches include reducing lock scope or avoiding holding one lock while acquiring another. Timeouts help detect or escape a wait, but recovery must release resources and preserve invariants.

    What interviewers listen for
    • Explain a circular wait
    • Identify the four conditions
    • Use consistent lock ordering
    • Recover without corrupting state

    Likely follow-up: How is deadlock different from starvation?

  3. 3.Why do operating systems use virtual memory?mid

    Virtual memory gives processes an address space that is mapped to physical memory through page tables. It supports isolation, protection and flexible allocation without requiring contiguous physical memory. A page fault means a mapping needs attention; it does not always mean disk I/O, because demand-zero allocation or copy-on-write can also fault. Heavy paging can make a system slow, so I would inspect memory pressure and the working set before simply adding threads.

    What interviewers listen for
    • Virtual addresses map to physical pages
    • Memory protection isolates processes
    • Page faults have multiple causes
    • Working sets affect performance

    Likely follow-up: What happens when a process repeatedly accesses more memory than RAM can hold?

  4. 4.How do concurrency and parallelism differ?easy

    Concurrency means multiple tasks can make progress over overlapping periods; parallelism means tasks execute at the same time. A single CPU core can support concurrency by switching between tasks. Multiple cores can enable parallel execution, subject to runtime constraints and workload structure. I would use concurrency to overlap waiting and parallelism to accelerate divisible computation. Both require care with shared state, and overhead can outweigh the benefit for small tasks.

    What interviewers listen for
    • Concurrency is overlapping progress
    • Parallelism is simultaneous execution
    • One core can support concurrency
    • Match the approach to workload and overhead

    Likely follow-up: Can an application be concurrent without being parallel?

  5. 5.How would you investigate a process whose memory keeps growing?mid

    Measure memory over a repeatable workload and distinguish a temporary peak or cache warmup from sustained growth. Compare heap snapshots or allocation profiles to find objects that remain reachable unexpectedly. Also inspect native allocations, open resources and memory mappings because heap metrics may not explain the full resident size. Look for unbounded caches, retained listeners and queues. Confirm a fix by repeating the same workload and observing a stable memory trend.

    What interviewers listen for
    • Reproduce and measure the trend
    • Inspect retained allocations
    • Include native memory and resources
    • Verify stabilization after the fix

    Likely follow-up: Why can memory leak in a garbage-collected language?

  6. 6.What trade-offs does a scheduler make when choosing a time slice?mid

    Short time slices can improve responsiveness by giving runnable tasks frequent opportunities to execute, but context switching adds overhead and can disturb caches. Long slices may improve efficiency for sustained work while making interactive tasks wait longer. The actual policy also considers priorities and fairness. I would evaluate response latency and throughput for the target workload rather than assume one time slice works everywhere. Tasks waiting for I/O need not consume their full slice.

    What interviewers listen for
    • Balance responsiveness and overhead
    • Consider cache and switching costs
    • Account for priorities and fairness
    • Measure latency and throughput

    Likely follow-up: How would an interactive workload differ from a batch workload?

  7. 7.Why should a condition-variable wait normally check its predicate in a loop?hard

    A condition variable is used with shared state protected by a mutex. Waiting atomically releases the mutex and blocks, then reacquires it before returning. A wakeup does not guarantee the desired predicate is true: another thread may have consumed the resource, and some APIs permit spurious wakeups. Check the predicate again while holding the mutex and wait if necessary. The shared predicate, rather than the notification itself, determines whether proceeding is safe.

    What interviewers listen for
    • Protect the predicate with a mutex
    • Waiting releases and reacquires the lock
    • Wakeups do not guarantee the condition
    • Recheck in a loop

    Likely follow-up: How can checking the predicate outside the lock lose a notification?

  8. 8.Why should files and sockets be closed explicitly?mid

    Files and sockets hold operating-system resources that are finite, even when the language manages object memory automatically. Relying on garbage collection can delay cleanup and exhaust descriptors or connections. Use the language’s structured cleanup mechanism so normal returns and exceptions both release the resource. Make ownership clear when a resource is shared. I would monitor descriptor counts and connection pools when a service fails only after running for a long time.

    What interviewers listen for
    • OS resources are finite
    • Memory collection may not release resources promptly
    • Use structured cleanup on error paths
    • Clarify ownership and monitor resource counts

    Likely follow-up: What symptoms would you expect from a file-descriptor leak?

Prefer multiple choice? All 11 Operating Systems MCQs with answers →

esc