Google Chrome isolates each browser tab into a distinct OS process. If a web page runs malicious JavaScript or crashes with a segmentation fault, only that single tab process dies. Conversely, high-performance web servers (e.g., Envoy, Netty) use thread pools sharing a cache to handle hundreds of thousands of concurrent client connections with minimal memory footprint.
// Linux POSIX fork() with Copy-On-Write (COW)
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
int main() {
pid_t pid = fork();
if (pid < 0) {
perror("fork failed");
return 1;
} else if (pid == 0) {
// Child process executes
printf("Child Process: PID = %d, Parent PID = %d\n", getpid(), getppid());
} else {
// Parent process waits for child to prevent Zombie state
wait(NULL);
printf("Parent Process: Child %d finished\n", pid);
}
return 0;
}Memory is structured into Text segment (compiled machine code, read-only), Data segment (initialized globals), BSS segment (uninitialized globals), Heap (grows upward via malloc/new), and Stack (grows downward for function calls and local variables).
fork() clones the parent process with Copy-On-Write (COW) page tables. execve() replaces the address space with a new executable binary.
The kernel tracks states: New -> Ready -> Running -> Waiting (Blocked) -> Terminated. The PCB stores PID, Program Counter, CPU registers, scheduling priority, and open file descriptors.
When the CPU scheduler preempts Process A for Process B, it saves A's registers into its PCB, updates CR3 page table base register (flushing the CPU TLB cache), and restores B's registers from its PCB.
| Feature / Dimension | Process | Thread |
|---|---|---|
| Address Space & Memory | Completely isolated virtual address space (Text, Data, Heap, Stack). | Shares Text, Data, Heap, and open file descriptors; private Stack & Registers. |
| Creation & Destruction Overhead | Heavyweight: OS must allocate page tables, file descriptors, and PCB. | Lightweight: Allocates only a TCB (Thread Control Block) and private stack (~2MB or 8KB). |
| Context Switch Cost | High: Switches page directory (CR3 register) and invalidates TLB cache entries. | Low: Maintains same page tables; only saves/restores CPU registers and stack pointer. |
| Communication Method | Requires Inter-Process Communication (IPC): Pipes, Sockets, Shared Memory, Message Queues. | Direct read/write access to shared global memory variables and heap pointers. |
| Failure / Crash Impact | Isolated: Crash (e.g., segfault) does not terminate neighboring processes. | Fatal: Unhandled crash or memory corruption in one thread kills the whole process. |
Detailed answers, interviewer pro tips, key takeaway summaries, and code examples formulated for technical rounds.
✅ Correction: On a single-core CPU, multithreading CPU-bound tasks adds context switching overhead, making total execution slower. Multithreading benefits single-core systems only when threads are I/O-bound (waiting on disk/network).
✅ Correction: After fork(), parent and child share the exact same underlying open file table entries in the kernel, sharing file offset pointers until separate close() calls are made.
Process = Isolated memory unit with PCB. Thread = Schedulable execution path inside a process sharing memory.