Process vs Thread — Visual & Conceptual Explanation
1) Fixing the Mental Model
A common misunderstanding:
A process is not “code executing” — it is an environment that allows execution.
Correct View
- Process = memory + resources
- Thread = execution happening inside that memory
2) What Does a Process Look Like?
Think of a process as a memory layout:
Memory Space:
-------------------------
| Code (instructions) |
| Data (globals) |
| Heap |
| |
| Stack ? |
-------------------------
At this point, a key question appears:
Is there only one stack?
3) Where Threads Appear
The reality:
Each thread has its own stack.
4) The Real Picture
Process:
================================================
Code (instructions) <-- shared
Data (globals) <-- shared
Heap <-- shared
-----------------------------------------------
Thread 1:
Registers
Stack 1
Thread 2:
Registers
Stack 2
Thread 3:
Registers
Stack 3
5) What is a Thread Exactly?
A thread consists of:
- Instruction Pointer (where execution is)
- CPU Registers (current state)
- Stack (function calls, local variables)
Key Insight
A thread is an execution context
6) Concrete Example
void *func(void *arg) {
int x = 10; // stored in stack
}
If 3 threads run this function:
-
Each thread has its own:
x- stack
But they share:
- the same code
- the same global variables
7) Visual Comparison
Shared vs Private
Shared Between Threads
----------------------
Code
Data (globals)
Heap
Thread A ---> Stack A + Registers
Thread B ---> Stack B + Registers
Thread C ---> Stack C + Registers
8) Intuition Model (Real-World Analogy)
-
Process = Kitchen
- recipes (code)
- ingredients (data)
- tools (heap)
-
Threads = Chefs
-
each chef has:
- their own workspace (stack)
- their own state (registers)
-
All chefs work in the same kitchen.
9) Important Clarification
A thread is NOT a separate memory section like code or heap.
It is:
- execution state
- plus its own stack
10) Execution Flow
Program (on disk)
↓
Process (memory + resources)
↓
Thread(s) execute instructions
↓
CPU runs one thread at a time
11) Multiple Threads Example
Shared Counter:
int counter = 0;
Thread 1 → increments
Thread 2 → increments
Thread 3 → increments
- All threads see the same
counter -
But each has:
- different execution point
- different stack
12) Final Mental Model
Process = Environment
Thread = Execution inside that environment
13) The Golden Insight
The process is the “stage”, and threads are the “actors performing on it”.
14) Critical Truth
- A process without threads → does nothing
- A thread without a process → cannot exist
End of Document
Comments (0)
No comments yet. Be the first!