Lab 5 - Concurrency Issues Continued
Part 1 - Dining Philosophers Problem
A very common concurrency problem which occurs is that of the Dining Philosophers. In this problem, there are 5 philosophers sitting at a circular table. In the center of the table is food. There are a total a five forks, one on both the right and left of each philosopher. Since there are only five forks, the philosophers must share their forks with the philosopher on each side of them. In order to eat, a philosopher must have both the left fork and the right fork. The purpose of this problem is to design a way to take forks such that no philosopher starves. Assume that when a philosopher gets done eating and returns the forks, he is still hungry and will continue to try to get the forks.
A common incorrect solution is given below:
GetForks()
Take Left Fork
Take Right Fork
Eat
Return Left Fork
Return Right Fork
The problem with this solution occurs when each of the philosophers grabs their left fork at the same time. At this point, everyone is waiting for the right fork. However, none of the philosophers will put a fork down and therefore they will never get to eat. This situation is called deadlock and is the result of circular waiting.
In order to implement this problem, you will need to use binary semaphores. Use one semaphore for each fork. Taking a fork can be implemented with the semTake() function, and returning a fork can be implemented with a semGive() function.
In this lab, you will need to:
Part 2 - A Real World Problem: Temperature Sensor Network
In this lab, you値l be designing a Temperature Sensor Network. You will be reading 3 temperature sensors, each at a rate of 1 sample/second. The data collected from these 3 sensors will be stored in 3 ring-buffers of size 10 each. Once every 5 seconds, you値l be averaging the content of the 3 ring-buffers and storing the result in 3 additional ring-buffers of size 2. Once every 10 seconds, you値l be averaging the content of the 3 lower-level ring-buffers and storing the results in 3 additional ring-buffers of size 1, than, you値l compute the average of the 3 lowest-level buffers and store the result in a single ring-buffer of size 1 Okay, by now you should be confused! Perhaps a diagram will help to clarify:
For this lab, you will need to:
To help get you started, some framework code is provided here source2.cpp. Note that a structure, called temperature_t is defined that declares the buffers you値l need. Also, start and stop routines (prog_start, prog_end) are provided to start/stop your system. Since there will be a total of 7 processes reading/writing the shared temperature_t structure, you must use semaphores for mutual exclusion.
Part 3 - Lab Report
In your lab report, be sure to discuss the following: