After “process vs thread”, interviewers often ask what a context switch saves, why switching processes costs more than switching threads, and how fork() differs from exec(). They may then explore the process lifecycle through copy-on-write, zombies and orphans. These questions test whether you understand not just the definitions, but what the operating system actually does.
Process vs thread
A process is an independent program in execution with its own memory space (code, data, heap and stack). A thread is a unit of execution within a process. Threads in the same process share code, data and heap, but each has its own stack and registers.
Shared memory makes communication between threads easier, and switching between threads within the same process is faster than switching between separate processes. Rather than simply describing a thread as a “lightweight process”, explain what is shared, what remains private, and how this affects communication and switching.
What happens during a context switch?
A context switch saves the state of the currently running process or thread, including registers and the program counter, then loads the saved state of another. This allows the CPU to switch execution between tasks. The switch itself is overhead: no useful work gets done during it, and excessive context switching can hurt performance.
The direct cost comes from saving and restoring registers and kernel state. The larger, indirect cost comes from cache and branch-predictor pollution. The incoming task may encounter a cold cache and run more slowly for a while.
A process switch also involves an address-space change, which means swapping page-table roots and, without tagged TLB entries, flushing the TLB. Threads within the same process avoid this additional work, making thread switches noticeably cheaper.
A strong interview answer distinguishes the cost of saving and restoring state from the additional cost of switching address spaces. Simply saying “processes are heavier” does not explain the mechanism.
fork() and its return values
fork() creates a new process that is a near-duplicate of the caller, including its address space, file descriptors and registers. It returns in both processes, so the same code path must distinguish between the parent and the child. A negative return value indicates failure, zero identifies the child, and the parent receives the child’s PID.
pid_t pid = fork();
if (pid < 0) {
/* fork failed */
} else if (pid == 0) {
/* child process */
} else {
/* parent: pid is the child's PID */
waitpid(pid, NULL, 0);
}
The return value explains why fork() “returns twice”: both processes continue from the same point but receive different values. The wait call in the parent branch collects the child’s exit status, which is also important to the process lifecycle.
fork() versus exec()
fork() creates a process, whereas exec() replaces the current process image with a different program. exec() preserves the PID and any open file descriptors not marked close-on-exec.
Neither call alone performs the complete sequence used in the common shell pattern: the shell forks, then calls exec() in the child.
This order allows the child to set up redirections before the new program runs. When comparing the calls in an interview, focus on what each changes: fork() creates a child process, while exec() replaces a process image.
Why copy-on-write matters
With copy-on-write, the parent’s pages are marked read-only and shared with the child rather than duplicated immediately. A page is copied only when either process writes to it.
This makes fork() cheap because data pages are not copied up front, although the kernel still has to copy the page tables. If the child immediately calls exec() and discards the inherited image, no additional page-copying cost is incurred.
The trade-off is that memory accounting becomes optimistic. If a forked child starts writing to many pages, it can trigger an out-of-memory condition long after fork() returned successfully. This explains both the benefit of copy-on-write and why a successful fork() does not guarantee that later memory requirements can be met.
Zombie versus orphan
A zombie is a process that has terminated but remains in the process table because its parent has not collected its exit status with wait(). It uses no CPU time and retains no user memory, only its process-table slot and exit code.
A few zombies can be normal and transient. A growing number indicates that a parent is not collecting exit statuses and can eventually lead to process ID exhaustion. Killing a zombie has no effect. The solution is to fix or restart the parent so that child exit statuses are collected.
An orphan, by contrast, is a process whose parent exited first. The kernel re-parents it to init (PID 1), or on Linux to a designated subreaper process if one exists. The new parent reaps it when it eventually terminates.
Orphans do not leak resources in the same way zombies do. Deliberately orphaning a child by forking twice is a classic way to daemonise a process.
How to answer out loud
Explain the mechanism first, then its consequence. For example:
“A process switch changes address spaces, so it can require swapping page-table roots and flushing the TLB when entries are not tagged; a thread switch in the same process avoids that extra work, so it is cheaper.”
This structure demonstrates that you understand both the terminology and why the distinction matters.
The full set of 170 operating system interview questions includes answers across the topic.
Top comments (0)