CS FundamentalsVery hardGATE CS

In Peterson's algorithm for P0 and P1, the entry code for Pi is `flag[i] = true; turn = j; while (flag[j] && turn == j);`. What goes wrong if the first two statements are swapped to `turn = j; flag[i] = true;`?

  1. A. Nothing, since the two writes are to different shared variables
  2. B. Mutual exclusion fails: both processes can be in the critical section
  3. C. Only progress fails: a process can wait although the other is outside
  4. D. Only bounded waiting fails; mutual exclusion still holds

Correct answer: B. Mutual exclusion fails: both processes can be in the critical section

P0 sets turn = 1; P1 sets turn = 0, flag[1] = true and enters since flag[0] is still false; P0 then sets flag[0] = true and finds turn == 0, so it enters too. The flag must be raised before yielding the turn.

More Operating Systems questions

Exams that ask this topic