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:
flagwhich is an array indexed from1toN, initialised tofalseturnwhich is an integer variable with values in0..N, initialised to0
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.
- Justify the solution proposed by Peterson with respect to the counter-example provided by TLC for algorithms
Mutex2andMutex3in the previous series of exercises. - Write a PlusCal model of Peterson’s algorithm. Each line is viewed as an atomic instruction.
- Check that the algorithm is correct w.r.t. invariant
Mutex, as well as absence of deadlocks, forN=2processes - The algorithm is incorrect for
N=3processes. 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:
levelindexed from1toNwhich is initialized to0,- and
turnindexed from1toN-1and initialized to0.
In the entry phase, the process goes through N-1 levels.
- The value of
level[i]is the current level of processi, or0if processidoes not request access to the critical section. - The value of
turn[l]is the identifier of the last process that has reached levell(or0if no process has reachedlso far). Roughly speaking, it tells which process is blocked at levell.
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.
- Translate the pseudo-code above into a PlusCal model where each line is modelled as an atomic instruction.
- Check that your model satisfies the invariant
Mutexas well as absence of deadlocks
Ensuring progress
- 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. - 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. - Check that the two properties added above do not hold for algorithm
Mutex3in 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.
- Explain why this is not realistic w.r.t. implementation of shared variables in standard programming languages (C, C++, Java, etc)
- Update your model to implement the
waitstatement as a non-atomic instruction. - Check that your model satisfies the invariant
Mutexas well as absence of deadlocks - Check that your model ensures progress under
(* PlusCal options (-sf) *).