Skip to content

AnasMultani17/Dining-Philosopher-Preemptive

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

3 Commits
Β 
Β 
Β 
Β 

Repository files navigation

Dining Philosophers Simulator 🍽️

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.

Live Demo

πŸ”— Deployed Link:
https://dining-philosopher-preemptive.vercel.app/

Features

βœ… 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

How it works

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

File Structure

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

How to Run

  1. Download or copy the index.html file.
  2. Open it in your web browser (Chrome, Firefox, Edge).
  3. Use the buttons to manually simulate philosophers taking and putting forks.
  4. Observe the live state updates and automatic deadlock/starvation handling.

Controls

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

Visualization Details

  • Philosophers (P0–P4): Positioned in a circle, each shows:
    • ID (e.g., P0)
    • Wait time (in seconds) when hungry
  • Forks (F0–F4): Placed between philosophers, color indicates availability.

Learning Objectives

βœ… 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.

Customization

  • Adjust the number of philosophers by modifying the N constant and adding/removing philosopher and fork divs.
  • Modify the starvation threshold (10000 ms) to tune starvation detection.
  • Extend with automatic round-robin or random simulation logic if desired.

License

This project is provided for educational and personal learning.
Feel free to modify, extend, or adapt for your OS, DBMS, or concurrency labs.

About

The Dining Philosophers Simulator visually demonstrates core concurrency concepts like mutual exclusion, deadlock, and starvation. It uses five philosophers and forks arranged in a circle, with manual controls, automatic deadlock/starvation detection, and real-time updates to help understand synchronization challenges in concurrent systems.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages