CS FundamentalsVery hardGATE CS

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] && turn == j); CS; flag[i] = false;`. What goes wrong?

  1. A. Nothing; it is equivalent
  2. B. It deadlocks whenever both try
  3. C. Mutual exclusion can be violated: whoever writes turn last still gets in, and the other can already be inside
  4. D. It only violates bounded waiting

Correct answer: C. Mutual exclusion can be violated: whoever writes turn last still gets in, and the other can already be inside

P0 sets flag, turn = 0 and enters (P1 not interested). P1 sets flag, turn = 1; its test is flag[0] && turn == 0, which is false since turn = 1, so P1 also enters. Correct Peterson yields the turn to the other process.

More Operating Systems questions

Exams that ask this topic