Threads vs Processes
You'll learn the key differences between threads and processes: how they share resources, their creation cost, and why switching between them has different overhead.
The Core Differences
Now that you know what both processes and threads are, let's understand what sets them apart.
Think of it this way: A process is like an entire house with its own kitchen, living room, and utilities. A thread is like a person living in that house—multiple people (threads) can share the same kitchen and living room (memory and resources), but each person still has their own personal space (stack and registers).
Resource Sharing
Processes are isolated from each other. Each process has its own separate:
- Memory space (heap, data, code segments)
- File descriptors
- Operating system resources
Threads within the same process share almost everything:
- Same memory space (heap, data, code)
- Same file descriptors
- Same OS resources
Each thread only has its own stack and CPU registers.
Creation Cost
Creating a new process is expensive. The OS must:
- Allocate separate memory space
- Copy or set up a new PCB
- Set up new page tables
Creating a new thread is much cheaper because it reuses the existing process's resources—only a small stack and thread-specific data structures need allocation.
Context Switching Overhead
Switching between processes is costly:
- Must save/restore entire PCB
- Must switch memory mappings (page tables)
- CPU caches often become invalid
Switching between threads of the same process is faster:
- Shared memory stays valid
- Only registers and stack pointer need switching
- Cache remains more useful
Key Takeaway: Threads are lighter-weight than processes—they share memory and resources within a process, cost less to create, and switch between faster, making them ideal for concurrent tasks that need to communicate frequently.