A file is shared between several reader and writer threads. Design a monitor to
ID: 3603153 • Letter: A
Question
A file is shared between several reader and writer threads. Design a monitor to control the access of the file by the different threads so that the following constraints are satisfied: (i) at most one writer can be active on the file at a particular time. (ii) When a writer is writing to the file, no reader can read from the file. (iii) More than one reader can be reading from the file simultaneously. (iv) When a writer is waiting to write, no more new reader should be allowed to read. (v) When a writer is writing and some other writer is waiting to write, then the writer is given more preference over a reader waiting to read. The general structure of each reader and writer thread is shown in the following: Monitor FileControl { // Definition of the monitor class to be filled in by you … … } FileControl fc; // An instance of the monitor Writer Thread: Reader Thread: while (True) { while (True) { ... ... fc.WriterEntry(); fc.ReaderEntry(); Write (file); Read (file); fc.WriterExit(); fc.ReaderExit(); … … } } Fill in the pseudo-code for the monitor FileControl as shown above.
Explanation / Answer
4.>semaphore mutex=1,wrtcount=0,rsem=1;
//writer's program
do{
P(mutex);
rdcount++;
if (wrtcount==1) P(rSem);
V(mutex);
READ
P(mutex);
rdcount--;
if (wrtcount==0) V(rSem);
V(mutex);
}while(true)
//reader's program
do{
p(rsem);
Write//Critical section
v(rsem)
3.>
5.>
//rest can be inferred using the above
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.