📝 Peterson's Solution (51 MCQs)
📖 From Operating System • 5. Process Synchronization • 51 questions available
What is Peterson's Solution?
Definition:
Peterson's solution is a software-based algorithm for two-process mutual exclusion using shared variables and to enforce critical section entry without hardware support.
Example:
Process sets and , then waits in before entering the critical section.
Reason:
It provides a provably correct software solution demonstrating that mutual exclusion, progress, and bounded waiting can be achieved purely through shared memory algorithms for .
📝 All Peterson's Solution MCQs
Q1. What is the fundamental nature of Peterson's solution?
📖 Explanation: Peterson's solution is a classic software-based algorithm designed to solve the critical-section problem. It uses shared memory variables to coordinate access between two processes, demonstrating the principles of mutual exclusion, progress, and bounded waiting.
Q2. Peterson's solution is restricted to how many processes?
📖 Explanation: Peterson's solution is specifically designed for two processes, typically denoted as P0 and P1. This restriction is fundamental to the algorithm's operation, as it relies on a simple turn variable to arbitrate access between the pair.
Q3. What are the two shared data items used in Peterson's solution?
📖 Explanation: Peterson's solution relies on two shared variables: an integer `turn` to indicate which process is allowed to enter the critical section, and a boolean array `flag[2]` to indicate whether each process is ready to enter its critical section. These variables coordinate the protocol.
Q4. In Peterson's solution, what does the variable `turn == i` signify?
📖 Explanation: When `turn == i`, it means that process Pi has the right to enter its critical section. This variable is used as a tie-breaker to resolve conflicts when both processes want to enter simultaneously, ensuring only one proceeds.
Q5. What does the `flag[i] = true` assignment in Peterson's solution indicate?
📖 Explanation: Setting `flag[i] = true` signals that process Pi is ready and willing to enter its critical section. This is a key step in the entry section, as it communicates intent to the other process, which is crucial for implementing mutual exclusion.
Q6. What is the purpose of the `while (flag[j] && turn == j)` loop in Peterson's solution?
📖 Explanation: The while loop is the waiting mechanism. A process will stay in this loop if the other process is ready to enter (`flag[j] == true`) and it is currently the other process's turn (`turn == j`). Once one of these conditions becomes false, the process can proceed into its critical section.
Q7. According to the proof of Peterson's solution, what condition must be true for process Pi to enter its critical section?
📖 Explanation: A process Pi can enter its critical section only if either the other process is not ready (`flag[j] == false`) or if it is Pi's turn (`turn == i`). This ensures mutual exclusion, as both processes cannot have this condition true at the same time.
Q8. What happens if both processes in Peterson's solution set `turn` to the other process's ID at approximately the same time?
📖 Explanation: If both processes try to set `turn` concurrently, one assignment will be overwritten by the other. The final value of `turn` is what matters. It acts as a deterministic tie-breaker, ensuring that only the process whose ID matches `turn` can proceed, thereby preserving mutual exclusion.
Q9. Which of the following is a limitation of Peterson's solution?
📖 Explanation: The most significant limitation of Peterson's solution is its restriction to only two processes. It is not designed for n-process mutual exclusion. This is a fundamental constraint of the algorithm, making it less scalable for general-purpose systems.
Q10. Why is Peterson's solution important despite its limitations?
📖 Explanation: Peterson's solution is important for educational and conceptual reasons. It provides a clear, elegant example of how software can solve the critical-section problem, helping to illustrate the challenges of mutual exclusion, progress, and bounded waiting. Its simplicity makes it a classic teaching tool.
Q11. What does `flag[j]` being `false` indicate in Peterson's solution?
📖 Explanation: If `flag[j]` is `false`, it indicates that process Pj is not currently ready to enter its critical section. This means process Pi does not have to wait for Pj and can proceed into its own critical section, assuming the `turn` condition is also met.
Q12. In Peterson's solution, what is the role of the `flag` array?
📖 Explanation: The `flag` array is used to signal intent. `flag[i] = true` means process Pi is ready to enter its critical section. This is a necessary condition for the other process to determine if it needs to wait or can proceed.
Q13. Which requirement of the critical-section problem does Peterson's solution successfully satisfy?
📖 Explanation: Peterson's solution is a classic, elegant algorithm that correctly satisfies all three requirements of the critical-section problem: Mutual Exclusion, Progress, and Bounded Waiting. This is why it is a foundational example in concurrent programming, despite its limitations.
Q14. What is the result of a process Pi executing `turn = j` in the entry section?
📖 Explanation: By setting `turn = j`, process Pi is explicitly granting priority or preference to the other process. This ensures fairness and prevents a single process from monopolizing the critical section, as the process is signaling that it is willing to let the other go first.
Q15. Why might Peterson's solution not work correctly on modern computer architectures?
📖 Explanation: Peterson's solution assumes that operations like loading and storing variables are atomic and occur in a predictable order. Modern architectures may reorder memory operations or have cache inconsistencies, leading to a situation where the algorithm's proof, based on sequential consistency, no longer holds, making it unreliable.
Q16. If process P0 sets `flag[0] = true` and `turn = 1`, and process P1 sets `flag[1] = true` and `turn = 0`, what ensures only one process enters the critical section?
📖 Explanation: The while loop condition `while (flag[j] && turn == j)` is the key. If both flags are true, the process that is not the current `turn` value will be stuck in its loop, while the other will proceed. The final value of `turn` ensures only one process exits the loop.
Q17. In the proof of bounded waiting for Peterson's solution, what is the maximum number of times process Pj can enter its critical section before Pi is allowed to enter?
📖 Explanation: Bounded waiting is guaranteed because Pi sets `turn = j`. If Pj tries to enter again, it will set `turn = i`, allowing Pi to enter. This means after Pi's request, Pj can enter at most once before Pi is allowed to proceed, satisfying the bounded waiting requirement.
Q18. What is the purpose of the `remainder section` in Peterson's solution?
📖 Explanation: The remainder section is executed after the critical section and exit section. It contains the rest of the process's code that does not involve the shared resource, allowing the process to perform other computations while not in the critical section.
Q19. Which statement accurately describes the `turn` variable in Peterson's solution?
📖 Explanation: The `turn` variable is a shared integer that stores the ID of the process whose turn it is to enter the critical section. It is used as a simple and fair mechanism to decide which process gets access when both are ready.
Q20. Peterson's solution is a good example of:
📖 Explanation: Peterson's solution is a classic software-based algorithm that correctly addresses the critical-section problem. Its key achievement is satisfying Mutual Exclusion, Progress, and Bounded Waiting using only shared memory variables, making it a fundamental example in computer science.
Q21. If process Pi is in its critical section, what does the proof of mutual exclusion in Peterson's solution state about process Pj?
📖 Explanation: The mutual exclusion proof shows that if Pi is in its critical section, the condition for Pj to enter (`flag[i] == false` or `turn == j`) will be false, as `flag[i]` is true and `turn` is set to `j` (for Pj's entry). This prevents Pj from exiting its while loop, thus preserving mutual exclusion.
Q22. How does the `flag` array help satisfy the progress requirement in Peterson's solution?
📖 Explanation: The `flag` array enables progress because a process will wait only if the other process has expressed interest (`flag[j] == true`). If the other process is not ready, the waiting process can enter, ensuring the system does not stall. This prevents indefinite postponement.
Q23. In the algorithm structure for process Pi, what is the correct sequence of actions?
📖 Explanation: The correct sequence is: 1) Set `flag[i] = true` to indicate readiness. 2) Set `turn = j` to give the other process priority. 3) Wait in the while loop if the other is ready and it's their turn. 4) Execute the critical section. 5) Set `flag[i] = false` to indicate exit. This is the exact algorithm for Pi.
Q24. What is the significance of the statement `turn = j` in the algorithm for process Pi?
📖 Explanation: The `turn = j` statement is crucial for ensuring progress and bounded waiting. By setting the turn to the other process, Pi demonstrates willingness to let Pj enter. This prevents any single process from hogging the critical section and ensures that the selection of the next process is fair, preventing starvation.
Q25. What is a key reason for studying Peterson's solution in operating systems courses?
📖 Explanation: Peterson's solution is a classic pedagogical tool. It provides an elegant and concise illustration of a software-based solution to the critical-section problem, demonstrating the core principles of mutual exclusion, progress, and bounded waiting in a way that is easy to understand and analyze.
Q26. What does the `while (flag[j] && turn == j)` condition check before a process can enter the critical section?
📖 Explanation: The while loop condition is the entry guard. It evaluates to `true` (and thus the process waits) only when the other process is ready (`flag[j] == true`) AND it is the other process's turn to enter (`turn == j`). If either is false, the process can proceed.
Q27. What is the primary proof mechanism used to show Peterson's solution ensures mutual exclusion?
📖 Explanation: The mutual exclusion proof hinges on the `turn` variable. The proof argues that if both processes were in their critical sections, both `flag` entries would be true. However, because `turn` can only hold one value (0 or 1), one of the processes must have been the last to set `turn`, which would cause the other to be stuck in its while loop, proving mutual exclusion.
Q28. How does Peterson's solution handle the situation where process Pj completes its critical section and sets `flag[j] = false`?
📖 Explanation: When Pj exits and sets `flag[j] = false`, the condition for Pi's while loop (`flag[j] && turn == j`) becomes false. This allows Pi to exit the loop and enter its critical section. The algorithm elegantly transfers access to the waiting process.
Q29. What does the variable `j` represent in the description of Peterson's solution for process Pi?
📖 Explanation: In the description, `j` is used to denote the other process. Since the algorithm is for two processes (P0 and P1), if `i` is the current process, `j` is calculated as `1 - i`. This notation simplifies the explanation of the algorithm by referring to the other participant.
Q30. Which of the following is NOT a characteristic of Peterson's solution?
📖 Explanation: Peterson's solution is specifically designed for two processes. It is not an n-process solution. This is a key limitation and a defining characteristic of the algorithm, which is otherwise a classic and elegant software synchronization example.
Q31. If process P0 sets `flag[0] = true` and `turn = 1`, and P1 is in its remainder section, what will P0 do?
📖 Explanation: Since P1 is in its remainder section, `flag[1]` is `false`. The while loop condition for P0 (`flag[1] && turn == 1`) evaluates to `false` because `flag[1]` is `false`. Therefore, P0 will not wait and will proceed directly into its critical section.
Q32. What is the fundamental reason Peterson's solution might fail on modern processors?
📖 Explanation: Peterson's solution assumes that memory operations like writes to `flag` and `turn` occur in the order they are programmed (sequential consistency). Modern CPUs often reorder memory operations for performance. This means a process might see the updated `turn` before the updated `flag`, causing the algorithm to fail as its proof is no longer valid.
Q33. In Peterson's solution, what happens after a process executes the `critical section`?
📖 Explanation: After a process finishes its critical section, it must execute the exit section, which in Peterson's solution is the statement `flag[i] = false`. This signals to the other process that it is no longer interested in the critical section, allowing the other process to potentially enter.
Q34. What is the role of the `flag` array in enforcing mutual exclusion?
📖 Explanation: The `flag` array is central to mutual exclusion. When a process wants to enter, it sets its `flag` to `true`. Before entering, it checks the other process's `flag`. If the other process's `flag` is `true`, it means the other process has also expressed interest, and the `turn` variable is used to resolve the conflict. This signaling mechanism prevents both from entering simultaneously.
Q35. The progress requirement in Peterson's solution is satisfied because:
📖 Explanation: Progress is satisfied because a process is only delayed when the other process is both interested and has priority. If the other process is not interested, the waiting process can enter immediately. This ensures that the system cannot be indefinitely postponed and will make forward progress.
Q36. Which of the following best describes the educational value of Peterson's solution?
📖 Explanation: Peterson's solution's primary value is educational. It provides an accessible and elegant demonstration of how software can solve the complex problem of process synchronization. It is an excellent starting point for understanding the principles of mutual exclusion, progress, and bounded waiting.
Q37. What is the maximum number of times a process can be bypassed in Peterson's solution?
📖 Explanation: Peterson's solution ensures bounded waiting by guaranteeing that a process will be bypassed at most once. Since a process Pi sets `turn = j`, Pj will enter if it is ready. If Pj tries to enter again immediately after, it will set `turn = i`, allowing Pi to enter. This strict alternation ensures fairness.
Q38. What is the primary assumption about memory operations in the classic proof of Peterson's solution?
📖 Explanation: The proof of Peterson's solution relies on the assumption of sequential consistency, meaning memory operations are executed in the order they appear in the program code and are indivisible. This assumption is often violated in modern architectures, leading to the algorithm's practical failure.
Q39. If process P0 and P1 are both ready to enter their critical sections, what determines which one enters first in Peterson's solution?
📖 Explanation: When both processes are ready, the `turn` variable is the deciding factor. The process whose ID matches the value of `turn` is the one that will be allowed to enter its critical section. The other process will be stuck in its while loop until the first process exits and resets its flag.
Q40. What action does a process take in the `exit section` of Peterson's solution?
📖 Explanation: The exit section is defined by the statement `flag[i] = false`. This is the crucial step where a process signals that it has finished with the critical section, effectively releasing it so that the other process may enter. This is a simple but essential part of the algorithm.
Q41. How does Peterson's solution demonstrate the concept of 'bounded waiting'?
📖 Explanation: Peterson's solution demonstrates bounded waiting by limiting the number of times a process can be bypassed. Since a process Pi sets `turn = j` when entering, it is effectively giving the other process priority. The other process can enter, but when it is done and tries to enter again, it must set `turn = i`, allowing Pi to proceed. This ensures no process waits forever.
Q42. Which of the following statements about the `turn` variable is true?
📖 Explanation: The `turn` variable is specifically used as a tie-breaker when both processes are attempting to enter their critical sections. If only one process is interested, the `turn` variable is not needed, as the other process's `flag` will be `false`, and the interested process can enter immediately.
Q43. Why is Peterson's solution considered a classic in operating system design?
📖 Explanation: Peterson's solution is a classic because it provides a concise, elegant, and understandable software-based solution to the critical-section problem. Its beauty lies in its simplicity and the way it elegantly handles the complexities of concurrency using just two shared variables.
Q44. What is the effect of process Pi executing `turn = j` before checking `flag[j]`?
📖 Explanation: By setting `turn = j`, process Pi intentionally gives priority to the other process (Pj). This is a key part of the algorithm that ensures bounded waiting. If both processes are ready, the one that set `turn` last (the other process) will have priority, resolving the conflict.
Q45. How does Peterson's solution handle the situation where both processes have `flag` set to `true`?
📖 Explanation: When both `flag` values are `true`, both processes are interested. The algorithm then relies on the `turn` variable as a deterministic tie-breaker. The process whose ID matches the value of `turn` will be allowed to proceed, while the other will wait, thus ensuring mutual exclusion is maintained.
Q46. What does the assignment `turn = j` in process Pi's entry section imply about the algorithm's philosophy?
📖 Explanation: By setting `turn = j`, Pi is, in a sense, being generous and letting Pj go first. If Pj subsequently sets `turn = i`, then Pi will go first. Thus, the process that executes the `turn` assignment last (the one with the most recent write) will be the one that does not have priority and will wait, while the other proceeds. This is a crucial insight for the algorithm's fairness.
Q47. Why is it important to understand the limitations of Peterson's solution regarding modern architectures?
📖 Explanation: Understanding the limitations is crucial for applying the right tools in the right context. While Peterson's solution is a great pedagogical tool, its reliance on sequential consistency means it's not safe for use on modern architectures. This highlights the need for hardware-supported or language-level synchronization primitives in production code.
Q48. What is the role of the `while` loop in the entry section of Peterson's solution?
📖 Explanation: The `while` loop is a form of busy waiting. The process continuously checks the condition `(flag[j] && turn == j)`. As long as this condition is true, the process cannot enter. This is a simple but CPU-intensive way to implement a wait, which is a common characteristic of early software solutions.
Q49. If process Pi is in its critical section, and process Pj has executed `flag[j] = true` and `turn = i`, what is the state of process Pj?
📖 Explanation: Pj has set its flag to true and is waiting. Since Pi is in the critical section, Pi's flag is true and `turn` is likely `j` (from when Pi entered). For Pj, its while loop condition `(flag[i] && turn == i)` will be true (because Pi is in its critical section and `turn` is `i`). Therefore, Pj will be stuck in its while loop, waiting for Pi to exit.
Q50. What will happen in Peterson's solution if process Pi never executes its `exit section` after entering the critical section?
📖 Explanation: If Pi never exits, it will not reset its `flag[i]` to `false`. For Pj, its while loop condition `(flag[i] && turn == i)` will remain true indefinitely, as `flag[i]` is true and `turn` is set to `i`. This causes Pj to wait forever. While Peterson's solution itself is correct, it assumes processes execute their exit sections, otherwise progress is violated.
Q51. How does the use of a simple software algorithm like Peterson's solution illustrate the challenges of process synchronization?
📖 Explanation: Peterson's solution beautifully illustrates the non-trivial nature of synchronization. It shows that even with two simple shared variables, designing a correct algorithm is not obvious. The fact that a seemingly correct algorithm can fail on modern hardware due to subtle issues like memory reordering highlights the complexity of the problem.