UMBC CMSC421 UMBC | CSEE | CMSC421 | Fall 1999 (Section 0101)

 

Dining Philosopher Monitor Design

Purpose

The purpose of this project is to design the dining philosopher monitor using Lock and Condition only.

Available Resources

We can use both the Lock and Condition classes to solve the dining philosophers problem. The dining philosopher class has the following public interfaces that we must support:

class PhilMonitor {
   Public:
     PhilMonitor();
     ~PhilMonitor ();
     Void PickUp(int I);      // must be called before eating to make sure that I
                              // can take the forks to my left and right hand side
     Void PutDown(int I);     // must be called after eating to put down the
                              // forks I used to eat with
     Void Test(int I);        // make sure phil 'I' can eat.
  Private:
     // I need some more in here
};

All I have to do in my design is describe the private members of the class and how they are used in the three main functions : PickUp, PutDown, and Test. Note that I will not be saying anything about Lock and Condition as they were given to me.

The Design

Each philosopher is a thread and each thread needs to call 'PickUp' before starting to eat and 'PutDown' after getting full. Private members and public functions are explained below.

New private members

Int State[5]       // Each phil. has his own state. 0 = thinking, 1 = hungry,
                   // 2 = eating.
Condition self[5]  // Each phil. has his own condition variable.
Lock L             // Lock to enter and leave the monitor.

Design of each function

Void PickUp(int I)
  Acquire the lock L 
  Set my state to Hungry
  Test to see if I can get the forks to my right and left (see Test())
  If I am not Eating, then I have to wait on my condition variable 'self'
  Release the lock L

Void PutDown(int I)
  Acquire the lock L 
  Set my state to Thinking
  Test to see if my left neighbor is Hungry (see Test() down)
  Test to see if my right neighbor is Hungry (see Test() down)
  Release the lock L

Void Test(int I)
  No lock is needed since Test is called within PickUp and PutDown. 
  If phil. 'I' is hungry and neither of his neighbors is eating then
    Set phil. I's state to Eating
    Wake up phil. I from his condition variable sleep

We also need to implement the code for each philosopher thread as follows:

Phil (int I)
  forever:
    PickUp (I)
    // Wait for some time while eating...
    PutDown (I)
    // Wait for some time while thinking...

Testing

The test strategy for this design will be to try this out with varying interrupt behavior. Basically, we can run this many times and make sure that it works. Another test we may want to run is to vary the think time and eating time for different philosophers, making sure that reducing thinking time still keeps things fair (and working).


Syllabus | Schedule | News & Notes | Grades | Feedback
Submit | Homework: 1 2 3 4 5 6 | Project: 1 2 3 4


Last updated 3 Dec 1999 by Ethan Miller (elm@csee.umbc.edu)