// ***************************************************************** // * Mike Erlinger // * CS 110 // * Dining Philosophers // * Project 2 // * $Id: phil2.cc,v 1.1 1998/03/17 17:24:50 mike Exp mike $ // * // * // * This program is an implementation of the Dining Philosophers // * It starts with the solution from Tanenbaum's book. // * There are either 4 input parameters or defaults are used: // * Number of Phils - Default 5 // * Eating Time - Default 6 sec // * # of Eats - Default 4 // * Thinking Time - Default 3 sec // * // * Semaphores: // * Mutex, controls shared memory, initialized to 1. // * Sems array of semaphores used to allow hungry phils to block // * waiting for a fork, initialized to 1 // * Done, controls when all processes done, initialized to number // * of phils + 1. // * // * Shared Memory // * An array of state variables for each phil. // * Values are: Thinking, Hungry, Eating, Done. // * Shared memory is acquired and attached in main. // * // * Operation // * there is a main program which sets up all the semaphores // * and shared data area. // * It then forks off a number of phils, each phil is its own process. // * main also forks off a waiter process, which prints out the status // * of the table every 2 seconds. // * main waits for all the phils and waiter to finish and then cleansup // * and dies. // * phils, think, then get hungry, then eat, then think, ... // * eventually, they are done. // * // * A run of the program follows. Look at it for format. I did a lot of output // * reformatting to get this and it may have actual runtime errors. turing:phils Dining Philosophers program No parameters, then there are 5 philosophers, who eat for 4 sessions with 6 seconds a session otherwise: parameter 1 = Number Philosophers: parameter 2 = Eating Time: parameter 3 = Number of Eating Sessions: -------------------------------------- I AM THE WAITER #: Phil 0 Phil 1 Phil 2 Phil 3 Phil 4 Thinking Hungry Eating Left 1 Thinking Thinking Thinking Thinking Thinking 5 0 0 0 2 Thinking Thinking Thinking Thinking Thinking 5 0 0 0 3 Hungry Eating Hungry Hungry Eating 0 3 2 0 4 Hungry Eating Hungry Hungry Eating 0 3 2 0 5 Hungry Eating Hungry Hungry Eating 0 3 2 0 6 Thinking Thinking Thinking Thinking Thinking 5 0 0 0 7 Hungry Eating Hungry Hungry Eating 0 3 2 0 8 Hungry Eating Hungry Hungry Eating 0 3 2 0 9 Hungry Eating Hungry Hungry Eating 0 3 2 0 10 Thinking Thinking Eating Thinking Thinking 4 0 1 0 11 Thinking Thinking Eating Thinking Thinking 4 0 1 0 12 Hungry Hungry Eating Hungry Eating 0 3 2 0 13 Hungry Eating Thinking Hungry Eating 1 2 2 0 14 Hungry Eating Thinking Hungry Eating 1 2 2 0 15 Thinking Thinking Eating Thinking Thinking 4 0 1 0 16 Hungry Hungry Eating Hungry Eating 0 3 2 0 etc....