Shop OBEX P1 Docs P2 Docs Learn Events
Friday fun : the Dining Philosophers — Parallax Forums

Friday fun : the Dining Philosophers

Andrey DemenevAndrey Demenev Posts: 377
edited 2011-02-04 20:17 in Propeller 1
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

Comments

  • LeonLeon Posts: 7,620
    edited 2011-02-04 08:09
    Hoare discusses the problem in his CSP book.

    Here is a solution written in occam:

    http://www.wotug.org/parallel/occam/projects/occam-for-all/hlps/sp.html
  • Heater.Heater. Posts: 21,230
    edited 2011-02-04 08:24
    RossH has demonstrated the dining philosophers problem in C with Catalina. I belive each of the philosophers is running in a separate COG
  • Andrey DemenevAndrey Demenev Posts: 377
    edited 2011-02-04 08:28
    Of course, that is the natural way to do it on Propeller :)
  • mparkmpark Posts: 1,305
    edited 2011-02-04 10:57
    Love the graphics!
  • RossHRossH Posts: 5,519
    edited 2011-02-04 13:46
    Hi Andrey,

    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.
  • TtailspinTtailspin Posts: 1,326
    edited 2011-02-04 19:26
    I wanted to try Your Program Andrey,
    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...
  • kuronekokuroneko Posts: 3,623
    edited 2011-02-04 19:42
    Ttailspin wrote: »
    I wanted to try Your Program Andrey,
    But, when I press F9, it says that I am 91 longs to much.
    Both bst (in compatibility mode) and proptool complain. Using bst with unused-method-removal compiles just fine.

    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).
  • Andrey DemenevAndrey Demenev Posts: 377
    edited 2011-02-04 19:56
    kuroneko, thanks for the note about stack. I have uploaded a modified version to first post. Tested with both BST with all optimizations off and PST.
  • TtailspinTtailspin Posts: 1,326
    edited 2011-02-04 20:17
    Cool, this version works perfectly, thank's Andrey.
    My wife saw the graphics on the screen, and she wanted to know, "how much THIS machine is going to cost..."
Sign In or Register to comment.