2. 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
3. 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
4. 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
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 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
10. ▸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
11. ▸ 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
12. ▸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
13. ▸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
16. 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
17. 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
18. 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
19. 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
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 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)
22. 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)
23. 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)
24. 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)
25. 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
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 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)
28. 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)
29. 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 }
30. 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
32. 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 }
33. 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 }
34. 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 }
36. 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 }
37. 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
39. 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
40. 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 }
41. 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
45. 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
}
46. 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
47. 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
53. 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)
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:
▹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