Process vs Thread: Core Architecture and Isolation
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.