Peterson’s Problem

Peterson’s solution provides a good algorithmic description of solving the critical-section problem and illustrates some of the complexities involved in designing software that addresses the requirements of mutual exclusion, progress, and bounded waiting.

do {
flag[i] = true;
turn = j;
while (flag[j] && turn == j);
/* critical section */
flag[i] = false;
/* remainder section */
}
while (true);

The structure of process Pi in Peterson’s solution. This solution is restricted to two processes that alternate execution between their critical sections and remainder sections. The processes are numbered P0 and P1. We use Pj for convenience to denote the other process when Pi is present; that is, j equals 1 − I, Peterson’s solution requires the two processes to share two data items −

int turn;
boolean flag[2];

The variable turn denotes whose turn it is to enter its critical section. I.e., if turn == i, then process Pi is allowed to execute in its critical section. If a process is ready to enter its critical section, the flag array is used to indicate that. For E.g., if flag[i] is true, this value indicates that Pi is ready to enter its critical section. With an explanation of these data structures complete, we are now ready to describe the algorithm shown in above. To enter the critical section, process Pi first sets flag[i] to be true and then sets turn to the value j, thereby asserting that if the other process wishes to enter the critical section, it can do so. Turn will be set to both i and j at roughly the same time, if both processes try to enter at the same time. Only one of these assignments will occur ultimately; the other will occur but will be overwritten immediately. The final value of turn determines which of the two processes is allowed to enter its critical section first. We now prove that this solution is correct. We need to show that −

• Mutual exclusion is preserved.
• The progress requirement is satisfied.
• The bounded-waiting requirement is met.

To prove 1, we note that each Pi enters its critical section only if either flag[j] == false or turn == i. Also note that, if both processes can be executing in their critical sections at the same time, then flag[0] == flag[1] == true. These two observations indicate that P0 and P1 could not have successfully executed their while statements at about the same time, since the value of turn can be either 0 or 1 but cannot be both. Hence, one of the processes — say, Pj — must have successfully executed the while statement, whereas Pi had to execute at least one additional statement (“turn == j”). However, at that time, flag[j] == true and turn == j, and this condition will persist as long as Pj is in its critical section; as a result, mutual exclusion is preserved.

To prove properties 2 and 3, we note that if a process is stuck in the while loop with the condition flag[j] == true and turn == j, process Pi can be prevented from entering the critical section only; this loop is the only one possible. flag[j] will be == false, and Pi can enter its critical section if Pj is not ready to enter the critical section. If Pj has set, flag[j] = true and is also executing in its while statement, then either turn == i or turn == j. If turn == i, Pi will enter the critical section then. Pj will enter the critical section, If turn == j. Although once Pj exits its critical section, it will reset flag[j] to false, allowing Pi to enter its critical section. Pj must also set turn to i, if Pj resets flag[j] to true. Hence, since Pi does not change the value of the variable turn while executing the while statement, Pi will enter the critical section (progress) after at most one entry by Pj (bounded waiting).