Art of Multiprocessor Programming 2 Mutual

Published  . 0 views
↓ Download
Art of Multiprocessor Programming 2 Mutual
1 / 1
Art of Multiprocessor Programming 2 Mutual - slide 1 of 93 Art of Multiprocessor Programming 2 Mutual - slide 2 of 93 Art of Multiprocessor Programming 2 Mutual - slide 3 of 93 Art of Multiprocessor Programming 2 Mutual - slide 4 of 93 Art of Multiprocessor Programming 2 Mutual - slide 5 of 93 Art of Multiprocessor Programming 2 Mutual - slide 6 of 93 Art of Multiprocessor Programming 2 Mutual - slide 7 of 93 Art of Multiprocessor Programming 2 Mutual - slide 8 of 93 Art of Multiprocessor Programming 2 Mutual - slide 9 of 93 Art of Multiprocessor Programming 2 Mutual - slide 10 of 93 Art of Multiprocessor Programming 2 Mutual - slide 11 of 93 Art of Multiprocessor Programming 2 Mutual - slide 12 of 93 Art of Multiprocessor Programming 2 Mutual - slide 13 of 93 Art of Multiprocessor Programming 2 Mutual - slide 14 of 93 Art of Multiprocessor Programming 2 Mutual - slide 15 of 93 Art of Multiprocessor Programming 2 Mutual - slide 16 of 93 Art of Multiprocessor Programming 2 Mutual - slide 17 of 93 Art of Multiprocessor Programming 2 Mutual - slide 18 of 93 Art of Multiprocessor Programming 2 Mutual - slide 19 of 93 Art of Multiprocessor Programming 2 Mutual - slide 20 of 93 Art of Multiprocessor Programming 2 Mutual - slide 21 of 93 Art of Multiprocessor Programming 2 Mutual - slide 22 of 93 Art of Multiprocessor Programming 2 Mutual - slide 23 of 93 Art of Multiprocessor Programming 2 Mutual - slide 24 of 93 Art of Multiprocessor Programming 2 Mutual - slide 25 of 93 Art of Multiprocessor Programming 2 Mutual - slide 26 of 93 Art of Multiprocessor Programming 2 Mutual - slide 27 of 93 Art of Multiprocessor Programming 2 Mutual - slide 28 of 93 Art of Multiprocessor Programming 2 Mutual - slide 29 of 93 Art of Multiprocessor Programming 2 Mutual - slide 30 of 93 Art of Multiprocessor Programming 2 Mutual - slide 31 of 93 Art of Multiprocessor Programming 2 Mutual - slide 32 of 93 Art of Multiprocessor Programming 2 Mutual - slide 33 of 93 Art of Multiprocessor Programming 2 Mutual - slide 34 of 93 Art of Multiprocessor Programming 2 Mutual - slide 35 of 93 Art of Multiprocessor Programming 2 Mutual - slide 36 of 93 Art of Multiprocessor Programming 2 Mutual - slide 37 of 93 Art of Multiprocessor Programming 2 Mutual - slide 38 of 93 Art of Multiprocessor Programming 2 Mutual - slide 39 of 93 Art of Multiprocessor Programming 2 Mutual - slide 40 of 93 Art of Multiprocessor Programming 2 Mutual - slide 41 of 93 Art of Multiprocessor Programming 2 Mutual - slide 42 of 93 Art of Multiprocessor Programming 2 Mutual - slide 43 of 93 Art of Multiprocessor Programming 2 Mutual - slide 44 of 93 Art of Multiprocessor Programming 2 Mutual - slide 45 of 93 Art of Multiprocessor Programming 2 Mutual - slide 46 of 93 Art of Multiprocessor Programming 2 Mutual - slide 47 of 93 Art of Multiprocessor Programming 2 Mutual - slide 48 of 93 Art of Multiprocessor Programming 2 Mutual - slide 49 of 93 Art of Multiprocessor Programming 2 Mutual - slide 50 of 93 Art of Multiprocessor Programming 2 Mutual - slide 51 of 93 Art of Multiprocessor Programming 2 Mutual - slide 52 of 93 Art of Multiprocessor Programming 2 Mutual - slide 53 of 93 Art of Multiprocessor Programming 2 Mutual - slide 54 of 93 Art of Multiprocessor Programming 2 Mutual - slide 55 of 93 Art of Multiprocessor Programming 2 Mutual - slide 56 of 93 Art of Multiprocessor Programming 2 Mutual - slide 57 of 93 Art of Multiprocessor Programming 2 Mutual - slide 58 of 93 Art of Multiprocessor Programming 2 Mutual - slide 59 of 93 Art of Multiprocessor Programming 2 Mutual - slide 60 of 93 Art of Multiprocessor Programming 2 Mutual - slide 61 of 93 Art of Multiprocessor Programming 2 Mutual - slide 62 of 93 Art of Multiprocessor Programming 2 Mutual - slide 63 of 93 Art of Multiprocessor Programming 2 Mutual - slide 64 of 93 Art of Multiprocessor Programming 2 Mutual - slide 65 of 93 Art of Multiprocessor Programming 2 Mutual - slide 66 of 93 Art of Multiprocessor Programming 2 Mutual - slide 67 of 93 Art of Multiprocessor Programming 2 Mutual - slide 68 of 93 Art of Multiprocessor Programming 2 Mutual - slide 69 of 93 Art of Multiprocessor Programming 2 Mutual - slide 70 of 93 Art of Multiprocessor Programming 2 Mutual - slide 71 of 93 Art of Multiprocessor Programming 2 Mutual - slide 72 of 93 Art of Multiprocessor Programming 2 Mutual - slide 73 of 93 Art of Multiprocessor Programming 2 Mutual - slide 74 of 93 Art of Multiprocessor Programming 2 Mutual - slide 75 of 93 Art of Multiprocessor Programming 2 Mutual - slide 76 of 93 Art of Multiprocessor Programming 2 Mutual - slide 77 of 93 Art of Multiprocessor Programming 2 Mutual - slide 78 of 93 Art of Multiprocessor Programming 2 Mutual - slide 79 of 93 Art of Multiprocessor Programming 2 Mutual - slide 80 of 93 Art of Multiprocessor Programming 2 Mutual - slide 81 of 93 Art of Multiprocessor Programming 2 Mutual - slide 82 of 93 Art of Multiprocessor Programming 2 Mutual - slide 83 of 93 Art of Multiprocessor Programming 2 Mutual - slide 84 of 93 Art of Multiprocessor Programming 2 Mutual - slide 85 of 93 Art of Multiprocessor Programming 2 Mutual - slide 86 of 93 Art of Multiprocessor Programming 2 Mutual - slide 87 of 93 Art of Multiprocessor Programming 2 Mutual - slide 88 of 93 Art of Multiprocessor Programming 2 Mutual - slide 89 of 93 Art of Multiprocessor Programming 2 Mutual - slide 90 of 93 Art of Multiprocessor Programming 2 Mutual - slide 91 of 93 Art of Multiprocessor Programming 2 Mutual - slide 92 of 93 Art of Multiprocessor Programming 2 Mutual - slide 93 of 93
Description: Art of Multiprocessor Programming 2 Mutual Exclusion We will clarify our understanding of mutual exclusion We will also show you how to reason about properties in an asynchronous concurrent setting Mutual Exclusion Art of Multiprocessor

Related Topics

Download Presentation

"Art of Multiprocessor Programming 2 Mutual" is the property of its rightful owner. Permission is granted to download and print the materials on this website for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.

Presentation Transcript

slide2. Art of Multiprocessor Programming 2 Mutual Exclusion We will clarify our understanding of mutual exclusion We will also show you how to reason about properties in an asynchronous concurrent setting<br>
slide3. Mutual Exclusion Art of Multiprocessor Programming 3 In 1965, 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."<br>
slide4. Art of Multiprocessor Programming 4 Mutual Exclusion Formal problem definitions Solutions for n threads Solutions for 2 threads Fair solutions Inherent costs<br>
slide5. Art of Multiprocessor Programming 5 Warning You will never use these protocols You are advised to understand them Get over it The same issues show up everywhere Except hidden and more complex<br>
slide6. Art of Multiprocessor Programming 6 Why is Concurrent Programming so Hard? Try preparing a seven-course banquet With one friend By yourself With twenty-seven friends Before we can talk about programs Need a precise language To talk about time and concurrency<br>
slide7. Art of Multiprocessor Programming 7 “Absolute, true and mathematical time, of itself and from its own nature, flows equably without relation to anything external.” (Isaac Newton, 1689)

“Time is what keeps everything from happening at once.”
(Ray Cummings, 1922) Time<br>
slide8. Art of Multiprocessor Programming 8 a0 Events An event a0 of thread A is No simultaneous events (break ties) Instantaneous<br>
slide9. Art of Multiprocessor Programming 9 a0 Threads a1 a2 … A thread A is (formally) a sequence
a0, a1, ... of events Notation: a0  a1 indicates order “Trace” model<br>
slide10. Art of Multiprocessor Programming 10 Example Thread Events Assign to shared variable Invoke method Assign to local variable Return from method Lots of other things …<br>
slide11. Art of Multiprocessor Programming 11 Threads are State Machines Events are transitions a0 a1 a2 a3<br>
slide12. Art of Multiprocessor Programming 12 States Thread State Local variables Program counter System state Shared memory All the thread states<br>
slide13. Concurrency Art of Multiprocessor Programming 13 Thread Thread<br>
slide14. Art of Multiprocessor Programming 14 Interleavings Events of two or more threads Not necessarily independent (why?) Interleaved<br>
slide15. Art of Multiprocessor Programming 15 a0 a1 Intervals A0 An interval A0 =(a0,a1) is Time between events a0 and a1<br>
slide16. Art of Multiprocessor Programming 16 Intervals may Overlap b0 b1 B0<br>
slide17. Art of Multiprocessor Programming 17 Intervals may be Disjoint b0 b1 B0<br>
slide18. Art of Multiprocessor Programming 18 Precedence b0 b1 B0 Interval A0 precedes interval B0<br>
slide19. Art of Multiprocessor Programming 19 Precedence Notation: A0  B0 End event of A0 before start event of B0 Formally Also called “happens before” or “precedes”<br>
slide20. Art of Multiprocessor Programming 20 Precedence Ordering A0  B0 is just like saying But wait 1066 AD  1492 AD,
Middle Ages  Renaissance what about this week vs this month?<br>
slide21. Art of Multiprocessor Programming 21 Precedence Ordering Never true that A  A If A B & B C then A C If A B then not true that B A Curiously: A B & B A might both be false!<br>
slide22. Art of Multiprocessor Programming 22 Partial Orders (review) Irreflexive Antisymmetric Never true that A  A If A  B then not true that B  A Transitive If A  B & B  C then A  C<br>
slide23. Art of Multiprocessor Programming 23 Total Orders (review) Irreflexive Antisymmetric Transitive But also for every distinct A, B,
Either A  B or B  A<br>
slide24. Art of Multiprocessor Programming 24 Repeated Events while (mumble) {
  a0; a1;
} a0k k-th occurrence of event a0 A0k k-th occurrence of interval A0 =(a0,a1)<br>
slide25. Art of Multiprocessor Programming 25 Implementing a Counter public class Counter {
  private long value;
  public long getAndIncrement() {
    temp  = value;
    value = temp + 1;
    return temp;
  }
} Make these steps indivisible using locks<br>
slide26. public interface Lock {
  public void lock();
 
  public void unlock();
 } Art of Multiprocessor Programming 26 Locks (Mutual Exclusion) acquire lock release lock<br>
slide27. Art of Multiprocessor Programming 27 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;
  }}<br>
slide28. 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;
  }} Art of Multiprocessor Programming 28 Using Locks critical section<br>
slide29. Art of Multiprocessor Programming 29 Mutual Exclusion CSik  CSjm Let CSik be thread i's k-th critical section execution Then either CSjm  CSik<br>
slide30. Art of Multiprocessor Programming 30 Deadlock-Free If some thread calls lock() Then other threads must complete lock() and unlock()calls infinitely often And never returns System as a whole makes progress Even if individual threads starve<br>
slide31. Art of Multiprocessor Programming 31 Starvation-Free If some thread calls lock() Individual threads make progress It will eventually return<br>
slide32. Art of Multiprocessor Programming 32 Two-Thread vs n-Thread Solutions 2-thread solutions first Fits on one slide Illustrate most basic ideas Then n-thread solutions<br>
slide33. class foo implements Lock {
  …
  // thread-local index, 0 or 1
  public void lock() {
    int i = ThreadID.get();
    int j = 1 - i;
  …
  }
} Art of Multiprocessor Programming 33 Two-Thread Conventions<br>
slide34. LockOne class LockOne implements Lock {
  private boolean[] flag = new boolean[2];
  public void lock() {
    flag[i] = true;
    while (flag[j]) {}
   }<br>
slide35. 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<br>
slide36. LockOne class LockOne implements Lock {
  private boolean[] flag = new boolean[2];
  public void lock() {
    flag[i] = true;
    while (flag[j]) {}
   }<br>
slide37. 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<br>
slide38. Art of Multiprocessor Programming 38 LockOne Satisfies Mutual Exclusion Assume CSAj overlaps CSBk (jth and kth) read and write … Consider each thread's last in lock() before entering Derive a contradiction<br>
slide39. Art of Multiprocessor Programming 39 writeA(flag[A]=true)  readA(flag[B]==false) CSA

writeB(flag[B]=true)  readB(flag[A]==false)  CSB From the Code class LockOne implements Lock {
  …
  public void lock() {
    flag[i] = true;
    while (flag[j]) {}
   }<br>
slide40. Art of Multiprocessor Programming 40 readA(flag[B]==false)  writeB(flag[B]=true)

readB(flag[A]==false)  writeA(flag[A]=true) From the Assumption<br>
slide41. Art of Multiprocessor Programming 41 Assumptions:
readA(flag[B]==false)  writeB(flag[B]=true)
readB(flag[A]==false)  writeA(flag[A]=true)
From the code
writeA(flag[A]=true)  readA(flag[B]==false)
writeB(flag[B]=true)  readB(flag[A]==false) Combining<br>
slide42. Art of Multiprocessor Programming 42 Assumptions:
readA(flag[B]==false)  writeB(flag[B]=true)
readB(flag[A]==false)  writeA(flag[A]=true)
From the code
writeA(flag[A]=true)  readA(flag[B]==false)
writeB(flag[B]=true)  readB(flag[A]==false) Combining<br>
slide43. Art of Multiprocessor Programming 43 Assumptions:
readA(flag[B]==false)  writeB(flag[B]=true)
readB(flag[A]==false)  writeA(flag[A]=true)
From the code
writeA(flag[A]=true)  readA(flag[B]==false)
writeB(flag[B]=true)  readB(flag[A]==false) Combining<br>
slide44. Art of Multiprocessor Programming 44 Assumptions:
readA(flag[B]==false)  writeB(flag[B]=true)
readB(flag[A]==false)  writeA(flag[A]=true)
From the code
writeA(flag[A]=true)  readA(flag[B]==false)
writeB(flag[B]=true)  readB(flag[A]==false) Combining<br>
slide45. Art of Multiprocessor Programming 45 Assumptions:
readA(flag[B]==false)  writeB(flag[B]=true)
readB(flag[A]==false)  writeA(flag[A]=true)
From the code
writeA(flag[A]=true)  readA(flag[B]==false)
writeB(flag[B]=true)  readB(flag[A]==false) Combining<br>
slide46. Art of Multiprocessor Programming 46 Assumptions:
readA(flag[B]==false)  writeB(flag[B]=true)
readB(flag[A]==false)  writeA(flag[A]=true)
From the code
writeA(flag[A]=true)  readA(flag[B]==false)
writeB(flag[B]=true)  readB(flag[A]==false) Combining<br>
slide47. Art of Multiprocessor Programming 47 Assumptions:
readA(flag[B]==false)  writeB(flag[B]=true)
readB(flag[A]==false)  writeA(flag[A]=true)
From the code
writeA(flag[A]=true)  readA(flag[B]==false)
writeB(flag[B]=true)  readB(flag[A]==false) Combining<br>
slide48. Art of Multiprocessor Programming 48 Cycle! Impossible in a partial order<br>
slide49. Art of Multiprocessor Programming 49 Deadlock Freedom flag[i] = true;    flag[j] = true;
while (flag[j]){}  while (flag[i]){} LockOne Fails deadlock-freedom Concurrent execution can deadlock<br>
slide50. Art of Multiprocessor Programming 50 LockTwo public class LockTwo implements Lock {
  private int victim;
  public void lock() {
   victim = i;
   while (victim == i) {};
  }
  public void unlock() {}
 }<br>
slide51. public class LockTwo implements Lock {
  private int victim;
  public void lock() {
   victim = i;
   while (victim == i) {};
  }
  public void unlock() {}
 } Art of Multiprocessor Programming 51 LockTwo Let other go first<br>
slide52. public class LockTwo implements Lock {
  private int victim;
  public void lock() {
   victim = i;
   while (victim == i) {};
  }
  public void unlock() {}
 } Art of Multiprocessor Programming 52 LockTwo Wait for permission …<br>
slide53. public class LockTwo implements Lock {
  private int victim;
  public void lock() {
   victim = i;
   while (victim == i) {};
  }
  public void unlock() {}
 } Art of Multiprocessor Programming 53 LockTwo Nothing to do<br>
slide54. Art of Multiprocessor Programming 54 public void LockTwo() {
  victim = i;
  while (victim == i) {};
 } LockTwo Claims Satisfies mutual exclusion Then victim == j If thread i in CS Cannot be both 0 and 1 Not deadlock free Sequential execution deadlocks Concurrent execution does not<br>
slide55. Art of Multiprocessor Programming 55 Peterson's Algorithm public void lock() {
  flag[i] = true;
  victim  = i;
  while (flag[j] && victim == i) {};
 }
 public void unlock() {
  flag[i] = false;
 }<br>
slide56. public void lock() {
  flag[i] = true;
  victim  = i;
  while (flag[j] && victim == i) {};
 }
 public void unlock() {
  flag[i] = false;
 } Art of Multiprocessor Programming 56 Peterson's Algorithm Defer to other Wait while other interested & I'm the victim No longer interested<br>
slide57. Art of Multiprocessor Programming 57 Mutual Exclusion (1) writeB(Flag[B]=true)writeB(victim=B) public void lock() {
  flag[i] = true;
  victim  = i;
  while (flag[j] && victim == i) {};
 }<br>
slide58. public void lock() {
  flag[i] = true;
  victim  = i;
  while (flag[j] && victim == i) {};
 } Art of Multiprocessor Programming 58 Also from the Code (2) writeA(victim=A)readA(flag[B])
readA(victim)<br>
slide59. Art of Multiprocessor Programming 59 Assumption W.L.O.G. assume A is the last thread to write victim (3) writeB(victim=B)writeA(victim=A)<br>
slide60. Art of Multiprocessor Programming 60 Combining Observations (1) writeB(flag[B]=true)writeB(victim=B) (3) writeB(victim=B)writeA(victim=A) (2) writeA(victim=A)readA(flag[B])
 readA(victim)<br>
slide61. Art of Multiprocessor Programming 61 Combining Observations (1) writeB(flag[B]=true)writeB(victim=B) (3) writeB(victim=B)writeA(victim=A) (2) writeA(victim=A)readA(flag[B])
 readA(victim)<br>
slide62. Art of Multiprocessor Programming 62 Combining Observations (1) writeB(flag[B]=true)writeB(victim=B) (3) writeB(victim=B)writeA(victim=A) (2) writeA(victim=A)readA(flag[B])
 readA(victim) A read flag[B] == true and victim == A, so it could not have entered the CS (QED)<br>
slide63. Art of Multiprocessor Programming 63 Deadlock Free public void lock() {
  …
  while (flag[j] && victim == i) {}; Thread blocked only if other's flag is true only at while loop only if it is the victim Solo: other's flag is false Both: one or the other not the victim<br>
slide64. Art of Multiprocessor Programming 64 Starvation Free public void lock() {
  flag[i] = true;
  victim    = i;
  while (flag[j] && victim == i) {};
}
public void unlock() {
  flag[i] = false;  
} Thread i blocked only if j repeatedly re-enters so that When j re-enters flag[j] == true and victim == i it sets victim to j So i gets in<br>
slide65. Art of Multiprocessor Programming 65 Bounded Waiting Want stronger fairness guarantees If A starts before B, then A enters before B? Thread not “overtaken” too much But what does “start” even mean?<br>
slide66. Art of Multiprocessor Programming 66 Bounded Waiting Divide lock() method into 2 intervals: Written DA Doorway always finishes in finite steps Waiting Written WA may take unbounded steps<br>
slide67. Art of Multiprocessor Programming 67 r-Bounded Waiting For threads A and B: A's k-th doorway precedes B's j-th doorway If DAk  DB j Then CSAk  CSBj+r A's kth critical section precedes B’s (j+r)th critical section B cannot overtake A more than r times First-come-first-served means r = 0<br>
slide68. Art of Multiprocessor Programming 68 What is “r” for Peterson's Algorithm? public void lock() {
  flag[i] = true;
  victim  = i;
  while (flag[j] && victim == i) {};
 }
 public void unlock() {
  flag[i] = false;
 } Answer: r = 0 Doorway<br>
slide69. Art of Multiprocessor Programming 69 First-Come-First-Served For threads A and B: A's kth doorway precedes B's kth doorway If DAk  DB j Then CSAk  CSBj A's kth critical section precedes B's kth critical section B cannot overtake A<br>
slide70. Art of Multiprocessor Programming 70 Bakery Algorithm First-Come-First-Served mutual exclusion Take a “number” How? Wait until lower numbers have been served Break ties in lexicographic order: (a,i) > (b,j) If a > b, or a = b and i > j<br>
slide71. Art of Multiprocessor Programming 71 Bakery Algorithm 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;
   }
 }
…<br>
slide72. 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;
   }
 }
… Art of Multiprocessor Programming 72 Bakery Algorithm<br>
slide73. Art of Multiprocessor Programming 73 Bakery Algorithm class Bakery implements Lock {
  …
 public void lock() {  
  flag[i]  = true;  
  label[i] = max(label[0], …,label[n-1])+1;
  while (for some k:
    flag[k] && (label[i],i) > (label[k],k));
 }<br>
slide74. class Bakery implements Lock {
  …
 public void lock() {  
  flag[i]  = true;  
  label[i] = max(label[0], …,label[n-1])+1;
  while (for some k:
    flag[k] && (label[i],i) > (label[k],k));
 } Art of Multiprocessor Programming 74 Bakery Algorithm Doorway<br>
slide75. class Bakery implements Lock {
  …
 public void lock() {  
  flag[i]  = true;  
  label[i] = max(label[0], …,label[n-1])+1;
  while (for some k:
    flag[k] && (label[i],i) > (label[k],k));
 } Art of Multiprocessor Programming 75 Bakery Algorithm I'm interested<br>
slide76. class Bakery implements Lock {
  …
 public void lock() {  
  flag[i]  = true;  
  label[i] = max(label[0], …,label[n-1])+1;
  while (for some k:
    flag[k] && (label[i],i) > (label[k],k));
 } Art of Multiprocessor Programming 76 Bakery Algorithm Take increasing label
(read labels in arbitrary order)<br>
slide77. class Bakery implements Lock {
  …
 public void lock() {  
  flag[i]  = true;  
  label[i] = max(label[0], …,label[n-1])+1;
  while (for some k:
    flag[k] && (label[i],i) > (label[k],k));
 } Art of Multiprocessor Programming 77 Bakery Algorithm Someone is interested …<br>
slide78. class Bakery implements Lock {
  …
 public void lock() {  
  flag[i]  = true;  
  label[i] = max(label[0], …,label[n-1])+1;
  while (for some k:
    flag[k] && (label[i],i) > (label[k],k));
 } Art of Multiprocessor Programming 78 Bakery Algorithm … whose (label,i) is earlier in lex order Someone is interested …<br>
slide79. class Bakery implements Lock {
  ...
  public void unlock() {  
    flag[i] = false;
  }
} Art of Multiprocessor Programming 79 Bakery Algorithm<br>
slide80. class Bakery implements Lock {
  ...
  public void unlock() {  
    flag[i] = false;
  }
} Art of Multiprocessor Programming 80 Bakery Algorithm No longer interested labels are always increasing<br>
slide81. Art of Multiprocessor Programming 81 No Deadlock There is always one thread with earliest label Ties are impossible (why?)<br>
slide82. Art of Multiprocessor Programming 82 First-Come-First-Served class Bakery implements Lock {
  …
 public void lock() {  
  flag[i]  = true;  
  label[i] = max(label[0], …,label[n-1])+1;
  while (for some k:
    flag[k] && (label[i],i) > (label[k],k));
 } If DA  DB then A's label is smaller writeA(label[A])  And readB(label[A])  writeB(label[B])  readB(flag[A]) So B sees A has smaller label Locked out while flag[A] is true<br>
slide83. Art of Multiprocessor Programming 83 Mutual Exclusion class Bakery implements Lock {
  …
 public void lock() {  
  flag[i]  = true;  
  label[i] = max(label[0], …,label[n-1])+1;
  while (for some k:
    flag[k] && (label[i],i) > (label[k],k));
 } Suppose A and B in CS together When B entered, it must have seen Suppose A has earlier label flag[A] is false, or label[A] > label[B]<br>
slide84. Art of Multiprocessor Programming 84 Mutual Exclusion Labels are strictly increasing so
B must have seen flag[A] == false Which contradicts the assumption that A has an earlier label LabelingB  readB(flag[A])  writeA(flag[A])  LabelingA<br>
slide85. Art of Multiprocessor Programming 85 Bakery Y232K Bug class Bakery implements Lock {
  …
 public void lock() {  
  flag[i]  = true;  
  label[i] = max(label[0], …,label[n-1])+1;
  while (for some k:
    flag[k] && (label[i],i) > (label[k],k));
 }<br>
slide86. class Bakery implements Lock {
  …
 public void lock() {  
  flag[i]  = true;  
  label[i] = max(label[0], …,label[n-1])+1;
  while (for some k:
    flag[k] && (label[i],i) > (label[k],k));
 } Art of Multiprocessor Programming 86 Bakery Y232K Bug Mutex breaks if label[i] overflows<br>
slide87. Art of Multiprocessor Programming 87 Does Overflow Actually Matter? Y2K No 64-bit counters Maybe 32-bit counters Art of Multiprocessor Programming 87 Yes 18 January 2038 (Unix time_t rollover) 16-bit counters<br>
slide88. Art of Multiprocessor Programming 88 Philosophical Question The Bakery Algorithm is Elegant, and Succinct Fair Q: So why isn't it practical? A: Well, you need N distinct variables<br>
slide89. Art of Multiprocessor Programming 89 Shared Memory Shared read/write memory locations are called Registers (historical reasons) Multi-Reader-Single-Writer (flag[]) Come in different flavors Multi-Reader-Multi-Writer (victim[]) Not that interesting: SRMW and SRSW<br>
slide90. Art of Multiprocessor Programming 90 Theorem At least N MRSW (multi-reader/single-writer) registers are needed to solve deadlock-free mutual exclusion. N registers such as flag[]…<br>
slide91. Art of Multiprocessor Programming 91 Summary of Lecture Today we know how to solve FCFS N-thread mutual exclusion using 2N RW-Registers In the 1960's several incorrect solutions to starvation-free mutual exclusion using RW-registers were published…<br>
slide92. Art of Multiprocessor Programming 92 Summary of Lecture N RW-Registers inefficient What about stronger hardware operations! But mathematically necessary … In later lectures we will understand what these operations are…<br>
slide93. Art of Multiprocessor Programming 93 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.<br>
slide94. Suppose A and B in CS together When B entered, it must have seen Suppose A has earlier label flag[A] is false, or label[A] > label[B] 64-bit counters Maybe 32-bit counters<br>