Peterson's Solution

Peterson's Solution is a classical software-based solution to the critical section problem. However, due to the way modern computer architectures perform basic machine-language instructions, such as load from and store to memory, there are no guarantees that Peterson’s solution will work correctly on such architectures, but we still want to show this solution's good algorithmic thinking and consideration of the complexities involved in the solution.

Peterson's solution is based on two processes, P0 and P1, which alternate between their critical sections and remainder sections. More generally, Pi is the current process, and Pj (where j = 1 - i) is the 'other' process. The solution requires two shared data items:

  1. int turn: Indicates whose turn it is (who is allowed) to enter into the critical section. If turn == i, then process Pi is allowed into its critical section.
  2. boolean flag[2]: Indicates when a process wants to enter into their critical section. For example, when process Pi wants to enter its critical section, it sets flag[i] to TRUE.