This project is a visual, interactive simulation of the Dining Philosophers Problem using HTML, CSS, and JavaScript for educational purposes. It helps you understand concurrency, deadlock, and starvation handling in the classic synchronization problem.
π Deployed Link:
https://dining-philosopher-preemptive.vercel.app/
β
Visual circular layout with 5 philosophers and 5 forks
β
Manual buttons to simulate philosophers taking and putting forks
β
Color-coded states:
- Blue: Thinking
- Orange: Hungry
- Red: Starving (>10s)
- Green: Eating
β Fork color indicators (gray = available, red = in use)
β Starvation detection: Automatically forces a philosopher to eat if waiting >10s
β Deadlock detection: Detects and resolves deadlock automatically
β Displays wait time for each philosopher when hungry
- Each philosopher requires two forks (left and right) to eat.
- Uses a mutex lock (simulated in JavaScript) to prevent race conditions.
- Philosophers transition:
- THINKING β HUNGRY β EATING β THINKING
- Deadlock detection: triggers if all philosophers are hungry and no forks are held.
- Starvation detection: forces eating if a philosopher has been hungry for >10 seconds.
This simulator is contained in a single HTML file:
- Inline CSS for styling
- JavaScript for the simulation logic
- Buttons for manual controls
- Circle layout visualization
- Download or copy the
index.htmlfile. - Open it in your web browser (Chrome, Firefox, Edge).
- Use the buttons to manually simulate philosophers taking and putting forks.
- Observe the live state updates and automatic deadlock/starvation handling.
Philosopher X take forksβ Attempt to pick up forks for Philosopher X.Philosopher X put forksβ Put down forks for Philosopher X, returning to thinking.
- Philosophers (P0βP4): Positioned in a circle, each shows:
- ID (e.g.,
P0) - Wait time (in seconds) when hungry
- ID (e.g.,
- Forks (F0βF4): Placed between philosophers, color indicates availability.
β
Understand mutual exclusion, deadlock, and starvation in concurrency.
β
Visualize the impact of neighbors on philosopher state transitions.
β
Observe how deadlock and starvation are detected and resolved in practice.
- Adjust the number of philosophers by modifying the
Nconstant and adding/removing philosopher and fork divs. - Modify the starvation threshold (
10000ms) to tune starvation detection. - Extend with automatic round-robin or random simulation logic if desired.
This project is provided for educational and personal learning.
Feel free to modify, extend, or adapt for your OS, DBMS, or concurrency labs.