+ All Categories
Home > Documents > Hwajung Lee. Mutual Exclusion CS p0 p1 p2 p3 Some applications are: Resource sharing Avoiding...

Hwajung Lee. Mutual Exclusion CS p0 p1 p2 p3 Some applications are: Resource sharing Avoiding...

Date post: 19-Jan-2016
Category:
Upload: charlene-bruce
View: 217 times
Download: 0 times
Share this document with a friend
Popular Tags:
35
Hwajung Lee
Transcript
Page 1: Hwajung Lee. Mutual Exclusion CS p0 p1 p2 p3 Some applications are:  Resource sharing  Avoiding concurrent update on shared data  Controlling the.

Hwajung Lee

Page 2: Hwajung Lee. Mutual Exclusion CS p0 p1 p2 p3 Some applications are:  Resource sharing  Avoiding concurrent update on shared data  Controlling the.

Mutual Exclusion

CS

CS

CS

CSp0

p1

p2

p3

Page 3: Hwajung Lee. Mutual Exclusion CS p0 p1 p2 p3 Some applications are:  Resource sharing  Avoiding concurrent update on shared data  Controlling the.

Some applications are:

Resource sharing Avoiding concurrent update on shared data

Medium Access Control in Ethernet Collision avoidance in wireless broadcasts

Page 4: Hwajung Lee. Mutual Exclusion CS p0 p1 p2 p3 Some applications are:  Resource sharing  Avoiding concurrent update on shared data  Controlling the.

ME1. [Mutual Exclusion] At most one process in the CS.(Safety property)ME2. [Freedom from Deadlock] No deadlock. (Safety property)ME3. [Progress] Every process trying to enter its CS must eventually succeed. (Liveness property)

Violation of ME3 is livelock (=starvation)

Progress is quantified by the criterion of bounded waiting. It measures a form of fairness by answering the question: Between two consecutive CS trips by one process, how many times other processes can enter the CS?

There are many solutions, both on the shared memory model and the message-passing model

Page 5: Hwajung Lee. Mutual Exclusion CS p0 p1 p2 p3 Some applications are:  Resource sharing  Avoiding concurrent update on shared data  Controlling the.

clients

Clientdo true

send request;reply received enter CS;send release;<other work>

od

Serverdo request received and not busy send reply; busy:= true request received and busy enqueue sender release received and queue is empty busy:= false release received and queue not empty send reply

to the head of the queueod

busy: boolean

server

queue

req replyrelease

Page 6: Hwajung Lee. Mutual Exclusion CS p0 p1 p2 p3 Some applications are:  Resource sharing  Avoiding concurrent update on shared data  Controlling the.

Centralized solution is simple. But the server is a single point of failure. This is BAD. ME1-ME3 is satisfied, but FIFO fairness is not guaranteed.

Why?

Can we do better? Yes!

Page 7: Hwajung Lee. Mutual Exclusion CS p0 p1 p2 p3 Some applications are:  Resource sharing  Avoiding concurrent update on shared data  Controlling the.

{Lamport’s algorithm}

1. To request entry into its CS, broadcast a timestamped request to all.

2. At each process i: Request received enqueue it in local Q. If process i is not in CS send ack, else postpone sending ack until it exit from CS.

3. Enter CS, when (i) You are at the head of your Q(ii) You have received ack from all

4. To exit from the CS, (i) Delete the request from your Q, and (ii) Broadcast a timestamped release

5. When a process receives a release message, it removes the sender from its Q.

0 1

2 3

Q0 Q1

Q2 Q3

Completely connected topology

Page 8: Hwajung Lee. Mutual Exclusion CS p0 p1 p2 p3 Some applications are:  Resource sharing  Avoiding concurrent update on shared data  Controlling the.

Can you show that it satisfies all the properties(i.e. ME1, ME2, ME3) of a correct solution?

Observation. Processes making a decision to enter CS must have identical views of their local queues, when all acks have been received.

Proof of ME1. At most one process can be in its CS at any time.

Proof by contradictionSuppose not, and both j, k enter their CS. This implies

j in CS Q.ts.j < Q.ts.k k in CS Q.ts.k < Q.ts.j

Impossible.

0 1

2 3

Q0 Q1

Q2 Q3

Page 9: Hwajung Lee. Mutual Exclusion CS p0 p1 p2 p3 Some applications are:  Resource sharing  Avoiding concurrent update on shared data  Controlling the.

Proof of ME2. (No deadlock) and ME3. (progress)

Basis. When process i makes a request, there may be at most n-1 processes ahead of process i in its request queue.

Inductive step. Assume K (1<= K <= n-1) processes ahead of process i in the request queue.

(1) # of process ahead (2) bounded number of steps to enter CS

0 1

2 3

Q0 Q1

Q2 Q3

Page 10: Hwajung Lee. Mutual Exclusion CS p0 p1 p2 p3 Some applications are:  Resource sharing  Avoiding concurrent update on shared data  Controlling the.

{Ricart & Agrawala’s algorithm}What is new?1. Broadcast a timestamped request to all.2. Upon receiving a request, send ack if

-You do not want to enter your CS, or -You are trying to enter your CS, but your timestamp is higher than that of the sender.(If you are already in CS, then buffer the request)

3. Enter CS, when you receive ack from all.4. Upon exit from CS, send ack to each pending request before making a new request.(No release message is necessary)

Page 11: Hwajung Lee. Mutual Exclusion CS p0 p1 p2 p3 Some applications are:  Resource sharing  Avoiding concurrent update on shared data  Controlling the.

{Ricart & Agrawala’s algorithm}

ME1. Prove that at most one process can be in CS.Proof (by contradiction)Suppose not. Two processes k and j can enter the CS at the

same time, only if both k and j received n-1 acks. However, both k and j cannot send ack to each other. Thus, impossible.

ME2. Prove that deadlock is not possible.ProofThe waiting chain is acyclic.

k waits for j k is behind j implicitly in the wait-for chain j does not wait for k

TS(j) < TS(k)

k j

Req(j)

Ack(j)Req(k)

Page 12: Hwajung Lee. Mutual Exclusion CS p0 p1 p2 p3 Some applications are:  Resource sharing  Avoiding concurrent update on shared data  Controlling the.

{Ricart & Agrawala’s algorithm}

ME3. Prove that FIFO fairness holds even if channels

are not FIFOProof. If TS(j) < TS(k), then process j is ranked higher

than (i.e., ahead of) k in the wait-for chain. Therefore, process j will enter the CS before process k.

Message complexity = 2(N-1)

(N-1 requests + N-1 acks - no release message)

TS(j) < TS(k)

k j

Req(j)

Ack(j)Req(k)

Page 13: Hwajung Lee. Mutual Exclusion CS p0 p1 p2 p3 Some applications are:  Resource sharing  Avoiding concurrent update on shared data  Controlling the.

{Maekawa’s algorithm}

- First solution with a sublinear O(√N) message complexity.

- “Close to” Ricart-Agrawala’s solution, but each process is required to obtain permission from only a subset of peers

Page 14: Hwajung Lee. Mutual Exclusion CS p0 p1 p2 p3 Some applications are:  Resource sharing  Avoiding concurrent update on shared data  Controlling the.

With each process i, associate a subset Si.Divide the set of processes into subsets that satisfy the following two conditions:i Sii,j : i,j n-1 :: Si Sj ≠

Main idea. Each process i is required to receive permission from Si only. Correctness requires that multiple processes will never receive permission from all members of their respective subsets.

1,2 1,5

2,5

S0S1

S2

Note: It divides processes into a number ofsubsets of identical size K. Every process i ispresent in the same number (D) of subsets.

Page 15: Hwajung Lee. Mutual Exclusion CS p0 p1 p2 p3 Some applications are:  Resource sharing  Avoiding concurrent update on shared data  Controlling the.

Example. Let there be seven processes 0, 1, 2, 3, 4, 5, 6

S0 = {0, 1, 2}S1 = {1, 3, 5}S2 = {2, 4, 5}S3 = {0, 3, 4}S4 = {1, 4, 6}S5 = {0, 5, 6}S6 = {2, 3, 6}

Page 16: Hwajung Lee. Mutual Exclusion CS p0 p1 p2 p3 Some applications are:  Resource sharing  Avoiding concurrent update on shared data  Controlling the.

Version 1 {Life of process I}

1. Send timestamped request to each process in Si.

2. Request received send ack to process with the lowest timestamp. Thereafter, "lock" (i.e. commit) yourself to that process, and keep others waiting.

3. Enter CS if you receive an ack from each member in Si.

4. To exit CS, send release to every process in Si.5. Release received unlock yourself. Then send

ack to the next process with the lowest timestamp.

S0 = {0, 1, 2}

S1 = {1, 3, 5}

S2 = {2, 4, 5}

S3 = {0, 3, 4}

S4 = {1, 4, 6}

S5 = {0, 5, 6}

S6 = {2, 3, 6}

Page 17: Hwajung Lee. Mutual Exclusion CS p0 p1 p2 p3 Some applications are:  Resource sharing  Avoiding concurrent update on shared data  Controlling the.

ME1. At most one process can enter its critical

section at any time.

Proof by contradiction

Let i and j attempt to enter their Critical Sections

Si ∩ Sj ≠ there is a process k Si ∩ Sj

Process k will never send ack to both.

So it will act as the arbitrator and establishes ME1

S0 = {0, 1, 2}

S1 = {1, 3, 5}

S2 = {2, 4, 5}

S3 = {0, 3, 4}

S4 = {1, 4, 6}

S5 = {0, 5, 6}

S6 = {2, 3, 6}

Page 18: Hwajung Lee. Mutual Exclusion CS p0 p1 p2 p3 Some applications are:  Resource sharing  Avoiding concurrent update on shared data  Controlling the.

ME2. No deadlock. Unfortunately deadlock is possible! Assume 0, 1, 2 want to enter their critical sections.

From S0= {0,1,2}, 0,2 send ack to 0, but 1

sends ack to 1;

From S1= {1,3,5}, 1,3 send ack to 1, but 5

sends ack to 2;

From S2= {2,4,5}, 4,5 send ack to 2, but 2

sends ack to 0; Now, 0 waits for 1, 1 waits for 2, and 2 waitsfor 0. So deadlock is possible!

S0 = {0, 1,

2}S1 = {1, 3,

5}S2 = {2, 4,

5}S3 = {0, 3,

4}S4 = {1, 4,

6}S5 = {0, 5,

6}S6 = {2, 3,

6}

Page 19: Hwajung Lee. Mutual Exclusion CS p0 p1 p2 p3 Some applications are:  Resource sharing  Avoiding concurrent update on shared data  Controlling the.

Avoiding deadlock If processes receive messages in

increasing order of timestamp, then deadlock “could be” avoided. But this is too strong an assumption.

Version 2 uses three additional messages:

- failed

- inquire

- relinquish

S0 = {0, 1, 2}

S1 = {1, 3, 5}

S2 = {2, 4, 5}

S3 = {0, 3, 4}

S4 = {1, 4, 6}

S5 = {0, 5, 6}

S6 = {2, 3, 6}

Page 20: Hwajung Lee. Mutual Exclusion CS p0 p1 p2 p3 Some applications are:  Resource sharing  Avoiding concurrent update on shared data  Controlling the.

New features in version 2

- Send ack and set lock as usual.- If lock is set and a request with larger

timestamp arrives, send failed (you have no chance). If the incoming request has a lower timestamp, then send inquire (are you in CS?) to the locked process.

- Receive inquire and at least one failed message send relinquish. The recipient resets the lock.

S0 = {0, 1, 2}

S1 = {1, 3, 5}

S2 = {2, 4, 5}

S3 = {0, 3, 4}

S4 = {1, 4, 6}

S5 = {0, 5, 6}

S6 = {2, 3, 6}

Page 21: Hwajung Lee. Mutual Exclusion CS p0 p1 p2 p3 Some applications are:  Resource sharing  Avoiding concurrent update on shared data  Controlling the.

0

1

2

34

5

6

12

18

0

1

2

34

5

6

12

18ack

req

req

inquire

25req failed

Page 22: Hwajung Lee. Mutual Exclusion CS p0 p1 p2 p3 Some applications are:  Resource sharing  Avoiding concurrent update on shared data  Controlling the.

- Let K = |Si|. Let each process be a member of D subsets. When N = 7, K = D = 3. When K=D, N = K(K-1)+1. So K is of the order √N

- The message complexity of Version 1 is 3√N. Maekawa’s analysis of Version 2 reveals a complexity of 7√N

O(√N) message complexity.

Page 23: Hwajung Lee. Mutual Exclusion CS p0 p1 p2 p3 Some applications are:  Resource sharing  Avoiding concurrent update on shared data  Controlling the.

Suzuki-Kasami algorithmThe Main idea

Completely connected network of processes

There is one token in the network. The holder of the token has the permission to enter CS.

Any other process trying to enter CS must acquire that token. Thus the token will move from one process to another based on demand.

I want to enter CSI want to enter CS

Page 24: Hwajung Lee. Mutual Exclusion CS p0 p1 p2 p3 Some applications are:  Resource sharing  Avoiding concurrent update on shared data  Controlling the.

Process i broadcasts (i, num)

Each process maintains-an array req: req[j] denotes the sequence num of the latest request from process j(Some requests will be stale soon)

Additionally, the holder of the token maintains-an array last: last[j] denotes the sequence number of the latest visit to CS from for process j.- a queue Q of waiting processes req: array[0..n-1] of integer

last: array [0..n-1] of integer

Sequence number of the request

req

req

req

req

reqlast

queue Q

Page 25: Hwajung Lee. Mutual Exclusion CS p0 p1 p2 p3 Some applications are:  Resource sharing  Avoiding concurrent update on shared data  Controlling the.

When a process i receives a request (k, num) from process k, it sets req[k] to max(req[k], num).

The holder of the token

--Completes its CS--Sets last[i]:= its own num--Updates Q by retaining each process k only if 1+ last[k] = req[k] (This guarantees the freshness of the request)--Sends the token to the head of Q, along with the array last and the tail of Q

In fact, token (Q, last)

Req: array[0..n-1] of integer

Last: Array [0..n-1] of integer

Page 26: Hwajung Lee. Mutual Exclusion CS p0 p1 p2 p3 Some applications are:  Resource sharing  Avoiding concurrent update on shared data  Controlling the.

{Program of process j}Initially, i: req[i] = last[i] = 0* Entry protocol *

req[j] := req[j] + 1Send (j, req[j]) to allWait until token (Q, last) arrivesCritical Section

* Exit protocol *last[j] := req[j]k ≠ j: k Q req[k] = last[k] + 1 append k to Q;if Q is not empty send (tail-of-Q, last) to head-of-Q fi

* Upon receiving a request (k, num) *req[k] := max(req[k], num)

Page 27: Hwajung Lee. Mutual Exclusion CS p0 p1 p2 p3 Some applications are:  Resource sharing  Avoiding concurrent update on shared data  Controlling the.

0

2

1

3

4

req=[1,0,0,0,0]last=[0,0,0,0,0]

req=[1,0,0,0,0]

req=[1,0,0,0,0]

req=[1,0,0,0,0]

req=[1,0,0,0,0]

initial state

Page 28: Hwajung Lee. Mutual Exclusion CS p0 p1 p2 p3 Some applications are:  Resource sharing  Avoiding concurrent update on shared data  Controlling the.

0

2

1

3

4

req=[1,1,1,0,0]last=[0,0,0,0,0]

req=[1,1,1,0,0]

req=[1,1,1,0,0]

req=[1,1,1,0,0]

req=[1,1,1,0,0]

1 & 2 send requests

Page 29: Hwajung Lee. Mutual Exclusion CS p0 p1 p2 p3 Some applications are:  Resource sharing  Avoiding concurrent update on shared data  Controlling the.

0

2

1

3

4

req=[1,1,1,0,0]last=[1,0,0,0,0]Q=(1,2)

req=[1,1,1,0,0]

req=[1,1,1,0,0]

req=[1,1,1,0,0]

req=[1,1,1,0,0]

0 prepares to exit CS

Page 30: Hwajung Lee. Mutual Exclusion CS p0 p1 p2 p3 Some applications are:  Resource sharing  Avoiding concurrent update on shared data  Controlling the.

0

2

1

3

4

req=[1,1,1,0,0]

req=[1,1,1,0,0]last=[1,0,0,0,0]Q=(2)

req=[1,1,1,0,0]

req=[1,1,1,0,0]

req=[1,1,1,0,0]

0 passes token (Q and last) to 1

Page 31: Hwajung Lee. Mutual Exclusion CS p0 p1 p2 p3 Some applications are:  Resource sharing  Avoiding concurrent update on shared data  Controlling the.

0

2

1

3

4

req=[2,1,1,1,0]

req=[2,1,1,1,0]last=[1,0,0,0,0]Q=(2,0,3)

req=[2,1,1,1,0]

req=[2,1,1,1,0]

req=[2,1,1,1,0]

0 and 3 send requests

Page 32: Hwajung Lee. Mutual Exclusion CS p0 p1 p2 p3 Some applications are:  Resource sharing  Avoiding concurrent update on shared data  Controlling the.

0

2

1

3

4

req=[2,1,1,1,0]

req=[2,1,1,1,0]

req=[2,1,1,1,0]last=[1,1,0,0,0]Q=(0,3)

req=[2,1,1,1,0]

req=[2,1,1,1,0]

1 sends token to 2

Message Complexity = (n-1) + 1 O(n)

Page 33: Hwajung Lee. Mutual Exclusion CS p0 p1 p2 p3 Some applications are:  Resource sharing  Avoiding concurrent update on shared data  Controlling the.

123 4 5

6 71,4

4,7

1

1

4

1,4,7 want to enter their CS

Page 34: Hwajung Lee. Mutual Exclusion CS p0 p1 p2 p3 Some applications are:  Resource sharing  Avoiding concurrent update on shared data  Controlling the.

123 4 5

6 71,4 4,7

1

4

3 sends the token to 6

Page 35: Hwajung Lee. Mutual Exclusion CS p0 p1 p2 p3 Some applications are:  Resource sharing  Avoiding concurrent update on shared data  Controlling the.

123 4 5

6 7

4

4,7

4

[Question] Proof ME1, ME2 and ME3 for this algorithm.

The message complexity is O(diameter) of the tree. Extensive empirical measurements show that the average diameter of randomly chosen trees of size n is O(log n). Therefore, the authors claim that the average message complexity is O(log n)

6 forwards the token to 1

4


Recommended