Peterson’s Algorithm in Process Synchronization
Peterson’s Algorithm is a classic solution to the critical section problem in process synchronization. It
ensures mutual exclusion meaning only one process can access the critical section at a time and
avoids race conditions. The algorithm uses two shared variables to manage the turn-taking
mechanism between two processes ensuring that both processes follow a fair order of execution. It’s
simple and effective for solving synchronization issues in two-process scenarios. In this article we will
learn about Peterson’s Algorithm, its working, and practical examples to help understand its use in
process synchronization.
What is Peterson’s Algorithm?
Peterson’s Algorithm is a well-known solution for ensuring mutual exclusion in process
synchronization. It is designed to manage access to shared resources between two processes in a
way that prevents conflicts or data corruption. The algorithm ensures that only one process can
enter the critical section at any given time while the other process waits its turn. Peterson’s
Algorithm uses two simple variables one to indicate whose turn it is to access the critical section and
another to show if a process is ready to enter. This method is often used in scenarios where two
processes need to share resources or data without interfering with each other. It is simple, easy to
understand, and serves as a foundational concept in process synchronization.
Algorithm for Pi process Algorithm for Pj process
do{ do{
flag[i] = true; flag[j] = true;
turn = j; turn = i;
while (flag[j] && turn == j); while (flag[i] && turn == i);
//critical section //critical section
flag[i] = false; flag[j] = false;
//remainder section //remainder section
}while(true); }while(true);
Peterson’s Algorithm Explanation
Peterson’s Algorithm is a mutual exclusion solution used to ensure that two processes do not enter
into the critical sections at the same time. The algorithm uses two main components: a turn variable
and a flag array.
The turn variable is an integer that indicates whose turn it is to enter the critical section.
The flag array contains Boolean values for each process, indicating whether a process wants
to enter the critical section.
Here’s how Peterson’s Algorithm works step-by-step:
Initial Setup: Initially, both processes set their respective flag values to false, meaning
neither wants to enter the critical section. The turn variable is set to the ID of one of the
processes (either 0 or 1), indicating that it’s that process’s turn to enter.
Intention to Enter: When a process wants to enter the critical section, it sets its flag value
to true signaling its intent to enter.
Set the Turn: Then the process, which is having the next turn, sets the turn variable to its
own ID. This will indicate that it is its turn to enter the critical section.
Waiting Loop: Both processes enter a loop where they check the flag of the other process
and the turn variable:
o If the other process wants to enter (i.e., flag[1 - processID] == true), and
o It’s the other process’s turn (i.e., turn == 1 - processID), then the process waits,
allowing the other process to enter the critical section.
This loop ensures that only one process can enter the critical section at a time, preventing a race
condition.
Critical Section: Once a process successfully exits the loop, it enters the critical section,
where it can safely access or modify the shared resource without interference from the other
process.
Exiting the Critical Section: After finishing its work in the critical section, the process resets
its flag to false. This signals that it no longer wants to enter the critical section, and the other
process can now have its turn.
By alternating turns and using these checks, Peterson’s algorithm ensures mutual exclusion, meaning
only one process can access the critical section at a time, and both processes get an equal
opportunity to do so.
Example of Peterson’s Algorithm
Peterson’s solution is often used as a simple example of mutual exclusion in concurrent
programming. Here are a few scenarios where it can be applied:
Accessing a shared printer: Peterson’s solution ensures that only one process can access the
printer at a time when two processes are trying to print documents.
Reading and writing to a shared file: It can be used when two processes need to read from
and write to the same file, preventing concurrent access issues.
Competing for a shared resource: When two processes are competing for a limited resource,
such as a network connection or critical hardware, Peterson’s solution ensures mutual
exclusion to avoid conflicts.