/    /  OS – Peterson’s Solution

Peterson’s Solution

 

Peterson’s solution is one of the most widely used solutions to the critical section. It is a classical software-based solution. 

 

In this solution, we use two shared variables:

  1. int turn – For a process whose turn is to enter the critical section. 
  2. boolean flag[i] – Value of TRUE indicates that the process wants to enter the critical section. It is initialized to FALSE, indicating no process wants to enter the critical section.

 

It allows two or more processes to share a single-use resource without conflict, using only shared memory for communication. 

 

Disadvantages of Peterson’s solution:

  1. Peterson’s solution is limited to two processes.
  2. It involves Busy Waiting.

 

Advantages of Peterson’s solution:

It is able to preserve all the three rules for the solution of the critical section:

  1. It assures Mutual Exclusion, as only one process can access the critical section at a time.
  2. It assures progress, as no process is blocked due to processes that are outside.
  3. It assures Bound Waiting as every process gets a chance.

 

Example of Peterson’s solution:

 

Let’s assume there are n number of processes. Each process needs to enter the Critical Section at some point. 

In this case, a FLAG[] array of size n is added, which by default is at FALSE. 

Whenever a process needs to enter the critical section, it is set to TRUE. 

From the above example: if the process Pi needs to enter, it will set FLAG[i] = TRUE. 

There is another variable TURN. Whenever a process is exiting the critical section it will change the TURN to another number from the list of ready processes.

 

Reference

Peterson’s Solution In Operating System