Skip to content

Moabed21/Philosophers

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

4 Commits
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿ› ๏ธ Philosophers : The "Lite" Version

1. The Core Difference: Buildings vs. Roommates

Concept: How code lives in memory.

The Process = The House ๐Ÿ 

  • 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).

The Thread = The Roommates ๐Ÿ‘ฏ

  • 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.

2. The CPU: The Juggler ๐Ÿคน

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).

3. Creating Life: fork() ๐Ÿงฌ

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() returns 0 โ†’ You are now in the Child timeline.
    • If fork() returns PID โ†’ You are in the Parent timeline.

โš ๏ธ 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.


4. Communication: How to Talk ๐Ÿ—ฃ๏ธ

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)

5. Synchronization: The Bathroom Key ๐Ÿ”‘

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.

  1. wait() (Lock):
    • Is the key on the hook?
    • Yes: Take key, enter, lock door.
    • No: Wait outside (Sleep) until it's free.
  2. 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.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published