# Explain how petersons solution helps to achieved process synchronization.

### Peterson Solution

Peterson’s solution is widely used solution to critical section problems. This algorithm was developed by a computer scientist Peterson that’s why it is named as a Peterson’s solution.

In this solution, when a process is executing in a critical state, then the other process only executes the rest of the code, and the opposite can happen. This method also helps to make sure that only a single process runs in the critical section at a specific time.

Example

PROCESS Pi
FLAG[i] = true
while( (turn != i) AND (CS is !free) ){ wait;
}
CRITICAL SECTION FLAG[i] = false
turn = j; //choose another process to go to CS

• Assume there are N processes (P1, P2, … PN) and every process at some point of time requires to enter the Critical Section
• A FLAG[] array of size N is maintained which is by default false. So, whenever a process requires to enter the critical section, it has to set its flag as true. For example, If Pi wants to enter it will set FLAG[i]=TRUE.
• Another variable called TURN indicates the process number which is currently wating to enter into the CS.
• The process which enters into the critical section while exiting would change the TURN to another number from the list of ready processes.
• Example: turn is 2 then P2 enters the Critical section and while exiting turn=3 and therefore P3 breaks out of wait loop.
If you found any type of error on the answer then please mention on the comment or report an answer or submit your new answer.