Concurrency Issues Continued
Part 1 - Knitting Nannies Problem
A very common concurrency problem which occurs is that of the knitting nannies. In this problem, there are 5 grandmothers sitting at a circular table. In the center of the table is a big ball of yarn. There are a total of 5 knitting needles, one on both the right and left of each person. Since there are only 5 knitting needles, the grandmothers must share their needles with the other grandmothers on each side of them. In order to knit, a grandmother must have both the left needle and the right needle. The purpose of this problem is to design a way to take needles such that no grannie does not knit. Assume that when a grandmother gets done knitting and returns the needles, she is still not done with her blanket and will continue to try to get the needles.
A common incorrect solution is given below:
GetNeedles()
Take Left Needle
Take Right Needle
Knit
Return Left Needle
Return Right Needle
The problem with this solution occurs when each of the grandmothers grabs their left needle at the same time. At this point, everyone is waiting for the right needle. However, none of the grandmothers will put a needle down and therefore they will never get to knit. 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 needle. Taking a needle can be implemented with the semTake() function, and returning a needle 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.