-------------------------- Dining Philosophers (D.P.) -------------------------- 5 Philosophers sit around a table with one chopstick between them. Philosopher's eat and think and nothing else for the rest of time. This is a classical OS problem. If you use the web, site vigorously. I will give you a 0 for any semblance of plagiarism or borrowed ideas that don't have such a citation. Write a philosopher that eats and thinks without starvation. You must not have a manager, so only the 5 philosopher threads should be running. You must not break mutual exclusion or allow preemption on the chopstick resources. Full credit solutions must gracefully solve both starvation and deadlock for full credit. I'd work on guaranteeing a solution to deadlock first and then use our critical section ideas to guarantee a solution from starvation. Remember bounded wait. Does your system have this? ------------------------ How to implement things: ------------------------ Create 5 threads. Each thread represents a philosopher. Use posix threads from last time. Anytime you see i+1, use (i+1)%N so that things wrap around. Let each philospher know their #. Number them 0 to 4. Use a mutex for each chopstick. There are 5 chopsticks numbered 0 to 4. Philosopher #i should use chopstick #i and chopstick #i+1. A philosopher may not eat unless that philosopher has locked the mutex for both chopsticks. Remember to use pthread_mutex_destroy(&mutex_object); to destroy your mutex. You can use ipcs and ipcrm to see any outstanding memory and remove it from the OS. Don't hand in code that leaves shared memory/mutex/etc sitting around on the system. --------------------------------------------------------------------------------- D.P. pseudocode - May help with mutual exclusion - but not deadlock or starvation --------------------------------------------------------------------------------- //in the code below, i indicates which philosopher (philo) we are. 0 - 4 void think(int i) { //report this event to the console - philo #i is thinking! /*** a do nothing function eventually you can consider adding a sleep here for a random period of time to simulate the fact that the philospher thinks for awhile, but a better test of starvation and bounded wait is not to have a sleep here. ***/ } void eat(int i) { // get chopstick i // get chopstick i+1 // eat for awhile - to start, leave this at 0 time, but eventually, add a sleep for random time // report this event to the console - philo #i is eating! // put down chopstick i+1 // put down chopstick i } void philospher(int i) { while(true) { think(i); eat(i); } }