🎓 BookMCQ
← Back to 5. Process Synchronization

📝 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.
12
Easy
12
Medium
12
Hard

📝 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?

A.A single shared lock and a process counter
B.A Boolean flag array with two entries and one shared turn variable ✅
C.Two private flags and a local turn variable for each process
D.A counting semaphore and a queue of process identifiers
💡 Difficulty: easy | ✅ Correct: B

📖 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?

A.P1 has already entered the critical section
B.P1 has completed its protected operation
C.P1 is requesting permission to enter the critical section ✅
D.P1 owns the shared turn variable permanently
💡 Difficulty: easy | ✅ Correct: C

📖 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?

A.It gives P_j priority if both processes are interested in entering ✅
B.It forces P_j to enter even when P_j is not requesting
C.It records that P_i has finished its critical section
D.It makes the intent flags unnecessary during contention
💡 Difficulty: easy | ✅ Correct: A

📖 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?

A.P0 waits until turn changes to 0
B.P1 enters even though it made no request
C.Both processes enter because the flags are shared
D.P0 enters because the other process is not requesting ✅
💡 Difficulty: easy | ✅ Correct: D

📖 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?

A.P0, because turn always favors the process with index 0
B.P1, because P0's waiting condition is true while P1's is false ✅
C.Both, because both processes have announced intent
D.Neither, because the two flags are simultaneously true
💡 Difficulty: medium | ✅ Correct: B

📖 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?

A.P0 must wait until turn changes
B.P1 enters automatically because it cleared its flag
C.P0 can proceed because the waiting conjunction is now false ✅
D.Both processes must reset their flags before either can enter
💡 Difficulty: easy | ✅ Correct: C

📖 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?

A.P0 also passes because turn is 0, so both may be inside together ✅
B.P0 must wait because P1's flag is true regardless of turn
C.P1 is forced to leave before P0 can publish its flag
D.The final turn value guarantees that P1 never enters
💡 Difficulty: hard | ✅ Correct: A

📖 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?

A.Mutual exclusion must fail immediately
B.Bounded waiting becomes an exact time guarantee
C.The turn variable becomes a private variable
D.Progress, because an uninterested process can still make the other wait ✅
💡 Difficulty: medium | ✅ Correct: D

📖 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?

A.Mutual exclusion ✅
B.The two-process limit
C.The representation of turn
D.The ability to execute a remainder section
💡 Difficulty: medium | ✅ Correct: A

📖 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?

A.P1 enters because P0 is no longer executing
B.Both processes enter because P0 is inactive
C.P1 can remain blocked by the stale flag even though P0 is inactive ✅
D.The turn variable automatically clears flag[0]
💡 Difficulty: hard | ✅ Correct: C

📖 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?

A.P must wait until Q requests and exits
B.Q enters because its identifier is stored in turn
C.P and Q enter together because Q is inactive
D.P can enter because Q's false flag makes the waiting condition false ✅
💡 Difficulty: easy | ✅ Correct: D

📖 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?

A.P1, because P0 has yielded priority and now waits ✅
B.P0, because it was already inside first
C.Both, because P0 has just exited
D.Neither, because both flags are true
💡 Difficulty: medium | ✅ Correct: A

📖 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?

A.P0 is inside while P1 waits in its entry loop
B.P0 is in its remainder section while P1 enters
C.P0 and P1 are both inside their critical sections at the same time ✅
D.Both processes are in their remainder sections
💡 Difficulty: easy | ✅ Correct: C

📖 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?

A.P0 can enter whenever it requests while P1 remains in its remainder section ✅
B.P0 must wait for P1 to enter once before every P0 entry
C.The process named by turn always enters even without setting its flag
D.Both processes may enter whenever their flags become true
💡 Difficulty: medium | ✅ Correct: A

📖 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?

A.Longer critical sections reduce all waiting activity
B.Spin checks increase with the owner's duration, illustrating busy-wait overhead ✅
C.The values prove that both processes enter simultaneously
D.The turn variable changes once for every spin check
💡 Difficulty: medium | ✅ Correct: B

📖 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?

A.Turn forces P0 and P1 to alternate even when P1 is inactive
B.P0 may enter repeatedly when P1 has no pending request ✅
C.Every entry requires both flags to be true
D.P0 can enter only when turn already equals 0
💡 Difficulty: hard | ✅ Correct: B

📖 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?

A.Enter because a preempted process loses its intent flag
B.Clear P0's flag on P0's behalf
C.Continue waiting because P0 has not completed its critical section ✅
D.Change turn to a third process
💡 Difficulty: easy | ✅ Correct: C

📖 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?

A.It guarantees a fixed wall-clock deadline under every scheduler
B.It limits how many competing entries may overtake a pending request ✅
C.It guarantees equal CPU time for both processes
D.It measures the duration of the critical section itself
💡 Difficulty: medium | ✅ Correct: B

📖 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?

A.The number of instructions in each critical section
B.The amount of CPU time assigned to each process
C.How many times the competing process may enter before the waiting request is served ✅
D.The number of times turn may be written during program execution
💡 Difficulty: hard | ✅ Correct: C

📖 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?

A.The observed runs are consistent with mutual exclusion, but they do not prove every interleaving safe ✅
B.The algorithm allows two processes inside in every untested run
C.The data proves bounded waiting is always met
D.The flags were never set during these executions
💡 Difficulty: medium | ✅ Correct: A

📖 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?

A.A third Boolean value cannot be stored in an array
B.Every process must use a separate local copy of turn
C.The original reasoning covers two contenders, not all new three-way request interleavings ✅
D.Peterson's algorithm never uses shared variables
💡 Difficulty: hard | ✅ Correct: C

📖 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?

A.flag[i] == false && turn == i
B.flag[j] == true && turn == j ✅
C.flag[j] == false || turn == i
D.flag[i] == true && turn == i
💡 Difficulty: easy | ✅ Correct: B

📖 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?

A.A guarantee that every critical section has equal length
B.A rule that process identifiers increase in order
C.A guarantee that the scheduler runs each process on the same core
D.Memory ordering that makes shared updates visible in the order assumed by the proof ✅
💡 Difficulty: hard | ✅ Correct: D

📖 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?

A.Each process must run to completion without interruption
B.Operations must appear in one global order that preserves each process's program order ✅
C.All memory operations must occur at the same physical instant
D.The scheduler must alternate processes after every instruction
💡 Difficulty: medium | ✅ Correct: B

📖 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?

A.A single test proves the algorithm supports more than two processes
B.Successful runs remove the need to initialize the flags
C.One observed execution cannot guarantee atomicity and ordering across all executions and supported memory models ✅
D.A successful test proves that all waiting is nonblocking
💡 Difficulty: hard | ✅ Correct: C

📖 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?

A.P0 waits because turn equals 1
B.Both processes wait because both flags are true
C.P1 enters while P0 also enters
D.P1 waits because its condition is true, while P0 can proceed ✅
💡 Difficulty: hard | ✅ Correct: D

📖 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?

A.Mutual exclusion must fail on the first request
B.Both processes are guaranteed to enter together
C.Bounded waiting, because the same process may repeatedly receive priority ✅
D.Progress, because a process with a false flag blocks the other
💡 Difficulty: medium | ✅ Correct: C

📖 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?

A.5.5 ms
B.6.0 ms
C.6.5 ms ✅
D.7.5 ms
💡 Difficulty: medium | ✅ Correct: C

📖 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?

A.Interrupt handlers always run only after both processes exit
B.The proof assumes only the two participating processes update coordination variables according to the protocol ✅
C.An interrupt handler automatically becomes process P0
D.The turn variable is unrelated to the intent flags
💡 Difficulty: hard | ✅ Correct: B

📖 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?

A.It coordinates two processes, not an arbitrary number of processes ✅
B.It requires a hardware semaphore for each process
C.It prevents processes from executing remainder sections
D.It works only when both processes have identical instruction counts
💡 Difficulty: easy | ✅ Correct: A

📖 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?

A.Yes, because any context switch invalidates the turn variable
B.Yes, because intent flags work only when interrupts are disabled
C.No, provided the shared operations retain the atomicity and ordering assumed by the algorithm ✅
D.No, because a single processor cannot execute concurrent processes
💡 Difficulty: easy | ✅ Correct: C

📖 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?

A.Set the other process's flag to false before entering
B.Change turn to the current process on every read
C.Remove the waiting condition from the entry section
D.Keep its own flag true until all critical-section work is complete, then clear it ✅
💡 Difficulty: hard | ✅ Correct: D

📖 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?

A.To give P0 permanent priority over P1
B.To represent that neither process is initially requesting entry ✅
C.To force the first entry to alternate between processes
D.To make turn unnecessary for all later requests
💡 Difficulty: medium | ✅ Correct: B

📖 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?

A.P1 enters because it wrote turn last
B.Both enter because P0 already passed its test
C.P0 enters when resumed, while P1 waits ✅
D.Neither enters because turn changed after P0 checked
💡 Difficulty: easy | ✅ Correct: C

📖 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?

A.P0 re-enters before P1 because it was the original owner
B.Both processes enter because both flags are true
C.Neither enters until turn is reset manually
D.P1 enters first, then P0 can proceed after P1 clears its flag ✅
💡 Difficulty: hard | ✅ Correct: D

📖 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?

A.Peterson's mutual-exclusion condition has necessarily failed
B.The turn variable must be a semaphore
C.Practical progress also depends on scheduling that lets a waiting process execute ✅
D.The algorithm guarantees entry after a fixed number of clock cycles
💡 Difficulty: hard | ✅ Correct: C

📖 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.

🔗 Related Topics (MCQs)