The Dining Philosophers
Overview
Create a program that demonstrates the advantages of using multithreading.
Description
A graphical program was created to demonstate the uses of multithreading to solve a variation of the dining philosophers problem (Link). The problem is used to illustrate the issue of deadlock and goes as follows. Five philosophers are sitting around a table. Each has one chopstick. Each has a bowl of rice. The philosophers can be doing one of three things: working, sleeping, or eating. The cycle a philosopher takes is working -> sleeping -> eating. To eat a philospher must have two chopsticks. This raises a problem however as there are only 5 chopsticks and 5 philosphers meaning only 2 philosphers can eat at one time. If each philosopher picks up one chopstick the reach a deadlock situation as each of the philosophers is waiting on another chopstick to eat the rice but no philospher is willing to give up the chopstick he already holds.
In the program each philosopher has its own thread. The chopsticks are available to any philosopher by a request from that philosopher. The chopsticks are held in a container.
To deadlock problem was solved in two ways. Firstly the function which allows a philosopher to access the chopstick container was protected by a mutex. Secondly a semaphore was used to stop all the philosophers entering the function to grab a chopstick. Using these two methods resolved the issue of the philosophers reaching a deadlock condition.
Details
Category | Details |
Software used | Visual Studio |
Framework/libraries used | OpenGL, process.h |
Target Platform | Windows PC |
Language(s) used | C++ |