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:

  1. Create a new project in Tornado.
  2. Download the source file source1.cpp and insert it into the project.
  3. Modify the progStart function to spawn 5 tasks, one for each philosopher.
  4. Implement the GetForks function(s). After acquiring both forks, print the number of the philosopher that is currently eating.
  5. Add any other necessary code which your project requires (i.e. run states, additional functions, etc.).

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:

  1. You値l need to implement a total of 7 tasks, label ed a through g.
  2. Tasks a, b, and c will be running at 1 Hz and will read the corresponding temperature sensor and store the value in the next buffer location, in a circular manner.
  3. Tasks d, e, and f will run at .2 Hz and will, each time they are invoked, average the 10 samples of the corresponding first-level buffers and store the result in the next second-level buffer location, in a circular manner.
  4. Finally, task g will run at .1 Hz and will, each time it is invoked, average the 2 samples of the corresponding second-level buffers and store the results in the third-level buffer location.
  5. Task g will compute the average across the three third-level buffers and store the result into the single forth-level buffer. In addition, task g will print the result to the console.

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:

  1. Explain why your solution for the dining philosopher problem is correct. Also identify the critical region, and explain how the circular wait for semaphores can cause deadlock.
  2. The dining philosopher problem has many solutions. Include pseudo-code for an alternative soltuion to the dining philosopher problem. Explain the benefits/drawback of each solution. Which do you think is better? Why?
  3. Write pseudo-code for designing the same tempurature network system using a single task and a delay function, and discuss the advantage/disadvantage of the multi-task versus single-task versions.


CS122B, Winter 2002