Академический Документы
Профессиональный Документы
Культура Документы
Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Sha it Modified by !a"ee Alur for C#S $%&' Spring (&&) *ni ersity of pennsyl ania
Solution
0 At the start' prisoners choose one to be the leader' rest act as follo+ers 0 =eader +ill be the only one to turn s+itch from on to off' and +ill count ho+ many times he does this 0 Each follo+er +ill turn the s+itch from off to on' exactly once
Art of Multiprocessor Programming %
0 Exercise9 Pro e that this solution is correct 0 .esign strategy for the case +hen initial position of s+itch is not 4no+n to prisoners
Art of Multiprocessor Programming >
=eader2s Strategy
0 =ocal ariable count' initially & 0 *pon entry to the room
? #f s+itch is off' do nothing ? #f s+itch is on' increment count and turn the s+itch off ? #f count @@ NA; then declare that all prisoners ha e been to the room at least once
Mutual Exclusion
0 Today +e +ill try to formali,e our understanding of mutual exclusion 0 1e +ill also use the opportunity to sho+ you ho+ to argue about and pro e arious properties in an asynchronous concurrent setting
Art of Multiprocessor Programming B
Mutual Exclusion
0 0 0 0 0 <ormal problem definitions Solutions for ( threads Solutions for n threads <air solutions #nherent costs
Art of Multiprocessor Programming C
1arning
0 Dou +ill ne er use these protocols 0 *nderstanding these is necessary
? The same issues sho+ up e ery+here ? Except hidden and more complex
0 =i eness Properties
? Something good happens e entually ? E7g7 .eadloc4 freedom' Star ation freedom
Art of Multiprocessor Programming ;&
E ents
0 An e ent a& of thread A is
? #nstantaneous ? No simultaneous e ents -brea4 ties/ a&
time
Art of Multiprocessor Programming ;;
Threads
0 A thread A is -formally/ a seEuence a&' a;' 777 of e ents
? 5Trace6 model ? Notation9 a& a; indicates order a&
time
Art of Multiprocessor Programming ;(
a;
a(
;8
a;
;%
States
0 Thread State
? Program counter ? =ocal ariables
0 System state
? Gb"ect fields -shared ariables/ ? *nion of thread states
;>
Concurrency
0 Thread A
time
;$
Concurrency
0 Thread A
time
0 Thread H
time
;B
#nterlea ings
0 E ents of t+o or more threads
? #nterlea ed ? Not necessarily independent -+hyI/
time
;C
#nter als
0 An inter al A& @-a&'a;/ is
? Time bet+een e ents a& and a; a&
time
Art of Multiprocessor Programming ;)
A&
a;
b& a&
time
H& a;
b;
A&
(&
b& a&
time
H&
b;
A&
a;
(;
Precedence
#nter al A& precedes inter al H&
b& a&
time
H&
b;
A&
a;
((
Precedence
Precedence Grdering
0 Ne er true that A A 0 #f A H then not true that H A 0 #f A H & H C then A C 0 <unny thing9 A H & H A might both be falseJ
Art of Multiprocessor Programming (%
Partial Grders
-re ie+/
0 #rreflexi e9
? Ne er true that A A
0 Antisymmetric9
? #f A H then not true that H A
0 Transiti e9
? #f A H & H C then A C
Art of Multiprocessor Programming (>
Total Grders
-re ie+/
0 Also
? #rreflexi e ? Antisymmetric ? Transiti e
!epeated E ents
while (mumble) { a0; a1; }
a0
A0k
(B
#mplementing a Counter
public class Counter { private long value; public long getAndIncrement() { temp = value; value = temp 1; return temp; Ma4e these } }
()
acquire lock
8&
8;
*sing =oc4s
public class Counter { private long value; private "oc# loc#; public long getAndIncrement() { loc#$loc#(); tr% { int temp = value; value = value 1; } !inall% { loc#$unloc#(); } return temp; }}
Art of Multiprocessor Programming 8(
*sing =oc4s
public class Counter { private long value; private "oc# loc#; public long getAndIncrement() { loc#$loc#(); acquire tr% { int temp = value; value = value 1; } !inall% { loc#$unloc#(); } return temp; }}
Art of Multiprocessor Programming
Lock
88
*sing =oc4s
public class Counter { private long value; private "oc# loc#; public long getAndIncrement() { loc#$loc#(); tr% { int temp = value; value = value 1; } !inall% { !elease loc4 loc#$unloc#(); } -no matter +hat/ return temp; }}
Art of Multiprocessor Programming 8%
*sing =oc4s
public class Counter { private long value; private "oc# loc#; public long getAndIncrement() { loc#$loc#(); tr% { int temp = value; value = value 1; } !inall% { loc#$unloc#(); } return temp; }}
Art of Multiprocessor Programming
Critical section
8>
Mutual Exclusion
be thread i2s 4Ath critical 0 =et CSi4 section execution
8$
Mutual Exclusion
be thread i2s 4Ath critical 0 =et CSi4 section execution be thread "2s mAth critical 0 And CS"m section execution
8B
Mutual Exclusion
be thread i2s 4Ath critical 0 =et CSi4 section execution be "2s mAth execution 0 And CS"m 0 Then either
? or
8C
Mutual Exclusion
be thread i2s 4Ath critical 0 =et CSi4 section execution be "2s mAth execution 0 And CS"m 0 Then either
? or CSi4 CS"m
Art of Multiprocessor Programming 8)
Mutual Exclusion
be thread i2s 4Ath critical 0 =et CSi4 section execution be "2s mAth execution 0 And CS"m 0 Then either
? or CSi4 CS"m
Art of Multiprocessor Programming
CS"m CSi4
%&
.eadloc4A<ree
0 #f some thread calls lock()
? And ne er returns ? Then other threads must complete lock() and unlock() calls infinitely often
%;
Star ationA<ree
0 #f some thread calls loc4-/
? #t +ill e entually return
%(
%8
%%
=oc4Gne
class "oc#.ne implements "oc# { private boolean/0 !lag = new boolean/10; public void loc#() { !lag/i0 = true; while (!lag/-0) {} }
%$
=oc4Gne
class "oc#.ne implements "oc# { private boolean/0 !lag = new boolean/10; public void loc#() { !lag/i0 = true; while (!lag/-0) {} Set my flag }
%B
=oc4Gne
class "oc#.ne implements "oc# { private boolean/0 !lag = new boolean/10; public void loc#() { !lag/i0 = true; while (!lag/-0) {} Set my flag }
>&
>;
Combining
0 Assumptions9
? readA(flag[ ]==false) write (flag[ ]=true) ? read (flag[A]==false) writeA(flag[A]=true)
>(
Combining
0 Assumptions9
? readA(flag[ ]==false) write (flag[ ]=true) ? read (flag[A]==false) writeA(flag[A]=true)
>8
Combining
0 Assumptions9
? readA(flag[ ]==false) write (flag[ ]=true) ? read (flag[A]==false) writeA(flag[A]=true)
>%
Combining
0 Assumptions9
? readA(flag[ ]==false) write (flag[ ]=true) ? read (flag[A]==false) writeA(flag[A]=true)
>>
Combining
0 Assumptions9
? readA(flag[ ]==false) write (flag[ ]=true) ? read (flag[A]==false) writeA(flag[A]=true)
>$
Combining
0 Assumptions9
? readA(flag[ ]==false) write (flag[ ]=true) ? read (flag[A]==false) writeA(flag[A]=true)
>B
CycleJ
in r e e l d $ i r s o s o la # "m rtia #a
Art of Multiprocessor Programming >C
.eadloc4 <reedom
0 =oc4Gne <ails deadloc4Afreedom
? Concurrent execution can deadloc4
!lag/i0 = true; while (!lag/-0){} !lag/-0 = true; while (!lag/i0){}
? SeEuential executions GL
>)
=oc4T+o
public class "oc#+wo implements "oc# { private int victim; public void loc#() { victim = i; while (victim == i) {}; } public void unloc#() {} }
$&
=oc4T+o
public class "oc#+wo implements "oc# { private int victim; Let other public void loc#() { first victim = i; while (victim == i) {}; } public void unloc#() {} }
go
$;
=oc4T+o
public class "oc#+wo implements "oc# { Wait for private int victim; public void loc#() { #ermission victim = i; while (victim == i) {}; } public void unloc#() {} }
$(
=oc4T+o
public class "oc#1 implements "oc# { private int victim; public void loc#() { %othing victim = i; while (victim == i) {}; } public void unloc#() {} }
to do
$8
=oc4T+o Claims
0 Satisfies mutual exclusion
? #f thread i in CS public void "oc#+wo() { victim = i; ? Then victim == while (victim == i) {}; ? Cannot be both & and ;
$%
Peterson2s Algorithm
public void loc#() { !lag/i0 = true; victim = i; while (!lag/-0 22 victim == i) {}; } public void unloc#() { !lag/i0 = !alse; }
$>
Peterson2s Algorithm
public void loc#() { !lag/i0 = true; victim = i; while (!lag/-0 22 victim == i) {}; } public void unloc#() { !lag/i0 = !alse; }
$$
Peterson2s Algorithm
public void loc#() { !lag/i0 = true; 'efer to other victim = i; while (!lag/-0 22 victim == i) {}; } public void unloc#() { !lag/i0 = !alse; }
$B
Peterson2s Algorithm
public void loc#() { !lag/i0 = true; 'efer to other victim = i; while (!lag/-0 22 victim == i) {}; } public void unloc#() { Wait while other !lag/i0 = !alse; interested ( "&m }
the )ictim
$C
Peterson2s Algorithm
public void loc#() { !lag/i0 = true; 'efer to other victim = i; while (!lag/-0 22 victim == i) {}; } public void unloc#() { Wait while other !lag/i0 = !alse; interested ( "&m }
%o longer interested
the )ictim
$)
Mutual Exclusion
public void loc#() { !lag/i0 = true; victim = i; while (!lag/-0 22 victim == i) {};
.eadloc4 <ree
public void loc#() { & while (!lag/-0 22 victim == i) {};
0 Thread bloc4ed
0 Solo9 other2s flag is false 0 Hoth9 one or the other must not be the ictim
Art of Multiprocessor Programming B;
? only at while loop ? Gnly if other2s flag is true ? only if it is the )ictim
and
reAenters
B(
<ilter
class 3ilter implements "oc# { int/0 level; '' level/i0 !or thread i int/0 victim; '' victim/"0 !or level " public 3ilter(int n) { n+* 0 level = new int/n0; le)el 0 0 , 0 0 0 0 0 victim = new int/n0; * !or (int i = 1; i 4 n; i ) { level/i0 = 0; }} - , &
.hread - at le)el ,
Art of Multiprocessor Programming
n+* )ictim
B%
<ilter
class 3ilter implements "oc# { & public void loc#(){ !or (int " = 1; " 4 n; " level/i0 = "; victim/"0 = i; ) {
<ilter
class 3ilter implements "oc# { & public void loc#() { !or (int " = 1; " 4 n; " level/i0 = "; victim/"0 = i; ) {
Gne le el at a time
B$
<ilter
class 3ilter implements "oc# { & public void loc#() { !or (int " = 1; " 4 n; " level/i0 = "; victim/"0 = i; ) {
<ilter
class 3ilter implements "oc# { int level/n0; int victim/n0; public void loc#() { !or (int " = 1; " 4 n; " ) { level/i0 = "; victim/"0 = i;
<ilter
class 3ilter implements "oc# { Wait as long as someone else is at int level/n0; int victim/n0; higher le)el0 and "&m designated public void loc#() { !or (int " = 1; " 4 n; " ) { level/i0 = "; victim/"0 = i;
same or )ictim
<ilter
class 3ilter implements "oc# { int level/n0; int victim/n0; public void loc#() { !or (int " = 1; " 4 n; " ) { level/i0 = "; victim/"0 = i;
}} public void release(int i) { .hread enters le)el L when level/i0 = 0; the loo# }}
Art of Multiprocessor Programming
it com#letes
C&
Claim
0 Start at le el =@& 0 At most nA= threads enter le el = 0 Mutual exclusion at le el =@nA;
ncs L=0 L=* L=n+L=n+*
C;
cs
#nduction Hypothesis
0 No more than nA=O; at le el =A; 0 #nduction step9 by contradiction 0 Assume all at le el =A; enter le el = 0 A last to +rite ictimM=N 0 H is any other thread at le el =
ncs
assume
L+* has n+L1* L has n+L
cs
#ro)e
C(
Proof Structure
ncs
A
Last to write )ictim[L]
cs
Show that A must ha)e seen in le)el[L] and since )ictim[L] == A could not ha)e entered
Art of Multiprocessor Programming C8
No Star ation
0 <ilter =oc4 satisfies properties9
? Pust li4e Peterson Alg at any le el ? So no one star es
C%
Hounded 1aiting
0 1ant stronger fairness guarantees 0 Thread not 5o erta4en6 too much 0 Need to ad"ust definitions F7
C>
Hounded 1aiting
0 .i ide loc#() method into ( parts9
? .oor+ay inter al9
0 1ritten 'A 0 al+ays finishes in finite steps
C$
<irstACome <irstASer ed
0 <or threads A and H9
? #f 'Ak '
2
? Then !SAk !S 2
0 A2s 4Ath critical section precedes H2s "Ath critical section 0 H cannot o erta4e A
CB
<airness Again
0 <ilter =oc4 satisfies properties9
? No one star es ? Hut ery +ea4 fairness 0 Can be o erta4en ar$itrary Q of times ? That2s pretty lameF
CC
Ha4ery Algorithm
0 Pro ides <irstAComeA<irstASer ed 0 Ho+I
? Ta4e a 5number6 ? 1ait until lo+er numbers ha e been ser ed ? -a'i/ R -b'"/
0 =exicographic order
Ha4ery Algorithm
class 7a#er% implements "oc# { boolean/0 !lag; "abel/0 label; public 7a#er% (int n) { !lag = new boolean/n0; label = new "abel/n0; !or (int i = 0; i 4 n; i ) { !lag/i0 = !alse; label/i0 = 0; } } &
)&
Ha4ery Algorithm
class 7a#er% implements "oc# { boolean/0 !lag; 4 "abel/0 label; 0 n+* public 7a#er% (int n) { !lag = new boolean/n0; f f t f f t f f label = new "abel/n0; 0 0 , 0 0 3 0 0 !or (int i = 0; i 4 n; i ) { !lag/i0 = !alse; label/i0 = 0; } } &
!S
);
Ha4ery Algorithm
class 7a#er% & public void !lag/i0 = label/i0 = implements "oc# { loc#() { true; ma)(label/00* &*label/n(10) 1;
)(
Ha4ery Algorithm
class 7a#er% & public void !lag/i0 = label/i0 = implements "oc# {
'oorway
)8
Ha4ery Algorithm
class 7a#er% & public void !lag/i0 = label/i0 = implements "oc# {
"&m interested
)%
Ha4ery Algorithm
class 7a#er% & public void !lag/i0 = label/i0 = implements "oc# {
)>
Ha4ery Algorithm
class 7a#er% & public void !lag/i0 = label/i0 = implements "oc# { loc#() { true; ma)(label/00* &*label/n(10) 1;
Someone is interested
)$
Ha4ery Algorithm
class 7a#er% implements "oc# { boolean !lag/n0; Someone int label/n0; public void loc#() { !lag/i0 = true; label/i0 = ma)(label/00* &*label/n(10) 1; while ( # !lag/#0 22 (label/i0*i) 6 (label/#0*#));
is interested
Ha4ery Algorithm
class 7a#er% implements "oc# { & public void unloc#() { !lag/i0 = !alse; } }
)C
Ha4ery Algorithm
class 7a#er% implements "oc# { & public void unloc#() { !lag/i0 = !alse; } }
%o longer interested
))
No .eadloc4
0 There is al+ays one thread +ith earliest label 0 Ties are impossible -+hyI/
;&&
<irstAComeA<irstASer ed
0 #f .A .Hthen A2s label is smaller 0 And9
? +riteA-labelMAN/ readH-labelMAN/ +riteH-labelMHN/ readH-flagMAN/
class 7a#er% implements "oc# { public void loc#() { !lag/i0 = true; label/i0 = ma)(label/00* &*label/n(10) 1; while ( # !lag/#0 22 (label/i0*i) 6 (label/#0*#)); }
;&;
Mutual Exclusion
0 Suppose A and H in CS together 0 Suppose A has earlier label 0 1hen H entered' it must ha e seen
? flagMAN is false' or ? labelMAN R labelMHN
Art of Multiprocessor Programming ;&(
class 7a#er% implements "oc# { public void loc#() { !lag/i0 = true; label/i0 = ma)(label/00* &*label/n(10) 1; while ( # !lag/#0 22 (label/i0*i) 6 (label/#0*#)); }
Mutual Exclusion
0 =abels are strictly increasing so 0 H must ha e seen flagMAN @@ false
;&8
Mutual Exclusion
0 =abels are strictly increasing so 0 H must ha e seen flagMAN @@ false 0 =abelingH readH-flagMAN/ +riteA-flagMAN/ =abelingA
;&%
Mutual Exclusion
0 =abels are strictly increasing so 0 H must ha e seen flagMAN @@ false 0 =abelingH readH-flagMAN/ +riteA-flagMAN/ =abelingA 0 1hich contradicts the assumption that A has an earlier label
Art of Multiprocessor Programming ;&>
Ha4ery D( L Hug
8(
class 7a#er% & public void !lag/i0 = label/i0 = implements "oc# { loc#() { true; ma)(label/00* &*label/n(10) 1;
;&$
Ha4ery D( L Hug
8(
class 7a#er% & public void !lag/i0 = label/i0 = implements "oc# { loc#() { true; ma)(label/00* &*label/n(10) 1;
;&B
0 No
0 Maybe
Shared Memory
0 Shared read3+rite memory locations called !egisters -historical reasons/ 0 Come in different fla ors
? MultiA!eaderASingleA1riter -3lag/0/ ? MultiA!eaderAMultiA1riter -7ictim/0/ ? Not that interesting9 S!M1 and S!S1
;;&
Theorem
At least N M!S1 -multiA reader3singleA+riter/ registers are needed to sol e deadloc4Afree mutual exclusion7 N registers li4e <lagMNF
;;;
!
write
one does' 0 sho+ a $ad e5ecution that !S iolates properties9 0 in our case assume an alg for deadloc4 free mutual exclusion using T N registers
Art of Multiprocessor Programming ;;(
!
write
!S
!S
!S
*pper Hound
0 Ha4ery algorithm
? *ses -% M!S1 registers
;;%
;;$
Write(:)
:
!S
!S
0 Threads run' reading and +riting ! 0 .eadloc4 free so at least one gets in
Art of Multiprocessor Programming ;;B
Write(:)
#n any protocol H has to +rite to the register before entering CS' so stop it "ust before
Art of Multiprocessor Programming ;;C
Proof9 Assume Co er of ;
A
Write(:)
!S
Proof9 Assume Co er of ;
A H !uns' first obliterating any trace of A' then also enters the critical section
;(&
Write(:)
!S
!S
Art of Multiprocessor Programming
Theorem
.eadloc4Afree mutual exclusion for 8 threads reEuires at least 8 multiA reader multiA+riter registers
;(;
Proof9 Assume Co er of (
A !
Write(: )
Write(:!)
Gnly ( registers
Art of Multiprocessor Programming ;((
!un A Solo
A !
Write(: )
Write(:!)
Gbliterate Traces of A
A !
Write(: )
Write(:!)
!S
Write(: )
Write(:!)
!S
!S
Proof Strategy
0 Pro ed9 a contradiction starting from a co ering state for ( registers 0 Claim9 a co ering state for ( registers is reachable from any state +here CS is empty
;($
Write(:A)
Write(: )
Write(:A)
Write(: )
0 Start +ith H co ering register !H for the ;st time 0 !un A until it is about to +rite to unco ered !A 0 Are +e doneI
Art of Multiprocessor Programming ;(C
Write(:A)
Write(: )
Write(:A)
Write(: )
0 !un obliterating traces of A in : 0 !un again until it is about to +rite to : 0 No+ +e are done
Art of Multiprocessor Programming ;8&
Write(:A)
Write(: )
Write(:!)
Summary of =ecture
0 #n the ;)$&2s many incorrect solutions to star ationAfree mutual exclusion using !1Aregisters +ere publishedF 0 Today +e 4no+ ho+ to sol e <#<G N thread mutual exclusion using (N !1A!egisters
Art of Multiprocessor Programming ;8(
Summary of =ecture
0 N !1A!egisters inefficient
? Hecause +rites ;co)er< older +rites
;88