Synchronization & Concurrency


Answer & solution
Answer: (1) signal(mutex); (2) signal(mutex); (3) R>0 || W!=0. (no official key was published)
The complete intended protocol is:
Reader() {
L1: wait(mutex);
if (W == 0) {
R = R + 1;
signal(mutex); // box 1
} else {
signal(mutex); // box 2
goto L1;
}
/* read shared data */
wait(mutex);
R = R - 1;
signal(mutex);
}
Writer() {
L2: wait(mutex);
if (R > 0 || W != 0) { // box 3
signal(mutex);
goto L2;
}
W = 1;
signal(mutex);
/* write shared data */
wait(mutex);
W = 0;
signal(mutex);
}wait/signal must implement a genuine atomic binary semaphore with appropriate memory visibility. Merely reading and writing an ordinary flag is not a substitute. All tests and updates of R,W occur under mutex, so checking admission and registering the admitted reader/writer are atomic relative to competitors. A writer enters only when no reader and no writer is active, and W=1 prevents later admissions until it finishes. Multiple readers are allowed while W=0. Both reader branches must release mutex; retaining it would block exits or retries and can deadlock. The mutex protects admission bookkeeping, not the whole read/write operation; R and W reserve access while mutex is released. Safety holds, but fairness is not guaranteed.