PsuedoCode due on 10/24. (Exam 10/26) 


Note - I say pseudocode because I'm not requiring you to compile and
make it work with threads/processes.  However, there should be no
question of what your code will do when you hand it in to me.  In this
particular instance, I want pseudocode that very closely resembles
what you will eventually type for your function as real code.

The dining philosophers problem with 'n' philosophers.  In your
implementation, you may focus on 'n'=5, but for full credit, you
should present a solution that can easily be extended to any 'n'
without deadlock or starvation.


Consider five philosophers who spend their lives thinking and
eating. The philosophers share a circular table surrounded by five
chairs, each belonging to one philospher. In the center of the table
is a bowl of rice and the table is laid with five single
chopsticks. Each philosopher (Pi) will see a chopstick to his right
(Ci) and left (C(i+1)%n). When a philosopher thinks, she does not
interact with her colleagues. From time to time, a philosopher gets
hungry and tries to pick up her chopsticks to eat. A philosopher may
only pick up one chopstick at a time and may not steal the chopstick
from a neighbor that is holding it. When a hungry philosopher has both
chopsticks, she eats. When she is finished eating, she puts down both
chopsticks and resumes thinking.

You are to write a philosopher that is free from deadlock and
starvation. You may assume that the philosopher has the variable i at
her disposal. For example the prototype for a philosopher might be:

void philosopher(int i);


The philosopher will get hungry, eat and think in a loop. You can
simulate this with a while(true) loop. You may also assume that your
philosopher knows how many philosophers there are at the table as a
global variable 'n'. Thus allows the 'last' philosopher (P4 in the n=5
case) to understand that her left chopstick is chopstick #0 or if you
prefer (i+1)%n.




So the overall structure of your psuedocode could look like:

//assumed globals
const int n = 5;
int state[n];

.
.
.

void philosopher(int i)
{

   while(true) 
     {
     //think until hungry
     state[i] = THINKING; 
     think();	

     // I am hungry, so get my chopsticks
     state[i] = HUNGRY; 
     // YOU HAVE TO WRITE THIS PART

     // eat until full
     state[i] = EATING;
     eat();

     // put down my chopsticks
     // YOU HAVE TO WRITE THIS PART

     }
}