Вы находитесь на странице: 1из 134

Mutual Exclusion

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

Prisoners and a s+itch pu,,le -.istributed coordination/


0 At the start' prisoners get together and decide a strategy' but later ha e no communication 0 1arden2s office has a s+itch -on3off/ 0 1arden repeatedly pic4s a prisoner' and brings him to office 0 Prisoner can declare 5all ha e isited the office at least once6' may toggle the s+itch' or lea e it unchanged7 Then +arden ta4es prisoner bac4 to his cell
Art of Multiprocessor Programming (

Prisoners and a s+itch pu,,le -.istributed coordination/


0 Assumption about the order in +hich prisoners are pic4ed9 if the prisoner +aits long enough' he +ill get pic4ed -fair scheduling/ 0 :oal9 .esign +inning strategy for prisoners so that -;/ declaration is ne er made prematurely -(/ e entually some prisoner ma4es declaration 0 <or no+' assume9 initially s+itch is off
Art of Multiprocessor Programming 8

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 %

Each <ollo+er2s Strategy


0 =ocal ariable done' initially <alse 0 *pon entry to the room
? #f .one @@True do nothing ? #f s+itch is on' do nothing ? #f s+itch is off and .one@@<alse' set .one to True and turn the s+itch on

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

Art of Multiprocessor Programming

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

Art of Multiprocessor Programming

<ormali,ing the Problem


0 T+o types of formal properties in asynchronous computation9 0 Safety Properties
? Nothing bad happens e er ? E7g7 Mutual exclusion' No o erta4ing

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(

Example Thread E ents


0 0 0 0 0 Assign to shared ariable Assign to local ariable #n o4e method !eturn from method =ots of other things F

Art of Multiprocessor Programming

;8

Threads are State Machines


E ents are transitions a8 a( a&

a;

Art of Multiprocessor Programming

;%

States
0 Thread State
? Program counter ? =ocal ariables

0 System state
? Gb"ect fields -shared ariables/ ? *nion of thread states

Art of Multiprocessor Programming

;>

Concurrency
0 Thread A
time

Art of Multiprocessor Programming

;$

Concurrency
0 Thread A
time

0 Thread H
time

Art of Multiprocessor Programming

;B

#nterlea ings
0 E ents of t+o or more threads
? #nterlea ed ? Not necessarily independent -+hyI/
time

Art of Multiprocessor Programming

;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;

#nter als may G erlap

b& a&
time

H& a;

b;

A&

Art of Multiprocessor Programming

(&

#nter als may be .is"oint

b& a&
time

H&

b;

A&

a;

Art of Multiprocessor Programming

(;

Precedence
#nter al A& precedes inter al H&

b& a&
time

H&

b;

A&

a;

Art of Multiprocessor Programming

((

Precedence

0 Notation9 A& H& 0 <ormally'


? End e ent of A& before start e ent of H& ? Also called 5happens before6 or 5precedes6
Art of Multiprocessor Programming (8

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

0 Except that for e ery distinct A' H'


? Either A H or H A
Art of Multiprocessor Programming ($

!epeated E ents
while (mumble) { a0; a1; }

4Ath occurrence of e ent a& 4Ath occurrence of inter al A& @-a&'a;/

a0

A0k

Art of Multiprocessor Programming

(B

#mplementing a Counter
public class Counter { private long value; public long getAndIncrement() { temp = value; value = temp 1; return temp; Ma4e these } }

steps indi isible using loc4s Art of Multiprocessor (C


Programming

=oc4s -Mutual Exclusion/


public inter!ace "oc# { public void loc#(); public void unloc#(); }

Art of Multiprocessor Programming

()

=oc4s -Mutual Exclusion/


public inter!ace "oc# { public void loc#(); public void unloc#(); }

acquire lock

Art of Multiprocessor Programming

8&

=oc4s -Mutual Exclusion/


public inter!ace "oc# { public void loc#(); public void unloc#(); }

acquire lock release lock

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 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

Art of Multiprocessor Programming

8$

Mutual Exclusion
be thread i2s 4Ath critical 0 =et CSi4 section execution be thread "2s mAth critical 0 And CS"m section execution

Art of Multiprocessor Programming

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

Art of Multiprocessor Programming

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

0 System as a +hole ma4es progress


? E en if indi iduals star e

Art of Multiprocessor Programming

%;

Star ationA<ree
0 #f some thread calls loc4-/
? #t +ill e entually return

0 #ndi idual threads ma4e progress

Art of Multiprocessor Programming

%(

T+oAThread s n AThread Solutions


0 T+oAthread solutions first
? #llustrate most basic ideas ? <its on one slide

0 Then nAThread solutions

Art of Multiprocessor Programming

%8

T+oAThread Con entions


class & implements "oc# { & '' thread(local inde)* 0 or 1 public void loc#() { int i = +hreadI,$get(); int - = 1 ( i; & } }

Art of Multiprocessor Programming

%%

T+oAThread Con entions


class & implements "oc# { & '' thread(local inde)* 0 or 1 public void loc#() { int i = +hreadI,$get(); int - = 1 ( i; & } }

Henceforth9 i is current thread' " is other thread


Art of Multiprocessor Programming %>

=oc4Gne
class "oc#.ne implements "oc# { private boolean/0 !lag = new boolean/10; public void loc#() { !lag/i0 = true; while (!lag/-0) {} }

Art of Multiprocessor Programming

%$

=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 }

Art of Multiprocessor Programming

%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 }

Wait for other flag to go false


Art of Multiprocessor Programming %C

=oc4Gne Satisfies Mutual Exclusion


0 Assume CSA" o erlaps CSH4 0 Consider each threadKs last -"Ath and 4Ath/ read and +rite in the loc4-/ method before entering 0 .eri e a contradiction
Art of Multiprocessor Programming %)

<rom the Code


0 writeA(flag[A]=true) readA(flag[ ]==false) !SA 0 write (flag[ ]=true) readclass (flag[A]==false) "oc#.ne implements "oc# !S {
& public void loc#() { !lag/i0 = true; while (!lag/-0) {} }

Art of Multiprocessor Programming

>&

<rom the Assumption


0 readA(flag[ ]==false) write (flag[ ]=true) 0 read (flag[A]==false) writeA(flag[A]=true)

Art of Multiprocessor Programming

>;

Combining
0 Assumptions9
? readA(flag[ ]==false) write (flag[ ]=true) ? read (flag[A]==false) writeA(flag[A]=true)

0 <rom the code


? writeA(flag[A]=true) readA(flag[ ]==false) ? write (flag[ ]=true) read (flag[A]==false)

Art of Multiprocessor Programming

>(

Combining
0 Assumptions9
? readA(flag[ ]==false) write (flag[ ]=true) ? read (flag[A]==false) writeA(flag[A]=true)

0 <rom the code


? writeA(flag[A]=true) readA(flag[ ]==false) ? write (flag[ ]=true) read (flag[A]==false)

Art of Multiprocessor Programming

>8

Combining
0 Assumptions9
? readA(flag[ ]==false) write (flag[ ]=true) ? read (flag[A]==false) writeA(flag[A]=true)

0 <rom the code


? writeA(flag[A]=true) readA(flag[ ]==false) ? write (flag[ ]=true) read (flag[A]==false)

Art of Multiprocessor Programming

>%

Combining
0 Assumptions9
? readA(flag[ ]==false) write (flag[ ]=true) ? read (flag[A]==false) writeA(flag[A]=true)

0 <rom the code


? writeA(flag[A]=true) readA(flag[ ]==false) ? write (flag[ ]=true) read (flag[A]==false)

Art of Multiprocessor Programming

>>

Combining
0 Assumptions9
? readA(flag[ ]==false) write (flag[ ]=true) ? read (flag[A]==false) writeA(flag[A]=true)

0 <rom the code


? writeA(flag[A]=true) readA(flag[ ]==false) ? write (flag[ ]=true) read (flag[A]==false)

Art of Multiprocessor Programming

>$

Combining
0 Assumptions9
? readA(flag[ ]==false) write (flag[ ]=true) ? read (flag[A]==false) writeA(flag[A]=true)

0 <rom the code


? writeA(flag[A]=true) readA(flag[ ]==false) ? write (flag[ ]=true) read (flag[A]==false)

Art of Multiprocessor Programming

>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

Art of Multiprocessor Programming

>)

=oc4T+o
public class "oc#+wo implements "oc# { private int victim; public void loc#() { victim = i; while (victim == i) {}; } public void unloc#() {} }

Art of Multiprocessor Programming

$&

=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

Art of Multiprocessor Programming

$;

=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#() {} }

Art of Multiprocessor Programming

$(

=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

Art of Multiprocessor Programming

$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 ;

0 Not deadloc4 free

? SeEuential execution deadloc4s ? Concurrent execution does not

Art of Multiprocessor Programming

$%

Peterson2s Algorithm
public void loc#() { !lag/i0 = true; victim = i; while (!lag/-0 22 victim == i) {}; } public void unloc#() { !lag/i0 = !alse; }

Art of Multiprocessor Programming

$>

Peterson2s Algorithm
public void loc#() { !lag/i0 = true; victim = i; while (!lag/-0 22 victim == i) {}; } public void unloc#() { !lag/i0 = !alse; }

Announce "&m interested

Art of Multiprocessor Programming

$$

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; }

Announce "&m interested

Art of Multiprocessor Programming

$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 }

Announce "&m interested

the )ictim

Art of Multiprocessor Programming

$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 }

Announce "&m interested

%o longer interested

the )ictim

Art of Multiprocessor Programming

$)

Mutual Exclusion
public void loc#() { !lag/i0 = true; victim = i; while (!lag/-0 22 victim == i) {};

0 #f thread 0 in critical section'


!lag/00=true' victim = 1

0 #f thread * in critical section'


!lag/10=true' victim = 0

Cannot both be true


Art of Multiprocessor Programming B&

.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

Star ation <ree


0 Thread i bloc4ed only if - repeatedly reAenters so that 0 1hen
!lag/-0 == true victim == i public void loc#() { !lag/i0 = true; victim = i; while (!lag/-0 22 victim == i) {}; } public void unloc#() { !lag/i0 = !alse; }

and

? it sets victim to -7 ? So i gets in

reAenters

Art of Multiprocessor Programming

B(

The <ilter Algorithm for n Threads


There are n+* 5+aiting rooms6 called le els ncs 0 At each le el
? At least one enters le el ? At least one bloc4ed if many try
cs

0 Gnly one thread ma4es it through


Art of Multiprocessor Programming B8

<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; ) {

while (( # 5= i level/#0 6= ") 22 victim/"0 == i );

}} public void unloc#() { level/i0 = 0; }}


Art of Multiprocessor Programming B>

<ilter
class 3ilter implements "oc# { & public void loc#() { !or (int " = 1; " 4 n; " level/i0 = "; victim/"0 = i; ) {

while (( # 5= i) level/#0 6= ") 22 victim/"0 == i);

}} public void release(int i) { level/i0 = 0; }}


Art of Multiprocessor Programming

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; ) {

while (( # 5= i) level/#0 6= ") 22 victim/"0 == i); '' bus% wait

}} public void release(int i) { level/i0 = 0; }}


Art of Multiprocessor Programming

Announce intention to enter le)el L


BB

<ilter
class 3ilter implements "oc# { int level/n0; int victim/n0; public void loc#() { !or (int " = 1; " 4 n; " ) { level/i0 = "; victim/"0 = i;

while (( # 5= i) level/#0 6= ") 22 victim/"0 == i);

}} public void release(int i) { level/i0 = 0; }}


Art of Multiprocessor Programming

/i)e #riority to anyone $ut me


BC

<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

while (( # 5= i) level/#0 6= ") 22 victim/"0 == i);

}} public void release(int i) { level/i0 = 0; }}


Art of Multiprocessor Programming B)

<ilter
class 3ilter implements "oc# { int level/n0; int victim/n0; public void loc#() { !or (int " = 1; " 4 n; " ) { level/i0 = "; victim/"0 = i;

while (( # 5= i) level/#0 6= ") 22 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

Art of Multiprocessor Programming

#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(

Art of Multiprocessor Programming

Proof Structure
ncs

A
Last to write )ictim[L]

Assumed to enter L+* n+L1* = , n+L1* = ,

cs

y way of contradiction all enter L

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

0 Hut +hat about fairnessI


? Threads can be o erta4en by others

Art of Multiprocessor Programming

C%

Hounded 1aiting
0 1ant stronger fairness guarantees 0 Thread not 5o erta4en6 too much 0 Need to ad"ust definitions F7

Art of Multiprocessor Programming

C>

Hounded 1aiting
0 .i ide loc#() method into ( parts9
? .oor+ay inter al9
0 1ritten 'A 0 al+ays finishes in finite steps

? 1aiting inter al9


0 1ritten WA 0 may ta4e unbounded steps

Art of Multiprocessor Programming

C$

<irstACome <irstASer ed
0 <or threads A and H9
? #f 'Ak '
2

0 A2s 4Ath door+ay precedes H2s "Ath door+ay

? Then !SAk !S 2
0 A2s 4Ath critical section precedes H2s "Ath critical section 0 H cannot o erta4e A

Art of Multiprocessor Programming

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

Art of Multiprocessor Programming

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

0 #f a R b' or a @ b and i R "


Art of Multiprocessor Programming C)

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; } } &

Art of Multiprocessor Programming

)&

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

Art of Multiprocessor Programming

);

Ha4ery Algorithm
class 7a#er% & public void !lag/i0 = label/i0 = implements "oc# { loc#() { true; ma)(label/00* &*label/n(10) 1;

while ( # !lag/#0 22 (label/i0*i) 6 (label/#0*#));

Art of Multiprocessor Programming

)(

Ha4ery Algorithm
class 7a#er% & public void !lag/i0 = label/i0 = implements "oc# {

'oorway

while ( # !lag/#0 22 (label/i0*i) 6 (label/#0*#));

loc#() { true; ma)(label/00* &*label/n(10) 1;

Art of Multiprocessor Programming

)8

Ha4ery Algorithm
class 7a#er% & public void !lag/i0 = label/i0 = implements "oc# {

"&m interested

while ( # !lag/#0 22 (label/i0*i) 6 (label/#0*#));

loc#() { true; ma)(label/00* &*label/n(10) 1;

Art of Multiprocessor Programming

)%

Ha4ery Algorithm
class 7a#er% & public void !lag/i0 = label/i0 = implements "oc# {

while ( # !lag/#0 22 (label/i0*i) 6 (label/#0*#));

loc#() { true; ma)(label/00* &*label/n(10) 1;

.ake increasing la$el (read la$els in some ar$itrary order)

Art of Multiprocessor Programming

)>

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

while ( # !lag/#0 22 (label/i0*i) 6 (label/#0*#));

Art of Multiprocessor Programming

)$

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

With lower (la$el0i) in le5icogra#hic order


Art of Multiprocessor Programming )B

Ha4ery Algorithm
class 7a#er% implements "oc# { & public void unloc#() { !lag/i0 = !alse; } }

Art of Multiprocessor Programming

)C

Ha4ery Algorithm
class 7a#er% implements "oc# { & public void unloc#() { !lag/i0 = !alse; } }

%o longer interested

la$els are always increasing

Art of Multiprocessor Programming

))

No .eadloc4
0 There is al+ays one thread +ith earliest label 0 Ties are impossible -+hyI/

Art of Multiprocessor Programming

;&&

<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*#)); }

0 So H is loc4ed out +hile flagMAN is Art of Multiprocessor true


Programming

;&;

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

Art of Multiprocessor Programming

;&8

Mutual Exclusion
0 =abels are strictly increasing so 0 H must ha e seen flagMAN @@ false 0 =abelingH readH-flagMAN/ +riteA-flagMAN/ =abelingA

Art of Multiprocessor Programming

;&%

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;

while ( # !lag/#0 22 (label/i0*i) 6 (label/#0*#));

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;

6ute5 $reaks if label/i0 o)erflows

while ( # !lag/#0 22 (label/i0*i) 6 (label/#0*#));

Art of Multiprocessor Programming

;&B

.oes G erflo+ Actually MatterI


0 Des
? D(L ? ;C Panuary (&8C -*nix time8t rollo er/ ? ;$Abit counters ? $%Abit counters ? 8(Abit counters
Art of Multiprocessor Programming ;&C

0 No

0 Maybe

#s this Gptimal SolutionI


0 The Ha4ery Algorithm is
? Succinct' ? Elegant' and ? <air7

0 S9 So +hy isn2t it practicalI 0 A9 1ell' you ha e to read N distinct ariables


Art of Multiprocessor Programming ;&)

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

Art of Multiprocessor Programming

;;&

Theorem
At least N M!S1 -multiA reader3singleA+riter/ registers are needed to sol e deadloc4Afree mutual exclusion7 N registers li4e <lagMNF

Art of Multiprocessor Programming

;;;

Pro ing Algorithmic #mpossibility


0To sho+ no algorithm exists9 0 assume by +ay of contradiction

!
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 ;;(

Proof9 Need NAM!S1 !egisters


8ach thread must write to some register A
write

!
write

!S

!S

!S

9can&t tell whether A is in critical section


Art of Multiprocessor Programming ;;8

*pper Hound
0 Ha4ery algorithm
? *ses -% M!S1 registers

0 So the bound is -pretty/ tight 0 Hut +hat if +e use M!M1 registersI


? =i4e )ictim[] I

Art of Multiprocessor Programming

;;%

Had Ne+s Theorem


At least N M!M1 multiA reader3multiA+riter registers are needed to sol e deadloc4Afree mutual exclusion7

(So multi#le writers don&t hel#)


Art of Multiprocessor Programming ;;>

Theorem -<or (Threads/


Theorem9 .eadloc4Afree mutual exclusion for ( threads reEuires at least ( multiAreader multiA+riter registers Proof9 assume one register suffices and deri e a contradiction

Art of Multiprocessor Programming

;;$

T+o Thread Execution


A

Write(:)

:
!S

!S

0 Threads run' reading and +riting ! 0 .eadloc4 free so at least one gets in
Art of Multiprocessor Programming ;;B

Co ering State for Gne !egister Al+ays Exists

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

A runs' possibly +rites to the register' enters CS


Art of Multiprocessor Programming ;;)

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

Art of Multiprocessor Programming

;(;

Proof9 Assume Co er of (
A !

Write(: )

Write(:!)

Gnly ( registers
Art of Multiprocessor Programming ;((

!un A Solo
A !

Write(: )

Write(:!)

1rites to one or both !S registers' enters CS


Art of Multiprocessor Programming ;(8

Gbliterate Traces of A
A !

Write(: )

Write(:!)

!S

Gther threads obliterate e idence that A entered CS


Art of Multiprocessor Programming ;(%

Mutual Exclusion <ails


A !

Write(: )

Write(:!)

!S

!S

CS loo4s empty' so another thread gets in


;(>

Art of Multiprocessor Programming

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

Art of Multiprocessor Programming

;($

Co ering State for T+o


A

Write(:A)

Write(: )

0 #f +e run H through CS 8 times' H must return t+ice to co er some register' say !H


Art of Multiprocessor Programming ;(B

Co ering State for T+o


A

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

Co ering State for T+o


A

Write(:A)

Write(: )

0 NGJ A could ha e +ritten to !H 0 So CS no longer loo4s empty


Art of Multiprocessor Programming ;()

Co ering State for T+o


A

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&

#nducti ely 1e Can Sho+


A !

Write(:A)

Write(: )

Write(:!)

0 There is a co ering state

? 1here k threads not in CS co er k distinct registers ? Proof follo+s +hen k = %+*


Art of Multiprocessor Programming ;8;

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

0 Need stronger hard+are operations


? that do not ha e the ;co)ering #ro$lem<

Art of Multiprocessor Programming

;88

Muddy children2s pu,,le -Common Lno+ledge/


0 A group of 4ids are playing7 A stranger +al4s by and announces 5Some of you ha e mud on your forehead 0 Each 4id can see e eryone else2s forehead' but not his3her o+n -and they don2t tal4 to one another/ 0 Stranger says 5!aise your hand if you conclude that you ha e mud on your forehead67 Nobody does7 0 Stranger 4eeps on repeating the statement7 0 #f 4 4ids ha e muddy foreheads' then exactly these 4 4ids raise their hands after the stranger repeats the statement exactly 4 times
Art of Multiprocessor Programming ;8%

Вам также может понравиться