Mutual&Exclusion& - Washington University in St. Louis · 2018. 1. 17. · Mutual Exclusion In his...
Transcript of Mutual&Exclusion& - Washington University in St. Louis · 2018. 1. 17. · Mutual Exclusion In his...
![Page 1: Mutual&Exclusion& - Washington University in St. Louis · 2018. 1. 17. · Mutual Exclusion In his 1965 paper "Solution of a problem in concurrent programming control," E. W. Dijkstra](https://reader036.fdocuments.net/reader036/viewer/2022081323/60006422b2acdf2212368361/html5/thumbnails/1.jpg)
Mutual Exclusion
Acknowledgement: Slides adopted from the companion slides for the book "The Art of Mul>processor Programming" by Maurice Herlihy and Nir Shavit
![Page 2: Mutual&Exclusion& - Washington University in St. Louis · 2018. 1. 17. · Mutual Exclusion In his 1965 paper "Solution of a problem in concurrent programming control," E. W. Dijkstra](https://reader036.fdocuments.net/reader036/viewer/2022081323/60006422b2acdf2212368361/html5/thumbnails/2.jpg)
What We'll Cover Today Chapter 2 of:
Digital copy can be obtained via WUSTL library: hKp://catalog.wustl.edu/search/
![Page 3: Mutual&Exclusion& - Washington University in St. Louis · 2018. 1. 17. · Mutual Exclusion In his 1965 paper "Solution of a problem in concurrent programming control," E. W. Dijkstra](https://reader036.fdocuments.net/reader036/viewer/2022081323/60006422b2acdf2212368361/html5/thumbnails/3.jpg)
Mutual Exclusion
• Cri$cal sec$on: a block of code that access shared modifiable data or resource that should be operated on by only one thread at a >me.
• Mutual exclusion: a property that ensures that a cri>cal sec>on is only executed by a thread at a >me.
![Page 4: Mutual&Exclusion& - Washington University in St. Louis · 2018. 1. 17. · Mutual Exclusion In his 1965 paper "Solution of a problem in concurrent programming control," E. W. Dijkstra](https://reader036.fdocuments.net/reader036/viewer/2022081323/60006422b2acdf2212368361/html5/thumbnails/4.jpg)
Mutual Exclusion
In his 1965 paper "Solution of a problem in concurrent programming control," E. W. Dijkstra wrote:
"Given in this paper is a solution to a problem which, to the knowledge of the author, has been an open question since at least 1962, irrespective of the solvability. [...] Although the setting of the problem might seem somewhat academic at first, the author trusts that anyone familiar with the logical problems that arise in computer coupling will appreciate the significance of the fact that this problem indeed can be solved.” [1]
![Page 5: Mutual&Exclusion& - Washington University in St. Louis · 2018. 1. 17. · Mutual Exclusion In his 1965 paper "Solution of a problem in concurrent programming control," E. W. Dijkstra](https://reader036.fdocuments.net/reader036/viewer/2022081323/60006422b2acdf2212368361/html5/thumbnails/5.jpg)
Mutual Exclusion
• Formal problem definitions • Solutions for 2 threads • Solutions for n threads • Fair solutions • Inherent costs
![Page 6: Mutual&Exclusion& - Washington University in St. Louis · 2018. 1. 17. · Mutual Exclusion In his 1965 paper "Solution of a problem in concurrent programming control," E. W. Dijkstra](https://reader036.fdocuments.net/reader036/viewer/2022081323/60006422b2acdf2212368361/html5/thumbnails/6.jpg)
Warning
• You will never use these protocols – Get over it
• You are advised to understand them – The same issues show up everywhere – Except hidden and more complex
But first, we need some terminology to describe >me and concurrency …
![Page 7: Mutual&Exclusion& - Washington University in St. Louis · 2018. 1. 17. · Mutual Exclusion In his 1965 paper "Solution of a problem in concurrent programming control," E. W. Dijkstra](https://reader036.fdocuments.net/reader036/viewer/2022081323/60006422b2acdf2212368361/html5/thumbnails/7.jpg)
Concurrent Programming and the No>on of Time
“Absolute, true and mathematical time, of itself and from its own nature, flows equably without relation to anything external.”
--- Issac Newton, 1689 time time
![Page 8: Mutual&Exclusion& - Washington University in St. Louis · 2018. 1. 17. · Mutual Exclusion In his 1965 paper "Solution of a problem in concurrent programming control," E. W. Dijkstra](https://reader036.fdocuments.net/reader036/viewer/2022081323/60006422b2acdf2212368361/html5/thumbnails/8.jpg)
Concurrent Programming and the No>on of Time
In a concurrent computa>on, there is really not a sense of >me (i.e., a global clock) that all threads in the computa>on can read and use to relate to each other.
Instead, we will think in terms of the ordering among par$ally ordered events, and will use >me as a tool for explaining the ordering. That is, the no>on of >me is local, not global.
![Page 9: Mutual&Exclusion& - Washington University in St. Louis · 2018. 1. 17. · Mutual Exclusion In his 1965 paper "Solution of a problem in concurrent programming control," E. W. Dijkstra](https://reader036.fdocuments.net/reader036/viewer/2022081323/60006422b2acdf2212368361/html5/thumbnails/9.jpg)
time
• An event a0 of thread A is – Instantaneous – No simultaneous events (break >es)
a0
Events
![Page 10: Mutual&Exclusion& - Washington University in St. Louis · 2018. 1. 17. · Mutual Exclusion In his 1965 paper "Solution of a problem in concurrent programming control," E. W. Dijkstra](https://reader036.fdocuments.net/reader036/viewer/2022081323/60006422b2acdf2212368361/html5/thumbnails/10.jpg)
time
• A thread A is (formally) a sequence a0, a1, ... of events – “Trace” model – Notation: a0 è a1 indicates order
a0
Threads
a1 a2 …
![Page 11: Mutual&Exclusion& - Washington University in St. Louis · 2018. 1. 17. · Mutual Exclusion In his 1965 paper "Solution of a problem in concurrent programming control," E. W. Dijkstra](https://reader036.fdocuments.net/reader036/viewer/2022081323/60006422b2acdf2212368361/html5/thumbnails/11.jpg)
• Assign to shared variable • Assign to local variable • Invoke method • Return from method • Lots of other things …
Example Thread Events
![Page 12: Mutual&Exclusion& - Washington University in St. Louis · 2018. 1. 17. · Mutual Exclusion In his 1965 paper "Solution of a problem in concurrent programming control," E. W. Dijkstra](https://reader036.fdocuments.net/reader036/viewer/2022081323/60006422b2acdf2212368361/html5/thumbnails/12.jpg)
Threads are State Machines
Events are transi>ons
a0
a1 a2
a3
![Page 13: Mutual&Exclusion& - Washington University in St. Louis · 2018. 1. 17. · Mutual Exclusion In his 1965 paper "Solution of a problem in concurrent programming control," E. W. Dijkstra](https://reader036.fdocuments.net/reader036/viewer/2022081323/60006422b2acdf2212368361/html5/thumbnails/13.jpg)
States
• Thread State – Program counter – Local variables
• System state – Object fields (shared variables) – Union of thread states
![Page 14: Mutual&Exclusion& - Washington University in St. Louis · 2018. 1. 17. · Mutual Exclusion In his 1965 paper "Solution of a problem in concurrent programming control," E. W. Dijkstra](https://reader036.fdocuments.net/reader036/viewer/2022081323/60006422b2acdf2212368361/html5/thumbnails/14.jpg)
time
time
• Thread A
• Thread B
Concurrency
![Page 15: Mutual&Exclusion& - Washington University in St. Louis · 2018. 1. 17. · Mutual Exclusion In his 1965 paper "Solution of a problem in concurrent programming control," E. W. Dijkstra](https://reader036.fdocuments.net/reader036/viewer/2022081323/60006422b2acdf2212368361/html5/thumbnails/15.jpg)
time
Interleavings
• Events of two or more threads – Interleaved – Not necessarily independent (why?)
![Page 16: Mutual&Exclusion& - Washington University in St. Louis · 2018. 1. 17. · Mutual Exclusion In his 1965 paper "Solution of a problem in concurrent programming control," E. W. Dijkstra](https://reader036.fdocuments.net/reader036/viewer/2022081323/60006422b2acdf2212368361/html5/thumbnails/16.jpg)
time
• An interval A0 =(a0,a1) is – Time between events a0 and a1
a0 a1
Intervals
A0
![Page 17: Mutual&Exclusion& - Washington University in St. Louis · 2018. 1. 17. · Mutual Exclusion In his 1965 paper "Solution of a problem in concurrent programming control," E. W. Dijkstra](https://reader036.fdocuments.net/reader036/viewer/2022081323/60006422b2acdf2212368361/html5/thumbnails/17.jpg)
time
Intervals may Overlap
a0 a1 A0
b0 b1 B0
![Page 18: Mutual&Exclusion& - Washington University in St. Louis · 2018. 1. 17. · Mutual Exclusion In his 1965 paper "Solution of a problem in concurrent programming control," E. W. Dijkstra](https://reader036.fdocuments.net/reader036/viewer/2022081323/60006422b2acdf2212368361/html5/thumbnails/18.jpg)
time
Intervals may be Disjoint
a0 a1 A0
b0 b1 B0
![Page 19: Mutual&Exclusion& - Washington University in St. Louis · 2018. 1. 17. · Mutual Exclusion In his 1965 paper "Solution of a problem in concurrent programming control," E. W. Dijkstra](https://reader036.fdocuments.net/reader036/viewer/2022081323/60006422b2acdf2212368361/html5/thumbnails/19.jpg)
time
Precedence
a0 a1 A0
b0 b1 B0
Interval A0 precedes interval B0
![Page 20: Mutual&Exclusion& - Washington University in St. Louis · 2018. 1. 17. · Mutual Exclusion In his 1965 paper "Solution of a problem in concurrent programming control," E. W. Dijkstra](https://reader036.fdocuments.net/reader036/viewer/2022081323/60006422b2acdf2212368361/html5/thumbnails/20.jpg)
Precedence
• Nota>on: A0 è B0 • Formally,
– End event of A0 before start event of B0 – Also called "happens before" or "precedes"
![Page 21: Mutual&Exclusion& - Washington University in St. Louis · 2018. 1. 17. · Mutual Exclusion In his 1965 paper "Solution of a problem in concurrent programming control," E. W. Dijkstra](https://reader036.fdocuments.net/reader036/viewer/2022081323/60006422b2acdf2212368361/html5/thumbnails/21.jpg)
Precedence Ordering
• Remark: A0 è B0 is just like saying – 1066 AD è 1492 AD, – Middle Ages è Renaissance,
• Oh wait, – what about this week vs this month?
![Page 22: Mutual&Exclusion& - Washington University in St. Louis · 2018. 1. 17. · Mutual Exclusion In his 1965 paper "Solution of a problem in concurrent programming control," E. W. Dijkstra](https://reader036.fdocuments.net/reader036/viewer/2022081323/60006422b2acdf2212368361/html5/thumbnails/22.jpg)
Precedence Ordering Is a Par>al Order
• Irreflexive: never true that A è A
• An$symmetric: if A èB then not true that B èA • Transi$ve: If A èB and B èC then A èC • Also: A èB and B èA might both be false!
![Page 23: Mutual&Exclusion& - Washington University in St. Louis · 2018. 1. 17. · Mutual Exclusion In his 1965 paper "Solution of a problem in concurrent programming control," E. W. Dijkstra](https://reader036.fdocuments.net/reader036/viewer/2022081323/60006422b2acdf2212368361/html5/thumbnails/23.jpg)
Repeated Events
while (mumble) { a0; a1; }
a0k
k-‐th occurrence of event a0
A0k
k-‐th occurrence of interval A0 =(a0,a1)
For simplicity, we omit the superscript when it's clear which instance we are referring to.
![Page 24: Mutual&Exclusion& - Washington University in St. Louis · 2018. 1. 17. · Mutual Exclusion In his 1965 paper "Solution of a problem in concurrent programming control," E. W. Dijkstra](https://reader036.fdocuments.net/reader036/viewer/2022081323/60006422b2acdf2212368361/html5/thumbnails/24.jpg)
Implemen>ng a Counter
public class Counter { private long value; public long getAndIncrement() { temp = value; value = temp + 1; return temp; } }
Make these steps indivisible using locks
cri$cal sec$on
![Page 25: Mutual&Exclusion& - Washington University in St. Louis · 2018. 1. 17. · Mutual Exclusion In his 1965 paper "Solution of a problem in concurrent programming control," E. W. Dijkstra](https://reader036.fdocuments.net/reader036/viewer/2022081323/60006422b2acdf2212368361/html5/thumbnails/25.jpg)
Locks (Mutual Exclusion)
public interface Lock { public void lock(); public void unlock(); }
![Page 26: Mutual&Exclusion& - Washington University in St. Louis · 2018. 1. 17. · Mutual Exclusion In his 1965 paper "Solution of a problem in concurrent programming control," E. W. Dijkstra](https://reader036.fdocuments.net/reader036/viewer/2022081323/60006422b2acdf2212368361/html5/thumbnails/26.jpg)
Using Locks public class Counter { private long value; private Lock lock; public long getAndIncrement() { lock.lock(); try { int temp = value; value = value + 1; } finally { lock.unlock(); } return temp; } }
![Page 27: Mutual&Exclusion& - Washington University in St. Louis · 2018. 1. 17. · Mutual Exclusion In his 1965 paper "Solution of a problem in concurrent programming control," E. W. Dijkstra](https://reader036.fdocuments.net/reader036/viewer/2022081323/60006422b2acdf2212368361/html5/thumbnails/27.jpg)
Using Locks public class Counter { private long value; private Lock lock; public long getAndIncrement() { lock.lock(); try { int temp = value; value = value + 1; } finally { lock.unlock(); } return temp; } }
acquire Lock
![Page 28: Mutual&Exclusion& - Washington University in St. Louis · 2018. 1. 17. · Mutual Exclusion In his 1965 paper "Solution of a problem in concurrent programming control," E. W. Dijkstra](https://reader036.fdocuments.net/reader036/viewer/2022081323/60006422b2acdf2212368361/html5/thumbnails/28.jpg)
Using Locks public class Counter { private long value; private Lock lock; public long getAndIncrement() { lock.lock(); try { int temp = value; value = value + 1; } finally { lock.unlock(); } return temp; } }
Release lock (no maBer what)
![Page 29: Mutual&Exclusion& - Washington University in St. Louis · 2018. 1. 17. · Mutual Exclusion In his 1965 paper "Solution of a problem in concurrent programming control," E. W. Dijkstra](https://reader036.fdocuments.net/reader036/viewer/2022081323/60006422b2acdf2212368361/html5/thumbnails/29.jpg)
Using Locks public class Counter { private long value; private Lock lock; public long getAndIncrement() { lock.lock(); try { int temp = value; value = value + 1; } finally { lock.unlock(); } return temp; } }
cri$cal sec$on
![Page 30: Mutual&Exclusion& - Washington University in St. Louis · 2018. 1. 17. · Mutual Exclusion In his 1965 paper "Solution of a problem in concurrent programming control," E. W. Dijkstra](https://reader036.fdocuments.net/reader036/viewer/2022081323/60006422b2acdf2212368361/html5/thumbnails/30.jpg)
• Let CSik be thread i's k-‐th cri>cal sec>on execu>on
• And CSjm be thread j's m-‐th cri>cal sec>on execu>on
Mutual Exclusion
![Page 31: Mutual&Exclusion& - Washington University in St. Louis · 2018. 1. 17. · Mutual Exclusion In his 1965 paper "Solution of a problem in concurrent programming control," E. W. Dijkstra](https://reader036.fdocuments.net/reader036/viewer/2022081323/60006422b2acdf2212368361/html5/thumbnails/31.jpg)
• Let CSik be thread i's k-‐th cri>cal sec>on execu>on
• And CSjm be thread j's m-‐th cri>cal sec>on execu>on
• Then either – or
Mutual Exclusion
CSik è CSj
m CSjm è CSi
k
![Page 32: Mutual&Exclusion& - Washington University in St. Louis · 2018. 1. 17. · Mutual Exclusion In his 1965 paper "Solution of a problem in concurrent programming control," E. W. Dijkstra](https://reader036.fdocuments.net/reader036/viewer/2022081323/60006422b2acdf2212368361/html5/thumbnails/32.jpg)
Proper>es of a Good Locking Algorithm
• Mutual exclusion • Deadlock freedom • Starva>on freedom
![Page 33: Mutual&Exclusion& - Washington University in St. Louis · 2018. 1. 17. · Mutual Exclusion In his 1965 paper "Solution of a problem in concurrent programming control," E. W. Dijkstra](https://reader036.fdocuments.net/reader036/viewer/2022081323/60006422b2acdf2212368361/html5/thumbnails/33.jpg)
Proper>es of a Good Locking Algorithm
• Mutual exclusion • Deadlock freedom: system as a whole makes progress. If some thread calls lock() and never returns, then other threads must complete lock() and unlock() calls infinitely ocen.
• Starva>on freedom
![Page 34: Mutual&Exclusion& - Washington University in St. Louis · 2018. 1. 17. · Mutual Exclusion In his 1965 paper "Solution of a problem in concurrent programming control," E. W. Dijkstra](https://reader036.fdocuments.net/reader036/viewer/2022081323/60006422b2acdf2212368361/html5/thumbnails/34.jpg)
Proper>es of a Good Locking Algorithm
• Mutual exclusion • Deadlock freedom: system as a whole makes progress. If some thread calls lock() and never returns, then other threads must complete lock() and unlock() calls infinitely ocen.
• Starva$on freedom: individual thread makes progress. (This implies deadlock freedom.) If some thread calls lock(), it will eventually return.
![Page 35: Mutual&Exclusion& - Washington University in St. Louis · 2018. 1. 17. · Mutual Exclusion In his 1965 paper "Solution of a problem in concurrent programming control," E. W. Dijkstra](https://reader036.fdocuments.net/reader036/viewer/2022081323/60006422b2acdf2212368361/html5/thumbnails/35.jpg)
Two-‐Thread vs n-‐Thread Solu>ons
• 2-‐thread solu>ons first – Illustrate most basic ideas – Fits on one slide
• Then n-‐thread solu>ons
![Page 36: Mutual&Exclusion& - Washington University in St. Louis · 2018. 1. 17. · Mutual Exclusion In his 1965 paper "Solution of a problem in concurrent programming control," E. W. Dijkstra](https://reader036.fdocuments.net/reader036/viewer/2022081323/60006422b2acdf2212368361/html5/thumbnails/36.jpg)
class … implements Lock { … // thread-local index, 0 or 1 public void lock() { int i = ThreadID.get(); int j = 1 - i; … } }
Two-‐Thread Conven>ons
Henceforth: i is current thread, j is other thread
![Page 37: Mutual&Exclusion& - Washington University in St. Louis · 2018. 1. 17. · Mutual Exclusion In his 1965 paper "Solution of a problem in concurrent programming control," E. W. Dijkstra](https://reader036.fdocuments.net/reader036/viewer/2022081323/60006422b2acdf2212368361/html5/thumbnails/37.jpg)
LockOne
class LockOne implements Lock { private boolean[] flag = new boolean[2]; public void lock() { flag[i] = true; while (flag[j]) {} }
Each thread has a flag
![Page 38: Mutual&Exclusion& - Washington University in St. Louis · 2018. 1. 17. · Mutual Exclusion In his 1965 paper "Solution of a problem in concurrent programming control," E. W. Dijkstra](https://reader036.fdocuments.net/reader036/viewer/2022081323/60006422b2acdf2212368361/html5/thumbnails/38.jpg)
LockOne
class LockOne implements Lock { private boolean[] flag = new boolean[2]; public void lock() { flag[i] = true; while (flag[j]) {} }
Set my flag
![Page 39: Mutual&Exclusion& - Washington University in St. Louis · 2018. 1. 17. · Mutual Exclusion In his 1965 paper "Solution of a problem in concurrent programming control," E. W. Dijkstra](https://reader036.fdocuments.net/reader036/viewer/2022081323/60006422b2acdf2212368361/html5/thumbnails/39.jpg)
LockOne
class LockOne implements Lock { private boolean[] flag = new boolean[2]; public void lock() { flag[i] = true; while (flag[j]) {} }
Wait for other flag to become false
![Page 40: Mutual&Exclusion& - Washington University in St. Louis · 2018. 1. 17. · Mutual Exclusion In his 1965 paper "Solution of a problem in concurrent programming control," E. W. Dijkstra](https://reader036.fdocuments.net/reader036/viewer/2022081323/60006422b2acdf2212368361/html5/thumbnails/40.jpg)
• WLOG, assume CSAj overlaps CSBk . • Consider each thread's last read and write in lock() before entering the overlapping cri>cal sec>on.
• Derive a contradic>on.
Proving Mutual Exclusion
Thread B lock() CSB
Thread A lock() CSA
time
![Page 41: Mutual&Exclusion& - Washington University in St. Louis · 2018. 1. 17. · Mutual Exclusion In his 1965 paper "Solution of a problem in concurrent programming control," E. W. Dijkstra](https://reader036.fdocuments.net/reader036/viewer/2022081323/60006422b2acdf2212368361/html5/thumbnails/41.jpg)
LockOne Satisfies Mutual Exclusion (1) From each thread's lock() execu>on:
writeA(flag[A]=true) à readA(flag[B]==false) àCSA writeB(flag[B]=true) à readB(flag[A]==false) à CSB
(2) Since A and B have not executed unlock(), no flags have been set to false. Thus, it must be that: readA(flag[B]==false) à writeB(flag[B]=true)
Combine observa>ons: writeA(flag[A]=true) à readA(flag[B]==false) (1)
à writeB(flag[B]=true) à readB(flag[A]==false) (2) Must have read flag[A] = true; contradic$on!
![Page 42: Mutual&Exclusion& - Washington University in St. Louis · 2018. 1. 17. · Mutual Exclusion In his 1965 paper "Solution of a problem in concurrent programming control," E. W. Dijkstra](https://reader036.fdocuments.net/reader036/viewer/2022081323/60006422b2acdf2212368361/html5/thumbnails/42.jpg)
LockOne Fails Deadlock-‐Freedom
• Concurrent execu>on can deadlock
• Sequen>al execu>ons OK
flag[i] = true; flag[j] = true; while (flag[j]){} while (flag[i]){}
![Page 43: Mutual&Exclusion& - Washington University in St. Louis · 2018. 1. 17. · Mutual Exclusion In his 1965 paper "Solution of a problem in concurrent programming control," E. W. Dijkstra](https://reader036.fdocuments.net/reader036/viewer/2022081323/60006422b2acdf2212368361/html5/thumbnails/43.jpg)
LockTwo
public class LockTwo implements Lock { private int victim; public void lock() { victim = i; while (victim == i) {}; } public void unlock() {} }
![Page 44: Mutual&Exclusion& - Washington University in St. Louis · 2018. 1. 17. · Mutual Exclusion In his 1965 paper "Solution of a problem in concurrent programming control," E. W. Dijkstra](https://reader036.fdocuments.net/reader036/viewer/2022081323/60006422b2acdf2212368361/html5/thumbnails/44.jpg)
LockTwo
public class LockTwo implements Lock { private int victim; public void lock() { victim = i; while (victim == i) {}; } public void unlock() {} }
Let other go first
![Page 45: Mutual&Exclusion& - Washington University in St. Louis · 2018. 1. 17. · Mutual Exclusion In his 1965 paper "Solution of a problem in concurrent programming control," E. W. Dijkstra](https://reader036.fdocuments.net/reader036/viewer/2022081323/60006422b2acdf2212368361/html5/thumbnails/45.jpg)
LockTwo
public class LockTwo implements Lock { private int victim; public void lock() { victim = i; while (victim == i) {}; } public void unlock() {} }
Wait for permission
![Page 46: Mutual&Exclusion& - Washington University in St. Louis · 2018. 1. 17. · Mutual Exclusion In his 1965 paper "Solution of a problem in concurrent programming control," E. W. Dijkstra](https://reader036.fdocuments.net/reader036/viewer/2022081323/60006422b2acdf2212368361/html5/thumbnails/46.jpg)
LockTwo
public class LockTwo implements Lock { private int victim; public void lock() { victim = i; while (victim == i) {}; } public void unlock() {} }
Nothing to do
![Page 47: Mutual&Exclusion& - Washington University in St. Louis · 2018. 1. 17. · Mutual Exclusion In his 1965 paper "Solution of a problem in concurrent programming control," E. W. Dijkstra](https://reader036.fdocuments.net/reader036/viewer/2022081323/60006422b2acdf2212368361/html5/thumbnails/47.jpg)
public void LockTwo() { victim = i; while (victim == i) {}; }
LockTwo Claims
• Sa>sfies mutual exclusion – If thread i in CS – Then vic$m == j – Cannot be both 0 and 1
• Not deadlock free – Sequen>al execu>on deadlocks – Concurrent execu>on does not
![Page 48: Mutual&Exclusion& - Washington University in St. Louis · 2018. 1. 17. · Mutual Exclusion In his 1965 paper "Solution of a problem in concurrent programming control," E. W. Dijkstra](https://reader036.fdocuments.net/reader036/viewer/2022081323/60006422b2acdf2212368361/html5/thumbnails/48.jpg)
Peterson's Algorithm [2]
public void lock() { flag[i] = true; victim = i; while (flag[j] && victim == i) {}; } public void unlock() { flag[i] = false; }
![Page 49: Mutual&Exclusion& - Washington University in St. Louis · 2018. 1. 17. · Mutual Exclusion In his 1965 paper "Solution of a problem in concurrent programming control," E. W. Dijkstra](https://reader036.fdocuments.net/reader036/viewer/2022081323/60006422b2acdf2212368361/html5/thumbnails/49.jpg)
Peterson's Algorithm [2]
public void lock() { flag[i] = true; victim = i; while (flag[j] && victim == i) {}; } public void unlock() { flag[i] = false; }
Announce interest
![Page 50: Mutual&Exclusion& - Washington University in St. Louis · 2018. 1. 17. · Mutual Exclusion In his 1965 paper "Solution of a problem in concurrent programming control," E. W. Dijkstra](https://reader036.fdocuments.net/reader036/viewer/2022081323/60006422b2acdf2212368361/html5/thumbnails/50.jpg)
Peterson's Algorithm [2]
public void lock() { flag[i] = true; victim = i; while (flag[j] && victim == i) {}; } public void unlock() { flag[i] = false; }
Defer to the other
![Page 51: Mutual&Exclusion& - Washington University in St. Louis · 2018. 1. 17. · Mutual Exclusion In his 1965 paper "Solution of a problem in concurrent programming control," E. W. Dijkstra](https://reader036.fdocuments.net/reader036/viewer/2022081323/60006422b2acdf2212368361/html5/thumbnails/51.jpg)
Peterson's Algorithm [2]
public void lock() { flag[i] = true; victim = i; while (flag[j] && victim == i) {}; } public void unlock() { flag[i] = false; }
Wait while other is interested & I'm the vic$m
![Page 52: Mutual&Exclusion& - Washington University in St. Louis · 2018. 1. 17. · Mutual Exclusion In his 1965 paper "Solution of a problem in concurrent programming control," E. W. Dijkstra](https://reader036.fdocuments.net/reader036/viewer/2022081323/60006422b2acdf2212368361/html5/thumbnails/52.jpg)
Peterson's Algorithm [2]
public void lock() { flag[i] = true; victim = i; while (flag[j] && victim == i) {}; } public void unlock() { flag[i] = false; }
No longer interested
![Page 53: Mutual&Exclusion& - Washington University in St. Louis · 2018. 1. 17. · Mutual Exclusion In his 1965 paper "Solution of a problem in concurrent programming control," E. W. Dijkstra](https://reader036.fdocuments.net/reader036/viewer/2022081323/60006422b2acdf2212368361/html5/thumbnails/53.jpg)
(1) writeB(Flag[B]=true)èwriteB(vic>m=B)
public void lock() { flag[i] = true; victim = i; while (flag[j] && victim == i) {}; }
Peterson's Sa>sfies Mutual Exclusion
![Page 54: Mutual&Exclusion& - Washington University in St. Louis · 2018. 1. 17. · Mutual Exclusion In his 1965 paper "Solution of a problem in concurrent programming control," E. W. Dijkstra](https://reader036.fdocuments.net/reader036/viewer/2022081323/60006422b2acdf2212368361/html5/thumbnails/54.jpg)
(2) writeA(victim=A)èreadA(flag[B]) èreadA(victim)
public void lock() { flag[i] = true; victim = i; while (flag[j] && victim == i) {}; }
Peterson's Sa>sfies Mutual Exclusion
![Page 55: Mutual&Exclusion& - Washington University in St. Louis · 2018. 1. 17. · Mutual Exclusion In his 1965 paper "Solution of a problem in concurrent programming control," E. W. Dijkstra](https://reader036.fdocuments.net/reader036/viewer/2022081323/60006422b2acdf2212368361/html5/thumbnails/55.jpg)
Peterson's Sa>sfies Mutual Exclusion
W.L.O.G. assume A is the last thread to write vic$m
(3) writeB(vic>m=B)èwriteA(vic>m=A)
![Page 56: Mutual&Exclusion& - Washington University in St. Louis · 2018. 1. 17. · Mutual Exclusion In his 1965 paper "Solution of a problem in concurrent programming control," E. W. Dijkstra](https://reader036.fdocuments.net/reader036/viewer/2022081323/60006422b2acdf2212368361/html5/thumbnails/56.jpg)
(1) writeB(flag[B]=true) è writeB(vic>m=B)
(3) writeB(vic>m=B) è writeA(vic>m=A)
(2) writeA(vic>m=A) è readA(flag[B]) è readA(vic>m)
Peterson's Sa>sfies Mutual Exclusion
![Page 57: Mutual&Exclusion& - Washington University in St. Louis · 2018. 1. 17. · Mutual Exclusion In his 1965 paper "Solution of a problem in concurrent programming control," E. W. Dijkstra](https://reader036.fdocuments.net/reader036/viewer/2022081323/60006422b2acdf2212368361/html5/thumbnails/57.jpg)
(1) writeB(flag[B]=true) è
(3) writeB(vic>m=B) è
(2) writeA(vic>m=A) è readA(flag[B]) è readA(vic>m)
Peterson's Sa>sfies Mutual Exclusion
A read flag[B] == true and victim == A, so it could not have entered the CS (QED)
![Page 58: Mutual&Exclusion& - Washington University in St. Louis · 2018. 1. 17. · Mutual Exclusion In his 1965 paper "Solution of a problem in concurrent programming control," E. W. Dijkstra](https://reader036.fdocuments.net/reader036/viewer/2022081323/60006422b2acdf2212368361/html5/thumbnails/58.jpg)
• Thread blocked – only at while loop – only if other's flag is true – only if it is the vic>m
• Solo: other's flag is false • Both: one or the other not the vic>m
public void lock() { … while (flag[j] && victim == i) {};
Peterson's Is Deadlock Free*
* Assuming a thread doesn't just enter the CS and die.
![Page 59: Mutual&Exclusion& - Washington University in St. Louis · 2018. 1. 17. · Mutual Exclusion In his 1965 paper "Solution of a problem in concurrent programming control," E. W. Dijkstra](https://reader036.fdocuments.net/reader036/viewer/2022081323/60006422b2acdf2212368361/html5/thumbnails/59.jpg)
• Thread A blocked only if thread B repeatedly reenters so that flag[j] == true && victim == i
• When thread B re-‐enters – it sets victim to j. – So thread A gets in
public void lock() { flag[i] = true; victim = i; while (flag[j] && victim == i) {}; }
Peterson's Is Starva>on Free*
* Assuming a thread doesn't just enter the CS and die.
![Page 60: Mutual&Exclusion& - Washington University in St. Louis · 2018. 1. 17. · Mutual Exclusion In his 1965 paper "Solution of a problem in concurrent programming control," E. W. Dijkstra](https://reader036.fdocuments.net/reader036/viewer/2022081323/60006422b2acdf2212368361/html5/thumbnails/60.jpg)
Lamport's Bakery Algorithm [3]
• Provides First-‐Come-‐First-‐Served for n threads
• Basic idea: – Take a “number” – Wait un>l lower numbers have been served
• If two threads get the same number, use thread ID to break >e: – (a,i) > (b,j) If a > b, or a = b and i > j (lexicographic order)
![Page 61: Mutual&Exclusion& - Washington University in St. Louis · 2018. 1. 17. · Mutual Exclusion In his 1965 paper "Solution of a problem in concurrent programming control," E. W. Dijkstra](https://reader036.fdocuments.net/reader036/viewer/2022081323/60006422b2acdf2212368361/html5/thumbnails/61.jpg)
class Bakery implements Lock { boolean[] flag; Label[] label; public Bakery(int n) { flag = new boolean[n]; label = new Label[n]; for (int i = 0; i < n; i++) { flag[i] = false; label[i] = 0; } } …
Bakery Algorithm
![Page 62: Mutual&Exclusion& - Washington University in St. Louis · 2018. 1. 17. · Mutual Exclusion In his 1965 paper "Solution of a problem in concurrent programming control," E. W. Dijkstra](https://reader036.fdocuments.net/reader036/viewer/2022081323/60006422b2acdf2212368361/html5/thumbnails/62.jpg)
class Bakery implements Lock { boolean[] flag; Label[] label; public Bakery(int n) { flag = new boolean[n]; label = new Label[n]; for (int i = 0; i < n; i++) { flag[i] = false; label[i] = 0; } } …
Bakery Algorithm
One per thread, indexed by thread ID
![Page 63: Mutual&Exclusion& - Washington University in St. Louis · 2018. 1. 17. · Mutual Exclusion In his 1965 paper "Solution of a problem in concurrent programming control," E. W. Dijkstra](https://reader036.fdocuments.net/reader036/viewer/2022081323/60006422b2acdf2212368361/html5/thumbnails/63.jpg)
Bakery Algorithm
class Bakery implements Lock { … public void lock() { flag[i] = true; label[i] = max(label[0], …, label[n-1])+1; while(∃k flag[k] && (label[i],i) > (label[k],k));
} public void unlock() { flag[i] = false; }
![Page 64: Mutual&Exclusion& - Washington University in St. Louis · 2018. 1. 17. · Mutual Exclusion In his 1965 paper "Solution of a problem in concurrent programming control," E. W. Dijkstra](https://reader036.fdocuments.net/reader036/viewer/2022081323/60006422b2acdf2212368361/html5/thumbnails/64.jpg)
Bakery Algorithm
class Bakery implements Lock { … public void lock() { flag[i] = true; label[i] = max(label[0], …, label[n-1])+1; while(∃k flag[k] && (label[i],i) > (label[k],k));
} public void unlock() { flag[i] = false; }
I'm interested
![Page 65: Mutual&Exclusion& - Washington University in St. Louis · 2018. 1. 17. · Mutual Exclusion In his 1965 paper "Solution of a problem in concurrent programming control," E. W. Dijkstra](https://reader036.fdocuments.net/reader036/viewer/2022081323/60006422b2acdf2212368361/html5/thumbnails/65.jpg)
Bakery Algorithm
class Bakery implements Lock { … public void lock() { flag[i] = true; label[i] = max(label[0], …, label[n-1])+1; while(∃k flag[k] && (label[i],i) > (label[k],k));
} public void unlock() { flag[i] = false; }
Take an increasing label (read labels in some arbitrary order)
![Page 66: Mutual&Exclusion& - Washington University in St. Louis · 2018. 1. 17. · Mutual Exclusion In his 1965 paper "Solution of a problem in concurrent programming control," E. W. Dijkstra](https://reader036.fdocuments.net/reader036/viewer/2022081323/60006422b2acdf2212368361/html5/thumbnails/66.jpg)
Bakery Algorithm
class Bakery implements Lock { … public void lock() { flag[i] = true; label[i] = max(label[0], …, label[n-1])+1; while(∃k flag[k] && (label[i],i) > (label[k],k));
} public void unlock() { flag[i] = false; }
Someone is interested
… whose (label,tID) in lexicographic order is lower
![Page 67: Mutual&Exclusion& - Washington University in St. Louis · 2018. 1. 17. · Mutual Exclusion In his 1965 paper "Solution of a problem in concurrent programming control," E. W. Dijkstra](https://reader036.fdocuments.net/reader036/viewer/2022081323/60006422b2acdf2212368361/html5/thumbnails/67.jpg)
Bakery Algorithm
class Bakery implements Lock { … public void lock() { flag[i] = true; label[i] = max(label[0], …, label[n-1])+1; while(∃k flag[k] && (label[i],i) > (label[k],k));
} public void unlock() { flag[i] = false; } Labels are always increasing.
![Page 68: Mutual&Exclusion& - Washington University in St. Louis · 2018. 1. 17. · Mutual Exclusion In his 1965 paper "Solution of a problem in concurrent programming control," E. W. Dijkstra](https://reader036.fdocuments.net/reader036/viewer/2022081323/60006422b2acdf2212368361/html5/thumbnails/68.jpg)
Bakery Algorithm
class Bakery implements Lock { … public void lock() { flag[i] = true; label[i] = max(label[0], …, label[n-1])+1; while(∃k flag[k] && (label[i],i) > (label[k],k));
} public void unlock() { flag[i] = false; } No longer interested
![Page 69: Mutual&Exclusion& - Washington University in St. Louis · 2018. 1. 17. · Mutual Exclusion In his 1965 paper "Solution of a problem in concurrent programming control," E. W. Dijkstra](https://reader036.fdocuments.net/reader036/viewer/2022081323/60006422b2acdf2212368361/html5/thumbnails/69.jpg)
Thread A lock() CSA
Bakery Sa>sfies Mutual Exclusion
Thread B lock() CSB
WLOG, also assume that thread A has a lower (label, tID) than B when both are in CS (at t3).
WLOG, assume CSA and CSB overlap:
t0 t1 t2 t3
Since a thread's label is always increasing, and B doesn't change its label once lock() returns, B must has a higher (label, tID) than A when it enters CSB.
![Page 70: Mutual&Exclusion& - Washington University in St. Louis · 2018. 1. 17. · Mutual Exclusion In his 1965 paper "Solution of a problem in concurrent programming control," E. W. Dijkstra](https://reader036.fdocuments.net/reader036/viewer/2022081323/60006422b2acdf2212368361/html5/thumbnails/70.jpg)
• B only enters CSB if it read – flag[A] is false, or – (label[A], A) > (label[B], B)
• We just showed that when B enters CSB, A has a lower (label, tID).
• B must have seen that flag[A] is false.
class Bakery implements Lock { public void lock() { flag[i] = true; label[i] = max(label[0],…, label[n-1])+1; while (∃k flag[k] && (label[i],i) > (label[k],k)); }
Bakery Sa>sfies Mutual Exclusion
![Page 71: Mutual&Exclusion& - Washington University in St. Louis · 2018. 1. 17. · Mutual Exclusion In his 1965 paper "Solution of a problem in concurrent programming control," E. W. Dijkstra](https://reader036.fdocuments.net/reader036/viewer/2022081323/60006422b2acdf2212368361/html5/thumbnails/71.jpg)
Bakery Sa>sfies Mutual Exclusion • B must have seen flag[A] == false (1) readB(flag[A]) è writeA(flag[A])
(since flag[A] hasn't changed) (2) writeB(label[B]) è readB(flag[A]) and
writeA(flag[A]) è readA(label[B]) è writeA(label[A]) (from the code)
Combining Observa>on: writeB(label[B]) è readB(flag[A]) (2)
è writeA(flag[A]) è readA(label[B]) è writeA(label[A]) (1)
so it must be that label[A] > label[B], which contradicts that A has a lower (label[A], tID). (QED)
![Page 72: Mutual&Exclusion& - Washington University in St. Louis · 2018. 1. 17. · Mutual Exclusion In his 1965 paper "Solution of a problem in concurrent programming control," E. W. Dijkstra](https://reader036.fdocuments.net/reader036/viewer/2022081323/60006422b2acdf2212368361/html5/thumbnails/72.jpg)
• There is always one thread with the lowest (label, tID)
• Ties are impossible
Bakery is Deadlock-‐Free
![Page 73: Mutual&Exclusion& - Washington University in St. Louis · 2018. 1. 17. · Mutual Exclusion In his 1965 paper "Solution of a problem in concurrent programming control," E. W. Dijkstra](https://reader036.fdocuments.net/reader036/viewer/2022081323/60006422b2acdf2212368361/html5/thumbnails/73.jpg)
First-‐Come-‐First-‐Serve • Divide lock() method into 2 parts:
– Doorway interval (DA): always finishes in finite steps
– Wai>ng interval (WA): may take unbounded steps • First-‐come first-‐serve: for threads A and B, if DA
k è DB
j then CSAk è CSBj
• r-‐Bounded wai>ng: for threads A and B, if DA
k è DB
j then CSAk è CSBj+r
![Page 74: Mutual&Exclusion& - Washington University in St. Louis · 2018. 1. 17. · Mutual Exclusion In his 1965 paper "Solution of a problem in concurrent programming control," E. W. Dijkstra](https://reader036.fdocuments.net/reader036/viewer/2022081323/60006422b2acdf2212368361/html5/thumbnails/74.jpg)
Bakery Is First-‐Come-‐First-‐Serve
class Bakery implements Lock { … public void lock() { flag[i] = true; label[i] = max(label[0], …, label[n-1])+1; while(∃k flag[k] && (label[i],i) > (label[k],k));
} public void unlock() { flag[i] = false; }
Doorway
![Page 75: Mutual&Exclusion& - Washington University in St. Louis · 2018. 1. 17. · Mutual Exclusion In his 1965 paper "Solution of a problem in concurrent programming control," E. W. Dijkstra](https://reader036.fdocuments.net/reader036/viewer/2022081323/60006422b2acdf2212368361/html5/thumbnails/75.jpg)
Bakery Is First-‐Come-‐First-‐Serve
class Bakery implements Lock { … public void lock() { flag[i] = true; label[i] = max(label[0], …, label[n-1])+1; while(∃k flag[k] && (label[i],i) > (label[k],k));
} public void unlock() { flag[i] = false; }
Wai$ng Interval
If DA è DB then B must see a lower label for A and be locked out while A is in CSA.
![Page 76: Mutual&Exclusion& - Washington University in St. Louis · 2018. 1. 17. · Mutual Exclusion In his 1965 paper "Solution of a problem in concurrent programming control," E. W. Dijkstra](https://reader036.fdocuments.net/reader036/viewer/2022081323/60006422b2acdf2212368361/html5/thumbnails/76.jpg)
class Bakery implements Lock { … public void lock() { flag[i] = true; label[i] = max(label[0], …, label[n-1])+1; while(∃k flag[k] && (label[i],i) > (label[k],k));
} public void unlock() { flag[i] = false; }
Lock breaks if label[i] overflows
Bakery Y232K Bug
64-‐bit system … we will let grand children worry about that.
![Page 77: Mutual&Exclusion& - Washington University in St. Louis · 2018. 1. 17. · Mutual Exclusion In his 1965 paper "Solution of a problem in concurrent programming control," E. W. Dijkstra](https://reader036.fdocuments.net/reader036/viewer/2022081323/60006422b2acdf2212368361/html5/thumbnails/77.jpg)
Philosophical Ques>on
• The Bakery Algorithm is – Succinct, – Elegant, and – Fair.
• Q: So why isn't it prac>cal? • A: Well, you have to read N dis>nct variables
![Page 78: Mutual&Exclusion& - Washington University in St. Louis · 2018. 1. 17. · Mutual Exclusion In his 1965 paper "Solution of a problem in concurrent programming control," E. W. Dijkstra](https://reader036.fdocuments.net/reader036/viewer/2022081323/60006422b2acdf2212368361/html5/thumbnails/78.jpg)
Shared Memory
• Shared read/write memory loca>ons called Registers (historical reasons)
• Come in different flavors – Mul>-‐Reader-‐Single-‐Writer (flag[] in Bakery) – Mul>-‐Reader-‐Mul>-‐Writer (vic$m in Peterson's) – Not that interes>ng: SRMW and SRSW
![Page 79: Mutual&Exclusion& - Washington University in St. Louis · 2018. 1. 17. · Mutual Exclusion In his 1965 paper "Solution of a problem in concurrent programming control," E. W. Dijkstra](https://reader036.fdocuments.net/reader036/viewer/2022081323/60006422b2acdf2212368361/html5/thumbnails/79.jpg)
At least N MRSW (mul>-‐reader/single-‐writer) registers are needed to solve deadlock-‐free mutual exclusion. N registers such as flag[]…
Theorem [4]
![Page 80: Mutual&Exclusion& - Washington University in St. Louis · 2018. 1. 17. · Mutual Exclusion In his 1965 paper "Solution of a problem in concurrent programming control," E. W. Dijkstra](https://reader036.fdocuments.net/reader036/viewer/2022081323/60006422b2acdf2212368361/html5/thumbnails/80.jpg)
Upper Bound
• Bakery algorithm – Uses 2N MRSW registers
• So the bound is (pretty) tight • But what if we use MRMW registers?
– Like victim ?
![Page 81: Mutual&Exclusion& - Washington University in St. Louis · 2018. 1. 17. · Mutual Exclusion In his 1965 paper "Solution of a problem in concurrent programming control," E. W. Dijkstra](https://reader036.fdocuments.net/reader036/viewer/2022081323/60006422b2acdf2212368361/html5/thumbnails/81.jpg)
Bad News Theorem [4]
At least N MRMW mul$-‐reader/mul$-‐writer registers are needed to solve deadlock-‐free mutual exclusion.
(So mul$ple writers don't help)
![Page 82: Mutual&Exclusion& - Washington University in St. Louis · 2018. 1. 17. · Mutual Exclusion In his 1965 paper "Solution of a problem in concurrent programming control," E. W. Dijkstra](https://reader036.fdocuments.net/reader036/viewer/2022081323/60006422b2acdf2212368361/html5/thumbnails/82.jpg)
Final Words • In the 1960's several incorrect solutions
to starvation-free mutual exclusion using RW-registers were published…
• Today we know how to solve FIFO N thread mutual exclusion using 2N MRSW-Registers.
• N Registers inefficient – Because writes “cover” older writes
• Need stronger hardware primitives
![Page 83: Mutual&Exclusion& - Washington University in St. Louis · 2018. 1. 17. · Mutual Exclusion In his 1965 paper "Solution of a problem in concurrent programming control," E. W. Dijkstra](https://reader036.fdocuments.net/reader036/viewer/2022081323/60006422b2acdf2212368361/html5/thumbnails/83.jpg)
References
[1] E. W. Dijkstra, Solu>on of a problem in concurrent programming control. Communica>ons of the ACM. 1965; 8 (9): 569. [2] G. Peterson, Myths about the mutual exclusion problem. Informa>on Processing LeKers. June 1981;12(3):115–116. [3] L. Lamport, A new solu>on of Dijkstra's concurrent programming problem. Communica>ons of the ACM. 1974; 17(5):543–545. [4] J. E. Burns, N. A. Lynch. Bounds on shared memory for mutual exclusion. Informa>on and Computa>on. December 1993;107(2):171–184.
![Page 84: Mutual&Exclusion& - Washington University in St. Louis · 2018. 1. 17. · Mutual Exclusion In his 1965 paper "Solution of a problem in concurrent programming control," E. W. Dijkstra](https://reader036.fdocuments.net/reader036/viewer/2022081323/60006422b2acdf2212368361/html5/thumbnails/84.jpg)
This work is licensed under a Creative Commons Attribution-ShareAlike 2.5 License.
• You are free: – to Share — to copy, distribute and transmit the work – to Remix — to adapt the work
• Under the following conditions: – Attribution. You must attribute the work to “The Art of
Multiprocessor Programming” (but not in any way that suggests that the authors endorse you or your use of the work).
– Share Alike. If you alter, transform, or build upon this work, you may distribute the resulting work only under the same, similar or a compatible license.
• For any reuse or distribution, you must make clear to others the license terms of this work. The best way to do this is with a link to – http://creativecommons.org/licenses/by-sa/3.0/.
• Any of the above conditions can be waived if you get permission from the copyright holder.
• Nothing in this license impairs or restricts the author's moral rights.