Peterson's solution in Operating Systems - Time & Space Complexity
We want to understand how the time it takes for Peterson's solution to work changes as more attempts to enter the critical section happen.
Specifically, how does waiting time grow when two processes try to access shared resources?
Analyze the time complexity of the following Peterson's solution code snippet for two processes.
// Shared variables
boolean flag[2] = {false, false};
int turn;
// Process i wants to enter critical section
flag[i] = true;
turn = j;
while (flag[j] && turn == j) {
// busy wait
}
// critical section
flag[i] = false;
This code lets two processes take turns safely to enter a critical section without conflicts.
Look at what repeats while waiting.
- Primary operation: The while loop that keeps checking if the other process is ready and whose turn it is.
- How many times: It repeats until the other process leaves the critical section or it's this process's turn.
The waiting time depends on how long the other process stays in the critical section.
| Input Size (n) | Approx. Operations |
|---|---|
| 1 (one attempt) | Few checks, almost no waiting |
| 10 (multiple attempts) | Waiting grows roughly linearly with how long the other process holds the resource |
| 100 (many attempts) | Waiting time increases but only for the process that arrives second each time |
Pattern observation: Waiting grows with how long the other process uses the critical section, but only one process waits at a time.
Time Complexity: O(n)
This means the waiting time grows linearly with the number of times the other process uses the critical section before this process can enter.
[X] Wrong: "Both processes wait at the same time, so waiting doubles."
[OK] Correct: Only one process waits while the other is in the critical section, so waiting is not doubled but linear for the waiting process.
Understanding how Peterson's solution manages waiting helps you explain synchronization and fairness in concurrent programming clearly and confidently.
"What if we added more than two processes? How would the time complexity of waiting change?"
Practice
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 AQuick Check:
Mutual exclusion = Prevent simultaneous access [OK]
- Confusing it with process scheduling
- Thinking it manages memory allocation
- Assuming it increases execution speed
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 CQuick Check:
Variables used = flag, turn; counter not used [OK]
- Assuming process_id is a variable in Peterson's solution
- Confusing counter with flag
- Thinking turn is a counter
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 BQuick Check:
Turn = 1 means P1 enters first [OK]
- Assuming both enter simultaneously
- Ignoring the turn variable
- Thinking the first flag set enters first
flag[0] = true;
turn = 1;
while(flag[1] && turn == 1) {
// wait
}
// critical section
flag[0] = false;Solution
Step 1: Review the Peterson's solution steps for process P0
Process P0 sets its flag true, sets turn to the other process (1), then waits while the other process wants to enter and it's their turn.Step 2: Check the correctness of the code snippet
The code correctly implements the waiting condition and resets the flag after the critical section.Final Answer:
No error; this is correct Peterson's solution code -> Option AQuick Check:
Code matches Peterson's solution pattern [OK]
- Changing turn to own process instead of other
- Misplacing flag reset before critical section
- Using OR instead of AND in while condition
Solution
Step 1: Understand fairness in Peterson's solution
Fairness means no process waits forever; both get a chance to enter the critical section.Step 2: Identify how fairness is implemented
The turn variable ensures that if both processes want access, the one whose turn it is gets priority, then the turn switches, alternating access.Final Answer:
By using a turn variable to alternate access between processes -> Option DQuick Check:
Turn variable alternates access = fairness [OK]
- Thinking priority decides access
- Assuming random choice controls fairness
- Believing resource is locked permanently
Ready to prove it?
Pass the test and this pattern moves unknown
