103 lines
3.8 KiB
HTML
103 lines
3.8 KiB
HTML
<HTML>
|
|
<HEAD>
|
|
|
|
<B>Reader/Writer Problem with Writer Priority: </B>
|
|
<p>
|
|
Using <I>general</I> semaphores, create a solution to the
|
|
reader/writer problem such that writers have priority over
|
|
readers. When writers have priority, the following rules
|
|
exist:
|
|
<UL>
|
|
<LI> A reader can read the shared file (e.g., database)
|
|
only when there are NO active or waiting writers.
|
|
<LI> A writer can only write to the shared file when the
|
|
writer is given mutually exclusive access to the file.
|
|
<LI> Multiple readers can read the shared file concurrently,
|
|
if the above rules are followed.
|
|
</UL>
|
|
|
|
<p>
|
|
Your BACI program should consist of the following three procedures:
|
|
<OL>
|
|
<LI> 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.
|
|
<LI> Reader(int reader): Multiple copies of this procedure may be executed
|
|
from the cobegin/coend block; reader is the id of this particular reader.
|
|
<LI> 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.
|
|
</OL>
|
|
|
|
<P>
|
|
Use two global declarations to declare the number of readers/writers
|
|
in the system:
|
|
<center>
|
|
const int Rnumber= 5; //number of readers// <BR>
|
|
const int Wnumber= 3; //number of writers//
|
|
</center>
|
|
If the above number of readers/writers are defined, then main() will
|
|
be: <BR>
|
|
|
|
main() { <BR>
|
|
|
|
Coordinator; <BR>
|
|
|
|
cobegin { <BR>
|
|
|
|
Reader(0); <BR>
|
|
|
|
Reader(1); <BR>
|
|
|
|
Reader(2); <BR>
|
|
|
|
Reader(3); <BR>
|
|
|
|
Reader(Rnumber-1); <BR>
|
|
|
|
Writer(0); <BR>
|
|
|
|
Writer(1); <BR>
|
|
|
|
Writer(Wnumber-1); <BR>
|
|
|
|
//add-delete lines when the number of readers or writers change. <BR>
|
|
|
|
//NOTE: BACI can only handle about 10 threads at a time <BR>
|
|
|
|
} <BR>
|
|
|
|
} <BR>
|
|
|
|
<p>
|
|
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.
|
|
|
|
<P>
|
|
Written deliverables:
|
|
<OL>
|
|
<LI> A listing of your code (*.lst).
|
|
<LI> The results from executing your program with the following
|
|
number of readers/writers:
|
|
<OL type=a>
|
|
<LI> Rnumber= 5 and Wnumber= 3
|
|
<LI> Rnumber= 6 and Wnumber= 1
|
|
<LI> Rnumber= 1 and Wnumber= 6
|
|
</OL>
|
|
<LI> A brief explanation of how your solution gives writer
|
|
priority.
|
|
</OL>
|
|
|
|
</BODY>
|
|
</HTML>
|
|
|