0
0
Operating Systemsknowledge~10 mins

Peterson's solution in Operating Systems - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Peterson's solution
Process 0 wants to enter
Set flag[0
Set turn = 1
Wait while flag[1
Enter critical section
Set flag[0
Process 0 leaves critical section
Each process signals intent, sets turn to the other, then waits if the other wants in and it's their turn, ensuring only one enters the critical section at a time.
Execution Sample
Operating Systems
flag[0] = true
turn = 1
while (flag[1] and turn == 1):
    pass
# critical section
flag[0] = false
Process 0 tries to enter critical section, waits if Process 1 is inside or has priority, then enters and finally leaves.
Analysis Table
StepProcessflag[0]flag[1]turnCondition (flag[other] and turn == other)Action
1P0truefalse1falseP0 sets flag[0]=true, turn=1, proceeds
2P1truetrue0trueP1 sets flag[1]=true, turn=0, waits because flag[0]=true and turn=0
3P0truetrue0falseP0 condition false (turn !=1), enters critical section
4P1truetrue0trueP1 still waits
5P0truetrue0falseP0 in critical section
6P1truetrue0trueP1 still waits
7P0truetrue0falseP0 in critical section
8P1truetrue0trueP1 still waits
9P0falsetrue0falseP0 leaves critical section, sets flag[0]=false
10P1falsetrue0falseP1 condition false, proceeds to critical section
💡 P0 enters critical section when flag[1] is false or turn is not 1, ensuring mutual exclusion.
State Tracker
VariableStartAfter Step 1After Step 2After Step 9After Step 10
flag[0]falsetruetruefalsefalse
flag[1]falsefalsetruetruetrue
turn01000
Key Insights - 3 Insights
Why does each process set the turn variable to the other process before waiting?
Setting turn to the other process gives priority to the other process if both want to enter, preventing deadlock as shown in execution_table steps 1 and 2.
What happens if both processes set their flags to true at the same time?
The process for which turn != other will proceed immediately (condition false), while the other waits until the first exits, ensuring mutual exclusion (see steps 3-8).
How does Peterson's solution ensure no two processes are in the critical section simultaneously?
Because a process waits if the other wants in and it's the other's turn, only one process can pass the waiting condition and enter the critical section at a time (step 9).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what is the condition value for Process 0's wait?
Afalse
Bundefined
Ctrue
Dnull
💡 Hint
Check the 'Condition' column at step 3 for Process 0.
At which step does Process 0 leave the critical section?
AStep 2
BStep 9
CStep 5
DStep 10
💡 Hint
Look for the action where P0 leaves critical section and sets flag[0]=false.
If Process 1 never sets flag[1] to false, what happens to Process 0?
AP0 ignores flag[1]
BP0 enters critical section immediately
CP0 waits forever
DP0 sets turn to itself
💡 Hint
Refer to the waiting condition in the execution_table where flag[1] is true and turn is 1.
Concept Snapshot
Peterson's solution uses two flags and a turn variable.
Each process sets its flag true and gives turn to the other.
It waits if the other wants in and it's their turn.
This ensures only one process enters critical section at a time.
No special hardware needed, works for two processes.
Full Transcript
Peterson's solution is a method to allow two processes to share a resource without conflict. Each process signals its desire to enter by setting a flag true and then sets the turn to the other process. It waits while the other process wants to enter and it is the other's turn. This waiting ensures only one process enters the critical section at a time, preventing conflicts. When a process finishes, it sets its flag false, allowing the other to proceed. This method avoids deadlock and ensures mutual exclusion using simple shared variables.