Title: THIRD PART Algorithms for Concurrent Distributed Systems: The Mutual Exclusion problem
1THIRD PARTAlgorithms for Concurrent
Distributed SystemsThe Mutual Exclusion problem
2Shared Memory Model
- Changes to the model from the MPS
- Processors communicate via a set of shared
variables, instead of passing messages - Each shared variable has a type, defining a set
of operations that can be performed atomically
(i.e., instantaneously, without interferences) - No inbuf and outbuf state components
- Configuration includes a value for each shared
variable - The only event type is a computation step by a
processor
3Shared Memory
processes
4Types of Shared Variables
- Read/Write
- Read-Modify-Write
- Test Set
- Fetch-and-add
- Compare-and-swap
- .
- .
- .
We will focus on the Read/Write type (the
simplest one to be realized)
5Read/Write Variables
Read(v)
Write(v,a)
v a
return(v)
- In one atomic step a processor can
- read the variable, or
- write the variable
- but not both!
6write 10
7write 10
10
8read
write 10
10
9read
write 10
10
10
10Simultaneous writes
write 20
write 10
11Simultaneous writes are scheduled
Possibility 1
write 20
write 10
10
12Simultaneous writes are scheduled
Possibility 2
write 20
write 10
20
13Simultaneous writes are scheduled
In general
write
write
x?a1,,ak
write
write
14Simultaneous Reads no problem!
read
read
a
a
a
All read the same value
15A more powerful typeRead-Modify-Write Variables
function on v
Atomic operation
RMW(v, f) temp v v f(v) return (temp)
Remark a R/W type cannot simulate a RMW type,
since reads and writes might interleave!
16Computation 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's semantics - pi 's state in new configuration changes
according to its old state and the result of the
operation
17Assumptions on the execution
18Mutual Exclusion (Mutex) 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
19Mutex 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
20Mutex 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 certain
number of times. - These conditions are increasingly strong.
21Mutex 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
22Complexity 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)
23Mutex Results Using R/W
number of distinct vars. upper bound lower bound
no deadlock n
no lockout 3n booleans (tournament alg.)
bounded waiting 2n unbounded (bakery alg.)
24Bakery Algorithm
- Guaranteeing
- Mutual exclusion
- Bounded waiting
- Using 2n shared read/write variables
- booleans Choosingi initially false, written by
pi and read by others - integers Numberi initially 0, written by pi
and read by others
25Bakery Algorithm
- Code for entry section
- Choosingi true
- Numberi maxNumber0,...,
- Numbern-1 1
- Choosingi false
- for j 0 to n-1 (except i) do
- wait until Choosingj false
- wait until Numberj 0 or
- (Numberj,j) gt (Numberi,i)
- endfor
- Code for exit section
- Numberi 0
26BA Provides Mutual Exclusion
- Lemma 1 If pi is in the critical section, then
Numberi gt 0. - Proof Trivial.
- Lemma 2 If pi is in the critical section and
Numberk ? 0 (k ? i), then (Numberk,k) gt
(Numberi,i). - Proof Since pi is in the CS, it passed the
second wait statement for jk. There are two
cases
pi 's most recent read of Numberk Case 1
returns 0 Case 2 returns (Numberk,k) gt
(Numberi,i)
pi in CS and Numberk ? 0
27Case 1
28Case 2
pi's most recent read of Numberk returns
(Numberk,k)gt(Numberi,i). So pk has already
taken its number.
So pk chooses a number not less than that of pi
in this interval, and does not change it until pi
exits from the CS
END of PROOF
29Mutual Exclusion for BA
- Mutual Exclusion Suppose pi and pk are
simultaneously in CS. - By Lemma 1, both have number gt 0.
- By Lemma 2,
- (Numberk,k) gt (Numberi,i) and
- (Numberi,i) gt (Numberk,k)
Contradiction!
30No Lockout for BA
- Assume in contradiction there is a starved
processor. - Starved processors are stuck at the wait
statements, not while choosing a number. - Let pi be a starved processor with smallest
(Numberi,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 number eventually
enters CS (not starved) and exits. - Thus pi cannot be stuck at the wait statements.
Contradiction!
31What about bounded waiting?
- YES Its 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).
32Space 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?
33Bounded 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
- W0 initially 0, written by p0 and read by p1
- W1 initially 0, written by p1 and read by p0
- Asymmetric code p0 always has priority over p1
34Bounded 2-Processor ME Algorithm with ND
- Code for p0 's entry section
- .
- .
- W0 1
- .
- .
- wait until W1 0
- Code for p0 's exit section
- .
- W0 0
35Bounded 2-Processor ME Algorithm with ND
- Code for p1 's entry section
- W1 0
- wait until W0 0
- W1 1
- .
- if (W0 1) then goto Line 1
- .
- Code for p1 's exit section
- .
- W1 0
36Discussion of 2-Processor ND Algorithm
- Satisfies mutual exclusion processors use W
variables to make sure of this - Satisfies no deadlock
- But unfair w.r.t. p1 (lockout)
- Fix by having the processors alternate in having
the priority
37Bounded 2-Processor ME Algorithm with NL
- Uses 3 binary shared read/write variables
- W0 initially 0, written by p0 and read by p1
- W1 initially 0, written by p1 and read by p0
- Priority initially 0, written and read by both
38Bounded 2-Processor ME Algorithm with NL
- Code for pis entry section
- Wi 0
- wait until W1-i 0 or Priority i
- Wi 1
- if (Priority 1-i) then
- if (W1-i 1) then goto Line 1
- else wait until (W1-i 0)
- Code for pis exit section
- Priority 1-i
- Wi 0
39Analysis ME
- Mutual Exclusion Suppose in contradiction p0
and p1 are simultaneously in CS.
Contradiction!
40Analysis No-Deadlock
- Useful for showing no-lockout.
- If one proc. ever enters remainder forever, other
one cannot be starved. - Ex If p1 enters remainder forever, then p0 will
keep seeing W1 0. - So any deadlock would starve both procs. in the
entry section
41Analysis 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.
Contradiction!
42Analysis 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.
Contradiction!
43Bounded 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.
44Bounded n-Processor ME Alg.
- Can we get a bounded space NL mutex algorithm for
ngt2 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
45Tournament Tree
1
2
3
5
6
7
4
p0, p1
p2, p3
p4, p5
p6, p7
46Tournament 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.
47The code
48More 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?
49Tournament 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.