1. The Real-Life Analogy
Think of your CPU as an industrial park. Switch the view to understand the difference.
The Factories (Processes)
Process = A Factory
Imagine a Process as a standalone Factory.
- domainIsolation: Each factory has its own land, electricity, and water. If one factory burns down, the one next door is safe.
- inventory_2Resources: Moving equipment between factories is slow (requires trucks/shipping). This is like Inter-Process Communication (IPC).
- engineeringOverhead: Building a new factory takes a long time and lots of money (System Resources).
memory Memory Model
PID: 101
Code
Data/Heap
Stack
PID: 102
Code
Data/Heap
Stack
Processes have completely separate memory blocks.
table_chart Technical Comparison
| Feature | Process | Thread |
|---|---|---|
| Definition | Program in execution | Segment of a process |
| Memory | Isolated (Own Heap/Stack) | Shared (Shared Heap, Own Stack) |
| Creation Cost | High (OS must allocate memory map) | Low (Just a stack & registers) |
| Context Switch | Slow (Flush cache/TLB) | Fast (Keep memory map) |
| Communication | Complex (Pipes, Sockets, IPC) | Simple (Shared Variables) |
| Stability | High (Independent) | Low (One crash kills all) |
terminal Code Lab: Shared Memory Simulation
See how memory sharing actually works in C++.
#include <unistd.h> #include <iostream> int global_val = 10; int main() { pid_t pid = fork(); if (pid == 0) { // Child Process global_val = 20; std::cout << "Child Val: " << global_val << std::endl; } else { // Parent Process sleep(1); // Wait for child std::cout << "Parent Val: " << global_val << std::endl; } return 0; }
Terminal Output
Click 'Run' to compile and execute...
Key ConceptCopy-on-Write: When
fork() is called, the OS duplicates the process memory. The child's modification to global_val happens in its own separate memory copy. The parent's variable remains untouched.