Provide a scenario that shows the following policy gives priority to writers. In your scenario, you should have two readers and a writer.
reader() {
while(TRUE) {
;
P(writePending);
P(readBlock);
P(mutex1);
readCount++;
if(readCount == 1)
P(writeBlock);
V(mutex1);
V(readBlock);
V(writePending);
access(resource);
P(mutex1);
readCount--;
if(readCount == 0)
V(writeBlock);
V(mutex1);
}
}
int readCount = 0, writeCount = 0;
semaphore mutex1 = 1, mutex2 = 1;
semaphore readBlock = 1, writeBlock = 1, writePending = 1;
fork(reader, 0);/* could be many */
fork(writer, 0);/* could be many */
writer() {
while(TRUE) {
;
P(mutex2);
writeCount++;
if(writeCount == 1)
P(readBlock);
V(mutex2);
P(writeBlock);
access(resource);
V(writeBlock);
P(mutex2)
writeCount--;
if(writeCount == 0)
V(readBlock);
V(mutex2);
}
}
Time Action Result
0 Reader 1 arrives
1 Reader 1 executes P (writePending) writePending = 0
2 Reader 1 executes P (readBlock) readBlock = 0
(You will need to fill in the rest.)