Consider Peterson's algorithm for mutual exclusion between two concurrent processes i and j. The program executed by process is shown below.
repeat
flag[i] = true;
turn = j;
while (P) do no-op;
Enter critical section, perform actions, then
exit critical section
Flag[i] = false;
Perform other non-critical section actions.
Until false;
For the program to guarantee mutual exclusion, the predicate P in the while loop should be
View worked solution
Answer: B
In Peterson's algorithm for mutual exclusion between two processes and :
- The boolean array
flagrepresents the intent of each process to enter its critical section. Settingflag[i] = truemeans process wants to enter. - The variable
turnresolves conflicts when both processes try to enter simultaneously. By settingturn = j, process politely yields the turn to process .
For process to safely enter its critical section, it must wait (busy-wait) if and only if:
- The other process wants to enter the critical section:
flag[j] == true. - It is currently process 's turn to enter:
turn == j.
Thus, the busy-waiting condition (predicate ) in the while loop of process must be:
If either of these conditions is false (e.g., if process is not interested, or if the turn has been given back to ), process breaks out of the loop and safely enters its critical section, ensuring mutual exclusion.
Therefore, the correct option is B.
Free practice · Your answers on this page are not saved.
