#### Transcript Token-passing Algorithms

```Token-passing Algorithms
Suzuki-Kasami algorithm
Completely connected network of processes
There is one token in the network. The owner
of the token has the permission to enter CS.
Token will move from one process to another
based on demand.
Suzuki-Kasami Algorithm
Sequence number
of the request
Q
Each process maintains
-an array req: req[j] denotes the sequence
no 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.
req: array[0..n-1] of integer
- a queue Q of waiting processes
last: array [0..n-1] of integer
Suzuki-Kasami Algorithm
When a process i receives a request (i, num) from
process k, it sets req[k] to max(req[k], num)
and enqueues the request in its Q
When process i sends a token to the head of Q,
it sets last[i] := its own num, and passes the array
last, as well as the tail of Q,
The holder of the token retains process k in its
Q only if 1+ last[k] = req[k]
Req: array[0..n-1] of integer
This guarantees the freshness of the request
Last: Array [0..n-1] of integer
Suzuki-Kasami’s algorithm
{Program of process j}
Initially, i: req[i] = last[i] = 0
* Entry protocol *
req[j] := req[j] + 1
Send (j, req[j]) to all
Wait until token (Q, last) arrives
Critical 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  end (tail-of-Q, last) to head-of-Q fi
* Upon receiving a request (k, num) *
req[k] := max(req[k], num)
Example
req=[1,0,0,0,0]
req=[1,0,0,0,0]
last=[0,0,0,0,0]
1
0
2
req=[1,0,0,0,0]
4
req=[1,0,0,0,0]
3
req=[1,0,0,0,0]
initial state
Example
req=[1,1,1,0,0]
req=[1,1,1,0,0]
last=[0,0,0,0,0]
1
0
2
req=[1,1,1,0,0]
4
req=[1,1,1,0,0]
3
req=[1,1,1,0,0]
1 & 2 send requests
Example
req=[1,1,1,0,0]
req=[1,1,1,0,0]
last=[1,0,0,0,0]
Q=(1,2)
1
0
2
req=[1,1,1,0,0]
4
req=[1,1,1,0,0]
3
req=[1,1,1,0,0]
0 prepares to exit CS
Example
req=[1,1,1,0,0]
1
0
req=[1,1,1,0,0]
last=[1,0,0,0,0]
Q=(2)
2
req=[1,1,1,0,0]
4
req=[1,1,1,0,0]
3
req=[1,1,1,0,0]
0 passes token (Q and last) to 1
Example
req=[2,1,1,1,0]
1
0
req=[2,1,1,1,0]
last=[1,0,0,0,0]
Q=(2,0,3)
2
req=[2,1,1,1,0]
4
req=[2,1,1,1,0]
3
req=[2,1,1,1,0]
0 and 3 send requests
Example
req=[2,1,1,1,0]
req=[2,1,1,1,0]
1
0
2
4
req=[2,1,1,1,0]
3
req=[2,1,1,1,0]
last=[1,1,0,0,0]
Q=(0,3)
req=[2,1,1,1,0]
1 sends token to 2
Raymond’s tree-based algorithm
3
1
2
1
4
5
4
1
6
1,4
7
4,7
1,4,7 want to enter their CS
Raymond’s Algorithm
3
2
1
1
4
5
4
6 1,4
7
4,7
2 sends the token to 6
Raymond’s Algorithm
3
1
2
6
4
7
4
4
4
5
4,7
6 forwards the token to 1
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 the
average message complexity to be O(log n)
Distributed snapshot
--
How many messages are in transit
on the internet?
-- What is the global state of a distributed
system of N processes?
How do we compute these?
One-dollar bank
2
(2,0)
0
(1,2)
1
(0,1)
Let a \$1 coin circulate in a network of a million banks.
How can someone count the total \$ in circulation? If
not counted “properly,” then one may think the total \$
in circulation to be one million.
Importance of snapshots
Major uses in
- termination detection
- rollback recovery
- global predicate computation
Consistent cut
A cut is a set of events.
(a  consistent cut C)  (b happened before a)  b  C
P1
b
a
c
m
P2
d
g
e
f
P3
k
Cut 1
(Consistent)
h
i
Cut 2
(Not consistent)
j
Consistent snapshot
The set of states immediately following a consistent
cut forms a consistent snapshot of a distributed
system.
• A snapshot that is of practical interest is the most
recent one. Let C1 and C2 be two consistent cuts
and C1  C2. Then C2 is more recent than C1.
• Analyze why certain cuts in the one-dollar bank are
inconsistent.
Consistent snapshot
How to record a consistent snapshot? Note that
1.
The recording must be non-invasive
2.
Recording must be done on-the-fly.
You cannot stop the system.
Chandy-Lamport Algorithm
Works on a
(1) strongly connected graph
(2) each channel is FIFO.
An initiator initiates the
algorithm by sending out
a marker (
)
White and red processes
Initially every process is white. When a
process receives a marker, it turns
red if it has not already done so.
Every action by a process, and every
message sent by a process gets the
color of that process.
Two steps
Step 1. In one atomic action, the initiator (a) Turns red
(b) Records its own state (c) sends a marker
along all outgoing channels
Step 2. Every other process, upon receiving a marker
for the first time (and before doing anything else)
(a) Turns red (b) Records its own state (c) sends
markers along all outgoing channels
The algorithm terminates when (1) every process
turns red, and (2) Every process has
received a marker through each incoming
channel.
Why does it work?
Lemma 1. No red message is received in a white action.
Why does it work?
All white
All red
SSS
Easy conceptualization of the snapshot state
Theorem. The global state recorded by Chandy-Lamport
algorithm is equivalent to the ideal snapshot state SSS.
Hint. A pair of actions (a, b) can be scheduled
in any order, if there is no causal order
between them, so (a; b) is equivalent to (b;
a)
Why does it work?
Let an observer observe the following actions:
w[i] w[k] r[k] w[j] r[i] w[l] r[j] r[l] …

w[i] w[k] w[j] r[k] r[i] w[l] r[j] r[l] … [Lemma 1]

w[i] w[k] w[j] r[k] w[l] r[i] r[j] r[l] … [Lemma 1]

w[i] w[k] w[j] w[l] r[k] r[i] r[j] r[l] … [done!]
Recorded state
Example 1. Count the tokens
Let us verify that Chandy-Lamport snapshot algorithm correctly counts
the tokens circulating in the system
D
C
A
B
How to account for the channel states?
Use sent and received variables for each process.
```