N operating systems cheat sheet: the 5 ideas interviews keep asking about, in plain words - Netscape _ □ ×
Back Forward Home Reload Guides Sign
URL: http://www.nicnonac.com/guides/operating-systems-cheat-sheet/
☷ operating systems cheat sheet: the 5 ideas interviews keep asking about, in plain words - nicnonac guides _□×

← back to guides

operating systems cheat sheet: the 5 ideas interviews keep asking about, in plain words

Stacks · 9 min read · posted Nov 30, 2026, 2:30 am

Operating systems questions in interviews keep coming back to the same few ideas. Each one is a small idea you can explain in a couple of sentences.

This is the cheat sheet from the reel. It covers the five ideas in it. For each one you get what it means, a short Python example, how to say it out loud in an interview, and the follow-up question that usually comes next.

The examples are Python because it's short to read. The ideas belong to the operating system, and they're the same in Java, C++ or Go.

the quick lookup

they ask the one-line answer
process vs thread a process has its own memory. threads live inside one process and share its memory
what is a race condition two threads use the same data at the same time, at least one writes, and the result depends on who gets there first
what is a mutex a lock only one thread can hold at a time, so the code inside it runs one thread at a time
what is a deadlock two threads each hold a lock the other needs, so both wait forever
what is virtual memory each process gets its own address space, and the OS maps its pages onto real memory

1. process vs thread

What it means. A process is a running program with its own memory. A thread is a path of execution inside a process. One process can have many threads, and they all share that process's memory: the same variables, the same heap. Each thread has its own stack.

import threading as th
import multiprocessing as mp
x = 0
def bump():
    global x
    x = 1
if __name__ == "__main__":
    p = mp.Process(target=bump)
    p.start(); p.join()
    print(x)
    t = th.Thread(target=bump)
    t.start(); t.join()
    print(x)

This is the one in the reel. It prints 0 and then 1. The child process sets its own x to 1, and the parent's x is still 0, because they don't share memory. The thread sets x to 1 in the memory it shares with the main thread, so the second print shows 1.

process thread
memory its own shared with the other threads in the process
cost to start and to switch higher lower
if it crashes the others keep running it can take the whole process down
talking to each other pipes, sockets, or shared memory you set up read the same variables

Say it like this. "A process has its own address space, so processes are isolated from each other. Threads share their process's memory. That makes them cheaper to create and makes sharing data easy, and that sharing is where race conditions come from."

The follow-up. "What is a context switch?" The OS pauses one thread, saves its registers and where it was up to, and loads another one's. Switching between processes also swaps the memory mapping, which is why it costs more.

If the interview is in Python you may also get "why use processes at all?". Standard Python has a global interpreter lock, so only one thread runs Python code at a time. Threads still help when the work is waiting on the network or the disk. For CPU-heavy work you use processes.

2. race condition

What it means. Two threads use the same data at the same time, at least one of them writes, and the answer depends on the timing.

import threading as th
count = 0
def add():
    global count
    for _ in range(100000):
        count += 1
a = th.Thread(target=add)
b = th.Thread(target=add)
a.start(); b.start()
a.join(); b.join()
print(count)

count += 1 looks like one step. It's three: read count, add 1, write it back. In the reel both threads read 5, both add 1, and both write 6. Two increments ran and the count went up by one. One update is lost.

Run this on standard Python and you'll usually get the right answer, 200000, because threads there rarely get switched in the middle of that line. That's what makes races nasty: the code is wrong and it passes most of the time. On a free-threaded build of Python, the kind with no global interpreter lock, the same code loses updates on most runs.

Say it like this. "A race condition is when the result depends on the order the threads happen to run in. It needs shared data and at least one writer. The classic case is a read, modify, write sequence that isn't atomic."

The follow-up. "How do you fix it?" Make the critical section, the part that touches the shared data, run one thread at a time with a lock. Or don't share: give each thread its own data and combine the results at the end. Or hand the work over through a thread-safe queue.

3. mutex

What it means. Mutex is short for mutual exclusion. It's a lock that one thread can hold at a time. A thread that wants it while another holds it has to wait.

import threading as th
count = 0
lock = th.Lock()
def add():
    global count
    for _ in range(100000):
        with lock:
            count += 1
a = th.Thread(target=add)
b = th.Thread(target=add)
a.start(); b.start()
a.join(); b.join()
print(count)

It's the same counter with one change: with lock: around the increment. In the reel, thread A takes the lock, reads 5 and writes 6 while B waits. Then B takes it, reads 6 and writes 7. This prints 200000 every time.

Say it like this. "A mutex protects a critical section. Only the thread holding it can run that code, so the read, modify, write happens as one unit. The cost is that threads now queue up, so you keep the locked part as small as you can."

The follow-up. "Mutex vs semaphore?" A mutex lets one thread in, and the thread that locked it is the one that unlocks it. A semaphore is a counter that lets up to n threads in at once, like a limit of 10 database connections, and any thread can release it.

What breaks it. Forgetting to release. If the code raises an exception between taking the lock and releasing it, the lock stays held and everything behind it waits forever. with lock: releases it on the way out, error or not.

4. deadlock

What it means. Two threads each hold a lock, and each waits for the lock the other one holds. Neither can go on, so neither ever lets go.

import threading as th
lock_a = th.Lock()
lock_b = th.Lock()
def one():
    with lock_a:
        with lock_b: pass
def two():
    with lock_b:
        with lock_a: pass
th.Thread(target=one).start()
th.Thread(target=two).start()

Thread one takes lock_a and then wants lock_b. Thread two takes lock_b and then wants lock_a. If each gets its first lock before the other asks for it, they wait on each other forever.

Like the race, this depends on timing. Run it as written and it usually finishes, because the first thread is done before the second one starts. Put a time.sleep(0.1) between the two with lines in each function and it hangs every time.

The four conditions. A deadlock needs all four of these at once. Break any one and it can't happen.

  1. Mutual exclusion: a lock can be held by only one thread.
  2. Hold and wait: a thread holds one lock while it waits for another.
  3. No preemption: nobody can take a lock away from a thread.
  4. Circular wait: one waits for two, and two waits for one.

Say it like this. "A deadlock is a cycle of threads, each waiting for a lock the next one holds. The usual fix is lock ordering: every thread takes the locks in the same order, so a cycle can't form."

def two():
    with lock_a:            # same order as one()
        with lock_b: pass

The follow-up. "How else can you avoid it?" Take the lock with a timeout and back off if you don't get it, lock.acquire(timeout=1). Hold one lock at a time where you can. Or use one bigger lock in place of two small ones.

5. virtual memory

What it means. Every process gets its own address space, and it looks to the process like it has the memory to itself. Those addresses are virtual. The OS and the CPU translate them to real, physical addresses, and they do it a page at a time. A page is a fixed-size chunk, usually 4 KB.

PAGE = 4096
table = {0: 7, 1: 3, 2: 9}

def translate(addr):
    page = addr // PAGE
    offset = addr % PAGE
    frame = table[page]
    return frame*PAGE + offset

In the reel the address is 8200. 8200 // 4096 is page 2, and 8200 % 4096 is offset 8. The page table says page 2 lives in frame 9. So the physical address is 9 * 4096 + 8, which is 36872. The page moved and the offset stayed the same.

A real page table is a tree with several levels that the OS keeps and the CPU walks, not a dict. The arithmetic is the same.

Say it like this. "Virtual memory gives each process its own address space. An address is split into a page number and an offset, a page table maps each page to a physical frame, and the hardware does that lookup on every access. It keeps processes out of each other's memory, and it lets the OS hand out more memory than the machine has by keeping pages that aren't in use on disk."

The follow-up. "What's a page fault?" The process touches a page that isn't in physical memory right now. The CPU hands control to the OS, which finds a free frame, loads the page from disk, updates the page table and lets the instruction run again. If the address was never valid in the first place, that's a segmentation fault.

"What's the TLB?" A small cache in the CPU of recent page to frame translations, so most accesses skip the page table walk.

how to practise with this

Explain each one out loud, without notes, in under 30 seconds. If you can't, you don't know it yet. Then answer the follow-up.

Run the examples. Add the lock to the race and watch the number settle. Add the sleeps to the deadlock and watch it hang, then fix the order.

honest limitations

Five ideas isn't the whole course. Scheduling, system calls, file systems and communication between processes all come up, more so for systems and infrastructure roles.

The examples are Python, and standard Python's global interpreter lock hides a lot of thread trouble. The same code in Java, C++ or Go shows the race far more readily.

The virtual memory example is the arithmetic only. Real systems use multi-level page tables, a TLB and more than one page size.

If you're preparing for the other rounds too, the system design cheat sheet, the coding interview cheat sheet and the data structures cheat sheet cover those.

← all guides

Done nicnonac.com 56.6k