Reader/Writer Problem with Writer Priority:
Using general semaphores, create a solution to the
reader/writer problem such that writers have priority over
readers. When writers have priority, the following rules
exist:
- A reader can read the shared file (e.g., database)
only when there are NO active or waiting writers.
- A writer can only write to the shared file when the
writer is given mutually exclusive access to the file.
- Multiple readers can read the shared file concurrently,
if the above rules are followed.
Your BACI program should consist of the following three procedures:
- Coordinator: This procedure should be executed before any readers or
writers are executed, and should be used to initialize the semaphores
and any other global variables that require initialization.
- Reader(int reader): Multiple copies of this procedure may be executed
from the cobegin/coend block; reader is the id of this particular reader.
- Writer(int writer): Multiple copies of this
procedure may also be executed from the cobegin/coend block; writer
is the id of this particular writer.
Use two global declarations to declare the number of readers/writers
in the system:
const int Rnumber= 5; //number of readers//
const int Wnumber= 3; //number of writers//
If the above number of readers/writers are defined, then main() will
be:
main() {
Coordinator;
cobegin {
Reader(0);
Reader(1);
Reader(2);
Reader(3);
Reader(Rnumber-1);
Writer(0);
Writer(1);
Writer(Wnumber-1);
//add-delete lines when the number of readers or writers change.
//NOTE: BACI can only handle about 10 threads at a time
}
}
In both the Reader and Writer procedures, during the actual
reading/writing phase of the shared file, have a for loop of length
three that prints to stdout "... reading is performed by process I ..." (or
"... writing is performed by process J ..."), where I/J is the id value
of the reader/writer.
In addition, both the Reader and Writer procedures should print a
message to stdout just before they perform a P() or V() operation on
any of the semaphores. In the message, each thread must identify itself
as a reader or a writer, the id of the reader/writer, the name of
the semaphore variable upon which it is operating, and the value
of the semaphore variable at that time.
Written deliverables:
- A listing of your code (*.lst).
- The results from executing your program with the following
number of readers/writers:
- Rnumber= 5 and Wnumber= 3
- Rnumber= 6 and Wnumber= 1
- Rnumber= 1 and Wnumber= 6
- A brief explanation of how your solution gives writer
priority.