Friday fun : the Dining Philosophers
Andrey Demenev
Posts: 377
A quick fun exercise demonstrating the classic dining philosophers problem which was formulated by Edsger W. Dijkstra and Tony Hoare.
Five philosophers spend their lives thinking and eating. The philosophers share a common dining room where there is a circular table surrounded by five chairs, each belonging to one philosopher. In the center is a large bowl of spaghetti, and the table is laid with five forks. On feeling hungry, a philosopher enters the room, sits in his own chair, and picks up the fork on the left of his plate. Unfortunately, the spaghetti is so tangled that he needs to pickup and use the fork on his right as well. When he has finished, he puts down both forks and leaves the room.
The dining philosophers problem, while it seems very simple, allows to study different issues of concurrent programming, such as resource starvation and deadlocks.
Here is video visualizing the dinning philosophers problem.
In attached archive find the code. The code is a modified version of the code used to create the above vide. The modificaions were made to model different behaviors of the philosophers:
1. Random - the philosophers think for random time, then wait for their forks, then eat for random time
2. Lazy - the philosophers become lazy, they do not think, and always hungry
3. Sync - at a certain moment all philosophers decide to enter the dining room.
These behaviors demonstrate different situations that can occur in cuncurrent programs
The program outputs video on pins 12..15 Have fun!
Edit: reduced stack reserved for each philosopher object to make the program fit RAM without compiler optimisations
Edit : updated video
Five philosophers spend their lives thinking and eating. The philosophers share a common dining room where there is a circular table surrounded by five chairs, each belonging to one philosopher. In the center is a large bowl of spaghetti, and the table is laid with five forks. On feeling hungry, a philosopher enters the room, sits in his own chair, and picks up the fork on the left of his plate. Unfortunately, the spaghetti is so tangled that he needs to pickup and use the fork on his right as well. When he has finished, he puts down both forks and leaves the room.
The dining philosophers problem, while it seems very simple, allows to study different issues of concurrent programming, such as resource starvation and deadlocks.
Here is video visualizing the dinning philosophers problem.
In attached archive find the code. The code is a modified version of the code used to create the above vide. The modificaions were made to model different behaviors of the philosophers:
1. Random - the philosophers think for random time, then wait for their forks, then eat for random time
2. Lazy - the philosophers become lazy, they do not think, and always hungry
3. Sync - at a certain moment all philosophers decide to enter the dining room.
These behaviors demonstrate different situations that can occur in cuncurrent programs
The program outputs video on pins 12..15 Have fun!
Edit: reduced stack reserved for each philosopher object to make the program fit RAM without compiler optimisations
Edit : updated video
zip
26K
Comments
Here is a solution written in occam:
http://www.wotug.org/parallel/occam/projects/occam-for-all/hlps/sp.html
Very nice - and very clever graphics.
As heater mentioned, Catalina includes two examples of the dining philosophers - one "multicog" version that simulates a philosopher using C programs running in separate cogs (like yours, this version is limited to 5 diners by the need to also run the video drivers etc) and another "multithreaded" version that simulates 16 diners on a single cog by using the Catalina multihreading library.
Both of mine are simple text versions - I think I like your version better!
Ross.
But, when I press F9, it says that I am 91 longs to much.
so I reduced Philo[5] down to Philo[3], and the text portion will run twice, but no graphics..:frown:
I am using the Prop Pro Dev Board.
Whatever did I do wrong? the PPDB, uses the 256k , do I need the 512k? err..EEPROM that is...
You could also try cutting down the stack for each philosopher. The 60 may just be a play-it-safe value, 32 (my default) compiles (untested).
My wife saw the graphics on the screen, and she wanted to know, "how much THIS machine is going to cost..."