Winter Semester 2026–27 · VIT-AP · BACSE106 · Operating Systems · Dr. Krithika L.B

Peterson's solution

Animated two-process protocol.

Module 3 Session 3.2 Lesson 3 of 4

Operating Systems: Peterson's Solution

A Classic Software Solution to the Critical-Section Problem for Two Processes

1. Introduction & Assumptions

Peterson’s Solution is a classic software-based algorithm designed by Gary L. Peterson in 1981 to solve the critical-section problem for two processes (P0 and P1).

Hardware Assumption: It assumes that basic load and store machine instructions are atomic—meaning they complete in a single execution step without interruption.

2. Shared Variables and Process Algorithm

Peterson's algorithm relies on two shared data items between Process Pi and Process Pj (where j = 1 − i):

boolean flag[2];
An array used to indicate if a process is ready to enter its critical section. Initialized to false. If flag[i] == true, process Pi wants to enter.
int turn;
Indicates whose turn it is to enter the critical section. If turn == i, process Pi is granted permission to enter.
// Algorithm for Process Pi (where j = 1 - i)
do {
    flag[i] = true;
    turn = j;
    while (flag[j] && turn == j);

    /* Critical Section */

    flag[i] = false;

    /* Remainder Section */
} while (true);

How the Entry Section Works:

  1. Express Intent: Pi sets flag[i] = true to declare it wants to enter its CS.
  2. Polite Yield: Pi sets turn = j, graciously offering process Pj the first chance to enter.
  3. Busy Wait: Pi waits in the while loop as long as Pj wants to enter (flag[j] == true) AND it is Pj's turn (turn == j).
  4. Exit Section: Once finished, Pi sets flag[i] = false, freeing up the resource for Pj.

3. Proof of Correctness

Peterson’s Solution successfully meets all three requirements for a valid critical-section solution:

1. Mutual Exclusion

Each Pi enters CS only if flag[j] == false or turn == i. If both processes try to enter simultaneously, both set flag to true. However, turn can only hold one value (0 or 1) at a time. The process whose write to turn happened second will overwrite the first, forcing itself to wait and granting access to the other.

2. Progress

If Pj is in its remainder section, flag[j] == false. Thus, Pi can instantly bypass the while loop and enter its critical section without being blocked by Pj.

3. Bounded Waiting

A process Pi will wait at most one entry cycle of Pj. As soon as Pj exits its CS, it sets flag[j] = false, immediately allowing Pi to enter. Even if Pj immediately re-enters its entry section, it sets turn = i, yielding to Pi.

4. Interactive Peterson's Algorithm Simulator

Use the controls below to step through Process 0 (P0) and Process 1 (P1) entering and exiting their critical sections. Observe how the shared flag array and turn variable prevent concurrent access.

Process P0

Remainder Section

Process P1

Remainder Section
Shared Memory State: flag[0] = false | flag[1] = false | turn = 0
MUTUAL EXCLUSION VIOLATED! (This will not occur in Peterson's Solution)