Monitor - correct answer ✔✔a lock + condition variables
Lock - correct answer ✔✔provides mutual exclusion to shared data
Condition variable - correct answer ✔✔provides a queue for waiting threads inside a critical section
Hoare vs. Mesa monitors - correct answer ✔✔1.) Hoare Monitors (used in most textbooks)
a.) Signal() transfers the CPU directly to a waiting thread
2.) Mesa Monitors (used in most real operating systems)
a.) Signal() only puts a waiting thread on the scheduler's ready queue
b.) By the time the waken thread gets the CPU, the waiting condition may no longer be true and needs
to be retested
Correct a broken solution - correct answer ✔✔Reader() {
lock.Acquire();
while (activeWriters > 0
|| waitingWriters > 0) {
++waitingReaders;
okToRead.Wait(&lock);
--waitingReaders;
}
++activeReaders;
lock.Release();
// access database
lock.Acquire();
, --activeReaders;
if (activeReaders == 0 &&
waitingWriters > 0) {
okToWrite.Signal(&lock);
}
lock.Release();
}
Writer() {
lock.Acquire();
while (activeWriters > 0
|| activeReaders > 0) {
++waitingWriters;
okToWrite.Wait(&lock);
--waitingWriters;
}
++activeWriters;
lock.Release();
// access database
lock.Acquire();
--activeWriters;
if (waitingWriters > 0) {
okToWrite.Signal(&lock);
} else if (waitingReaders > 0) {
okToRead.Broadcast(&lock);
}
lock.Release();
}