What is Peterson's solution?

Peterson's solution is a classic software-based algorithm that ensures mutual exclusion between two processes without requiring any hardware support. It can be implemented on any platform using only two shared variables: an interested array and a turn variable.

The algorithm allows two processes to safely access a critical section by using these variables to coordinate entry and prevent race conditions. Each process declares its interest in entering the critical section and yields priority to the other process if both want to enter simultaneously.

Peterson's Solution Algorithm

#define N 2
#define TRUE 1
#define FALSE 0

int interested[N] = {FALSE, FALSE};
int turn;

void entry_section(int process)
{
    int other = 1 - process;
    interested[process] = TRUE;
    turn = other;
    while(interested[other] == TRUE && turn == other);
}

void exit_section(int process)
{
    interested[process] = FALSE;
}

How It Works

The algorithm uses two key variables:

  • interested[2] − Boolean array where interested[i] indicates if process i wants to enter the critical section

  • turn − Integer variable that gives priority to one process when both want to enter simultaneously

Step-by-Step Execution

When a process wants to enter the critical section:

Step Action Purpose
1 Calculate other = 1 - process Identify the other process (0?1, 1?0)
2 Set interested[process] = TRUE Declare intention to enter critical section
3 Set turn = other Give priority to the other process
4 Wait while interested[other] && turn == other Wait if other process has priority and is interested
5 Enter critical section Execute critical section code
6 Set interested[process] = FALSE Signal exit from critical section

Example Execution

Consider two processes P0 and P1 trying to access the critical section:

Scenario 1: Only P0 wants to enter

  • P0 sets interested[0] = TRUE, turn = 1

  • Since interested[1] = FALSE, P0 enters immediately

Scenario 2: Both P0 and P1 want to enter simultaneously

  • P0 sets interested[0] = TRUE, turn = 1

  • P1 sets interested[1] = TRUE, turn = 0

  • P0 waits since interested[1] == TRUE && turn == 1 is false

  • P1 waits since interested[0] == TRUE && turn == 0 is true

  • P0 enters first (last writer of turn variable loses priority)

Properties of Peterson's Solution

Property Description How Achieved
Mutual Exclusion Only one process in critical section Both processes can't satisfy while condition simultaneously
Progress No deadlock occurs At least one process will enter if both are interested
Bounded Waiting No process waits indefinitely Turn variable ensures fair alternation

Advantages and Disadvantages

Advantages:

  • No hardware support required

  • Simple and elegant solution

  • Guarantees all three critical section properties

Disadvantages:

  • Busy waiting (spin lock) wastes CPU cycles

  • Works only for two processes

  • Assumes atomic read/write operations on shared variables

Conclusion

Peterson's solution is a fundamental algorithm in operating systems that demonstrates how mutual exclusion can be achieved using only software techniques. While it has limitations like busy waiting and restriction to two processes, it remains an important theoretical foundation for understanding synchronization mechanisms.

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

7K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements