CS 284 Operating Systems
Programming Assignment #6
Due: 10/1/2001 - Class Time
Write a multi-threaded C program that simulates the "Dining Philosophers"
problem as discussed on page 283. You may take an approach similar to that of
the book, or you may approach the problem differently - your choice. Regardless,
the following guidelines need to be observed:
- Philosophers
- Each Philosopher is a thread
- Each Philosopher will eat Breakfast, then Lunch, then Dinner
- No Philosopher should start the 'next' meal until all Philosophers have
completed the current meal
- Chopsticks
- Arrange for a 'shared' array of int to represent the sticks
- A Philosopher is NOT allowed to grab both sticks 'simultaneously; i.e.
- mutex lock the stick array
- if ONE of the sticks you need is available - take it
- unlock the stick array
- usleep(100);
- repeat the process for the other stick needed
- IF you can't get BOTH sticks
- give up the one that you are holding
- usleep(500);
- start the process over
- IF you CAN get both sticks
- simulate eating for sleep(a couple seconds);
- give back both sticks
- perform deep thought until you are awakened for the next meal;
i.e. cond_wait(xxx,yyy)
- Synchronization
- main() is allow to coordinate the Philosopher activity if you want
- keep the logic as simple as possible
- don't depend on thr_yield() to 'accidentally' make things happen in
the correct order. You MAY use thr_yield() to keep one Philosopher from
hogging all the CPU time.
- Output
- log which Philosopher eats which meal (don't produce tons of output)
- main() should log when 'breakfast' is finished, lunch is finished etc.
- main() should not exit until all Philosopher threads have thr_exit()ed.
You will use the 'script' command to capture all necessary information
for handin.