Skip to content

djalilhebal/softviz-philosophers

Repository files navigation

PhiloFX: Dining Philosophers

Dinning philosophers problem visualization in Java #JavaFX #JavaScriptingAPI #Nashorn
(This is a rough work-in-progress--see the TODO file.)

Logo

After Trois Threads (useless vizualization) and Traffic Sim (web simulation), I think it's time for a more serious simulation in Java.

Screenshot of the graphical user interface

Screenshot of the command line interface

How to use

Write the philosopher(i) (or philosophe(i)) procedure to solve the dining philosophers problem using semaphores.

From the command line (on Linux!):

java -jar philo.jar PRESET
  • Presets:
    • sol0: Mutex, only one philo can eat at a time.
    • sol1: Sem for each fork, deadlocks are possible (written in French)
    • sol2: Sem for each philo, philosophers wake one another to eat
    • sol3: The last philo is left-handed
    • sol4: Only N - 1 philos can attempt to acquire forks at any given time

* These are read from the presets folder in the current working directory.

Predefined functions

  • N is the total number of philosophers

  • think() or penser(): Lasts for 5-10 seconds, refills philosopher's "health".

  • eat() or manger(): Lasts for 5-10 seconds

  • takeFork(i) or prendreFourchette(i): Actually take the fork!

  • putFork(i) or poserFourchette(i): Actually put the fork down!

  • Sem(n): Same as new Semaphore(n, true)

  • p(sem): Same as sem.acquire()

  • v(sem): Same as sem.release()

How it works

...

Nashorn to execute user code

UML class diagram

Java

  • JavaScript's setTimeout/setInterval counterpart in Java: Timer and TimerTask.

  • Visualizing using SVG by updating it through the Document interface via JavaFX's WebEngine.

Notes

  • Alternative scripting engine: Mozilla's Rheno

References and Good Reads

  • Oracle's Java Scripting Programmer's Guide
  • Andrew S. Tanenbaum - Modern Operating Systems 4th ed.

Credits and Thanks

License

By @djalilhebal and @wanisramdani, under CC BY 3.0

About

The dinning philosophers problem in JavaFX

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published