Concept: How code lives in memory.
- What is it? A standalone program running in memory.
- Isolation: Fully independent. If one house burns down, the neighbor is safe.
- Cost: Expensive to build (heavy system resources).
- Privacy: Has its own private kitchen (Memory) and keys (Permissions).
- What is it? A worker living inside the Process.
- Sharing: They share the kitchen (Heap), living room (Global Vars), and bathroom (Files).
- Risk: If one roommate sets the kitchen on fire (Segfault), the whole house burns down.
Concept: How one CPU runs many things.
- The Illusion: It looks like 10 apps are running at once.
- The Reality: The CPU is juggling. It runs App A for 0.001 seconds, then App B, then App A again.
- Context Switch: The moment the CPU freezes App A to switch to App B. This "pause" costs energy (overhead).
- Scheduler: The "Boss" who decides whose turn it is next.
Rule of Thumb:
- Concurrency: One juggler handling 3 balls (Time-slicing).
- Parallelism: Three jugglers, each with 1 ball (Multi-Core).
Concept: Making new processes in C.
When you run fork(), you don't start from scratch. You clone.
- Parent: The original.
- Child: A perfect duplicate (same code, same variables at that moment).
- The Split:
- If
fork()returns0โ You are now in the Child timeline. - If
fork()returnsPIDโ You are in the Parent timeline.
- If
โ ๏ธ Danger Zone: Zombies If the Child finishes but the Parent is too busy to check on it (waitpid), the Child becomes a Zombieโdead, but still taking up space in the RAM.
Concept: IPC (Inter-Process Communication).
Since Processes (Houses) are separate, they need special tools to talk.
| Method | The Analogy | Speed | Safety |
|---|---|---|---|
| Pipe | A pneumatic tube. You put data in one end, it pops out the other. | Slower | High (Safe) |
| Shared Memory | A whiteboard in the hallway. Anyone can write on it. | Instant | Low (Race Conditions) |
Concept: Avoiding conflicts (Race Conditions).
The Problem: Two threads try to update the same variable at the exact same time. The data gets corrupted.
The Solution: The Semaphore. Think of it as the key to a single-toilet bathroom.
wait()(Lock):- Is the key on the hook?
- Yes: Take key, enter, lock door.
- No: Wait outside (Sleep) until it's free.
post()(Unlock):- Exit, put key back on hook.
- The next person waiting grabs it.
Atomic: This means grabbing the key happens in one instant motion. You can't be interrupted while reaching for it.