Bird
Raised Fist0
Operating Systemsknowledge~6 mins

Peterson's solution in Operating Systems - Full Explanation

Choose your learning style10 modes available
Introduction
Imagine two people trying to use the same bathroom but only one can enter at a time. Without a clear way to decide who goes first, they might both try to enter or wait forever. Peterson's solution solves this problem for two processes trying to use a shared resource without conflicts or delays.
Explanation
Mutual Exclusion
Peterson's solution ensures that only one process can enter its critical section at a time. It uses two shared variables to coordinate access, preventing both processes from running their critical code simultaneously.
Only one process can be in the critical section at any moment.
Turn Variable
A shared variable called 'turn' indicates which process has the priority to enter the critical section. This variable helps avoid deadlock by giving one process the chance to proceed while the other waits.
The 'turn' variable controls which process gets priority to enter.
Flag Variables
Each process has a flag that it sets to true when it wants to enter the critical section. These flags let the processes know if the other is interested in entering, helping them decide whether to wait or proceed.
Flags show each process's intention to enter the critical section.
Waiting Condition
A process waits if the other process wants to enter and it is not its turn. This waiting condition ensures fairness and prevents both processes from entering at the same time.
Processes wait if it's not their turn and the other wants to enter.
Real World Analogy

Two friends want to use a single bathroom. Each friend raises their hand to show they want to enter. They agree to let the friend whose turn it is go first. If it's not your turn and the other friend wants to enter, you wait patiently until your turn comes.

Mutual Exclusion → Only one friend can be inside the bathroom at a time.
Turn Variable → The agreed order deciding which friend goes first.
Flag Variables → Each friend raising their hand to show they want to enter.
Waiting Condition → Waiting outside if it's not your turn and the other friend wants to enter.
Diagram
Diagram
┌───────────────┐       ┌───────────────┐
│ Process 0     │       │ Process 1     │
│               │       │               │
│ flag[0] = true│       │ flag[1] = true│
│ turn = 1      │◄──────│ turn = 0      │
│ Wait if flag[1] and turn == 1 │       │ Wait if flag[0] and turn == 0 │
│ Enter critical│       │ Enter critical│
│ section      │       │ section       │
└───────────────┘       └───────────────┘
Diagram showing two processes using flags and turn variable to coordinate entry into critical section.
Key Facts
Mutual Exclusion → Ensures only one process accesses the critical section at a time.
Turn Variable → Shared variable indicating which process has priority to enter.
Flag Variable → Boolean variable showing a process's intention to enter the critical section.
Deadlock → A situation where processes wait forever and none proceed.
Peterson's solution → A software method to achieve mutual exclusion between two processes.
Common Confusions
Believing Peterson's solution works for more than two processes.
Believing Peterson's solution works for more than two processes. Peterson's solution is designed only for two processes; it does not scale to more without modification.
Thinking the 'turn' variable alone prevents conflicts.
Thinking the 'turn' variable alone prevents conflicts. Both 'turn' and 'flag' variables work together; 'turn' alone cannot ensure mutual exclusion.
Assuming Peterson's solution works on all hardware without issues.
Assuming Peterson's solution works on all hardware without issues. Peterson's solution requires sequential consistency in memory operations, which some modern hardware may not guarantee without memory barriers.
Summary
Peterson's solution uses two flags and a turn variable to let two processes share a resource safely.
It guarantees that only one process enters the critical section at a time, preventing conflicts.
The solution is simple but only works correctly for two processes and requires specific memory behavior.

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

  1. 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.
  2. Step 2: Identify the main goal of the solution

    The main goal is to ensure mutual exclusion, preventing simultaneous access to shared resources.
  3. Final Answer:

    To prevent two processes from accessing a shared resource simultaneously -> Option A
  4. Quick Check:

    Mutual exclusion = Prevent simultaneous access [OK]
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

  1. 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.
  2. 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.
  3. Final Answer:

    counter -> Option C
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    P1 -> Option B
  4. 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:
flag[0] = true;
turn = 1;
while(flag[1] && turn == 1) {
  // wait
}
// critical section
flag[0] = false;
medium
A. No error; this is correct Peterson's solution code
B. turn should be set to 0 instead of 1
C. flag[0] should be set to false before the while loop
D. The while condition should use OR instead of AND

Solution

  1. 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.
  2. Step 2: Check the correctness of the code snippet

    The code correctly implements the waiting condition and resets the flag after the critical section.
  3. Final Answer:

    No error; this is correct Peterson's solution code -> Option A
  4. Quick Check:

    Code matches Peterson's solution pattern [OK]
Hint: Check waiting condition and flag reset carefully [OK]
Common Mistakes:
  • Changing turn to own process instead of other
  • Misplacing flag reset before critical section
  • Using OR instead of AND in while condition
5. How does Peterson's solution ensure fairness between two processes competing for a critical section?
hard
A. By allowing the process with higher priority to enter first
B. By locking the resource permanently for the first process
C. By using a random number generator to decide access
D. By using a turn variable to alternate access between processes

Solution

  1. Step 1: Understand fairness in Peterson's solution

    Fairness means no process waits forever; both get a chance to enter the critical section.
  2. 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.
  3. Final Answer:

    By using a turn variable to alternate access between processes -> Option D
  4. Quick Check:

    Turn variable alternates access = fairness [OK]
Hint: Turn variable alternates access to ensure fairness [OK]
Common Mistakes:
  • Thinking priority decides access
  • Assuming random choice controls fairness
  • Believing resource is locked permanently
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard