• Courses
  • Tutorials
  • Jobs
  • Practice
  • Contests
November 09, 2022 |4.9K Views
Peterson Solution in Operating Systems
  Share   Like
Description
Discussion

In this video, we will understand what is Peterson’s solution in operating system

What is Peterson's Solution

It is used to solve the process synchronisation problems where two or more processes access shared resources. 

Peterson’s Solution is based on two main ideas: 
1) Willingness of a process to acquire the lock on the critical section. 
2) Turn of a process to acquire lock. 


It provides shared access to memory between the co-operating processes. It ensures mutual exclusion among process sharing resources. The Producer Consumer is a classical problem which can be solved using the Peterson’s Solution. To synchronise any two processes, 

Peterson’s solution uses two variables: 
1) Flag: a boolean array of size 2 
2) Turn: integer variable 
 

Initially both the flags in the array are set to false. Peterson’s Solution is a software-based solution to race conditions. Although it is not used in modern-day computing, it provides an algorithm-level way to solve the critical section problem. Peterson’s Solution works well in the case of two cooperating processes as well as multiple processes wanting to share the critical section. 

Working of Peterson's Solution

Whenever a process wants to execute in its critical section, it sets its flag to true. Also the turn variable is set to the index of the second cooperating process so as to allow the second process to enter the critical section (if required). The process enters into busy waiting until the other process has completely executed in its critical section. 


Then, the current process starts executing in its critical section and utilises the shared resources. Once the current process has executed completely, it sets its own flag to false to indicate that it does not want to execute any further. Therefore, the current process executes for a fixed amount of time before exiting from the system. 
 

Peterson's solution:
https://www.geeksforgeeks.org/petersons-algorithm-in-process-synchronization/

Read More