Peterson’s Algorithm Bakery Algorithm Fast Algorithm Szymanski’s Algorithm Some More Critical Section Solutions Dr. Liam O’Connor University of Edinburgh LFCS (and UNSW) Term 2 2020 1
Peterson’s Algorithm Bakery Algorithm Fast Algorithm Szymanski’s Algorithm Where we are at In the last few lectures we discussed the critical section problem, the four properties of critical section solutions, and some solutions for two processes. In this lecture, we will introduce some more modern algorithms for this problem, and compare their properties. 2
Peterson’s Algorithm Bakery Algorithm Fast Algorithm Szymanski’s Algorithm From 2 to n Processes In the 5th attempt of lecture 2 (a.k.a. Dekker’s Algorithm) we used an extra shared variable turn to remember whose turn it would be next to enter its CS when there’s contention. This turns out to be simple for 2 processes but complex for n . 3
Peterson’s Algorithm Bakery Algorithm Fast Algorithm Szymanski’s Algorithm Tie-Breaker (Peterson’s) Algorithm for 2 Processes Algorithm 1.1: Peterson’s algorithm boolean wantp ← false, wantq ← false integer last ← 1 p q forever do forever do non-critical section non-critical section p1: q1: wantp ← true wantq ← true p2: q2: last ← 1 last ← 2 p3: q3: await wantq = false or await wantp = false or p4: q4: last = 2 last = 1 critical section critical section p5: q5: wantp ← false wantq ← false p6: q6: 4
Peterson’s Algorithm Bakery Algorithm Fast Algorithm Szymanski’s Algorithm Tie-Breaker Code for n Processes Algorithm 1.2: Peterson’s algorithm ( n processes, process i ) integer array in[1..n] ← [0,. . . ,0] integer array last[1..n] ← [0,. . . ,0] loop forever non-critical section p1: for all processes j in[i] ← j p2: last[j] ← i p3: for all processes k � = i await in[k] < j or last[j] � = i p4: critical section p5: in[i] ← 0 p6: Liam: n waiting rooms analogy 5
Peterson’s Algorithm Bakery Algorithm Fast Algorithm Szymanski’s Algorithm Properties of the Tie-Breaker Algorithm Do we satisfy: Eventual entry? Linear waiting ? Linear Waiting Linear waiting is the property that says the number of times a process is “overtaken” ( bypassed ) in the preprotocol is bounded by n (the number of processes). Fiddliness! According “Some Myths about Famous Mutual Exclusion Algorithms” by Alagarsamy (2003), the n -process variant does not ensure linear waiting or even eventual entry. However Promela disagrees (assuming weak fairness). For Brownie Points: Figure out why! 6
Peterson’s Algorithm Bakery Algorithm Fast Algorithm Szymanski’s Algorithm Algorithm 1.3: Simplified bakery algorithm (two processes) integer np ← 0, nq ← 0 p q forever do forever do non-critical section non-critical section p1: q1: np ← nq + 1 nq ← np + 1 p2: q2: await nq = 0 or np ≤ nq await np = 0 or nq < np p3: q3: critical section critical section p4: q4: np ← 0 nq ← 0 p5: q5: Note the asymmetry here! Why do we need it? What if we don’t have atomicity for each statement? 7
Peterson’s Algorithm Bakery Algorithm Fast Algorithm Szymanski’s Algorithm Mutual Exclusion The following are invariants np = 0 ≡ p 1 .. 2 (1) nq = 0 ≡ q 1 .. 2 (2) p 4 ⇒ nq = 0 ∨ np ≤ nq (3) q 4 ⇒ np = 0 ∨ nq < np (4) and hence also ¬ ( p 4 ∧ q 4). 8
Peterson’s Algorithm Bakery Algorithm Fast Algorithm Szymanski’s Algorithm Other Safety Properties Deadlock freedom: The disjunction nq = 0 ∨ np ≤ nq ∨ np = 0 ∨ nq < np of the conditions on the await statements at p3/q3 is equivalent to ⊤ . Hence it is not possible for both processes to be blocked there. Absence of unnecessary delay: Even if one process prefers to stay in its non-critical section, no deadlock will occur by the first two invariants (1) and (2). 9
Peterson’s Algorithm Bakery Algorithm Fast Algorithm Szymanski’s Algorithm Eventual Entry For p to fail to reach its CS despite wanting to, it needs to be stuck at p3 where it will evaluate the condition infinitely often by weak fairness. To remain stuck, each of these evaluations must yield false. In LTL: �� ¬ ( nq = 0 ∨ np ≤ nq ) which implies �� nq � = 0 , and (5) �� nq < np . (6) Because there is no deadlock, (5) implies that process q goes through infinitely many iterations of the main loop without getting lost in the non-critical section. But then it must set nq to the constant np + 1. From then onwards it is no longer possible to fail the test ( nq = 0 ∨ np ≤ nq ), contradiction. 10
Peterson’s Algorithm Bakery Algorithm Fast Algorithm Szymanski’s Algorithm 2 → n Algorithm 1.4: Simplified bakery algorithm ( N processes) integer array[1..n] number ← [0,. . . ,0] loop forever non-critical section p1: number[i] ← max(number) + 1 p2: for all other processes j p3: await (number[j] = 0) or (number[i] ≪ number[j]) p4: critical section p5: number[i] ← 0 p6: once again relying on atomicity of non-LCR lines of Ben-Ari pseudo-code; ≪ breaks ties using PIDs. 11
Peterson’s Algorithm Bakery Algorithm Fast Algorithm Szymanski’s Algorithm An Implementable Algorithm Algorithm 1.5: Lamport’s bakery algorithm boolean array[1..n] choosing ← [false,. . . ,false] integer array[1..n] number ← [0,. . . ,0] forever do non-critical section p1: choosing[i] ← true p2: number[i] ← 1 + max(number) p3: choosing[i] ← false p4: for all other processes j p5: await choosing[j] = false p6: await (number[j] = 0) or (number[i] ≪ number[j]) p7: critical section p8: number[i] ← 0 p9: 12
Peterson’s Algorithm Bakery Algorithm Fast Algorithm Szymanski’s Algorithm Properties of Lamport’s bakery algorithm “The algorithm has the remarkable property that if a read and a write operation to a single memory location occur simultaneously, then only the write operation must be performed correctly. The read may return any arbitrary value!” Lamport, 1974 (CACM) Cons: O ( n ) pre-protocol; unbounded ticket numbers Assertion 1: If p k 1 .. 2 ∧ p i 5 .. 9 and k then reaches p 5 .. 9 while i is still there, then number[ i ] < number[ k ] Assertion 2: p i 8 .. 9 ∧ p k 5 .. 9 ∧ i � = k ⇒ (number[ i ] , i ) ≪ (number[ k ] , k ) 13
Peterson’s Algorithm Bakery Algorithm Fast Algorithm Szymanski’s Algorithm When contention is low. . . access to the CS should be fast, that is, consist of a fixed number of steps (aka O (1)). 14
Peterson’s Algorithm Bakery Algorithm Fast Algorithm Szymanski’s Algorithm Almost correct fast solution Algorithm 1.6: Fast algorithm for two processes (outline) integer gate1 ← 0, gate2 ← 0 p q forever do forever do non-critical section non-critical section gate1 ← p gate1 ← q p1: q1: if gate2 � = 0 goto p1 if gate2 � = 0 goto q1 p2: q2: gate2 ← p gate2 ← q p3: q3: if gate1 � = p if gate1 � = q p4: q4: if gate2 � = p goto p1 if gate2 � = q goto q1 p5: q5: critical section critical section gate2 ← 0 gate2 ← 0 p6: q6: 15
Peterson’s Algorithm Bakery Algorithm Fast Algorithm Szymanski’s Algorithm Invariants p 5 ∧ gate2 = p ⇒ ¬ ( q 3 ∨ q 4 ∨ q 6) (7) q 5 ∧ gate2 = q ⇒ ¬ ( p 3 ∨ p 4 ∨ p 6) (8) p 4 ∧ gate1 = p ⇒ gate2 � = 0 (9) p 6 ⇒ gate2 � = 0 ∧ ¬ q 6 ∧ ( q 3 ∨ q 4 ⇒ gate1 � = q ) (10) q 4 ∧ gate1 = q ⇒ gate2 � = 0 (11) q 6 ⇒ gate2 � = 0 ∧ ¬ p 6 ∧ ( p 3 ∨ p 4 ⇒ gate1 � = p ) (12) Mutual exclusion follows from invariants (10) and (12). Problem : (7) and (8) aren’t actually invariants of this algorithm. 16
Peterson’s Algorithm Bakery Algorithm Fast Algorithm Szymanski’s Algorithm Algorithm 1.7: Fast algorithm for two processes integer gate1 ← 0, gate2 ← 0 boolean wantp ← false, wantq ← false p q gate1 ← p gate1 ← q p1: q1: wantp ← true wantq ← true if gate2 � = 0 if gate2 � = 0 p2: q2: wantp ← false wantq ← false goto p1 goto q1 gate2 ← p gate2 ← q p3: q3: if gate1 � = p if gate1 � = q p4: q4: wantp ← false wantq ← false await wantq = false await wantp = false if gate2 � = p goto p1 if gate2 � = q goto q1 p5: q5: else wantp ← true else wantq ← true critical section critical section gate2 ← 0 gate2 ← 0 p6: q6: wantp ← false wantq ← false 17
Peterson’s Algorithm Bakery Algorithm Fast Algorithm Szymanski’s Algorithm Mutex review None of the mutual exclusion algorithms presented so far scores full marks. Selected problems: don’t scale well beyond 2 processes (Dekker) have a O ( n 2 ) pre-protocol (Peterson) rely on special instruction (e.g. xc, ts, etc.) use unbounded ticket numbers (e.g. bakery) sacrifice eventual entry (e.g. fast) 18
Peterson’s Algorithm Bakery Algorithm Fast Algorithm Szymanski’s Algorithm Szymanski’s Algorithm has none of these problems, enforces linear wait , requires at most 4 p − ⌈ p n ⌉ writes for p CS entries by n competing processes, and can be made immune to process failures and restarts as well as read errors occurring during writes. How does he do it? 19
Recommend
More recommend