// ----------------------------------------------------------------- // peterson.txt: Peterson's mutual exclusion algorithm // Concrete Abstractions: Concurrent Systems // (c) 2020, Wolfgang Schreiner, RISC, JKU Linz, Austria // ----------------------------------------------------------------- type Proc2 = ℕ[1]; type PC2 = ℕ[4]; shared system Peterson { // the program counter of each process var pc: Array[2,PC2] = Array[2,PC2](0); // the flag indicating that the process wants to enter the critical region var flag: Array[2,Bool] = Array[2,Bool](⊥); // the identity of the process whose turn it is to enter the critical region var turn: Proc2; // the mutual exclusion property (to prove this, invariant 0 is not needed) invariant ¬(pc[0] = 4 ∧ pc[1] = 4); // the system invariants invariant ∀i:Proc2 with pc[i] = 0. ¬flag[i]; invariant ∀i:Proc2 with pc[i] = 1. flag[i]; invariant ∀i:Proc2 with pc[i] = 2. flag[i]; invariant ∀i:Proc2 with pc[i] = 3. flag[i]; invariant ∀i:Proc2 with pc[i] = 4. flag[i] ∧ (¬flag[1-i] ∨ turn ≠ 1-i ∨ pc[1-i]=1); // the violation of this property indicates progress // invariant pc[0] ≠ 4 ∧ pc[1] ≠ 4; init (t:Proc2) { turn ≔ t; } action flag(i:Proc2) with pc[i] = 0; { flag[i] ≔ ⊤; pc[i] ≔ 1; } action turn(i:Proc2) with pc[i] = 1; { turn ≔ 1-i; pc[i] ≔ 2; } action enter2(i:Proc2) with pc[i] = 2 ∧ ¬flag[1-i]; { pc[i] ≔ 4; } action loop2(i:Proc2) with pc[i] = 2 ∧ flag[1-i]; { pc[i] ≔ 3; } action enter3(i:Proc2) with pc[i] = 3 ∧ turn = i; { pc[i] ≔ 4; } action loop3(i:Proc2) with pc[i] = 3 ∧ turn ≠ i; { pc[i] ≔ 2; } action exit(i:Proc2) with pc[i] = 4; { flag[i] ≔ ⊥; pc[i] ≔ 0; } } val N:ℕ; axiom minTwo ⇔ N ≥ 2; type ProcN = ℕ[N-1]; type PCN = ℕ[6]; shared system PetersonN { // the program counter of each process var pc: Array[N,PCN] = Array[N,PCN](0); // room -1..N-1 of each process var room: Array[N,ℤ[-1,N-1]] = Array[N,ℤ[-1,N-1]](-1); // process that was the last to enter each room 0..N-1 var last: Array[N-1,ProcN] = Array[N-1,ProcN](0); // auxiliary variable for existence check loop var k: Array[N,ℕ[N]] ≔ Array[N,ℕ[N]](0); // mutual exclusion property invariant ¬∃i1:ProcN,i2:ProcN with i1 ≠ i2. pc[i1] = 6 ∧ pc[i2] = 6; // core invariant that implies mutual exclusion invariant ∀r:ℕ[N-1]. (#i:ProcN with room[i]=r) ≤ N-r; // auxiliary room invariants invariant ∀i:ProcN with pc[i] = 0. room[i] = -1; invariant ∀i:ProcN with pc[i] = 1. room[i] ≥ 0; invariant ∀i:ProcN with pc[i] = 2. 0 ≤ room[i] ∧ room[i] < N-1; invariant ∀i:ProcN with pc[i] = 3. 0 ≤ room[i] ∧ room[i] < N-1; invariant ∀i:ProcN with pc[i] = 4. 0 ≤ room[i] ∧ room[i] < N-1; invariant ∀i:ProcN with pc[i] = 5. 0 ≤ room[i] ∧ room[i] < N-1; invariant ∀i:ProcN with pc[i] = 6. room[i] = N-1; // invariants needed to show that core invariant is preserved by action "room" invariant ∀r:ℕ[N-2] with room[last[r]] ≠ r. (∀i0:ProcN with i0 ≠ last[r] ∧ (pc[i0] = 1 ∨ pc[i0] = 2). room[i0] ≤ r) ∧ (∀i0:ProcN with i0 ≠ last[r] ∧ pc[i0] ≥ 3. room[i0] < r); invariant ∀i:ProcN with pc[i] = 1 ∧ room[i] < N-1 ∧ last[room[i]] = i. (∀i0:ProcN with pc[i0] = 1 ∨ pc[i0] = 2. room[i0] ≤ room[i]) ∧ (∀i0:ProcN with pc[i0] ≥ 3. room[i0] < room[i]); invariant ∀i:ProcN with pc[i] = 4 ∧ i = last[room[i]]. (∀i0:ProcN with i0 ≠ i ∧ (pc[i0] = 1 ∨ pc[i0] = 2) ∧ i0 < k[i]. room[i0] ≤ room[i]) ∧ (∀i0:ProcN with i0 ≠ i ∧ pc[i0] ≥ 3 ∧ i0 < k[i]. room[i0] < room[i]); invariant ∀i:ProcN with pc[i] = 5 ∧ i = last[room[i]]. (∀i0:ProcN with i0 ≠ i ∧ (pc[i0] = 1 ∨ pc[i0] = 2). room[i0] ≤ room[i]) ∧ (∀i0:ProcN with i0 ≠ i ∧ pc[i0] ≥ 3. room[i0] < room[i]); // the violation of this property indicates progress // invariant ∀i:ProcN. pc[i] ≠ 6; // mutual exclusion ltl □ ¬∃i1:ProcN,i2:ProcN with i1 ≠ i2. 〚 pc[i1] = 6 ∧ pc[i2] = 6 〛; // from pc 0, we can only progress to pc 1 ltl ∀i:ProcN. □ ( 〚 pc[i] = 0 〛 ⇒ ( 〚 pc[i] = 0 〛 WeakUntil 〚 pc[i] = 1 〛 ) ); // WRONG: from pc 1, we can only progress to pc 2 ltl ∀i:ProcN. □ ( 〚 pc[i] = 1 〛 ⇒ ( 〚 pc[i] = 1 〛 WeakUntil 〚 pc[i] = 2 〛 ) ); // progress ltl[fairness] ∀i:ProcN. □( 〚 pc[i] = 2 〛 ⇒ ◇ 〚 pc[i] = 6 〛 ); action start(i:ProcN) with pc[i] = 0; fairness weak_all; { room[i] ≔ 0; pc[i] ≔ 1; } action enter(i:ProcN) with pc[i] = 1 ∧ room[i] = N-1; fairness strong_all; { pc[i] ≔ 6; } action loop(i:ProcN) with pc[i] = 1 ∧ room[i] < N-1; fairness strong_all; { pc[i] ≔ 2; } action last(i:ProcN) with pc[i] = 2; fairness weak_all; { last[room[i]] ≔ i; pc[i] ≔ 3; } action endwait3(i:ProcN) with pc[i] = 3 ∧ last[room[i]] ≠ i; fairness strong_all; { pc[i] ≔ 5; } action wait3(i:ProcN) with pc[i] = 3 ∧ last[room[i]] = i; fairness strong_all; { k[i] ≔ 0; pc[i] ≔ 4; } action endwait4(i:ProcN) with pc[i] = 4 ∧ k[i] = N; fairness strong_all; { pc[i] ≔ 5; } action wait4(i:ProcN) with pc[i] = 4 ∧ k[i] < N ∧ (k[i] ≠ i ∧ room[k[i]] ≥ room[i]); fairness strong_all; { pc[i] ≔ 3; } action next(i:ProcN) with pc[i] = 4 ∧ k[i] < N ∧ (k[i] = i ∨ room[k[i]] < room[i]); fairness strong_all; { k[i] ≔ k[i]+1; } action room(i:ProcN) with pc[i] = 5; fairness weak_all; { room[i] ≔ room[i]+1; pc[i] ≔ 1; } action exit(i:ProcN) with pc[i] = 6; fairness weak_all; { room[i] ≔ -1; pc[i] ≔ 0; } } // ------------------------------------------------------------------ // end of file // ------------------------------------------------------------------