



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




In this project you will gain additional familiarity writing programs involving concurrency control. You will implement a couple of classical IPC problems to become more comfortable with Semaphores, Mutexes, Condition Variables, and the Monitor approach.
Set up a new directory for this project called cs333/p3 and download all the files from
~walpole/public_html/class/cs333/fall2005/spank/p
Even though some of the files have the same names, be sure to get new copies for each project. In general some files may be modified.
Please keep your old files from previous projects separate and don’t modify them once you submit them. This is a good rule for all programming projects in all classes. If there is ever any question about whether code was completed on time, we can always go back and look at the Unix “file modification date” information.
For this project, you should get the following files:
makefile DISK System.h System.c Runtime.s Switch.s List.h List.c Thread.h Thread.c Synch.h Synch.c Main.h Main.c
The file Synch.c contains my solution for project 2. You may use either my version of this file, or the version of Synch.c you created.
I have also included the file
Proj2Sol.pdf
which contains my solution code for Producer-Consumer and Dining Philosophers. If you had difficulty with it, you can take a look at the code I wrote.
You will modify and submit the following files:
Main.h Main.c
The textbook gives a solution to the “Sleeping Barber” problem in C on page 131; translate this into a working Spankish program.
You’ll also need to create some code to print out what happens when you run the program.
What will you do for the “cut_hair” and the “get_haircut” methods? You’ll need at least one print statement in each routine, but be careful: they both should run at more-or-less the same time.
One barber is okay, but how many customers will you model? Will you just throw a bunch of customers at the barbershop all at once? This might not test your code very well.
How long will the haircut last? You can implement waiting with a busy loop, such as
for i = 1 to 100 .. want to yield here?... endFor
The goals are:
(1) Practice creating a Spankish class from scratch (2) Practice testing your code (3) Gain experience with semaphores, mutex locks, and thread synchronization
Here is the problem scenario: groups of customers come in to a “gaming parlor” to play games. They go to the front desk to obtain one or more dice, which are used by the group while they are playing their
variables. However, within an entry method, the code should not touch the monitor’s mutex lock, or look inside the condition variables.
Hint: Model the front desk as a monitor. Use condition variables instead of semaphores.
If deadlock occurs, the program will freeze up and some requests for dice will go unsatisfied forever. Regardless of the order in which the groups make their requests, your solution should be structured such that deadlock can never occur.
Your solution must not be subject to any race conditions. In other words, regardless of the order in which the groups make their requests and return their dice, each die must never be allocated to more than one group at a time. It should never be the case that groups are allowed to proceed when there are too few dice. Likewise, if a group has returned its dice, other groups which are waiting must be allowed to proceed once enough dice have become available.
Starvation can occur if it is possible that one thread can be delayed infinitely by other threads’ requests. If the game parlor problem is extended to assume that each group will continue to play game after game forever, then starvation might be possible, if you are not careful in your solution. If some group X comes in requesting a lot of dice, it will be made to wait until enough dice are available. If it is possible that other groups can come in, request a small number of dice, and have their requests granted, then group X might get delayed forever, since there are never enough dice at the front desk at once to satisfy group X’s needs. In other words, it might be possible for starvation of X to occur. In your solution starvation should not be possible. (Of course, with each group limited to playing only 5 games, all the outstanding dice will always get returned eventually and starvation can never occur, but your solution should also avoid starvation in the presence of a never-ending sequence of Request and Return operations.)
To verify that your code is working, please insert print statements to produce output like this...
Initializing Thread Scheduler... Initializing Idle Process... A requests 4 ------------------------------Number of dice now avail = 8 A proceeds with 4 ------------------------------Number of dice now avail = 4 B requests 4 ------------------------------Number of dice now avail = 4 B proceeds with 4 ------------------------------Number of dice now avail = 0 D requests 5 ------------------------------Number of dice now avail = 0 E requests 2 ------------------------------Number of dice now avail = 0 A releases and adds back 4 ------------------------------Number of dice now avail = 4 B releases and adds back 4 ------------------------------Number of dice now avail = 8 C requests 5
------------------------------Number of dice now avail = 8 H requests 1 ------------------------------Number of dice now avail = 8 B requests 4 ------------------------------Number of dice now avail = 8 D proceeds with 5 ------------------------------Number of dice now avail = 3 ...etc...
This output makes it fairly easy to see what the program is doing and verify that it is correct.
Below is the method I used to produce this output. Feel free to include this method in your program.
behavior GameParlor
... Other methods... method Print (str: String, count: int) -- -- This method prints the current thread's name and the arguments. -- It also prints the current number of dice available. -- print (currentThread.name) print (" ") print (str) print (" ") printInt (count) nl () print ("------------------------------Number of dice now avail = ") printInt (numberDiceAvail) nl () endMethod
endBehavior
This method is called in several places...
At the beginning of my Request method: self.Print ("requests", numNeeded)
At the end of my Request method: self.Print ("proceeds with", numNeeded)
In my Return method: self.Print ("releases and adds back", numReturned)
Please submit hardcopy of the following files:
If you email questions to me or the TA, please include “cs333” in the subject line so we don’t accidentally identify your email as junk! (This is not necessary with the mailing list, since MailMan automatically adds [cs333].)