Processes & threads
Operating Systems ยท 10 interview questions
A process is a program in execution together with everything it owns: its address space, open file descriptors, and accounting information. Processes are isolated from each other, so one crashing does not corrupt another.
A thread is an execution path within a process. Threads of the same process share the address space, heap and file descriptors, but each has its own stack, registers and program counter. That sharing is what makes threads cheap to create and switch between โ and also what makes them dangerous, because two threads can touch the same memory with no protection.
The cost difference is concrete. Switching between processes requires swapping page tables and usually flushing the TLB; switching between threads of one process does not, because the address space is unchanged.
Processes & threads interview questions
- What's the difference between a process and a thread?
- A process has its own address space and resources and is isolated from others. A thread runs inside a process, sharing its address space, heap and file descriptors, while keeping its own stack, registers and program counter.
- What exactly do threads of one process share, and what is private?
- Shared: code, global and heap memory, open files, signal handlers. Private: stack, registers, program counter, and thread-local storage.
- Why they ask: The precise split is the answer โ 'they share memory' is only half of it.
- Why is a process context switch more expensive than a thread switch?
- It changes the address space, so the page table base register is reloaded and the TLB is typically flushed, costing many subsequent misses. Threads in one process share the address space, so none of that is needed.
- What is a process control block?
- The kernel's record of a process: its id and state, saved registers and program counter, memory management information, open file table and accounting data. It's what makes a context switch resumable.
- What are the main process states?
- New, ready, running, waiting (blocked) and terminated. Ready means runnable but not scheduled; waiting means blocked on I/O or an event and not schedulable until it completes.
- What does fork() return, and to whom?
- It returns twice โ 0 in the child, and the child's process id in the parent. A negative value means the fork failed. That asymmetry is how each side knows which one it is.
- What is a zombie process?
- A process that has exited but whose exit status has not been collected by its parent, so its entry stays in the process table. It uses no memory or CPU, but leaking them exhausts process ids.
- What happens to a process whose parent dies first?
- It becomes an orphan and is re-parented to init (pid 1), which reaps it when it exits. Orphans are harmless โ zombies are the leak.
- Why they ask: Interviewers often ask both together to see whether you confuse them.
- What's the difference between user-level and kernel-level threads?
- Kernel threads are scheduled by the OS, so they use multiple cores and one blocking call doesn't stop the others. User-level threads are managed by a library and are cheaper to switch, but a blocking syscall in one can stall every thread in the process.
- How does copy-on-write make fork() cheap?
- The child initially shares the parent's physical pages, marked read-only. A page is copied only when either side writes to it. A fork followed immediately by exec therefore copies almost nothing.
You'll forget most of this by next week
That's not a discipline problem, it's how memory works. In the app these come back on an expanding schedule โ right before you'd lose them.
Start free for 7 days