Peterson's Solution

In the above pseudocode, process i wants to enter their critical section (flag[i] = TRUE), but first lets process j enter their critical section if its wants to (turn = j). After process j executes the code if wanted (while (flag[j] && turn = j);), process i is finally allowed to enter their critical section. Once process i completes the execution inside the critical section, it lowers the flag (flag[i] = FALSE).

To prove that the solution is correct, we must examine the three conditions listed previously:

  1. Mutual Exclusion: If one process is executing their critical section when the other wishes to do so, the second process will become blocked by the flag of the first process. If both processes attempt to enter at the same time, the last process to execute turn = j will be blocked. So, this condition holds.
  2. Progress: The shared variable turn assures that only one process at a time can be blocked, and the flag variable allows one process to release the other when exiting their critical section. Since one of the processes will continue executing, there is progress and this condition holds.
  3. Bounded Waiting: As each process enters their entry section, they set the turn variable to be the other processes turn. Since no process ever sets it back to their own turn, this ensures that each process will have to let the other process go first at most one time before it becomes their turn again, so this condition holds.