程序代写代做代考 Readers/Writers Monitor Solution

Readers/Writers Monitor Solution
monitor ReadersWriters
int nr := 0, nw := 0
cond okToRead, okToWrite;
readEnter() {
while (nw > 0) // writer is in
Wait(okToRead); // block nr++; // mark that reader is in
}
readExit() {
nr−−; // one reader is out
Signal(okToWrite);
writeEnter() {
while (nw > 0 or nr > 0)
}
writeExit() {
nw−−; // one writer is out
Signal(okToWrite); // hint to writers that maybe one can get in Broadcast(okToRead); // hint to readers that maybe they can get in
}
end monitor
}
// hint to writers that maybe one can get in
// someone is in // block
Wait(okToWrite);
nw++; // mark that writer is in
1