RTUComputer ScienceYr 2024 · Sem 52024

Q3Operating System

Question

4 marks

Explain the Peterson's Solution for the Critical Section Problem.

Answer

Peterson's Solution is a software-based algorithm that correctly solves the Critical Section Problem for two processes using shared variables.

Peterson's Solution is a classic software-based solution to the Critical Section Problem for two processes (P0 and P1). It uses two shared variables: (1) int turn — indicates whose turn it is to enter the critical section. (2) boolean flag[2] — flag[i] = true means process Pi is ready to enter its critical section.

Entry Section: flag[i] = true; turn = j; while (flag[j] && turn == j); / busy wait / Critical Section: (access shared resource) Exit Section: flag[i] = false; Remainder Section: (rest of program)

  • Mutual Exclusion: Both processes can only be in the CS simultaneously if both flag[0] and flag[1] are true, and both turn==0 and turn==1. Since turn is a single variable, it cannot be both 0 and 1 simultaneously. Hence mutual exclusion is satisfied.
  • Progress: If no process is in the CS and both want to enter, the one whose turn it is enters. The while loop condition will eventually become false for one process.
  • Bounded Waiting: After setting turn = j, process Pi waits at most one critical section execution by Pj before entering.

Limitation: Peterson's Solution may not work correctly on modern architectures with reordered instructions (memory reordering). Memory barriers or atomic operations are required on such hardware. It is also limited to two processes; for n processes, more complex algorithms (Bakery Algorithm) are needed.

Back to Paper