Process and Thread Interview Questions with fork() Output Problems
Last updated 2026-11-19
Processes and threads are the first thing most OS interviewers ask about, because every later topic (scheduling, synchronization, memory) builds on them. Expect to define the process states, explain what a context switch saves, compare threads with processes, and predict how many processes a fork() program creates. This page works those examples with exact counts and also covers inter-process communication, thread models and the Python GIL.
For scheduling the processes once created see CPU scheduling interview questions, and for protecting shared data between threads see process synchronization. The broader plan is in how to crack an operating systems interview.
| fork rule | n unconditional fork() calls in sequence create 2^n processes |
|---|---|
| Also covered | IPC, thread models, zombie and orphan, Amdahl law, GIL |
| Code | C fork/wait, Python threads vs processes |
Process states in one line each
- New: being created. Ready: waiting in the ready queue for the CPU. Running: executing on a CPU.
- Waiting (blocked): waiting for I/O or an event. Terminated: finished, resources being released.
- Transitions: ready to running (dispatch), running to ready (preempted or yields), running to waiting (I/O request), waiting to ready (I/O complete).
Process and Thread Interview Questions with fork() Output Problems: questions with worked answers
1 What is the difference between a program, a process and a thread?
easyA program is a passive file of instructions on disk. A process is a program in execution, with its own address space, program counter, registers, stack, heap and open files, tracked by a PCB. A thread is the unit of CPU scheduling inside a process; threads of one process share the code, data, heap and file descriptors, and each has its own stack, registers and program counter.
2 List the process states and the transitions between them.
easyThe five-state model has new, ready, running, waiting and terminated. A new process is admitted to ready. The scheduler dispatches a ready process to running. A running process returns to ready on a timer interrupt, goes to waiting when it requests I/O or waits for an event, and goes to terminated on exit. A waiting process moves to ready when the event happens. There is no direct move from waiting to running, because the scheduler must choose it first.
3 What does a context switch save and restore for a process versus a thread?
mediumFor a process it saves the CPU registers, program counter and stack pointer into the PCB, and switches the memory context (page-table base register), which also invalidates or tags TLB entries. For a thread of the same process only the registers, program counter and stack pointer change, because the address space stays the same. That is why thread switches are faster.
4 How many processes are created by this code, and how many times is hello printed?
mediumEach fork() splits every existing process into two, so after three unconditional forks there are 2^3 = 8 processes: 1 original plus 7 new ones. All eight execute printf after the last fork, so hello is printed 8 times. In general n sequential fork calls give 2^n processes and 2^n - 1 new children.
c#include <stdio.h> #include <unistd.h> int main() { fork(); fork(); fork(); printf("hello\n"); return 0; }5 What does fork() return, and how many processes does fork() && fork() create?
mediumfork returns 0 in the child, the child's PID (positive) in the parent, and -1 on failure. In fork() && fork() the left fork runs in the original process P, creating child C1. In P the result is nonzero, so the right fork runs, creating C2. In C1 the left result is 0, so the && short-circuits and the second fork is skipped. Total processes = P, C1, C2 = 3, so 2 new processes are created. With || the evaluation flips: the right side runs only where the left returned 0.
6 Write a C program that forks a child, has the child run ls, and has the parent wait.
mediumThe child replaces its image with exec, and the parent calls wait to collect its exit status, which prevents a zombie. exec never returns on success, so the code after it runs only if it failed.
c#include <stdio.h> #include <stdlib.h> #include <sys/wait.h> #include <unistd.h> int main() { pid_t pid = fork(); if (pid < 0) { perror("fork"); exit(1); } if (pid == 0) { execlp("ls", "ls", "-l", (char*)NULL); perror("exec"); // only reached on failure _exit(127); } int status; waitpid(pid, &status, 0); printf("child exited with %d\n", WEXITSTATUS(status)); return 0; }7 Differentiate between zombie and orphan processes and say how to avoid zombies.
mediumA zombie has terminated, but its parent has not read its exit status with wait(), so its process-table entry remains (shown as Z or defunct in ps). An orphan is a live process whose parent has died; init or systemd adopts it. Avoid zombies by calling wait or waitpid, handling SIGCHLD, or setting SIGCHLD to be ignored so the kernel reaps children automatically. Killing a zombie has no effect; the parent must reap it or exit.
8 What are the inter-process communication mechanisms?
mediumPipes (unidirectional byte stream between related processes) and named pipes (FIFOs) for unrelated ones; message queues; shared memory, the fastest because no copying through the kernel but needing explicit synchronization; sockets, for processes on the same or different machines; signals for simple notifications; and memory-mapped files. Shared memory is chosen for large data volumes, message passing for simplicity and safety.
9 Compare user-level and kernel-level threads.
mediumUser-level threads are managed by a library without kernel knowledge: creation and switching are very fast, but if one thread blocks in a system call the whole process blocks, and they cannot run on multiple cores. Kernel-level threads are scheduled by the OS, so one can block while others run, and they use multiple cores, but creation and switching cost more. Modern systems use one-to-one mapping (Linux, Windows). Many-to-one and many-to-many models were used earlier.
10 Numerical: 75 percent of a program is parallelisable. What is the speedup on 4 cores according to Amdahl's law, and what is the limit?
mediumSpeedup = 1 / ((1 - P) + P / N) with P = 0.75, N = 4: 1 / (0.25 + 0.1875) = 1 / 0.4375 = 2.29. The limit as N grows is 1 / (1 - P) = 1 / 0.25 = 4, so no number of cores can give more than 4 times, because the serial 25 percent always takes the same time.
11 Why are threads called lightweight? What do they share and not share?
mediumCreating a thread does not need a new address space or copies of open files, so it takes far less time and memory than creating a process. Threads share the code, global data, heap, open files and signal handlers; they do not share the stack, registers, program counter or thread-local storage. Sharing makes communication fast, but it also means that any unsynchronized write to shared data is a race.
12 What is a thread pool and why is it used?
mediumA thread pool creates a fixed set of worker threads in advance and hands them tasks from a queue. It avoids the cost of creating a thread per request, bounds the number of concurrent threads so the system is not overwhelmed, and lets tasks queue when all workers are busy. Java's ExecutorService and Python's concurrent.futures are examples. Pool size is tuned to the number of cores for CPU-bound work and larger for I/O-bound work.
13 What is the Python GIL and how does it affect threads versus processes?
hardThe Global Interpreter Lock in CPython lets only one thread execute Python bytecode at a time, so CPU-bound Python threads do not run in parallel on multiple cores. Threads still help I/O-bound programs, because the lock is released while waiting on I/O. For CPU-bound work use multiprocessing or concurrent.futures.ProcessPoolExecutor, where each process has its own interpreter and GIL, at the cost of inter-process data transfer. Recent CPython versions have an experimental free-threaded build that removes the GIL.
14 How does Linux implement threads internally?
hardLinux represents every schedulable entity as a task_struct. Both fork() and pthread_create() call the clone() system call; the flags decide what is shared. A thread is created with CLONE_VM, CLONE_FILES, CLONE_FS and CLONE_SIGHAND, sharing the address space, file table and signal handlers, while a fork passes no sharing flags and copies the address space with copy-on-write. All threads of a process share a thread-group ID (the PID seen by users) but have distinct thread IDs.
15 What happens to the other threads when one thread calls exit or crashes?
hardIf any thread calls exit() or the process receives a fatal signal such as SIGSEGV from one thread, the whole process terminates, including all its threads, because they share one address space and signal disposition. If only a thread calls pthread_exit it ends alone, and the process continues until the last thread finishes. This is why a bug in one thread can bring down a multithreaded server, and why critical isolation uses separate processes, as browsers do per tab.
Frequently asked questions
Why is thread creation faster than process creation?
A thread shares the existing address space, page tables and open files, so the kernel only allocates a stack and a small control structure. A process needs a new address space, page tables (copy-on-write on fork) and copies of descriptor tables.
Can two threads of one process run on different CPUs at once?
Yes, with kernel-level threads on a multicore machine, they can run truly in parallel. In CPython the GIL limits Python bytecode execution to one thread at a time.
What is the difference between concurrency and parallelism?
Concurrency is dealing with many tasks at once by interleaving them, possible on one core. Parallelism is executing multiple tasks at the same instant, which needs multiple cores or processors.
What is the maximum number of processes or threads?
It is limited by configuration and memory, not by a fixed constant: on Linux by ulimit -u, /proc/sys/kernel/pid_max and available memory for stacks and kernel structures.
Related guides
- Process Synchronization Interview Questions: Semaphores, Mutex and Classic Problems
- CPU Scheduling Interview Questions with Gantt Chart Numericals
- Operating Systems Interview Questions with Worked Answers
- Deadlock Interview Questions with Banker's Algorithm Worked Example
- Linux Interview Questions and Answers: Concepts, Permissions and Processes
- Java Interview Questions and Answers
- Python Interview Questions and Answers
More in CS Fundamentals Interview Questions
- Process Synchronization Interview Questions: Semaphores, Mutex and Classic Problems
- Redis Interview Questions and Answers
- REST API Interview Questions and Answers
- Routing Protocols Interview Questions: RIP, OSPF, BGP and Shortest Path Examples
- SDLC and Agile Interview Questions
- Shell Scripting Interview Questions with Bash Examples
- SOLID Principles Interview Questions with Examples
- System Design Interview Questions for Freshers
- System Design Interview Questions with Worked Designs
- TCP vs UDP Interview Questions with Socket Code and Numericals
- TCP/IP Interview Questions: Handshake, Congestion Control and Numericals
- Transactions and ACID Interview Questions