CS FundamentalsVery hardGATE CS
Readers–writers with readers' preference: semaphores mutex = 1 (protects readcount), wrt = 1. Reader: wait(mutex); readcount++; if (readcount == 1) wait(wrt); signal(mutex); read; wait(mutex); readcount--; if (readcount == 0) signal(wrt); signal(mutex). If a writer is writing, 3 readers arrive one after another. Where do they wait?
- A. All three wait on wrt
- B. All three wait on mutex
- C. The first two wait on wrt, the third on mutex
- D. The first reader waits on wrt; the other two wait on mutex
Correct answer: D. The first reader waits on wrt; the other two wait on mutex
The first reader holds mutex while it blocks on wrt (readcount == 1), so the next readers cannot even get mutex to increment readcount; they queue on mutex.
More Operating Systems questions
- A bounded buffer of size N uses semaphores empty = N, full = 0, mutex = 1. Producer: wait(empty); wait(mutex); add; signal(mutex)…
- Monitors versus semaphores: in a Hoare-style monitor, when a process executes x.signal() and some process is waiting on condition x, which…
- Why must a Mesa-style condition wait be written as while (!cond) wait(c); rather than if (!cond) wait(c);?
- How many times is "x" printed? (Every fork() succeeds.) c int main() { if (fork() == 0) { fork(); fork(); } else { fork(); } printf("xn")…
- How many processes print "x" (including the original)? (Every fork() succeeds.) c int main() { for (int i = 0; i < 4; i++) if (fork() !=…
- How many times is "x" printed? (Every fork() succeeds.) c int main() { pidt a = fork(); pidt b = fork(); if (a > 0 && b > 0) fork()…