CS FundamentalsModerateGATE CS
Why must a Mesa-style condition wait be written as `while (!cond) wait(c);` rather than `if (!cond) wait(c);`?
- A. Because wait() can return only after a timeout
- B. Because after being signalled the waiter does not run immediately, and the condition may be false again when it reacquires the lock
- C. Because if() cannot call wait() inside a monitor
- D. Because signal() always wakes every waiter
Correct answer: B. Because after being signalled the waiter does not run immediately, and the condition may be false again when it reacquires the lock
Under signal-and-continue, another thread can run between the signal and the waiter regaining the lock and invalidate the condition; spurious wakeups are also allowed. Re-checking in a loop is the safe pattern.
More Operating Systems questions
- 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()…
- How many times is "x" printed? (Every fork() succeeds.) c int main() { if ((fork() && fork()) || fork()) fork(); printf("xn"); }
- How many times is "x" printed? (Every fork() succeeds; output is not buffered.) c int main() { for (int i = 0; i < 3; i++) { fork(); if (i…
- What is the sum of all values printed? (Every fork() succeeds.) c int main() { int c = 0; for (int i = 0; i < 3; i++) if (fork() == 0)…