OS & Systems FundamentalsProcesses vs ThreadsEasy⏱️ ~2 min

Process vs Thread: Core Architecture and Isolation

Definition
A process is an isolated execution environment with its own memory space, while a thread is a lightweight execution unit that shares memory with other threads in the same process.

The Fundamental Difference

When your operating system launches a program, it creates a process. This process gets its own virtual address space, typically 4GB on 32 bit systems or 128TB on 64 bit systems. No other process can read or write this memory directly. The CPU and operating system enforce this isolation at the hardware level through memory protection.

Threads exist within a process. A single process can spawn multiple threads, and all threads share the same heap memory (dynamically allocated memory for objects and data structures). However, each thread gets its own stack (memory for function calls and local variables), typically 1-8MB per thread.

Why Isolation Matters

Process isolation provides crash containment. If one process corrupts its memory or crashes, other processes continue running unaffected. This happens because each process operates in a separate virtual address space, and the operating system terminates only the faulting process.

Thread crashes behave differently. If one thread corrupts shared heap memory, every thread in that process sees the corruption. If one thread triggers a segmentation fault (illegal memory access), the operating system terminates the entire process, killing all its threads.

Resource Costs

Creating a process requires the operating system to allocate a new virtual address space, set up page tables (data structures mapping virtual to physical memory), and duplicate file descriptor tables. This takes 1-10ms depending on the system.

Creating a thread only requires allocating a new stack and registering with the scheduler. This takes 10-100 microseconds, making thread creation 10-100x faster than process creation.

Context Switching

When the CPU switches between processes, it must flush the TLB (Translation Lookaside Buffer, a cache for virtual to physical address translations). This flush forces subsequent memory accesses to reload translations, adding 1000-2000 CPU cycles of overhead.

Thread switches within the same process avoid TLB flushes because threads share the same address space. This makes thread context switches 2-5x faster than process switches.

💡 Key Insight: The choice between processes and threads is fundamentally about trading isolation for performance. Processes give you crash containment and security boundaries. Threads give you shared memory and faster communication.
💡 Key Takeaways
Processes have isolated memory spaces enforced by hardware; threads share heap memory within a process but have separate stacks
Process creation takes 1-10ms; thread creation takes 10-100 microseconds, making threads 10-100x faster to spawn
A crash in one thread kills the entire process; a crash in one process leaves other processes unaffected
Process context switches require TLB flushes adding 1000-2000 CPU cycles; thread switches avoid this overhead
Each thread stack consumes 1-8MB; excessive thread creation can exhaust memory before CPU becomes the bottleneck
📌 Interview Tips
1When asked why web servers use threads, explain that handling 10,000 connections with processes would require 10,000 separate address spaces, while threads share one address space with minimal overhead
2If asked about security boundaries, explain that untrusted code should run in separate processes because a malicious thread can read any memory in its process
3When discussing worker architectures, mention that process pools trade 10-100x creation overhead for crash isolation, which matters when running potentially unstable code
← Back to Processes vs Threads Overview