📖 Scenario: You are learning how two processes can share a resource without interfering with each other. Peterson's solution is a simple way to make sure only one process uses the resource at a time.
🎯 Goal: Build a step-by-step understanding of Peterson's solution by creating the key variables and logic that allow two processes to take turns accessing a shared resource safely.
📋 What You'll Learn
Create two boolean flags to indicate if each process wants to enter the critical section
Create a variable to indicate which process's turn it is
Write the entry protocol logic for one process using Peterson's solution
Write the exit protocol logic for the same process to release the critical section
💡 Why This Matters
🌍 Real World
Peterson's solution is a classic example used to teach how two processes can share resources safely without conflicts.
💼 Career
Understanding mutual exclusion is important for software developers and system programmers who work with concurrent or parallel programs.
Progress0 / 4 steps
1
DATA SETUP: Create the flags for process interest
Create two boolean variables called flag0 and flag1 and set both to False. These flags show if process 0 or process 1 wants to enter the critical section.
Operating Systems
Hint
Think of flag0 and flag1 as signals from each process saying 'I want to enter'.
2
CONFIGURATION: Create the turn variable
Add a variable called turn and set it to 0. This variable will show whose turn it is to enter the critical section.
Operating Systems
Hint
The turn variable helps the processes take turns fairly.
3
CORE LOGIC: Write the entry protocol for process 0
Write the entry protocol for process 0 using Peterson's solution. Set flag0 = True to show process 0 wants to enter. Set turn = 1 to give process 1 the chance. Then use a while loop that waits while flag1 is True and turn is 1.
Operating Systems
Hint
This logic makes process 0 wait if process 1 wants to enter and it is process 1's turn.
4
COMPLETION: Write the exit protocol for process 0
Write the exit protocol for process 0 by setting flag0 = False. This shows process 0 is leaving the critical section and lets process 1 enter if it wants.
Operating Systems
Hint
Setting flag0 to False signals that process 0 is done using the shared resource.
Practice
(1/5)
1. What is the main purpose of Peterson's solution in operating systems?
easy
A. To prevent two processes from accessing a shared resource simultaneously
B. To increase the speed of process execution
C. To allocate memory to processes
D. To schedule processes based on priority
Solution
Step 1: Understand the problem Peterson's solution addresses
Peterson's solution is designed to handle the critical section problem where two processes must not access a shared resource at the same time.
Step 2: Identify the main goal of the solution
The main goal is to ensure mutual exclusion, preventing simultaneous access to shared resources.
Final Answer:
To prevent two processes from accessing a shared resource simultaneously -> Option A
Hint: Peterson's solution controls access to shared resources [OK]
Common Mistakes:
Confusing it with process scheduling
Thinking it manages memory allocation
Assuming it increases execution speed
2. Which of the following variables is NOT used in Peterson's solution?
easy
A. flag
B. turn
C. counter
D. process_id
Solution
Step 1: Recall the variables used in Peterson's solution
Peterson's solution uses two flags (one per process) and a turn variable to control access.
Step 2: Identify the variable not part of the solution
The variable 'counter' is not used in Peterson's solution; it is unrelated to mutual exclusion control.
Final Answer:
counter -> Option C
Quick Check:
Variables used = flag, turn; counter not used [OK]
Hint: Remember only flag and turn variables are used [OK]
Common Mistakes:
Assuming process_id is a variable in Peterson's solution
Confusing counter with flag
Thinking turn is a counter
3. Consider two processes P0 and P1 using Peterson's solution. If both set their flags to true and turn to 1, which process will enter the critical section first?
medium
A. P0
B. P1
C. Both enter simultaneously
D. Neither enters
Solution
Step 1: Analyze the turn variable's role
The turn variable indicates which process's turn it is to enter the critical section when both want access.
Step 2: Apply the given values
Both flags are true, meaning both want to enter. Since turn is set to 1, process P1 has priority to enter first.
Final Answer:
P1 -> Option B
Quick Check:
Turn = 1 means P1 enters first [OK]
Hint: Turn variable decides which process enters first [OK]
Common Mistakes:
Assuming both enter simultaneously
Ignoring the turn variable
Thinking the first flag set enters first
4. Identify the error in this Peterson's solution snippet for process P0: