📝 Peterson's Solution (36 MCQs)
Software-Based Mutual Exclusion for Two Processes
Peterson's solution is a classical software algorithm for solving the critical-section problem between two concurrent processes. It uses two shared Boolean flags and a shared turn variable to coordinate entry. Each process announces its intention to enter and gives the other process priority when both compete. The algorithm demonstrates how mutual exclusion, progress, and bounded waiting can be achieved without special atomic hardware instructions under idealised assumptions.
Why Two Competing Processes Need Coordinated Intentions
Simply checking whether another process wants to enter is insufficient because both may observe an available critical section simultaneously. Peterson's solution combines intention indicators with a turn variable to resolve this competition. Its design avoids allowing either participant to repeatedly enter while the other has been waiting under the algorithm's assumptions.
Flag Registration and Turn-Based Entry Protocol
For process Pi, the algorithm sets flag[i] to true, assigns turn to the other process, and waits while the other flag is true and turn favours the other process. After leaving its critical section, Pi resets flag[i] to false. The shared flag array records intentions, while turn resolves simultaneous requests.
Two Processes Requesting a Critical Section
Suppose P0 and P1 both attempt entry simultaneously. Both set their flags to true, but their turn assignments occur in an interleaved order, leaving one process with priority. The favoured process enters, while the other waits. When the first resets its flag on exit, the waiting process satisfies its entry condition and proceeds.
Correctness Properties and Modern Hardware Limitations
Under sequentially consistent atomic reads and writes, Peterson's solution satisfies mutual exclusion, progress, and bounded waiting for two processes. It is useful for understanding software synchronization but is not a general production locking mechanism. Modern processors and compilers may reorder memory operations, so suitable memory-ordering guarantees are essential for a practical implementation.
Misconceptions About Peterson's Algorithm
'Peterson's solution supports any number of processes directly' is incorrect because the basic algorithm addresses two participants. 'The turn variable alone guarantees mutual exclusion' is incorrect because flags also record intentions. 'The algorithm works unchanged under every memory model' is incorrect because modern execution may require ordering controls.
Exam Focus: Tracing Flags and the Turn Variable
Trace flag[0], flag[1], and turn through simultaneous entry attempts and process exits. Prove mutual exclusion by examining the waiting conditions, then explain progress and bounded waiting separately. Questions may ask which process enters first, identify the effect of resetting a flag, or examine the assumptions required for correctness.
Key Terms of Peterson's Solution
- flag[i] - Indicates process i wants entry.
- turn - Identifies preferred competing process.
- Busy waiting - Repeated checking of an entry condition.
- Sequential consistency - Consistent global operation ordering.
- Mutual exclusion - Prevents simultaneous critical-section entry.
- Memory ordering - Controls visibility and ordering of operations.
📝 All Peterson's Solution MCQs
Q1. [Shared variables and protocol] For two processes P0 and P1, which shared data structure does the standard Peterson algorithm use to coordinate entry?
📖 Explanation: Peterson's method needs one shared Boolean flag per process to publish intent and one shared turn value to resolve simultaneous requests. Separate local copies cannot coordinate competing decisions across processes.
Q2. What does flag[1] = true communicate in the standard protocol?
📖 Explanation: A true flag records a request to enter, not proof that the process has already entered. It remains true through critical-section execution and is cleared only after protected work finishes.
Q3. Why does process P_i assign turn = j after setting flag[i] = true?
📖 Explanation: The flag announces interest, while turn yields priority to the other participant when both flags are true. If the other flag is false, the waiting conjunction fails and uncontested entry remains possible.
Q4. P0 has set flag[0] = true. The shared values are flag[1] = false and turn = 1. What does P0 do at its waiting condition?
📖 Explanation: P0 waits only while flag[1] is true and turn equals 1. Because flag[1] is false, the conjunction is false, so P0 enters regardless of the stored turn value for this uncontended request.
Q5. Both flags are true and turn = 1. Under sequentially consistent execution, which process can pass its waiting condition?
📖 Explanation: P0's condition is true && true, so P0 spins. P1's condition is true && false, so P1 enters. The single turn value cannot favor both participants simultaneously at the same time under sequential consistency.
Q6. P0 is waiting while flag[1] is true and turn = 1. P1 withdraws its request and sets flag[1] = false. What happens at P0's next check?
📖 Explanation: P0's loop requires both conditions to remain true. Once P1 clears its flag, the conjunction becomes false, so P0 can proceed without waiting for turn to change or for P1 to enter.
Q7. [Ordering and interleavings] A modified version writes turn before publishing intent. Trace: P0 writes turn = 1; P1 writes turn = 0 and flag[1] = true; P1 checks while flag[0] = false and enters; P0 then sets flag[0] = true and checks. What failure can occur?
📖 Explanation: P1 tests while flag[0] is still false and enters before P0 publishes intent. P0 later sees turn equal to 0, so its own wait condition is false too; mutual exclusion fails.
Q8. A student replaces the waiting test flag[j] && (turn == j) with flag[j] || (turn == j). If flag[j] is false but turn equals j, what property can be lost?
📖 Explanation: With OR, a process can wait merely because turn names the other process, even when that process has no request. The other flag should make an uncontended request eligible immediately.
Q9. P0 is still performing a protected shared-data update but clears flag[0] before that update finishes. P1 then observes flag[0] = false and enters. Which property is directly violated?
📖 Explanation: The flag must remain true until every protected operation is complete. Clearing it early advertises that P0 has left, allowing P1 to enter while P0 still accesses shared data inside the critical section.
Q10. P0 finishes its critical section but forgets to set flag[0] = false. P1 requests entry, sets turn = 0, and waits. P0 remains in its remainder section and makes no new request. What is the likely result?
📖 Explanation: P1 sees flag[0] still true and turn equal to 0, so it keeps spinning although P0 is inactive. The stale flag blocks progress and can cause indefinite waiting under this execution.
Q11. Q is in its remainder section, so flag[Q] = false. P requests entry while turn = Q. Which statement is correct?
📖 Explanation: The waiting expression is flag[other] && (turn == other). Since Q's flag is false, P does not wait; the turn value resolves contention only when Q also requests entry in this uncontended case.
Q12. P0 is in its critical section while P1 waits. P0 exits, then immediately requests entry again by setting flag[0] = true and turn = 1. Assuming P1's flag remains true, who can enter next?
📖 Explanation: When P0 publishes its renewed request, it sets turn to 1. P0 must then wait because P1 remains interested, while P1's condition is false and allows it to enter first.
Q13. [Correctness properties] Which execution state is a direct violation of mutual exclusion in Peterson's two-process protocol?
📖 Explanation: Mutual exclusion permits at most one process inside the protected region at any instant. The other listed states describe valid competition or noncritical execution, not simultaneous ownership under the algorithm's assumptions.
Q14. Which situation demonstrates progress rather than strict alternation?
📖 Explanation: If P1 is in its remainder section, its flag is false. P0 may enter whenever it requests, even repeatedly; turn does not force alternation when there is no competition between the two processes.
Q15. A test records critical-section duration and average spin checks by the waiting process: (1 ms, 20); (2 ms, 40); (3 ms, 60); (4 ms, 80); (5 ms, 100). Which conclusion best fits these plotted values?
📖 Explanation: The measured spin count rises with critical-section duration, showing that a longer owner interval makes the waiting process poll more often. This reflects busy-wait cost, not a mutual-exclusion failure by itself.
Q16. A run records these pairs: (P0 requests while flag[1] stays false, P0 successful entries): (1,1); (2,2); (3,3); (4,4); (5,5). What does this trend demonstrate?
📖 Explanation: The plotted entries match each request while P1 remains uninterested. This shows that turn does not impose strict alternation; an inactive competitor cannot block P0 through the waiting condition under this protocol.
Q17. P0 is inside its critical section when P1 requests entry and begins waiting. The scheduler preempts P0 before it exits. What should P1 do while P0 is paused?
📖 Explanation: Although P1 has requested entry, P0 has not yet released the protected region. Preemption pauses P0 but does not clear its flag, so P1 must continue waiting to preserve mutual exclusion.
Q18. A student says bounded waiting means a process is guaranteed to enter within 5 milliseconds. What is the correct interpretation?
📖 Explanation: Bounded waiting limits how many times another process may enter before a pending request is served. It does not set a time deadline, because scheduling delays and section lengths vary.
Q19. Under the standard two-process protocol, what does bounded waiting restrict when both processes continue requesting entry?
📖 Explanation: With two processes, the algorithm prevents the active competitor from overtaking a continuously waiting request without bound. The guarantee is a count of intervening entries, not equal CPU time for that pending request.
Q20. A model checker records the maximum number of processes inside during five tested executions: (run 1, 1); (run 2, 1); (run 3, 1); (run 4, 1); (run 5, 1). What is the strongest justified conclusion?
📖 Explanation: All five runs show at most one process inside, consistent with mutual exclusion. A finite sample cannot prove correctness for every possible interleaving; exhaustive reasoning or proof is still required.
Q21. [Assumptions and implementation limits] A developer adds flag[2] for a third process but leaves the two-process turn and waiting rule unchanged. Why is this not a proven three-process solution?
📖 Explanation: The standard proof reasons about two flags and one turn value shared by exactly two contenders. Adding another flag creates new simultaneous-request cases that the unchanged rule does not resolve or prove fair.
Q22. Which expression is the standard waiting condition for process P_i, where j is the other process?
📖 Explanation: A process waits only when its competitor has raised its flag and the shared turn gives that competitor priority. Removing either part changes the behavior and may break a required property.
Q23. Two processors publish their intent flags, but store buffers delay visibility. Each processor then reads the other flag as false and enters. Which missing guarantee explains this possible failure?
📖 Explanation: Both cores can read the other's old false flag if their writes are not yet visible. Each then leaves the loop, so ordinary source-code order alone cannot preserve mutual exclusion without ordering guarantees.
Q24. What does sequential consistency require for the shared reads and writes used in the classical proof?
📖 Explanation: Sequential consistency means all shared-memory operations can be viewed in one global order that respects each process's program order. The proof relies on that ordering when analyzing interleavings of reads and writes.
Q25. An implementation passes one stress test on a particular processor. Why does this alone not establish that Peterson's algorithm is correctly implemented?
📖 Explanation: A successful test shows only that one observed execution worked. It cannot guarantee atomicity or ordering for every interleaving, compiler transformation, processor, or language memory model that the implementation may encounter.
Q26. Both flags are true. The trace is P0: flag[0] = true; turn = 1. Then P1: flag[1] = true; turn = 0. Which process waits at its first test?
📖 Explanation: P1 tests true && true and waits; P0 tests true && false and enters. Because turn has one value, only P1's wait condition remains satisfied. P0 proceeds first in this interleaving.
Q27. Suppose both processes keep setting their flags, but the entry protocol no longer updates turn. If turn remains fixed at 0, which property can fail?
📖 Explanation: Without updating turn on each request, a fixed value can repeatedly favor the same process during contention. The other may be overtaken repeatedly, so bounded waiting is no longer guaranteed.
Q28. An instrumented run keeps scheduling overhead fixed at 0.5 ms and records (P0 critical-section duration, P1 elapsed wait): (1,1.5); (2,2.5); (3,3.5); (4,4.5); (5,5.5). Based on this measured trend, what wait is expected when P0's duration is 6 ms?
📖 Explanation: Each measured wait is the critical-section duration plus 0.5 ms of fixed scheduling overhead. For 6 ms, the expected wait is 6 + 0.5 = 6.5 ms, following the stated linear measurements.
Q29. [Performance and execution environment] An interrupt handler modifies flag[0] while P0 and P1 are using Peterson's protocol. Why must the original correctness proof be reconsidered?
📖 Explanation: The proof assumes only P0 and P1 update flags and turn according to the protocol. An interrupt handler that changes them introduces an extra actor, requiring a new correctness analysis.
Q30. Which limitation applies to the basic Peterson algorithm without modification?
📖 Explanation: The basic algorithm has two participants because its turn variable distinguishes only P0 and P1. More processes require a different protocol, such as a proven generalized mutual-exclusion algorithm for larger groups.
Q31. A single-processor system switches between P0 and P1 between instructions. Must Peterson's algorithm disable interrupts merely to coordinate these two processes?
📖 Explanation: On one processor, context switches can interleave the two processes between atomic shared-memory operations. Disabling interrupts is not the defining mechanism, but the assumed atomicity and ordering must hold for correctness.
Q32. A process clears its flag before completing its final protected write. Which correction restores the intended protocol?
📖 Explanation: P0 withdraws its intent before the protected update finishes, so P1 may enter and access the same data. The flag must be cleared only after the critical section is complete and no protected work remains.
Q33. Why are both intent flags normally initialized to false before either process begins?
📖 Explanation: False initial flags correctly state that neither process is requesting entry. This prevents a process from treating an inactive competitor as interested and avoids artificial waiting during startup before either begins its protocol.
Q34. Initially both flags are false. P0 sets flag[0] = true, sets turn = 1, and evaluates its waiting condition as false. P0 is preempted before entering. P1 then sets flag[1] = true, sets turn = 0, and checks its condition. What follows?
📖 Explanation: P0 already evaluated its waiting condition as false before the preemption. P1 then sees P0's published flag and yields because turn is 0. P0 resumes and enters safely without overlapping another critical section.
Q35. P0 is inside its critical section. P1 requests entry and waits. P0 exits, then immediately re-requests by setting flag[0] = true and turn = 1. What is the resulting entry order under the standard model?
📖 Explanation: P0's renewed request sets turn to 1, making P0 wait while P1 remains interested. P1 then sees turn is not 0, enters, and clears its flag on exit; P0 proceeds afterward.
Q36. P1 is waiting in the entry loop, and P0 has cleared its flag. The operating-system scheduler never dispatches P1 again. What does this reveal?
📖 Explanation: The algorithm cannot make a process execute if the scheduler never dispatches it. The shared-state protocol may preserve safety, but practical progress also depends on scheduling that lets waiting processes run.