Formal modeling and verification of Peterson's algorithm

We have studied different (incorrect) algorithms for mutual exclusion in a distributed setting. In this series of exercises, our goal is to model and verify Peterson’s algorithm.

Part 1

Peterson’s algorithm for 2 processes

The algorithm below shows Peterson’s algorithm for process i (see Wikipedia page for extra information).

do forever {
  // entry protocol
  flag[i] := true
  turn := i
  wait until (turn != i) or (forall j != i, flag[j] = false)
  // in critical section
  ...
  // exit protocol
  flag[i] := false
}

The algorithm above uses two shared variables:

  • flag which is an array indexed from 1 to N, initialised to false
  • turn which is an integer variable with values in 0..N, initialised to 0

In the entering phase of the algorithm, a process first requests access to the critical section by setting its flag. Then, the turn variable is used to arbitrate between processes that require access concurrently. If turn=i, then process i gives priority to the other process. Thus process i can enter the critical section if either no other process requires access, or if turn != i.

  1. Justify the solution proposed by Peterson with respect to the counter-example provided by TLC for algorithms Mutex2 and Mutex3 in the previous series of exercises.
  2. Write a PlusCal model of Peterson’s algorithm. Each line is viewed as an atomic instruction.
  3. Check that the algorithm is correct w.r.t. invariant Mutex, as well as absence of deadlocks, for N=2 processes
  4. The algorithm is incorrect for N=3 processes. Use the model-checker to get a counter-example. From the counter-example, tell which one of the two properties is violated, and explain why.

Modelling Peterson’s algorithm for N processes

We consider a more general version of Peterson’s algorithm that is expected to be correct for N processes. The pseudo-code is shown for process i below.

do forever {
  // entry protocol
  for l := 1 to N-1 {
    level[i] := l;
    turn[l] := i;
    wait until (turn[l] != i) or (forall j != i, level[j] < l);
  }
  // in critical section
  ...
  // exit protocol
  level[i] := 0
}

The algorithm uses two shared arrays:

  • level indexed from 1 to N which is initialized to 0,
  • and turn indexed from 1 to N-1 and initialized to 0.

In the entry phase, the process goes through N-1 levels.

  • The value of level[i] is the current level of process i, or 0 if process i does not request access to the critical section.
  • The value of turn[l] is the identifier of the last process that has reached level l (or 0 if no process has reached l so far). Roughly speaking, it tells which process is blocked at level l.

When a process enters level l, it is blocked until either another process enters level l, or all the other processes are at lower levels (cf. wait instruction in the algorithm above). Hence, one process among all those at level l is blocked there, whereas all the other processes at level l move up. As a result, there is at most N-l processes that exit level l. In particular, at most one process can exit level N-1 and enter the critical section. Thus, the role of the variable turn is to ensure mutual exclusion generalizing the turn variable used in the version of Peterson’s algorithm for 2 processes.

The level variables are used to ensure progress. The process at the highest level can go to the next level (or enter the critical section) even if it is the last process at this level. Observe that this is crucial to ensure progress if less that N processes actually try to enter the critical section.

  1. Translate the pseudo-code above into a PlusCal model where each line is modelled as an atomic instruction.
  2. Check that your model satisfies the invariant Mutex as well as absence of deadlocks

Ensuring progress

  1. Cite two properties that an algorithm with no deadlock, and that satisfies the invariant Mutex, may violate hence providing an unsatisfying solution to the mutual exclusion problem.
  2. Update the specification accordingly. Then model-check all properties, adding (* PlusCal options (-sf) *) before the definition of your PlusCal algorithm. Your model should satisfy all requirements.
  3. Check that the two properties added above do not hold for algorithm Mutex3 in the previous series of exercises. What can you conclude about specifications?

Part 2

Modelling Peterson’s algorithm in a distributed settings

In the pseudo-code of Peterson’s algorithm above, the wait instruction accesses several shared variables in a single atomic instruction.

  1. Explain why this is not realistic w.r.t. implementation of shared variables in standard programming languages (C, C++, Java, etc)
  2. Update your model to implement the wait statement as a non-atomic instruction.
  3. Check that your model satisfies the invariant Mutex as well as absence of deadlocks
  4. Check that your model ensures progress under (* PlusCal options (-sf) *).