Comp 486 Project #2: Synchronization
Introduction
You have the option of completing one of two possible projects. The
first possibility is to implement a solution to the producers-consumers
problem. The second is to implement a solution to the sleeping-barber
problem. In either case, your solution should be implemented using
using pthread semaphores in C.
The synchronization protocol for the producers-consumers problem is
provided in your textbook. For the sleeping-barber problem, you will need to
develop a correct protocol before you start coding. Because of this
added difficulty, the sleeping-barber problem will be graded on a 105/100
scale. Note that this is a classic problem, and you
could easily look up a correct protocol. You are welcome to do that
(as long as you provide a citation), but if you do, you will not
be eligible for the extra credit portion of the grade. I'll also be
happy to point you to a correct protocol if you get stuck.
The Producers-Consumers Problem
Complete the shell project described on page 236 of your textbook.
The only thing I'll add to add to those requirements is that your
program should produce a trace of the operations it performs. Every
thread should print a message when it enters its entry section and
when it executes its critical section. These messages should include
the id of the thread. A sample output might look something
like the following:
consumer 23 is ready to consume.
producer 24 is ready to add 423 to the buffer.
producer 25 is ready to add 543 to the buffer.
producer 24 has updated the buffer:
[432, -1, -1, -1, -1]
consumer 23 has updated the buffer:
[-1, -1, -1, -1, -1]
...
The exact format is up to you, but it should be possible to reconstruct the
sequence of events from the trace.
The Sleeping-Barber Problem
The sleeping barber problem is described in exercise in Exercise 6.11
in your book. Your main file should look something like the following:
void main(int argc, char **argv) {
//Initialize necessary data.
//Start the barber thread.
//Create customer threads in a while loop, with a random pause between
// each customer.
return 0;
}
Your program should take three arguments on the command line:
- The length of time that the program should run.
- The average duration of a haircut.
- The average time between customer arrivals.
The output of your program should be trace data similar to that
described above for the producers-consumers problem.
Hints: You might want to use at least two semaphores, one that
represents the number of waiting customers, and one that represents
the number of waiting barbers. You may also need a mutex to control
access to shared variables. Keep in mind that the haircut itself need
not take place inside of a critical section.
Handing In
Your submission should take the form of a tarred directory as described in
HW #1 . That directory should contain:
- The source for your project. This should be well commented, and
should include such niceties as your name and date in every file.
- A plain text README file documenting your project. This
file should include your name(s), a short description of the project,
instructions for running your code, a description of any known bugs,
and anything else I need to know to evaluate your submission.
Grading
The project will be graded as follows. A multiplier of 1.05 will be
applied to sleeping-barber submissions.
| Comments, documentation, code formatting,
and proper submission format |
20% |
| Coding style |
20% |
| Functionality |
60% |
This page is maintained by Nathan Sprague nsprague{at}kzoo{dot}edu.