third part algorithms for concurrent distributed systems: the mutual exclusion problem

49
THIRD PART Algorithms for Concurrent Distributed Systems: The Mutual Exclusion problem

Upload: shing

Post on 21-Jan-2016

35 views

Category:

Documents


0 download

DESCRIPTION

THIRD PART Algorithms for Concurrent Distributed Systems: The Mutual Exclusion problem. Concurrent Distributed Systems. Changes to the model from the MPS: - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: THIRD PART Algorithms for Concurrent Distributed Systems: The Mutual Exclusion problem

THIRD PART

Algorithms for Concurrent Distributed Systems:

The Mutual Exclusion problem

Page 2: THIRD PART Algorithms for Concurrent Distributed Systems: The Mutual Exclusion problem

Concurrent Distributed Systems

Changes to the model from the MPS:– MPS basically models a distributed system in

which processors needs to coordinate to perform a widesystem goal (e.g., electing their leader)

– Now, we turn our attention to concurrent systems, where the processors run in parallel but without necessarily cooperating (for instance, they might just be a set of laptops in a LAN)

Page 3: THIRD PART Algorithms for Concurrent Distributed Systems: The Mutual Exclusion problem

Shared Memory ModelChanges to the model from the MPS:

– No cooperation no communication channels between processors and no inbuf and outbuf state components

– Processors communicate via a set of shared variables, instead of passing messages no any communication graph!

– Each shared variable has a type, defining a set of operations that can be performed atomically (i.e., instantaneously, without interferences)

Page 4: THIRD PART Algorithms for Concurrent Distributed Systems: The Mutual Exclusion problem

Shared Memory

1p

3p

2p

4p

processors

Page 5: THIRD PART Algorithms for Concurrent Distributed Systems: The Mutual Exclusion problem

Types of Shared Variables1. Read/Write2. Read-Modify-Write3. Test & Set4. Fetch-and-add5. Compare-and-swap

.

.

.

We will focus on the Read/Write type (the simplest one to be realized)

Page 6: THIRD PART Algorithms for Concurrent Distributed Systems: The Mutual Exclusion problem

Read/Write VariablesRead(v) Write(v,a)

return(v); v = a;

In one atomic step a processor can:– read the variable, or– write the variable… but not both!

Page 7: THIRD PART Algorithms for Concurrent Distributed Systems: The Mutual Exclusion problem

1p write 10

Page 8: THIRD PART Algorithms for Concurrent Distributed Systems: The Mutual Exclusion problem

1p write 1010

Page 9: THIRD PART Algorithms for Concurrent Distributed Systems: The Mutual Exclusion problem

1p write 1010

2pread

Page 10: THIRD PART Algorithms for Concurrent Distributed Systems: The Mutual Exclusion problem

1p write 1010

2pread

10

Page 11: THIRD PART Algorithms for Concurrent Distributed Systems: The Mutual Exclusion problem

1p write 10 2pwrite 20

Simultaneous writes

Page 12: THIRD PART Algorithms for Concurrent Distributed Systems: The Mutual Exclusion problem

1p write 10 2pwrite 20

Possibility 1

10

Simultaneous writes are scheduled:

Page 13: THIRD PART Algorithms for Concurrent Distributed Systems: The Mutual Exclusion problem

1p write 10 2pwrite 20

Possibility 2

20

Simultaneous writes are scheduled:

Page 14: THIRD PART Algorithms for Concurrent Distributed Systems: The Mutual Exclusion problem

1p write 2pwrite

In general:

1a2a

3pwrite 3a

kpwrite ka

xє{a1,…,ak}

Simultaneous writes are scheduled:

Page 15: THIRD PART Algorithms for Concurrent Distributed Systems: The Mutual Exclusion problem

1p read 2pread

Simultaneous Reads: no problem!

aa a

All read the same value

Page 16: THIRD PART Algorithms for Concurrent Distributed Systems: The Mutual Exclusion problem

Computation Step in the Shared Memory Model

• When processor pi takes a step:– pi 's state in old configuration specifies

which shared variable is to be accessed and with which operation

– operation is done: shared variable's value in the new configuration changes according to the operation

– pi 's state in new configuration changes according to its old state and the result of the operation

Page 17: THIRD PART Algorithms for Concurrent Distributed Systems: The Mutual Exclusion problem

The mutual exclusion (mutex) problem

–The main challenge in managing concurrent systems is coordinating access to resources that are shared among processes

–Assumptions on the system:•Non-anonymous•Asynchronous•Fault-free

Page 18: THIRD PART Algorithms for Concurrent Distributed Systems: The Mutual Exclusion problem

• Each processor's code is divided into four sections:

– entry: synchronize with others to ensure mutually exclusive access to the …

– critical: use some resource; when done, enter the…

– exit: clean up; when done, enter the…– remainder: not interested in using the resource

Mutex code sections

entry

critical

exit

remainder

Page 19: THIRD PART Algorithms for Concurrent Distributed Systems: The Mutual Exclusion problem

Mutex Algorithms

• A mutual exclusion algorithm specifies code for entry and exit sections to ensure:– mutual exclusion: at most one

processor is in its critical section at any time, and

– some kind of liveness condition. There are three commonly considered ones…

Page 20: THIRD PART Algorithms for Concurrent Distributed Systems: The Mutual Exclusion problem

Mutex Liveness Conditions

• no deadlock: if a processor is in its entry section at some time, then later some processor is in its critical section

• no lockout: if a processor is in its entry section at some time, then later the same processor is in its critical section

• bounded waiting: no lockout + while a processor is in its entry section, any other processor can enter the critical section no more than a bounded number of times.

These conditions are increasingly strong.

Page 21: THIRD PART Algorithms for Concurrent Distributed Systems: The Mutual Exclusion problem

Mutex Algorithms: assumptions

• The code for the entry and exit sections is allowed to assume that– no processor stays in its critical section

forever– shared variables used in the entry and

exit sections are not accessed during the critical and remainder sections

Page 22: THIRD PART Algorithms for Concurrent Distributed Systems: The Mutual Exclusion problem

Complexity Measure for Mutex

• Main complexity measure of interest for shared memory mutex algorithms is amount of shared space needed.

• Space complexity is affected by:– how powerful is the type of the shared

variables– how strong is the progress property to

be satisfied (no deadlock vs. no lockout vs. bounded waiting)

Page 23: THIRD PART Algorithms for Concurrent Distributed Systems: The Mutual Exclusion problem

Mutex Results Using R/W

LivenessCondition

upper bound lower bound

no deadlock n

no lockout 3n booleans(tournament alg.)

bounded waiting

2n unbounded(bakery alg.)

Page 24: THIRD PART Algorithms for Concurrent Distributed Systems: The Mutual Exclusion problem

Bakery Algorithm

• Guaranteeing:– Mutual exclusion– Bounded waiting

• Using 2n shared read/write variables– booleans Choosing[i]: initially false,

written by pi and read by others

– integers Number[i]: initially 0, written by pi and read by others

Page 25: THIRD PART Algorithms for Concurrent Distributed Systems: The Mutual Exclusion problem

Bakery AlgorithmCode for entry section:

Choosing[i] := trueNumber[i] := max{Number[0],...,

Number[n-1]} + 1Choosing[i] := falsefor j := 0 to n-1 (except i) do

wait until Choosing[j] = falsewait until Number[j] = 0 or (Number[j],j) > (Number[i],i)

endfor

Code for exit section:Number[i] := 0

Page 26: THIRD PART Algorithms for Concurrent Distributed Systems: The Mutual Exclusion problem

BA Provides Mutual Exclusion

Lemma 1: If pi is in the critical section, then Number[i] > 0.Proof: Trivial.Lemma 2: If pi is in the critical section and Number[k] ≠ 0

(k ≠ i), then (Number[k],k) > (Number[i],i). Proof: Observe that a chosen number changes only after

exiting from the CS. Since pi is in the CS, it passed the second wait statement for j=k. There are two cases:

pi in CS andNumber[k] ≠ 0

pi 's most recentread of Number[k];Case 1: returns 0Case 2: returns (Number[k],k) > (Number[i],i)

Page 27: THIRD PART Algorithms for Concurrent Distributed Systems: The Mutual Exclusion problem

Case 1

pi in CS andNumber[k] ≠ 0

pi's most recentread of Number[k],returns 0.So pk is in remainderor choosing number.

pi's most recentread of Choosing[k],returns false. So pk is not in the middleof choosing number.

pi's most recentwrite to Number[i]

So pk chooses its number in this interval,sees pi's number, and chooses a larger one (and so it will never enter in CS before pi, which means that its number cannot change before pi exits from the CS, and so the claim is true)

Page 28: THIRD PART Algorithms for Concurrent Distributed Systems: The Mutual Exclusion problem

Case 2

pi in CS andNumber[k] ≠ 0

pi's most recentread of Number[k] returns (Number[k],k)>(Number[i],i).So pk has already taken its number.

So pk chooses a number not less than that of pi in this interval. Observe that it maybe pk finished its choice before than pi, but since (Number[k],k)>(Number[i],i), it must be Number[i]≤Number[k]. Thus, pk will never enter in CS before pi. Indeed, either pk will be stopped by pi in the first wait statement (in case Number[i]=Number[k] but pi is still choosing its number), or in the second one (in case Number[i]<Number[k]), and so the claim is true

END of PROOF

Page 29: THIRD PART Algorithms for Concurrent Distributed Systems: The Mutual Exclusion problem

Mutual Exclusion for BA

• Mutual Exclusion: Suppose pi and pk are simultaneously in CS.– By Lemma 1, both have number > 0.– By Lemma 2,

•(Number[k],k) > (Number[i],i) and •(Number[i],i) > (Number[k],k)

Page 30: THIRD PART Algorithms for Concurrent Distributed Systems: The Mutual Exclusion problem

No Lockout for BA• Assume in contradiction there is a starved processor.• Starved processors are stuck at the wait statements,

not while choosing a number.• Starved processors can be stuck only at the second

wait statement, clearly.• Let pi be a starved processor with smallest

(Number[i],i).• Any processor entering entry section after pi has

chosen its number, chooses a larger number, and therefore cannot overtake pi

• Every processor with a smaller ticket eventually enters CS (not starved) and exits, setting its number to 0.

• Thus pi cannot be stuck at the second wait statement forever by another processor.

Page 31: THIRD PART Algorithms for Concurrent Distributed Systems: The Mutual Exclusion problem

What about bounded waiting?

YES: It’s easy to see that any processor in the entry section can be overtaken at most once by any other processor (and so in total it can be overtaken at most n-1 times).

Page 32: THIRD PART Algorithms for Concurrent Distributed Systems: The Mutual Exclusion problem

Space Complexity of BA

• Number of shared variables is 2n

· Choosing variables are booleans

· Number variables are unbounded: as long as the CS is occupied and some processor enters the entry section, the ticket number increases

• Is it possible for an algorithm to use less shared space?

Page 33: THIRD PART Algorithms for Concurrent Distributed Systems: The Mutual Exclusion problem

Bounded 2-Processor ME Algorithm with ND

• Start with a bounded algorithm for 2 processors with ND, then extend to NL, then extend to n processors.

• Uses 2 binary shared read/write variables:W[0]: initially 0, written by p0 and read by p1

W[1]: initially 0, written by p1 and read by p0

• Asymmetric code: p0 always has priority over p1

Page 34: THIRD PART Algorithms for Concurrent Distributed Systems: The Mutual Exclusion problem

Bounded 2-Processor ME Algorithm with ND

Code for p0 's entry section:1 .2 .3 W[0] := 14 .5 .6 wait until W[1] = 0

Code for p0 's exit section:7 .8 W[0] := 0

Page 35: THIRD PART Algorithms for Concurrent Distributed Systems: The Mutual Exclusion problem

Bounded 2-Processor ME Algorithm with ND

Code for p1 's entry section:1 W[1] := 02 wait until W[0] = 03 W[1] := 14 .5 if (W[0] = 1) then goto Line 16 .

Code for p1 's exit section:7 .8 W[1] := 0

Page 36: THIRD PART Algorithms for Concurrent Distributed Systems: The Mutual Exclusion problem

Discussion of 2-Processor ND Algorithm

• Satisfies mutual exclusion: processors use W variables to make sure of this (a processor enters only when the other W variable is set to 0)

• Satisfies no deadlock

• But unfair w.r.t. p1 (lockout)

• Fix by having the processors alternate in having the priority

Page 37: THIRD PART Algorithms for Concurrent Distributed Systems: The Mutual Exclusion problem

Bounded 2-Processor ME Algorithm with NL

Uses 3 binary shared read/write variables:

• W[0]: initially 0, written by p0 and read by p1

• W[1]: initially 0, written by p1 and read by p0

• Priority: initially 0, written and read by both

Page 38: THIRD PART Algorithms for Concurrent Distributed Systems: The Mutual Exclusion problem

Bounded 2-Processor ME Algorithm with NL

Code for pi’s entry section:

1 W[i] := 02 wait until W[1-i] = 0 or Priority = i3 W[i] := 14 if (Priority = 1-i) then5 if (W[1-i] = 1) then goto Line 16 else wait until (W[1-i] = 0)

Code for pi’s exit section:

7 Priority := 1-i8 W[i] := 0

Page 39: THIRD PART Algorithms for Concurrent Distributed Systems: The Mutual Exclusion problem

Analysis: MEMutual Exclusion: Suppose in contradiction p0 and p1 are simultaneously

in CS. W.l.o.g., assume p1 last write of W before entering CS happens not later than p0 last write of W before entering CS

W[0]=W[1]=1,both procs in CS

p1's mostrecent writeof 1 to W[1](Line 3)

p0 's mostrecent writeof 1 to W[0](Line 3)

p0 's most recent read of W[1] before entering CS (Line 5 or 6):must return 1, not 0!

Page 40: THIRD PART Algorithms for Concurrent Distributed Systems: The Mutual Exclusion problem

Analysis: No-Deadlock

• Useful for showing no-lockout.• If one processor ever enters

remainder forever, other one cannot be starved.– Ex: If p1 enters remainder forever, then

p0 will keep seeing W[1] = 0.

• So any deadlock would starve both processors in the entry section

Page 41: THIRD PART Algorithms for Concurrent Distributed Systems: The Mutual Exclusion problem

Analysis: No-Deadlock

• Suppose in contradiction there is deadlock.• W.l.o.g., suppose Priority gets stuck at 0

after both processors are stuck in their entry sections.

p0 and p1

stuck in entryPriority=0

p0 not stuckin Line 2, skipsLine 5, stuck in Line 6 with W[0]=1waiting forW[1] to be 0

p0 seesW[1]=0,enters CS

p1 sent back inLine 5, stuckat Line 2 withW[1]=0, waitingfor W[0] to be 0

Page 42: THIRD PART Algorithms for Concurrent Distributed Systems: The Mutual Exclusion problem

Analysis: No-Lockout• Suppose in contradiction p0 is starved.• Since there is no deadlock, p1 enters CS infinitely

often.• The first time p1 executes Line 7 in exit section

after p0 is stuck in entry, Priority gets stuck at 0.

p1 at Line 7;Priority=0forever after

p0 stuckin entry

p0 stuck atLine 6 withW[0]=1, waitingfor W[1] to be 0

p1 entersentry, getsstuck atLine 2, waitingfor W[0] to be 0: it cannot reenter in CS!DEADLOCK!

Page 43: THIRD PART Algorithms for Concurrent Distributed Systems: The Mutual Exclusion problem

Bounded Waiting?

• NO: A processor, even if having priority, might be overtaken repeatedly (in principle, an unbounded number of times) when it is in between Line 2 and 3.

Page 44: THIRD PART Algorithms for Concurrent Distributed Systems: The Mutual Exclusion problem

Bounded n-Processor ME Alg.

• Can we get a bounded space NL mutex algorithm for n>2 processors?

• Yes!• Based on the notion of a tournament tree:

complete binary tree with n-1 nodes– tree is conceptual only! does not represent

message passing channels

• A copy of the 2-proc. algorithm is associated with each tree node– includes separate copies of the 3 shared

variables

Page 45: THIRD PART Algorithms for Concurrent Distributed Systems: The Mutual Exclusion problem

Tournament Tree

1

2 3

4 5 6 7

p0, p1 p2, p3 p4, p5 p6, p7

Page 46: THIRD PART Algorithms for Concurrent Distributed Systems: The Mutual Exclusion problem

Tournament Tree ME Algorithm

• Each proc. begins entry section at a specific leaf (two procs per leaf)

• A proc. proceeds to next level in tree by winning the 2-proc. competition for current tree node:– on left side, play role of p0

– on right side, play role of p1

• When a proc. wins the 2-proc. algorithm associated with the tree root, it enters CS.

Page 47: THIRD PART Algorithms for Concurrent Distributed Systems: The Mutual Exclusion problem

The code

Page 48: THIRD PART Algorithms for Concurrent Distributed Systems: The Mutual Exclusion problem

More on Tournament Tree Alg.

• Code is recursive• pi begins at tree node 2k + i/2, playing

role of pi mod 2, where k = log n -1.• After winning node v, "critical section"

for node v is– entry code for all nodes on path from v/2

to root– real critical section– exit code for all nodes on path from root to

v/2

Page 49: THIRD PART Algorithms for Concurrent Distributed Systems: The Mutual Exclusion problem

Tournament Tree Analysis• Correctness: based on correctness of 2-

processor algorithm and tournament structure:– ME for tournament alg. follows from ME for 2-

proc. alg. at tree root.– NL for tournament alg. follows from NL for the

2-proc. algs. at all nodes of tree

• Space Complexity: 3n boolean read/write shared variables.

• Bounded Waiting? No, as for the 2-processor algorithm.