art of multiprocessor programmingeventos.cmm.uchile.cl/.../sites/46/2018/01/mutex.pdf ·...
TRANSCRIPT
![Page 1: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../sites/46/2018/01/mutex.pdf · Deadlock-Free • If some thread calls lock() –And never returns –Then other threads must](https://reader035.vdocument.in/reader035/viewer/2022081615/5fdc3190cc5edf623c505297/html5/thumbnails/1.jpg)
Mutual Exclusion
Companion slides for
The Art of Multiprocessor Programming
by Maurice Herlihy & Nir Shavit
![Page 2: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../sites/46/2018/01/mutex.pdf · Deadlock-Free • If some thread calls lock() –And never returns –Then other threads must](https://reader035.vdocument.in/reader035/viewer/2022081615/5fdc3190cc5edf623c505297/html5/thumbnails/2.jpg)
What Is Mutual Exclusion?
![Page 3: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../sites/46/2018/01/mutex.pdf · Deadlock-Free • If some thread calls lock() –And never returns –Then other threads must](https://reader035.vdocument.in/reader035/viewer/2022081615/5fdc3190cc5edf623c505297/html5/thumbnails/3.jpg)
What Is Mutual Exclusion?
![Page 4: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../sites/46/2018/01/mutex.pdf · Deadlock-Free • If some thread calls lock() –And never returns –Then other threads must](https://reader035.vdocument.in/reader035/viewer/2022081615/5fdc3190cc5edf623c505297/html5/thumbnails/4.jpg)
What Is Mutual Exclusion?
![Page 5: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../sites/46/2018/01/mutex.pdf · Deadlock-Free • If some thread calls lock() –And never returns –Then other threads must](https://reader035.vdocument.in/reader035/viewer/2022081615/5fdc3190cc5edf623c505297/html5/thumbnails/5.jpg)
What Is Mutual Exclusion?
![Page 6: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../sites/46/2018/01/mutex.pdf · Deadlock-Free • If some thread calls lock() –And never returns –Then other threads must](https://reader035.vdocument.in/reader035/viewer/2022081615/5fdc3190cc5edf623c505297/html5/thumbnails/6.jpg)
A More Realistic Example
myValue = counter++
myValue ← read(counter) write(counter, myValue+1)
Is this good?
![Page 7: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../sites/46/2018/01/mutex.pdf · Deadlock-Free • If some thread calls lock() –And never returns –Then other threads must](https://reader035.vdocument.in/reader035/viewer/2022081615/5fdc3190cc5edf623c505297/html5/thumbnails/7.jpg)
A More Realistic Example
myValue = counter++
lock() myValue ← read(counter) write(counter, myValue+1) unlock()
Is this good?
![Page 8: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../sites/46/2018/01/mutex.pdf · Deadlock-Free • If some thread calls lock() –And never returns –Then other threads must](https://reader035.vdocument.in/reader035/viewer/2022081615/5fdc3190cc5edf623c505297/html5/thumbnails/8.jpg)
The Critical Section
Bla bla
Bla bla bla
…
…
lock()
…
…
unlock()
…
…
Critical
section
![Page 9: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../sites/46/2018/01/mutex.pdf · Deadlock-Free • If some thread calls lock() –And never returns –Then other threads must](https://reader035.vdocument.in/reader035/viewer/2022081615/5fdc3190cc5edf623c505297/html5/thumbnails/9.jpg)
Requirements
• Mutual exclusion:
• Critical sections may not overlap time
process i is in the
critical section
process j is in the
critical section
![Page 10: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../sites/46/2018/01/mutex.pdf · Deadlock-Free • If some thread calls lock() –And never returns –Then other threads must](https://reader035.vdocument.in/reader035/viewer/2022081615/5fdc3190cc5edf623c505297/html5/thumbnails/10.jpg)
Requirements
• Violation of mutual exclusion:
time
![Page 11: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../sites/46/2018/01/mutex.pdf · Deadlock-Free • If some thread calls lock() –And never returns –Then other threads must](https://reader035.vdocument.in/reader035/viewer/2022081615/5fdc3190cc5edf623c505297/html5/thumbnails/11.jpg)
Liveness
• The trivial solution for mutual exclusion:
![Page 12: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../sites/46/2018/01/mutex.pdf · Deadlock-Free • If some thread calls lock() –And never returns –Then other threads must](https://reader035.vdocument.in/reader035/viewer/2022081615/5fdc3190cc5edf623c505297/html5/thumbnails/12.jpg)
Deadlock-Free
• If some thread calls lock()
– And never returns
– Then other threads must complete lock()
and unlock() calls infinitely often
• System as a whole makes progress
– Even if individuals starve
![Page 13: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../sites/46/2018/01/mutex.pdf · Deadlock-Free • If some thread calls lock() –And never returns –Then other threads must](https://reader035.vdocument.in/reader035/viewer/2022081615/5fdc3190cc5edf623c505297/html5/thumbnails/13.jpg)
Starvation-Free
• If some thread calls lock()
– It will eventually return
• Individual threads make progress
![Page 14: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../sites/46/2018/01/mutex.pdf · Deadlock-Free • If some thread calls lock() –And never returns –Then other threads must](https://reader035.vdocument.in/reader035/viewer/2022081615/5fdc3190cc5edf623c505297/html5/thumbnails/14.jpg)
2-PROCESS MUTUAL
EXCLUSION
![Page 15: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../sites/46/2018/01/mutex.pdf · Deadlock-Free • If some thread calls lock() –And never returns –Then other threads must](https://reader035.vdocument.in/reader035/viewer/2022081615/5fdc3190cc5edf623c505297/html5/thumbnails/15.jpg)
Attempt 1
process 0 process 0
Lock() code for process i:
flag[i] = 1;
while(flag[1-i] == 1) ; // wait
flag[2]
![Page 16: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../sites/46/2018/01/mutex.pdf · Deadlock-Free • If some thread calls lock() –And never returns –Then other threads must](https://reader035.vdocument.in/reader035/viewer/2022081615/5fdc3190cc5edf623c505297/html5/thumbnails/16.jpg)
Attempt 1
process 0 process 0
Lock() code for process i:
flag[i] = 1;
while(flag[1-i] == 1) ; // wait
flag[2]
![Page 17: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../sites/46/2018/01/mutex.pdf · Deadlock-Free • If some thread calls lock() –And never returns –Then other threads must](https://reader035.vdocument.in/reader035/viewer/2022081615/5fdc3190cc5edf623c505297/html5/thumbnails/17.jpg)
Attempt 1
process 0 process 0
Lock() code for process i:
flag[i] = 1;
while(flag[1-i] == 1) ; // wait
flag[2]
![Page 18: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../sites/46/2018/01/mutex.pdf · Deadlock-Free • If some thread calls lock() –And never returns –Then other threads must](https://reader035.vdocument.in/reader035/viewer/2022081615/5fdc3190cc5edf623c505297/html5/thumbnails/18.jpg)
Attempt 1
process 0 process 0
Lock() code for process i:
flag[i] = 1;
while(flag[1-i] == 1) ; // wait
Unlock() code for process i:
flag[i] = 0;
flag[2]
![Page 19: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../sites/46/2018/01/mutex.pdf · Deadlock-Free • If some thread calls lock() –And never returns –Then other threads must](https://reader035.vdocument.in/reader035/viewer/2022081615/5fdc3190cc5edf623c505297/html5/thumbnails/19.jpg)
Attempt 1: Mutual Exclusion?
• Yes
• Suppose both are in the critical section…
Lock() code for process i:
flag[i] = 1;
while(flag[1-i] == 1) ; // wait
Unlock() code for process i:
flag[i] = 0;
![Page 20: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../sites/46/2018/01/mutex.pdf · Deadlock-Free • If some thread calls lock() –And never returns –Then other threads must](https://reader035.vdocument.in/reader035/viewer/2022081615/5fdc3190cc5edf623c505297/html5/thumbnails/20.jpg)
Attempt 1: Deadlock Freedom?
• Nope
Lock() code for process i:
flag[i] = 1;
while(flag[1-i] == 1) ; // wait
Unlock() code for process i:
flag[i] = 0;
![Page 21: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../sites/46/2018/01/mutex.pdf · Deadlock-Free • If some thread calls lock() –And never returns –Then other threads must](https://reader035.vdocument.in/reader035/viewer/2022081615/5fdc3190cc5edf623c505297/html5/thumbnails/21.jpg)
Deadlock:
process 0 process 0
Lock() code for process i:
flag[i] = 1;
while(flag[1-i] == 1) ; // wait
flag[2]
![Page 22: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../sites/46/2018/01/mutex.pdf · Deadlock-Free • If some thread calls lock() –And never returns –Then other threads must](https://reader035.vdocument.in/reader035/viewer/2022081615/5fdc3190cc5edf623c505297/html5/thumbnails/22.jpg)
Deadlock:
process 0 process 0
Lock() code for process i:
flag[i] = 1;
while(flag[1-i] == 1) ; // wait
flag[2]
But sequential
executions
are OK!
![Page 23: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../sites/46/2018/01/mutex.pdf · Deadlock-Free • If some thread calls lock() –And never returns –Then other threads must](https://reader035.vdocument.in/reader035/viewer/2022081615/5fdc3190cc5edf623c505297/html5/thumbnails/23.jpg)
Attempt 2
code for process i:
int victim;
lock()
{
victim = i;
while (victim == i) {} // wait
}
unlock() {}
![Page 24: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../sites/46/2018/01/mutex.pdf · Deadlock-Free • If some thread calls lock() –And never returns –Then other threads must](https://reader035.vdocument.in/reader035/viewer/2022081615/5fdc3190cc5edf623c505297/html5/thumbnails/24.jpg)
Attempt 2
code for process i:
int victim;
lock()
{
victim = i;
while (victim == i) {} // wait
}
unlock() {}
![Page 25: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../sites/46/2018/01/mutex.pdf · Deadlock-Free • If some thread calls lock() –And never returns –Then other threads must](https://reader035.vdocument.in/reader035/viewer/2022081615/5fdc3190cc5edf623c505297/html5/thumbnails/25.jpg)
lock() {
victim = i;
while (victim == i) {};
}
Attempt 2
• Satisfies mutual exclusion
– If thread i in CS
– Then victim == j
– Cannot be both 0 and 1
• Not deadlock free
– Sequential execution deadlocks
– Concurrent execution does not
![Page 26: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../sites/46/2018/01/mutex.pdf · Deadlock-Free • If some thread calls lock() –And never returns –Then other threads must](https://reader035.vdocument.in/reader035/viewer/2022081615/5fdc3190cc5edf623c505297/html5/thumbnails/26.jpg)
Peterson's Algorithm
lock()
{
flag[i] = true;
victim = i;
while (flag[j] && victim == i) {};
}
unlock()
flag[i] = false;
}
![Page 27: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../sites/46/2018/01/mutex.pdf · Deadlock-Free • If some thread calls lock() –And never returns –Then other threads must](https://reader035.vdocument.in/reader035/viewer/2022081615/5fdc3190cc5edf623c505297/html5/thumbnails/27.jpg)
N-PROCESS MUTEX:
LAMPORT’S BAKERY ALGORITHM
![Page 28: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../sites/46/2018/01/mutex.pdf · Deadlock-Free • If some thread calls lock() –And never returns –Then other threads must](https://reader035.vdocument.in/reader035/viewer/2022081615/5fdc3190cc5edf623c505297/html5/thumbnails/28.jpg)
Lamport’s Bakery Algorithm
A queue in Israel…
![Page 29: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../sites/46/2018/01/mutex.pdf · Deadlock-Free • If some thread calls lock() –And never returns –Then other threads must](https://reader035.vdocument.in/reader035/viewer/2022081615/5fdc3190cc5edf623c505297/html5/thumbnails/29.jpg)
Lamport’s Bakery Algorithm
A queue in California
![Page 30: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../sites/46/2018/01/mutex.pdf · Deadlock-Free • If some thread calls lock() –And never returns –Then other threads must](https://reader035.vdocument.in/reader035/viewer/2022081615/5fdc3190cc5edf623c505297/html5/thumbnails/30.jpg)
Bakery Algorithm
• Lock():
1. “Take a number”
2. Wait until lower numbers are done
• Lexicographic order
– (a,i) > (b,j) • If a > b, or a = b and i > j
![Page 31: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../sites/46/2018/01/mutex.pdf · Deadlock-Free • If some thread calls lock() –And never returns –Then other threads must](https://reader035.vdocument.in/reader035/viewer/2022081615/5fdc3190cc5edf623c505297/html5/thumbnails/31.jpg)
Bakery Algorithm
flag[N]
number[N]
![Page 32: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../sites/46/2018/01/mutex.pdf · Deadlock-Free • If some thread calls lock() –And never returns –Then other threads must](https://reader035.vdocument.in/reader035/viewer/2022081615/5fdc3190cc5edf623c505297/html5/thumbnails/32.jpg)
Bakery Algorithm
flag[N]
number[N]
max_num = max { number[1],…,number[N] };
number[i] = max_num;
![Page 33: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../sites/46/2018/01/mutex.pdf · Deadlock-Free • If some thread calls lock() –And never returns –Then other threads must](https://reader035.vdocument.in/reader035/viewer/2022081615/5fdc3190cc5edf623c505297/html5/thumbnails/33.jpg)
Bakery Algorithm
flag[N]
number[N]
wait while ∃k: flag[k] == 1
and
(number[k],k) < (number[i],i)
![Page 34: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../sites/46/2018/01/mutex.pdf · Deadlock-Free • If some thread calls lock() –And never returns –Then other threads must](https://reader035.vdocument.in/reader035/viewer/2022081615/5fdc3190cc5edf623c505297/html5/thumbnails/34.jpg)
Deep Philosophical Question
• The Bakery Algorithm is
– Succinct,
– Elegant, and
– Fair.
• Q: So why isn't it practical?
• A: N distinct variables!
![Page 35: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../sites/46/2018/01/mutex.pdf · Deadlock-Free • If some thread calls lock() –And never returns –Then other threads must](https://reader035.vdocument.in/reader035/viewer/2022081615/5fdc3190cc5edf623c505297/html5/thumbnails/35.jpg)
Theorem
At least N registers are needed to
solve deadlock-free mutual exclusion.
(No matter how big they are!)
![Page 36: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../sites/46/2018/01/mutex.pdf · Deadlock-Free • If some thread calls lock() –And never returns –Then other threads must](https://reader035.vdocument.in/reader035/viewer/2022081615/5fdc3190cc5edf623c505297/html5/thumbnails/36.jpg)
Theorem (For 2 Threads)
Theorem: Deadlock-free mutual
exclusion for 2 threads requires at least
2 multi-reader multi-writer registers
Proof: assume one register suffices
and derive a contradiction
![Page 37: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../sites/46/2018/01/mutex.pdf · Deadlock-Free • If some thread calls lock() –And never returns –Then other threads must](https://reader035.vdocument.in/reader035/viewer/2022081615/5fdc3190cc5edf623c505297/html5/thumbnails/37.jpg)
Proof (2 Threads)
B
A
R
CS
Claim: A must write to R Suppose not…
CS
same!
![Page 38: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../sites/46/2018/01/mutex.pdf · Deadlock-Free • If some thread calls lock() –And never returns –Then other threads must](https://reader035.vdocument.in/reader035/viewer/2022081615/5fdc3190cc5edf623c505297/html5/thumbnails/38.jpg)
Proof (2 Threads)
B
A
R
CS
CS
CS
![Page 39: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../sites/46/2018/01/mutex.pdf · Deadlock-Free • If some thread calls lock() –And never returns –Then other threads must](https://reader035.vdocument.in/reader035/viewer/2022081615/5fdc3190cc5edf623c505297/html5/thumbnails/39.jpg)
Theorem
Deadlock-free mutual exclusion for 3
threads requires at least 3 registers
![Page 40: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../sites/46/2018/01/mutex.pdf · Deadlock-Free • If some thread calls lock() –And never returns –Then other threads must](https://reader035.vdocument.in/reader035/viewer/2022081615/5fdc3190cc5edf623c505297/html5/thumbnails/40.jpg)
Proof: Assume Cover of 2
Write(RB)
B
Write(RC)
C A
Only 2 registers
Registers look as if CS empty
![Page 41: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../sites/46/2018/01/mutex.pdf · Deadlock-Free • If some thread calls lock() –And never returns –Then other threads must](https://reader035.vdocument.in/reader035/viewer/2022081615/5fdc3190cc5edf623c505297/html5/thumbnails/41.jpg)
Run A Solo
Write(RB)
B
Write(RC)
C A
Writes to one or both
registers, enters CS CS
![Page 42: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../sites/46/2018/01/mutex.pdf · Deadlock-Free • If some thread calls lock() –And never returns –Then other threads must](https://reader035.vdocument.in/reader035/viewer/2022081615/5fdc3190cc5edf623c505297/html5/thumbnails/42.jpg)
Obliterate Traces of A
Write(RB)
B
Write(RC)
C A
Other threads obliterate evidence
that A entered CS CS
![Page 43: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../sites/46/2018/01/mutex.pdf · Deadlock-Free • If some thread calls lock() –And never returns –Then other threads must](https://reader035.vdocument.in/reader035/viewer/2022081615/5fdc3190cc5edf623c505297/html5/thumbnails/43.jpg)
Mutual Exclusion Fails
Write(RB)
B
Write(RC)
C A
CS CS
CS looks empty, so
another thread gets
in
![Page 44: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../sites/46/2018/01/mutex.pdf · Deadlock-Free • If some thread calls lock() –And never returns –Then other threads must](https://reader035.vdocument.in/reader035/viewer/2022081615/5fdc3190cc5edf623c505297/html5/thumbnails/44.jpg)
Proof Strategy
• Proved: a contradiction starting from a
covering state for 2 registers
• Claim: can reach a covering state
![Page 45: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../sites/46/2018/01/mutex.pdf · Deadlock-Free • If some thread calls lock() –And never returns –Then other threads must](https://reader035.vdocument.in/reader035/viewer/2022081615/5fdc3190cc5edf623c505297/html5/thumbnails/45.jpg)
• If we run B through CS 3 times, first write of B
must twice be to some register, say RB
Covering State for Two
Write(RB)
B
Write(RA)
A
![Page 46: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../sites/46/2018/01/mutex.pdf · Deadlock-Free • If some thread calls lock() –And never returns –Then other threads must](https://reader035.vdocument.in/reader035/viewer/2022081615/5fdc3190cc5edf623c505297/html5/thumbnails/46.jpg)
Covering State for Two
• Start with B covering register RB for the 1st time • Run A until it is about to write to uncovered RA
• Are we done?
Write(RB)
B
Write(RA)
A
![Page 47: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../sites/46/2018/01/mutex.pdf · Deadlock-Free • If some thread calls lock() –And never returns –Then other threads must](https://reader035.vdocument.in/reader035/viewer/2022081615/5fdc3190cc5edf623c505297/html5/thumbnails/47.jpg)
Covering State for Two
• NO! A could have written to RB
• So CS no longer looks empty to thread C
Write(RB)
B
Write(RA)
A
![Page 48: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../sites/46/2018/01/mutex.pdf · Deadlock-Free • If some thread calls lock() –And never returns –Then other threads must](https://reader035.vdocument.in/reader035/viewer/2022081615/5fdc3190cc5edf623c505297/html5/thumbnails/48.jpg)
Covering State for Two
• Run B obliterating traces of A in RB
• Run B again until it is about to write to RB
• Now we are done
Write(RB)
B
Write(RA)
A
![Page 49: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../sites/46/2018/01/mutex.pdf · Deadlock-Free • If some thread calls lock() –And never returns –Then other threads must](https://reader035.vdocument.in/reader035/viewer/2022081615/5fdc3190cc5edf623c505297/html5/thumbnails/49.jpg)
Inductively We Can Show
• There is a covering state
– Where k threads not in CS cover k distinct registers
– Proof follows when k = N-1
Write(RB)
B
Write(RC)
C
Write(RA)
A
![Page 50: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../sites/46/2018/01/mutex.pdf · Deadlock-Free • If some thread calls lock() –And never returns –Then other threads must](https://reader035.vdocument.in/reader035/viewer/2022081615/5fdc3190cc5edf623c505297/html5/thumbnails/50.jpg)
Summary of Lecture
• In the 1960's several incorrect solutions
to mutual exclusion using registers were
published…
• We saw how to solve N thread mutual
exclusion using 2N R/W Registers
• … but N R/W registers are needed
![Page 51: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../sites/46/2018/01/mutex.pdf · Deadlock-Free • If some thread calls lock() –And never returns –Then other threads must](https://reader035.vdocument.in/reader035/viewer/2022081615/5fdc3190cc5edf623c505297/html5/thumbnails/51.jpg)
“Cheating” Using Test&Set…
• test&set(x):
if x == 1 return 0
if x == 0 set x = 1 and return 1