The system is in a safe state only if:
W = 0 and R >= 0, or W <= 1 and R = 0
This mutual exclusion problem can be solved by the
use of blocking semaphores. The following files contain an example of solution
using blocking semaphores.
The use of this solution can lead to the fact that writers will wait forever
for the access if the number of readers is too high. In order to eliminate this
problem, the addition of a variable writersWaiting can allow the system
to know if writers are waiting for the access, and thus, not allow new readers
to get access. Thus, the number of readers will decrease until a writer will be
able to get the access. File below implements this concept: