CS FundamentalsHardGATE CS
A counting semaphore is implemented WITHOUT busy waiting: wait() decrements the value and, if it becomes negative, puts the caller on the semaphore's queue and blocks it. What must be true of wait() and signal() themselves?
- A. They must be called only from user mode
- B. They must execute atomically, e.g. by a short spinlock or by disabling interrupts on a uniprocessor
- C. They need no protection because the queue handles races
- D. They must always context switch
Correct answer: B. They must execute atomically, e.g. by a short spinlock or by disabling interrupts on a uniprocessor
The test-and-decrement and queue manipulation are themselves critical sections; a brief spinlock (multiprocessor) or interrupt disabling (uniprocessor) protects them. The busy waiting is thus moved to these very short sections.
More Operating Systems questions
- Strict alternation uses a shared turn variable: P0: while (turn != 0); CS; turn = 1; P1: while (turn != 1); CS; turn = 0;. P0 wants to…
- Two processes each run flag[i] = true; while (flag[j]); CS; flag[i] = false; (no turn variable). Which statement is correct?
- Two processes each run while (flag[j]); flag[i] = true; CS; flag[i] = false; (check first, then set). Which property fails?
- Peterson's algorithm is written with turn = i; (giving the turn to ITSELF) instead of turn = j;: flag[i] = true; turn = i; while (flag[j]…
- stdout is a pipe (fully buffered) and every fork() succeeds. What is the complete output, up to the order of the lines? c int main() {…
- How many times is "x" printed? (Every fork() succeeds; output unbuffered.) c int main() { for (int i = 0; i < 2; i++) { if (fork() == 0)…