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.

Peterson's Algorithm

Peterson's 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 to denote the other process when Pi is present; that is, j = 1 - i.

Shared Data Variables

Peterson's solution requires the two processes to share two data items:

int turn;
boolean flag[2];
  • turn − Indicates whose turn it is to enter the critical section. If turn == i, then process Pi is allowed to execute in its critical section.

  • flag[2] − Array used to indicate if a process is ready to enter its critical section. If flag[i] is true, then Pi is ready to enter its critical section.

Algorithm Structure

The structure of process Pi in Peterson's solution:

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

How Peterson's Algorithm Works

To enter the critical section, process Pi first sets flag[i] to 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. If both processes try to enter at the same time, turn will be set to both i and j at roughly 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 process is allowed to enter its critical section first.

Peterson's Algorithm Flow flag[i] = true turn = j while (flag[j] && turn==j) Critical Section flag[i] = false Loop continues

Correctness Proof

Peterson's solution satisfies all three requirements for solving the critical section problem:

1. Mutual Exclusion

Each Pi enters its critical section only if either flag[j] == false or turn == i. If both processes execute in their critical sections simultaneously, then flag[0] == flag[1] == true. However, the value of turn can be either 0 or 1 but cannot be both. Hence, one process must have successfully executed the while statement, while the other had to execute at least one additional statement. Since flag[j] == true and turn == j will persist as long as Pj is in its critical section, mutual exclusion is preserved.

2. Progress and Bounded Waiting

A process Pi can be prevented from entering the critical section only if it's stuck in the while loop with flag[j] == true and turn == j. If Pj is not ready to enter the critical section, flag[j] will be false, and Pi can enter. If Pj has set flag[j] = true and is also executing its while statement, then either turn == i or turn == j. Once Pj exits its critical section, it resets flag[j] to false, allowing Pi to enter. Thus, Pi will enter the critical section after at most one entry by Pj.

Advantages and Disadvantages

Advantages Disadvantages
Guarantees mutual exclusion Works only for two processes
Satisfies progress requirement Uses busy waiting (CPU cycles wasted)
Ensures bounded waiting Can cause "spin lock" problem
Simple software solution Not practical for multiprocessor systems

Conclusion

Peterson's solution is a classic software-based approach to the critical section problem that works for exactly two processes. While it provides a theoretically sound solution that satisfies mutual exclusion, progress, and bounded waiting, its practical limitations include busy waiting and restriction to two processes only.

Updated on: 2026-03-17T09:01:38+05:30

19K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements