Formal design of a distributed mutual exclusion protocol
We consider the problem of mutual exclusion in distributed systems which cannot provide an atomic test-and-set instruction. We use model-checking to design an algorithm that solves this problem. We start from a design that we prove incorrect. Then, we use the counter-examples provided by the TLC model-checker to improve the design, until we reach an algorithm that matches the specification.
A first proposal
The solution below is a first attempt to design a distributed mutual exclusion algorithm. To make the problem simpler, we consider only 2 processes (i.e. N=2). We assume that the processes can share memory as this can be implemented in a distributed system.
------------------------------- MODULE mutex1 -------------------------------
EXTENDS Naturals, TLC
CONSTANTS N (* Number of processes MUST BE = 2 *)
(*
--algorithm Mutex1 {
variables flags = [ i \in 1..N |-> FALSE ]; (* flags[i]=TRUE when process
i wants to enter CS *)
process (P \in 1..N)
{
loop: while (TRUE) {
test: await( \A q \in 1..N: flags[q]=FALSE );
set: flags[self] := TRUE;
cs: skip; (* In CS *)
release: flags[self] := FALSE;
}
}
}
*)
\* BEGIN TRANSLATION
\* END TRANSLATION
(* Propositional variables *)
CS(i) == pc[i] = "cs"
(* There is at most one process in the critical section *)
Mutex == (\A p \in 1..N: \A q \in (1..N \{p}): ~(CS(p) /\ CS(q)))
=============================================================================
- Save the algorithm above in file
mutex1.tla, and the configuration below in filemutex1.cfg. Model-check the algorithm with respect to the invariantMutex, as well as absence of deadlock. Which one of the two properties is violated?
SPECIFICATION Spec
CONSTANT N = 2
CHECK_DEADLOCK TRUE
INVARIANT Mutex
- We propose to fix the algorithm by swapping the statements that correspond to labels
testandset. Justify this proposition with respect to the counter-example provided by the model-checker.
A second proposal
The second proposal implements the modification suggested above. In order to enter the critical section, a process first advertises that it requires access to the critical section, then it checks that no other process wants to access the critical section.
------------------------------- MODULE mutex2 -------------------------------
EXTENDS Naturals, TLC
CONSTANTS N (* Number of processes MUST BE = 2 *)
(*
--algorithm Mutex2 {
variables flags = [ i \in 1..N |-> FALSE ]; (* flags[i]=TRUE when process i wants to enter CS *)
process (P \in 1..N)
{
loop: while (TRUE) {
set: flags[self] := TRUE;
test: await( \A q \in (1..N \ {self}): flags[q]=FALSE );
cs: skip; (* In CS *)
release: flags[self] := FALSE;
}
}
}
*)
\* BEGIN TRANSLATION
\* END TRANSLATION
(* Propositional variables *)
CS(i) == pc[i] = "cs"
(* There is at most one process in the critical section *)
Mutex == (\A p \in 1..N: \A q \in (1..N \{p}): ~(CS(p) /\ CS(q)))
=============================================================================
- Save the algorithm above in file
mutex2.tlaand copy the configuration filemutex1.cfgintomutex2,cfg. Model-check the algorithm w.r.t. invariantMutex, as well as for absence of deadlock Which one of the two properties is violated? Explain why. - We propose to fix this algorithm by replacing the blocking instruction
awaitwith a non-blocking test. If another process wants to access the critical section, then we set our flag toFALSEand start again (i.e. we go to labelrelease). Otherwise, we enter the critical section. Using the counter-example returned by the model-checker on algorithmmutex2, explain why this solution should solve the issue.
A third proposal
The third proposal implements the modification suggested above. In order to enter the critical section, a process first advertises that it requires access to the critical section, then it checks that no other process wants to access the critical section. If another process requires access to the critical section, then it resets its flag to FALSE and starts again.
------------------------------- MODULE mutex3 -------------------------------
EXTENDS Naturals, TLC
CONSTANTS N (* Number of processes MUST BE = 2 *)
(*
--algorithm Mutex3 {
variables flags = [ i \in 1..N |-> FALSE ]; (* flags[i]=TRUE when process i wants to enter CS *)
process (P \in 1..N)
{
loop: while (TRUE) {
set: flags[self] := TRUE;
test: if (\E q \in (1..N \ {self}): flags[q] = TRUE)
unset: goto release;
cs: skip; (* In CS *)
release: flags[self] := FALSE;
}
}
}
*)
\* BEGIN TRANSLATION
\* END TRANSLATION
(* Propositional variables *)
CS(i) == pc[i] = "cs"
(* There is at most one process in the critical section *)
Mutex == (\A p \in 1..N: \A q \in (1..N \{p}): ~(CS(p) /\ CS(q)))
=============================================================================
- Save the algorithm above in file
mutex3.tlaand copymutex1.cfgintomutex3.cfg. Model-check the algorithm above invariantMutex, as well as for absence of deadlock. Are all properties true for our model? - Generate the state-space of the algorithm as a graphviz dot file (see tools). Inspect the state-space and the runs, and check if this algorithm is a satisfying solution to the mutual exclusion problem.