Theoretical Foundations
▸Clocks
▹Logical clocks (Lamport)
▹Partially ordered clocks (Fidge)
▸Causally ordered message delivery
▹Broadcast-based (Birman et al.)
▹Unicast-based (Raynal, Schiper & Toueg)
▸System states
▹Global snapshots (Chandy & Lamport)
▹Termination detection (Huang)
Theoretical Foundations
1
Activity
▸Processes are either active or passive
▸Active:
▹processing a computation or application
▹Only active processes send messages
▹May become passive at any time
▸Passive:
▹not processing a computation or application
▹Reactivated only upon receiving a message
Theoretical Foundations > Activity
2
Termination
▸A computation or application is said to be
terminated iff all processes are idle and there
are no messages in transit
Theoretical Foundations > Termination
3
Huang’s Protocol
▸Assumptions:
▹Control process initiates the computation
▹Communication channels exist between every
process pair (i.e., a complete network topology)
▹Message delay is arbitrary but finite
(i.e., a lossless network)
▹No process failures
Theoretical Foundations > Termination > Huang’s Protocol
4
Huang’s Protocol
▸Rule 1: Sending an application message
When a process p sends a message m, it divides it
weight Wp into W1 and W2. It keeps W1 and sends
W2 along with the message.
send(m, W2);
Wp = W1;
Theoretical Foundations > Termination > Huang’s Protocol
5
Huang’s Protocol
▸Rule 2: Receiving an application message
When process q receives a message, it adds the
message’s weight Wm to its own weight Wq.
If q is idle, q is reactivated.
receive(m, Wm);
Wq = Wq + Wm;
Theoretical Foundations > Termination > Huang’s Protocol
6
Huang’s Protocol
▸Rule 3: Becoming idle
When process q becomes idle, it sends its weight
Wq along with a control message to the control
process c.
send(m, Wq);
idle();
Theoretical Foundations > Termination > Huang’s Protocol
7
Huang’s Protocol
▸Rule 4: Monitoring
When the control process c receives a control
message, it adds the message’s weight Wm to its
own weight Wc. If Wc equals 1, the computation
has terminated.
receive(m, Wm);
Wc = Wc + Wm;
if(Wc == 1) termination = true;
Theoretical Foundations > Termination > Huang’s Protocol
8
▸Can represent W as a binary number in an
unbounded array of bits with the binary point
before the first bit
▹(i.e., “100” has a binary value of 0.100… or a
decimal value of 0.5)
▸The control process c starts by sending a
message with Wm = “1” and sets Wc = “1”
▸Termination occurs when Wc rolls over
(i.e., the bit array Wc requires a new leading bit)
Binary Representation
Theoretical Foundations > Termination > Huang’s Protocol
9
▸Evenly divide the binary array into window slots
▸Every weight W is represented by (…)i, where (…) is
a window of bits and i is the slot number
▸Let H be the maximum array size (e.g., H = 16 bits)
▸Let h be the window size (e.g., h = 4 bits)
▸Since H = 4h, we need 2 bits (because 22 = 4) to
represent the slot number
▸Hence, only 6 bits is required to represent a slot
(e.g., 0000 0000 0000 0001 = 0001|11)
Space-Efficient Representation
Theoretical Foundations > Termination > Huang’s Protocol
10
▸ When a process p has weight Wp = (0…01)i and needs to
send a message, it slides the window right one slot and
divides Wp into W1 and W2
▸ Example:
▹Wp = 0001|01 (i.e., 0000 0001)
▹p sets W1 to 1000|10 (i.e., 0000 0000 1)
▹p sets W2 to 1000|10 (i.e., 0000 0000 1)
▸ When a process p receives a message with weight Wm
and the sum of Wp and Wm do not fit in one window slot,
p forwards the smaller weight to the control process
▸ Hence, only the weight of the control (WC) needs 16 bits
Space-Efficient Scheme
Theoretical Foundations > Termination > Huang’s Protocol
11
▸When a process p needs to send a message and
has weight Wp = (0…01)i, where i is the last
window slot, p must first send a weight request
to the control with its weight attached
▸When the control process c receives a weight
request of form (0…01)i, it will send a weight
supply message to the requesting process of the
form (0…01)j, where j < i
Space-Efficient Scheme
Theoretical Foundations > Termination > Huang’s Protocol
12
▸Does the computation terminate at c4?
▸There must be at least one other active process
Termination Detection Exercise
Theoretical Foundations > Termination > Huang’s Protocol
13
C
D
E
1
0011
0011
01
01
00011
00011
00011
00011
00011
11 11011 1111
011
0001
control
m35 m36
0
0
0
c4
No, WC != 1
Resource Allocation
▸Distributed mutual exclusion
▹Permission-based (Ricart & Agrawala)
▹Quorum-based (Maekawa)
▹Token-based (Raymond)
Resource Allocation
14
Distributed Mutual Exclusion:
Permission-based Algorithms
CS/CE/TE 6378
Advanced Operating Systems
Distributed Mutual Exclusion
▸Mutual Exclusion:
▹a resource granted to a process must be released
before it can be granted to another process
▸Conditions for Distributed Mutual Exclusion:
1. Requests for a resource should be granted in the
order in which they were made
2. Every granted resource will eventually be released,
to ensure every request will eventually be granted
Resource Allocation > Distributed Mutual Exclusion
16
Permission-Based Algorithms
▸Algorithms that ensure mutual exclusion by
obtaining permission from every process in the
system
▸Whenever a process requests a resource, it must
request permission from every other process in
the system
▸Two algorithms:
▹Lamport (1978)
▹Ricart & Agrawala (1981)
Resource Allocation > Distributed Mutual Exclusion > Permission-Based
17
Lamport Algorithm
▸Assumptions:
▹Channels causally order message (i.e., FIFO)
▹Every message is eventually received
(i.e., lossless network)
▹Processes do not fail
▹Every process has its own request queue RQ, which is
never seen by other processes
▹Scalar logical clocks are used for timestamps
▹A total ordering relation is used to determine priority
Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Lamport
18
Lamport Algorithm
▸Rule 1
To request a resource, process i sends a request
message Tm:i to every other process, where Tm is
the timestamp of the message. Process i then
stores a copy of the message in its request
queue.
Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Lamport
19
Lamport Example
▸Total ordering: C ≺ D ≺ E
Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Lamport
20
C
D
E
RQC = { }
RQD = { }
RQE = { }
Lamport Example
▸D requests the resource by sending request
messages to C and E
Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Lamport
21
C
D
E
1
RQC = { }
RQD = { }
RQE = { }
req(1:D)
req(1:D)
Lamport Example
▸D then stores its own request
Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Lamport
22
C
D
E
1
RQC = { }
RQD = { 1:D
}
RQE = { }
req(1:D)
req(1:D)
Lamport Example
▸C requests the resource by sending request
messages to D and E
Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Lamport
23
C
D
E
1
1
RQC = { }
RQD = { 1:D
}
RQE = { }
req(1:D)
req(1:D)
req(1:C)
Lamport Example
▸C then stores its own request
Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Lamport
24
C
D
E
1
1
RQC = { 1:C }
RQD = { 1:D
}
RQE = { }
req(1:D)
req(1:D)
req(1:C)
Lamport Algorithm
▸Rule 2
When process j receives a request message Tm:i,
it places it in its request queue and sends a
timestamped acknowledgment back to process i.
Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Lamport
25
Lamport Example
Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Lamport
26
C
D
E
1
1
RQC = { 1:C }
RQD = { 1:D
}
RQE = { }
req(1:D)
req(1:D)
req(1:C)
Lamport Example
▸E places D’s request in its queue and sends an
acknowledgment back
Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Lamport
27
C
D
E
1
1
2 3
RQC = { 1:C }
RQD = { 1:D
}
RQE = { 1:D }
req(1:D)
req(1:D)
req(1:C)
ack(3:E)
Lamport Example
▸D places C’s request in its queue and sends an
acknowledgment back
Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Lamport
28
C
D
E
1
1
2 3
2 3
RQC = { 1:C }
RQD = { 1:C,
1:D }
RQE = { 1:D }
req(1:D)
req(1:D)
req(1:C)
ack(3:D)
ack(3:E)
Lamport Example
▸C places D’s request in its queue and sends an
acknowledgment back
Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Lamport
29
C
D
E
1 2 3
1
2 3
2 3
RQE = { 1:D }
req(1:D)
req(1:D)
req(1:C) ack(3:C)
ack(3:D)
ack(3:E)
RQD = { 1:C,
1:D }
RQC = { 1:C,
1:D }
Lamport Algorithm
▸Rule 5
Process i is granted the resource when the
following conditions are satisfied:
1. There is a request Tm:i in its request queue that is
ordered before any other request by the relation ⇒
2. Process i has received a message from every other
process with a timestamp later than Tm
Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Lamport
30
Lamport Example
Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Lamport
31
C
D
E
1 2 3
1
2 3
2 3
RQE = { 1:D }
req(1:D)
req(1:D)
req(1:C) ack(3:C)
ack(3:D)
ack(3:E)
RQD = { 1:C,
1:D }
RQC = { 1:C,
1:D }
Lamport Example
▸D isn’t granted the resource since 1:C ⇒ 1:D
Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Lamport
32
C
D
E
1 2 3
1
2 3
2 3
4
RQE = { 1:D }
req(1:D)
req(1:D)
req(1:C) ack(3:C)
ack(3:D)
ack(3:E)
RQC = { 1:C,
1:D }
RQD = { 1:C,
1:D }
Lamport Example
▸C isn’t granted the resource since it has not
received a message from E
Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Lamport
33
C
D
E
1 2 3 4
1
2 3
2 3
4
RQE = { 1:D }
req(1:D)
req(1:D)
req(1:C) ack(3:C)
ack(3:D)
ack(3:E)
RQC = { 1:C,
1:D }
RQD = { 1:C,
1:D }
Lamport Example
▸E places C’s request in its queue and sends an
acknowledgment back
Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Lamport
34
C
D
E
1 2 3 4
1
2 3
2 3
4
4 5
RQE = { 1:C, 1:D }
req(1:D)
req(1:D)
req(1:C) ack(3:C)
ack(3:D)
ack(3:E) ack(5:E)
RQC = { 1:C,
1:D }
RQD = { 1:C,
1:D }
Lamport Example
▸D isn’t granted the resource since 1:C ⇒ 1:D
Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Lamport
35
C
D
E
1 2 3 4
1
2 3
2 3
4
4 5
5
RQE = { 1:C, 1:D }
req(1:D)
req(1:D)
req(1:C) ack(3:C)
ack(3:D)
ack(3:E) ack(5:E)
RQC = { 1:C,
1:D }
RQD = { 1:C,
1:D }
Lamport Example
▸C is granted the resource since 1:C ⇒ 1:D and it
has received more recent messages from D and E
Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Lamport
36
C
D
E
1 2 3 4 6
1
2 3
2 3
4
4 5
5
req(1:D)
req(1:D)
req(1:C) ack(3:C)
ack(3:D)
ack(3:E) ack(5:E)
RQC = { 1:C,
1:D }
RQD = { 1:C,
1:D }
RQE = { 1:C, 1:D }
Lamport Algorithm
▸Rule 3
To release a resource, process i removes any Tm:i
requests from its request queue and sends a
timestamped release message to every other
process.
Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Lamport
37
Lamport Example
▸C is finished with resource
Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Lamport
38
C
D
E
1 2 3 4 6
1
2 3
2 3
4
4 5
5
req(1:D)
req(1:D)
req(1:C) ack(3:C)
ack(3:D)
ack(3:E) ack(5:E)
RQC = { 1:C,
1:D }
RQD = { 1:C,
1:D }
RQE = { 1:C, 1:D }
7
Lamport Example
▸C removes its request from its own queue
Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Lamport
39
C
D
E
1 2 3 4 6
1
2 3
2 3
4
4 5
5
req(1:D)
req(1:D)
req(1:C) ack(3:C)
ack(3:D)
ack(3:E) ack(5:E)
RQC = { 1:D }
RQD = { 1:C,
1:D }
RQE = { 1:C, 1:D }
7
Lamport Example
▸C releases the resource by sending release
messages to D and E
Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Lamport
40
C
D
E
1 2 3 4 6 7
1
2 3
2 3
4
4 5
5
req(1:D)
req(1:D)
req(1:C) rel(7:C)
ack(3:C)
ack(3:D)
ack(3:E) ack(5:E)
RQD = { 1:C,
1:D }
RQE = { 1:C, 1:D }
RQC = { 1:D }
Lamport Algorithm
▸Rule 4
When process j receives a release message from
process i, it removes any Tm:i requests from its
request queue.
Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Lamport
41
Lamport Example
Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Lamport
42
C
D
E
1 2 3 4 6 7
1
2 3
2 3
4
4 5
5
req(1:D)
req(1:D)
req(1:C) rel(7:C)
ack(3:C)
ack(3:D)
ack(3:E) ack(5:E)
RQD = { 1:C,
1:D }
RQE = { 1:C, 1:D }
RQC = { 1:D }
Lamport Example
▸E removes C’s request
Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Lamport
43
C
D
E
1 2 3 4 6 7
1
2 3
2 3
4
4 5
5
8
req(1:D)
req(1:D)
req(1:C) rel(7:C)
ack(3:C)
ack(3:D)
ack(3:E) ack(5:E)
RQD = { 1:C,
1:D }
RQE = { 1:D }
RQC = { 1:D }
Lamport Example
▸D removes C’s request
Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Lamport
44
C
D
E
1 2 3 4 6 7
1
2 3
2 3
4
4 5
5
8
8
req(1:D)
req(1:D)
req(1:C) rel(7:C)
ack(3:C)
ack(3:D)
ack(3:E) ack(5:E)
RQD = { 1:D
}
RQC = { 1:D }
RQE = { 1:D }
Lamport Example
▸D is granted the resource since it has received
more recent messages than 1:D from C and E
Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Lamport
45
C
D
E
1 2 3 4 6 7
1
2 3
2 3
4
4 5
5
8
8
req(1:D)
req(1:D)
req(1:C) rel(7:C)
ack(3:C)
ack(3:D)
ack(3:E) ack(5:E)
RQC = { 1:D }
RQE = { 1:D }
RQD = { 1:D
}
Complexity of Lamport Algorithm
▸ Parameters:
▹N: number of processes in the system
▹T: message transmission time
▹E: critical section execution time
▸ Message complexity: 3(N – 1)
▹N – 1 request messages
▹N – 1 acknowledgement messages
▹N – 1 release messages
▸ Response time (under light competition): 2T + E
▹Transmission time for request messages
▹Transmission time for acknowledgement messages
▹Execution time of current critical section
Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Lamport
46
Ricart & Agrawala Algorithm
▸Assumptions:
▹Every message is eventually received but not necessarily
in order sent (i.e., lossless network)
▹Processes do not fail
▹Every request is sequenced based on the highest
sequence number seen before by that process
▹Every process is numbered to determine priority in the
event of equivalent sequence numbers
(lower the number, higher the priority)
▹Each rule is implemented in a separate thread with a
shared semaphore
Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Ricart & Agrawala
47
Ricart & Agrawala Algorithm
▸Rule 1: Mutual Exclusion Invocation
requesting_critical_section = true;
sequence_no = highest_sequence_no + 1;
outstanding_replies = N – 1;
for(i = 0; i < N; i++) {
if(i != me) send(REQUEST(sequence_no, me), i);
}
while(outstanding_replies > 0) { }
critical_section();
requesting_critical_section = false;
for(i = 0; i < N; i++) {
if(reply_deferred[i]) {
reply_deferred[i] = false;
send(REPLY, i);
}
}
Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Ricart & Agrawala
48
Ricart & Agrawala Example
▸Node numbering (for priority):
▹P = 0, Q = 1, R = 2
Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Ricart & Agrawala
49
P
Q
R
Ricart & Agrawala Example
▸R makes request (requesting_critical_section = true)
▸R’s sequence_no = highest_sequence_no + 1 = 0 + 1 = 1
▸R’s outstanding_replies = N – 1 = 3 – 1 = 2
Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Ricart & Agrawala
50
P
Q
R
Ricart & Agrawala Example
▸R sends REQUEST(sequence_no, me) messages to
P and Q
Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Ricart & Agrawala
51
P
Q
R
req(1,2)
Ricart & Agrawala Example
▸Q makes request (requesting_critical_section = true)
▸Q’s sequence_no = highest_sequence_no + 1 = 0 + 1 = 1
▸Q’s outstanding_replies = N – 1 = 3 – 1 = 2
Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Ricart & Agrawala
52
P
Q
R
req(1,2)
Ricart & Agrawala Example
▸Q sends REQUEST(sequence_no, me) messages to
P and R
Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Ricart & Agrawala
53
P
Q
R
req(1,1)
req(1,1)
req(1,2)
Ricart & Agrawala Algorithm
▸Rule 2: Receiving Requests(j, k)
highest_sequence_no = max(highest_sequence_no, j);
defer = requesting_critical_section &&
((j > sequence_no) ||(j == sequence_no && k >
me));
if(defer) {
reply_deferred[k] = true;
}
else {
send(REPLY, k);
}
Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Ricart & Agrawala
54
Ricart & Agrawala Example
▸P receives Q’s REQUEST(1,1) message
▸ highest_sequence_no = max(highest_sequence_no, j)
= max(0, 1) = 1
Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Ricart & Agrawala
55
P
Q
R
req(1,1)
req(1,1)
req(1,2)
Ricart & Agrawala Example
▸ defer = requesting_critical_section &&
((j > sequence_no) ||(j == sequence_no && k > me))
= false && … = false
▸P sends REPLY message to Q
Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Ricart & Agrawala
56
P
Q
R
reply
req(1,1)
req(1,1)
req(1,2)
Ricart & Agrawala Example
▸Q receives R’s REQUEST(1,2) message
▸ highest_sequence_no = max(highest_sequence_no, j)
= max(0, 1) = 1
Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Ricart & Agrawala
57
P
Q
R
reply
req(1,1)
req(1,1)
req(1,2)
Ricart & Agrawala Example
▸ defer = requesting_critical_section &&
((j > sequence_no) ||(j == sequence_no && k > me))
= true && ((1 > 1) || (1 == 1 && 2 > 1)) = true
▸Q defers R’s request (reply_deferred[k] = true)
Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Ricart & Agrawala
58
P
Q
R
reply
req(1,1)
req(1,1)
req(1,2)
Ricart & Agrawala Example
▸R receives Q’s REQUEST(1,1) message
▸ highest_sequence_no = max(highest_sequence_no, j)
= max(0, 1) = 1
Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Ricart & Agrawala
59
P
Q
R
reply
req(1,1)
req(1,1)
req(1,2)
Ricart & Agrawala Example
▸ defer = requesting_critical_section &&
((j > sequence_no) ||(j == sequence_no && k > me))
= true && ((1 > 1) || (1 == 1 && 1 > 2)) = false
▸R sends REPLY message to Q
Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Ricart & Agrawala
60
P
Q
R
reply
reply
req(1,1)
req(1,1)
req(1,2)
Ricart & Agrawala Example
▸P makes request (requesting_critical_section = true)
▸P’s sequence_no = highest_sequence_no + 1 = 1 + 1 = 2
▸P’s outstanding_replies = N – 1 = 3 – 1 = 2
Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Ricart & Agrawala
61
P
Q
R
reply
reply
req(1,1)
req(1,1)
req(1,2)
Ricart & Agrawala Example
▸P sends REQUEST(sequence_no, me) messages to
Q and R
Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Ricart & Agrawala
62
P
Q
R
reply
reply
req(1,1)
req(1,1)
req(2,0)
req(1,2)
Ricart & Agrawala Algorithm
▸Rule 3: Receiving Replies
outstanding_replies--;
Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Ricart & Agrawala
63
Ricart & Agrawala Example
▸Q receives R’s REPLY message
▸Q’s outstanding_replies = 2 – 1 = 1
Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Ricart & Agrawala
64
P
Q
R
reply
reply
req(1,1)
req(1,1)
req(2,0)
req(1,2)
Ricart & Agrawala Example
▸Q receives P’s REPLY message
▸Q’s outstanding_replies = 1 – 1 = 0
Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Ricart & Agrawala
65
P
Q
R
reply
reply
req(1,1)
req(1,1)
req(2,0)
req(1,2)
Ricart & Agrawala Algorithm
▸Rule 1: Mutual Exclusion Invocation
requesting_critical_section = true;
sequence_no = highest_sequence_no + 1;
outstanding_replies = N – 1;
for(i = 0; i < N; i++) {
if(i != me) send(REQUEST(sequence_no, me), i);
}
while(outstanding_replies > 0) { }
critical_section();
requesting_critical_section = false;
for(i = 0; i < N; i++) {
if(reply_deferred[i]) {
reply_deferred[i] = false;
send(REPLY, i);
}
}
Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Ricart & Agrawala
66
Ricart & Agrawala Example
▸Q enters it’s critical_section()
Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Ricart & Agrawala
67
P
Q
R
reply
reply
req(1,1)
req(1,1)
req(2,0)
req(1,2)
Ricart & Agrawala Example
▸Q receives P’s REQUEST(2,0) message
▸ highest_sequence_no = max(highest_sequence_no, j)
= max(1, 2) = 2
Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Ricart & Agrawala
68
P
Q
R
reply
reply
req(1,1)
req(1,1)
req(2,0)
req(1,2)
Ricart & Agrawala Example
▸ defer = requesting_critical_section &&
((j > sequence_no) ||(j == sequence_no && k > me))
= true && ((2 > 1) || (2 == 1 && 0 > 1)) = true
▸Q defers P’s request (reply_deferred[k] = true)
Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Ricart & Agrawala
69
P
Q
R
reply
reply
req(1,1)
req(1,1)
req(2,0)
req(1,2)
Ricart & Agrawala Example
▸R receives P’s REQUEST(2,0) message
▸ highest_sequence_no = max(highest_sequence_no, j)
= max(1, 2) = 2
Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Ricart & Agrawala
70
P
Q
R
reply
reply
req(1,1)
req(1,1)
req(2,0)
req(1,2)
Ricart & Agrawala Example
▸ defer = requesting_critical_section &&
((j > sequence_no) ||(j == sequence_no && k > me))
= true && ((2 > 1) || (2 == 1 && 0 > 2)) = true
▸R defers P’s request (reply_deferred[k] = true)
Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Ricart & Agrawala
71
P
Q
R
reply
reply
req(1,1)
req(1,1)
req(2,0)
req(1,2)
Ricart & Agrawala Algorithm
▸Rule 1: Mutual Exclusion Invocation
requesting_critical_section = true;
sequence_no = highest_sequence_no + 1;
outstanding_replies = N – 1;
for(i = 0; i < N; i++) {
if(i != me) send(REQUEST(sequence_no, me), i);
}
while(outstanding_replies > 0) { }
critical_section();
requesting_critical_section = false;
for(i = 0; i < N; i++) {
if(reply_deferred[i]) {
reply_deferred[i] = false;
send(REPLY, i);
}
}
Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Ricart & Agrawala
72
Ricart & Agrawala Example
▸Q exits critical section
▸Since reply_deferred[0], Q sends REPLY message to P
▸Since reply_deferred[2], Q sends REPLY message to R
Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Ricart & Agrawala
73
P
Q
R
reply
reply
req(1,1)
req(1,1)
req(2,0) reply
reply
req(1,2)
Ricart & Agrawala Example
▸P and R receive Q’s REPLY messages
▸P’s outstanding_replies = 2 – 1 = 1
▸R’s outstanding_replies = 2 – 1 = 1
Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Ricart & Agrawala
74
P
Q
R
reply
reply
req(1,1)
req(1,1)
req(2,0) reply
reply
req(1,2)
Ricart & Agrawala Example
▸P receives R’s REQUEST(1,2) message
▸ highest_sequence_no = max(highest_sequence_no, j)
= max(2, 1) = 2
Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Ricart & Agrawala
75
P
Q
R
reply
reply
req(1,1)
req(1,1)
req(2,0) reply
reply
req(1,2)
Ricart & Agrawala Example
▸ defer = requesting_critical_section &&
((j > sequence_no) ||(j == sequence_no && k > me))
= true && ((1 > 2) || (1 == 2 && 2 > 0)) = false
▸P sends REPLY message to R
Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Ricart & Agrawala
76
P
Q
R
reply
reply
req(1,1)
req(1,1)
req(2,0) reply
reply
reply
req(1,2)
Ricart & Agrawala Example
▸R receives P’s REPLY message
▸R’s outstanding_replies = 1 – 1 = 0
▸R enters it’s critical_section()
Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Ricart & Agrawala
77
P
Q
R
reply
reply
req(1,1)
req(1,1)
req(2,0) reply
reply
reply
req(1,2)
Ricart & Agrawala Example
▸R exits critical section
▸Since reply_deferred[0], R sends REPLY message to P
Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Ricart & Agrawala
78
P
Q
R
reply
reply
req(1,1)
req(1,1)
req(2,0) reply
reply
reply
reply
req(1,2)
Ricart & Agrawala Example
▸P receives R’s REPLY message
▸P’s outstanding_replies = 1 – 1 = 0
▸P enters it’s critical_section()
Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Ricart & Agrawala
79
P
Q
R
reply
reply
req(1,1)
req(1,1)
req(2,0) reply
reply
reply
reply
req(1,2)
Complexity of Ricart & Agrawala
▸Message complexity: 2(N – 1)
▹N – 1 request messages
▹N – 1 reply messages
▸Response time (under light competition): 2T + E
▹Transmission time for request messages
▹Transmission time for reply messages
▹Execution time of current critical section
Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Ricart & Agrawala
80
Deadlock and Starvation
▸Deadlock:
▹Occurs when no process in the system is in its critical
section and no requesting process can ever proceed to
its own critical section
▸Starvation:
▹Occurs when one process must wait indefinitely to
enter its critical section even though other nodes are
entering and exiting their own critical sections
Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Ricart & Agrawala
81
Upcoming Classes
▸Quorum-based Distributed Mutual Exclusion
▸Token-based Distributed Mutual Exclusion
82

os distributed system theoretical foundation

  • 1.
    Theoretical Foundations ▸Clocks ▹Logical clocks(Lamport) ▹Partially ordered clocks (Fidge) ▸Causally ordered message delivery ▹Broadcast-based (Birman et al.) ▹Unicast-based (Raynal, Schiper & Toueg) ▸System states ▹Global snapshots (Chandy & Lamport) ▹Termination detection (Huang) Theoretical Foundations 1
  • 2.
    Activity ▸Processes are eitheractive or passive ▸Active: ▹processing a computation or application ▹Only active processes send messages ▹May become passive at any time ▸Passive: ▹not processing a computation or application ▹Reactivated only upon receiving a message Theoretical Foundations > Activity 2
  • 3.
    Termination ▸A computation orapplication is said to be terminated iff all processes are idle and there are no messages in transit Theoretical Foundations > Termination 3
  • 4.
    Huang’s Protocol ▸Assumptions: ▹Control processinitiates the computation ▹Communication channels exist between every process pair (i.e., a complete network topology) ▹Message delay is arbitrary but finite (i.e., a lossless network) ▹No process failures Theoretical Foundations > Termination > Huang’s Protocol 4
  • 5.
    Huang’s Protocol ▸Rule 1:Sending an application message When a process p sends a message m, it divides it weight Wp into W1 and W2. It keeps W1 and sends W2 along with the message. send(m, W2); Wp = W1; Theoretical Foundations > Termination > Huang’s Protocol 5
  • 6.
    Huang’s Protocol ▸Rule 2:Receiving an application message When process q receives a message, it adds the message’s weight Wm to its own weight Wq. If q is idle, q is reactivated. receive(m, Wm); Wq = Wq + Wm; Theoretical Foundations > Termination > Huang’s Protocol 6
  • 7.
    Huang’s Protocol ▸Rule 3:Becoming idle When process q becomes idle, it sends its weight Wq along with a control message to the control process c. send(m, Wq); idle(); Theoretical Foundations > Termination > Huang’s Protocol 7
  • 8.
    Huang’s Protocol ▸Rule 4:Monitoring When the control process c receives a control message, it adds the message’s weight Wm to its own weight Wc. If Wc equals 1, the computation has terminated. receive(m, Wm); Wc = Wc + Wm; if(Wc == 1) termination = true; Theoretical Foundations > Termination > Huang’s Protocol 8
  • 9.
    ▸Can represent Was a binary number in an unbounded array of bits with the binary point before the first bit ▹(i.e., “100” has a binary value of 0.100… or a decimal value of 0.5) ▸The control process c starts by sending a message with Wm = “1” and sets Wc = “1” ▸Termination occurs when Wc rolls over (i.e., the bit array Wc requires a new leading bit) Binary Representation Theoretical Foundations > Termination > Huang’s Protocol 9
  • 10.
    ▸Evenly divide thebinary array into window slots ▸Every weight W is represented by (…)i, where (…) is a window of bits and i is the slot number ▸Let H be the maximum array size (e.g., H = 16 bits) ▸Let h be the window size (e.g., h = 4 bits) ▸Since H = 4h, we need 2 bits (because 22 = 4) to represent the slot number ▸Hence, only 6 bits is required to represent a slot (e.g., 0000 0000 0000 0001 = 0001|11) Space-Efficient Representation Theoretical Foundations > Termination > Huang’s Protocol 10
  • 11.
    ▸ When aprocess p has weight Wp = (0…01)i and needs to send a message, it slides the window right one slot and divides Wp into W1 and W2 ▸ Example: ▹Wp = 0001|01 (i.e., 0000 0001) ▹p sets W1 to 1000|10 (i.e., 0000 0000 1) ▹p sets W2 to 1000|10 (i.e., 0000 0000 1) ▸ When a process p receives a message with weight Wm and the sum of Wp and Wm do not fit in one window slot, p forwards the smaller weight to the control process ▸ Hence, only the weight of the control (WC) needs 16 bits Space-Efficient Scheme Theoretical Foundations > Termination > Huang’s Protocol 11
  • 12.
    ▸When a processp needs to send a message and has weight Wp = (0…01)i, where i is the last window slot, p must first send a weight request to the control with its weight attached ▸When the control process c receives a weight request of form (0…01)i, it will send a weight supply message to the requesting process of the form (0…01)j, where j < i Space-Efficient Scheme Theoretical Foundations > Termination > Huang’s Protocol 12
  • 13.
    ▸Does the computationterminate at c4? ▸There must be at least one other active process Termination Detection Exercise Theoretical Foundations > Termination > Huang’s Protocol 13 C D E 1 0011 0011 01 01 00011 00011 00011 00011 00011 11 11011 1111 011 0001 control m35 m36 0 0 0 c4 No, WC != 1
  • 14.
    Resource Allocation ▸Distributed mutualexclusion ▹Permission-based (Ricart & Agrawala) ▹Quorum-based (Maekawa) ▹Token-based (Raymond) Resource Allocation 14
  • 15.
    Distributed Mutual Exclusion: Permission-basedAlgorithms CS/CE/TE 6378 Advanced Operating Systems
  • 16.
    Distributed Mutual Exclusion ▸MutualExclusion: ▹a resource granted to a process must be released before it can be granted to another process ▸Conditions for Distributed Mutual Exclusion: 1. Requests for a resource should be granted in the order in which they were made 2. Every granted resource will eventually be released, to ensure every request will eventually be granted Resource Allocation > Distributed Mutual Exclusion 16
  • 17.
    Permission-Based Algorithms ▸Algorithms thatensure mutual exclusion by obtaining permission from every process in the system ▸Whenever a process requests a resource, it must request permission from every other process in the system ▸Two algorithms: ▹Lamport (1978) ▹Ricart & Agrawala (1981) Resource Allocation > Distributed Mutual Exclusion > Permission-Based 17
  • 18.
    Lamport Algorithm ▸Assumptions: ▹Channels causallyorder message (i.e., FIFO) ▹Every message is eventually received (i.e., lossless network) ▹Processes do not fail ▹Every process has its own request queue RQ, which is never seen by other processes ▹Scalar logical clocks are used for timestamps ▹A total ordering relation is used to determine priority Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Lamport 18
  • 19.
    Lamport Algorithm ▸Rule 1 Torequest a resource, process i sends a request message Tm:i to every other process, where Tm is the timestamp of the message. Process i then stores a copy of the message in its request queue. Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Lamport 19
  • 20.
    Lamport Example ▸Total ordering:C ≺ D ≺ E Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Lamport 20 C D E RQC = { } RQD = { } RQE = { }
  • 21.
    Lamport Example ▸D requeststhe resource by sending request messages to C and E Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Lamport 21 C D E 1 RQC = { } RQD = { } RQE = { } req(1:D) req(1:D)
  • 22.
    Lamport Example ▸D thenstores its own request Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Lamport 22 C D E 1 RQC = { } RQD = { 1:D } RQE = { } req(1:D) req(1:D)
  • 23.
    Lamport Example ▸C requeststhe resource by sending request messages to D and E Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Lamport 23 C D E 1 1 RQC = { } RQD = { 1:D } RQE = { } req(1:D) req(1:D) req(1:C)
  • 24.
    Lamport Example ▸C thenstores its own request Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Lamport 24 C D E 1 1 RQC = { 1:C } RQD = { 1:D } RQE = { } req(1:D) req(1:D) req(1:C)
  • 25.
    Lamport Algorithm ▸Rule 2 Whenprocess j receives a request message Tm:i, it places it in its request queue and sends a timestamped acknowledgment back to process i. Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Lamport 25
  • 26.
    Lamport Example Resource Allocation> Distributed Mutual Exclusion > Permission-Based > Lamport 26 C D E 1 1 RQC = { 1:C } RQD = { 1:D } RQE = { } req(1:D) req(1:D) req(1:C)
  • 27.
    Lamport Example ▸E placesD’s request in its queue and sends an acknowledgment back Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Lamport 27 C D E 1 1 2 3 RQC = { 1:C } RQD = { 1:D } RQE = { 1:D } req(1:D) req(1:D) req(1:C) ack(3:E)
  • 28.
    Lamport Example ▸D placesC’s request in its queue and sends an acknowledgment back Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Lamport 28 C D E 1 1 2 3 2 3 RQC = { 1:C } RQD = { 1:C, 1:D } RQE = { 1:D } req(1:D) req(1:D) req(1:C) ack(3:D) ack(3:E)
  • 29.
    Lamport Example ▸C placesD’s request in its queue and sends an acknowledgment back Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Lamport 29 C D E 1 2 3 1 2 3 2 3 RQE = { 1:D } req(1:D) req(1:D) req(1:C) ack(3:C) ack(3:D) ack(3:E) RQD = { 1:C, 1:D } RQC = { 1:C, 1:D }
  • 30.
    Lamport Algorithm ▸Rule 5 Processi is granted the resource when the following conditions are satisfied: 1. There is a request Tm:i in its request queue that is ordered before any other request by the relation ⇒ 2. Process i has received a message from every other process with a timestamp later than Tm Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Lamport 30
  • 31.
    Lamport Example Resource Allocation> Distributed Mutual Exclusion > Permission-Based > Lamport 31 C D E 1 2 3 1 2 3 2 3 RQE = { 1:D } req(1:D) req(1:D) req(1:C) ack(3:C) ack(3:D) ack(3:E) RQD = { 1:C, 1:D } RQC = { 1:C, 1:D }
  • 32.
    Lamport Example ▸D isn’tgranted the resource since 1:C ⇒ 1:D Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Lamport 32 C D E 1 2 3 1 2 3 2 3 4 RQE = { 1:D } req(1:D) req(1:D) req(1:C) ack(3:C) ack(3:D) ack(3:E) RQC = { 1:C, 1:D } RQD = { 1:C, 1:D }
  • 33.
    Lamport Example ▸C isn’tgranted the resource since it has not received a message from E Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Lamport 33 C D E 1 2 3 4 1 2 3 2 3 4 RQE = { 1:D } req(1:D) req(1:D) req(1:C) ack(3:C) ack(3:D) ack(3:E) RQC = { 1:C, 1:D } RQD = { 1:C, 1:D }
  • 34.
    Lamport Example ▸E placesC’s request in its queue and sends an acknowledgment back Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Lamport 34 C D E 1 2 3 4 1 2 3 2 3 4 4 5 RQE = { 1:C, 1:D } req(1:D) req(1:D) req(1:C) ack(3:C) ack(3:D) ack(3:E) ack(5:E) RQC = { 1:C, 1:D } RQD = { 1:C, 1:D }
  • 35.
    Lamport Example ▸D isn’tgranted the resource since 1:C ⇒ 1:D Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Lamport 35 C D E 1 2 3 4 1 2 3 2 3 4 4 5 5 RQE = { 1:C, 1:D } req(1:D) req(1:D) req(1:C) ack(3:C) ack(3:D) ack(3:E) ack(5:E) RQC = { 1:C, 1:D } RQD = { 1:C, 1:D }
  • 36.
    Lamport Example ▸C isgranted the resource since 1:C ⇒ 1:D and it has received more recent messages from D and E Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Lamport 36 C D E 1 2 3 4 6 1 2 3 2 3 4 4 5 5 req(1:D) req(1:D) req(1:C) ack(3:C) ack(3:D) ack(3:E) ack(5:E) RQC = { 1:C, 1:D } RQD = { 1:C, 1:D } RQE = { 1:C, 1:D }
  • 37.
    Lamport Algorithm ▸Rule 3 Torelease a resource, process i removes any Tm:i requests from its request queue and sends a timestamped release message to every other process. Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Lamport 37
  • 38.
    Lamport Example ▸C isfinished with resource Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Lamport 38 C D E 1 2 3 4 6 1 2 3 2 3 4 4 5 5 req(1:D) req(1:D) req(1:C) ack(3:C) ack(3:D) ack(3:E) ack(5:E) RQC = { 1:C, 1:D } RQD = { 1:C, 1:D } RQE = { 1:C, 1:D } 7
  • 39.
    Lamport Example ▸C removesits request from its own queue Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Lamport 39 C D E 1 2 3 4 6 1 2 3 2 3 4 4 5 5 req(1:D) req(1:D) req(1:C) ack(3:C) ack(3:D) ack(3:E) ack(5:E) RQC = { 1:D } RQD = { 1:C, 1:D } RQE = { 1:C, 1:D } 7
  • 40.
    Lamport Example ▸C releasesthe resource by sending release messages to D and E Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Lamport 40 C D E 1 2 3 4 6 7 1 2 3 2 3 4 4 5 5 req(1:D) req(1:D) req(1:C) rel(7:C) ack(3:C) ack(3:D) ack(3:E) ack(5:E) RQD = { 1:C, 1:D } RQE = { 1:C, 1:D } RQC = { 1:D }
  • 41.
    Lamport Algorithm ▸Rule 4 Whenprocess j receives a release message from process i, it removes any Tm:i requests from its request queue. Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Lamport 41
  • 42.
    Lamport Example Resource Allocation> Distributed Mutual Exclusion > Permission-Based > Lamport 42 C D E 1 2 3 4 6 7 1 2 3 2 3 4 4 5 5 req(1:D) req(1:D) req(1:C) rel(7:C) ack(3:C) ack(3:D) ack(3:E) ack(5:E) RQD = { 1:C, 1:D } RQE = { 1:C, 1:D } RQC = { 1:D }
  • 43.
    Lamport Example ▸E removesC’s request Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Lamport 43 C D E 1 2 3 4 6 7 1 2 3 2 3 4 4 5 5 8 req(1:D) req(1:D) req(1:C) rel(7:C) ack(3:C) ack(3:D) ack(3:E) ack(5:E) RQD = { 1:C, 1:D } RQE = { 1:D } RQC = { 1:D }
  • 44.
    Lamport Example ▸D removesC’s request Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Lamport 44 C D E 1 2 3 4 6 7 1 2 3 2 3 4 4 5 5 8 8 req(1:D) req(1:D) req(1:C) rel(7:C) ack(3:C) ack(3:D) ack(3:E) ack(5:E) RQD = { 1:D } RQC = { 1:D } RQE = { 1:D }
  • 45.
    Lamport Example ▸D isgranted the resource since it has received more recent messages than 1:D from C and E Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Lamport 45 C D E 1 2 3 4 6 7 1 2 3 2 3 4 4 5 5 8 8 req(1:D) req(1:D) req(1:C) rel(7:C) ack(3:C) ack(3:D) ack(3:E) ack(5:E) RQC = { 1:D } RQE = { 1:D } RQD = { 1:D }
  • 46.
    Complexity of LamportAlgorithm ▸ Parameters: ▹N: number of processes in the system ▹T: message transmission time ▹E: critical section execution time ▸ Message complexity: 3(N – 1) ▹N – 1 request messages ▹N – 1 acknowledgement messages ▹N – 1 release messages ▸ Response time (under light competition): 2T + E ▹Transmission time for request messages ▹Transmission time for acknowledgement messages ▹Execution time of current critical section Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Lamport 46
  • 47.
    Ricart & AgrawalaAlgorithm ▸Assumptions: ▹Every message is eventually received but not necessarily in order sent (i.e., lossless network) ▹Processes do not fail ▹Every request is sequenced based on the highest sequence number seen before by that process ▹Every process is numbered to determine priority in the event of equivalent sequence numbers (lower the number, higher the priority) ▹Each rule is implemented in a separate thread with a shared semaphore Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Ricart & Agrawala 47
  • 48.
    Ricart & AgrawalaAlgorithm ▸Rule 1: Mutual Exclusion Invocation requesting_critical_section = true; sequence_no = highest_sequence_no + 1; outstanding_replies = N – 1; for(i = 0; i < N; i++) { if(i != me) send(REQUEST(sequence_no, me), i); } while(outstanding_replies > 0) { } critical_section(); requesting_critical_section = false; for(i = 0; i < N; i++) { if(reply_deferred[i]) { reply_deferred[i] = false; send(REPLY, i); } } Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Ricart & Agrawala 48
  • 49.
    Ricart & AgrawalaExample ▸Node numbering (for priority): ▹P = 0, Q = 1, R = 2 Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Ricart & Agrawala 49 P Q R
  • 50.
    Ricart & AgrawalaExample ▸R makes request (requesting_critical_section = true) ▸R’s sequence_no = highest_sequence_no + 1 = 0 + 1 = 1 ▸R’s outstanding_replies = N – 1 = 3 – 1 = 2 Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Ricart & Agrawala 50 P Q R
  • 51.
    Ricart & AgrawalaExample ▸R sends REQUEST(sequence_no, me) messages to P and Q Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Ricart & Agrawala 51 P Q R req(1,2)
  • 52.
    Ricart & AgrawalaExample ▸Q makes request (requesting_critical_section = true) ▸Q’s sequence_no = highest_sequence_no + 1 = 0 + 1 = 1 ▸Q’s outstanding_replies = N – 1 = 3 – 1 = 2 Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Ricart & Agrawala 52 P Q R req(1,2)
  • 53.
    Ricart & AgrawalaExample ▸Q sends REQUEST(sequence_no, me) messages to P and R Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Ricart & Agrawala 53 P Q R req(1,1) req(1,1) req(1,2)
  • 54.
    Ricart & AgrawalaAlgorithm ▸Rule 2: Receiving Requests(j, k) highest_sequence_no = max(highest_sequence_no, j); defer = requesting_critical_section && ((j > sequence_no) ||(j == sequence_no && k > me)); if(defer) { reply_deferred[k] = true; } else { send(REPLY, k); } Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Ricart & Agrawala 54
  • 55.
    Ricart & AgrawalaExample ▸P receives Q’s REQUEST(1,1) message ▸ highest_sequence_no = max(highest_sequence_no, j) = max(0, 1) = 1 Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Ricart & Agrawala 55 P Q R req(1,1) req(1,1) req(1,2)
  • 56.
    Ricart & AgrawalaExample ▸ defer = requesting_critical_section && ((j > sequence_no) ||(j == sequence_no && k > me)) = false && … = false ▸P sends REPLY message to Q Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Ricart & Agrawala 56 P Q R reply req(1,1) req(1,1) req(1,2)
  • 57.
    Ricart & AgrawalaExample ▸Q receives R’s REQUEST(1,2) message ▸ highest_sequence_no = max(highest_sequence_no, j) = max(0, 1) = 1 Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Ricart & Agrawala 57 P Q R reply req(1,1) req(1,1) req(1,2)
  • 58.
    Ricart & AgrawalaExample ▸ defer = requesting_critical_section && ((j > sequence_no) ||(j == sequence_no && k > me)) = true && ((1 > 1) || (1 == 1 && 2 > 1)) = true ▸Q defers R’s request (reply_deferred[k] = true) Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Ricart & Agrawala 58 P Q R reply req(1,1) req(1,1) req(1,2)
  • 59.
    Ricart & AgrawalaExample ▸R receives Q’s REQUEST(1,1) message ▸ highest_sequence_no = max(highest_sequence_no, j) = max(0, 1) = 1 Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Ricart & Agrawala 59 P Q R reply req(1,1) req(1,1) req(1,2)
  • 60.
    Ricart & AgrawalaExample ▸ defer = requesting_critical_section && ((j > sequence_no) ||(j == sequence_no && k > me)) = true && ((1 > 1) || (1 == 1 && 1 > 2)) = false ▸R sends REPLY message to Q Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Ricart & Agrawala 60 P Q R reply reply req(1,1) req(1,1) req(1,2)
  • 61.
    Ricart & AgrawalaExample ▸P makes request (requesting_critical_section = true) ▸P’s sequence_no = highest_sequence_no + 1 = 1 + 1 = 2 ▸P’s outstanding_replies = N – 1 = 3 – 1 = 2 Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Ricart & Agrawala 61 P Q R reply reply req(1,1) req(1,1) req(1,2)
  • 62.
    Ricart & AgrawalaExample ▸P sends REQUEST(sequence_no, me) messages to Q and R Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Ricart & Agrawala 62 P Q R reply reply req(1,1) req(1,1) req(2,0) req(1,2)
  • 63.
    Ricart & AgrawalaAlgorithm ▸Rule 3: Receiving Replies outstanding_replies--; Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Ricart & Agrawala 63
  • 64.
    Ricart & AgrawalaExample ▸Q receives R’s REPLY message ▸Q’s outstanding_replies = 2 – 1 = 1 Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Ricart & Agrawala 64 P Q R reply reply req(1,1) req(1,1) req(2,0) req(1,2)
  • 65.
    Ricart & AgrawalaExample ▸Q receives P’s REPLY message ▸Q’s outstanding_replies = 1 – 1 = 0 Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Ricart & Agrawala 65 P Q R reply reply req(1,1) req(1,1) req(2,0) req(1,2)
  • 66.
    Ricart & AgrawalaAlgorithm ▸Rule 1: Mutual Exclusion Invocation requesting_critical_section = true; sequence_no = highest_sequence_no + 1; outstanding_replies = N – 1; for(i = 0; i < N; i++) { if(i != me) send(REQUEST(sequence_no, me), i); } while(outstanding_replies > 0) { } critical_section(); requesting_critical_section = false; for(i = 0; i < N; i++) { if(reply_deferred[i]) { reply_deferred[i] = false; send(REPLY, i); } } Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Ricart & Agrawala 66
  • 67.
    Ricart & AgrawalaExample ▸Q enters it’s critical_section() Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Ricart & Agrawala 67 P Q R reply reply req(1,1) req(1,1) req(2,0) req(1,2)
  • 68.
    Ricart & AgrawalaExample ▸Q receives P’s REQUEST(2,0) message ▸ highest_sequence_no = max(highest_sequence_no, j) = max(1, 2) = 2 Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Ricart & Agrawala 68 P Q R reply reply req(1,1) req(1,1) req(2,0) req(1,2)
  • 69.
    Ricart & AgrawalaExample ▸ defer = requesting_critical_section && ((j > sequence_no) ||(j == sequence_no && k > me)) = true && ((2 > 1) || (2 == 1 && 0 > 1)) = true ▸Q defers P’s request (reply_deferred[k] = true) Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Ricart & Agrawala 69 P Q R reply reply req(1,1) req(1,1) req(2,0) req(1,2)
  • 70.
    Ricart & AgrawalaExample ▸R receives P’s REQUEST(2,0) message ▸ highest_sequence_no = max(highest_sequence_no, j) = max(1, 2) = 2 Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Ricart & Agrawala 70 P Q R reply reply req(1,1) req(1,1) req(2,0) req(1,2)
  • 71.
    Ricart & AgrawalaExample ▸ defer = requesting_critical_section && ((j > sequence_no) ||(j == sequence_no && k > me)) = true && ((2 > 1) || (2 == 1 && 0 > 2)) = true ▸R defers P’s request (reply_deferred[k] = true) Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Ricart & Agrawala 71 P Q R reply reply req(1,1) req(1,1) req(2,0) req(1,2)
  • 72.
    Ricart & AgrawalaAlgorithm ▸Rule 1: Mutual Exclusion Invocation requesting_critical_section = true; sequence_no = highest_sequence_no + 1; outstanding_replies = N – 1; for(i = 0; i < N; i++) { if(i != me) send(REQUEST(sequence_no, me), i); } while(outstanding_replies > 0) { } critical_section(); requesting_critical_section = false; for(i = 0; i < N; i++) { if(reply_deferred[i]) { reply_deferred[i] = false; send(REPLY, i); } } Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Ricart & Agrawala 72
  • 73.
    Ricart & AgrawalaExample ▸Q exits critical section ▸Since reply_deferred[0], Q sends REPLY message to P ▸Since reply_deferred[2], Q sends REPLY message to R Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Ricart & Agrawala 73 P Q R reply reply req(1,1) req(1,1) req(2,0) reply reply req(1,2)
  • 74.
    Ricart & AgrawalaExample ▸P and R receive Q’s REPLY messages ▸P’s outstanding_replies = 2 – 1 = 1 ▸R’s outstanding_replies = 2 – 1 = 1 Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Ricart & Agrawala 74 P Q R reply reply req(1,1) req(1,1) req(2,0) reply reply req(1,2)
  • 75.
    Ricart & AgrawalaExample ▸P receives R’s REQUEST(1,2) message ▸ highest_sequence_no = max(highest_sequence_no, j) = max(2, 1) = 2 Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Ricart & Agrawala 75 P Q R reply reply req(1,1) req(1,1) req(2,0) reply reply req(1,2)
  • 76.
    Ricart & AgrawalaExample ▸ defer = requesting_critical_section && ((j > sequence_no) ||(j == sequence_no && k > me)) = true && ((1 > 2) || (1 == 2 && 2 > 0)) = false ▸P sends REPLY message to R Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Ricart & Agrawala 76 P Q R reply reply req(1,1) req(1,1) req(2,0) reply reply reply req(1,2)
  • 77.
    Ricart & AgrawalaExample ▸R receives P’s REPLY message ▸R’s outstanding_replies = 1 – 1 = 0 ▸R enters it’s critical_section() Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Ricart & Agrawala 77 P Q R reply reply req(1,1) req(1,1) req(2,0) reply reply reply req(1,2)
  • 78.
    Ricart & AgrawalaExample ▸R exits critical section ▸Since reply_deferred[0], R sends REPLY message to P Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Ricart & Agrawala 78 P Q R reply reply req(1,1) req(1,1) req(2,0) reply reply reply reply req(1,2)
  • 79.
    Ricart & AgrawalaExample ▸P receives R’s REPLY message ▸P’s outstanding_replies = 1 – 1 = 0 ▸P enters it’s critical_section() Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Ricart & Agrawala 79 P Q R reply reply req(1,1) req(1,1) req(2,0) reply reply reply reply req(1,2)
  • 80.
    Complexity of Ricart& Agrawala ▸Message complexity: 2(N – 1) ▹N – 1 request messages ▹N – 1 reply messages ▸Response time (under light competition): 2T + E ▹Transmission time for request messages ▹Transmission time for reply messages ▹Execution time of current critical section Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Ricart & Agrawala 80
  • 81.
    Deadlock and Starvation ▸Deadlock: ▹Occurswhen no process in the system is in its critical section and no requesting process can ever proceed to its own critical section ▸Starvation: ▹Occurs when one process must wait indefinitely to enter its critical section even though other nodes are entering and exiting their own critical sections Resource Allocation > Distributed Mutual Exclusion > Permission-Based > Ricart & Agrawala 81
  • 82.
    Upcoming Classes ▸Quorum-based DistributedMutual Exclusion ▸Token-based Distributed Mutual Exclusion 82