CEG 7370: Distributed Computing Principles

Token Passing

Prabhaker Mateti

This web page is organized in way that is useful during my lecture, instead of ppt slides.


1. Probe/Echo for Topology of a Network

type kind = enum(probe, echo);
type boolmatrix = [1..n][1..n]: bool;
channel pec[1..n](k: kind, sender: int, links: boolmatrix);
channel fin(links: boolmatrixbsp;  # final echo

Initiator::

constant source = ...;     # initiator's index
var topo: boolmatrix;
send pec[source](probe, source, emptySet)
receive fin(topo)          # network topology

Node[p:1..n] variables

    var links[1..n] : bool := neighbors of node p;
    var localtopo: boolmatrix := ([1..n][1..n] false);
    var newtopo: boolmatrix;
    var first, sender: 1..n;
    var k : kind;
    var needecho: int := num neighbors of p - 1

Node[p:1..n] algorithm

    localtopo[p] := links;
    receive pec[p](k, first, newtopo);     # k = probe
    for all q := 1 to n such that links[q] and q ≠ first do
         send pec[q](probe, p, emptySet)
    od
    do needecho > 0 →
        receive pec[p](k, sender, newtopo)
        if k = probe →
             send pec[sender](echo, p, emptySet)
        [] k = echo →
             localtopo := localtopo or newtopo;
             needecho := needecho - 1
        fi
    od
    if p = source → send fin(localtopo)
    [] p ≠ source → send pec[first](echo, p, localtopo)
    fi

Figure 7.19 [Andrews] Probe/echo algorithm for topology of a network

2. Mutual Exclusion with a Token Ring

four [1..n] channels kch, enter, go, exit whose entries are ()

DMUTEX

The token is a message on some kch[i].

invariant
     (There is exactly one token) and
     (for-all i: 1 ≤ i ≤ n: P[i] in its CS implies Helper[i] has the token)

empty(ch)

channel ch has no messages
non blocking
by the time "the next" receive is done "situation" may have changed.

Helper[i: 1..n]

i-th Helper uses only the i-th numbered channels.

do true → { DMUTEX }
     receive kch[i]()
     if not(empty(enter[i])) →
          receive enter[i]();           # will not block
          send     go[i]();
          receive exit[i]()
     fi
     send kch[i mod n + 1]()      # pass the token on
od

P[i: 1..n]

do true →
     send enter[i]()      # P[i] wishes to enter its CS
     receive go[i]()
     CS
     send exit[t]()     # exit protocol
     non-critical section
od

Figure 7.22. Mutual exclusion with a token ring.

3. Termination Detection

Distributed State

Global state is distributed.

Not visible as a snapshot to any process.

Messages in transit (i.e., "still" in the channels)

Process Idle/Busy

Distributed Computation = send/recv + synergy

Any process can subdivide a dc and request others to work on pieces

Busy = Process is working on the assigned piece of computation

Idle = Process is done with the assigned piece of computation

Ring

P[i] does a send on ch[i+1]

P[i] does a receive on ch[i]

Termination Detection in a ring

  1. {invariant RING: P[1] blue implies
        (P[1] .. P[1+token] are blue) ^
        (ch[2] .. ch[token mod n + 1] are empty)}
  2. actions of P[1] when it first becomes idle:
         color[1] := blue; token := 0; send ch[2](token)
  3. actions of P[i: 1..n] upon receiving a regular message:
         color[i] := red
  4. actions of P[i: 2..n] upon receiving the token:
         color[i] := blue; token := token+1; send ch[i mod n + 1](token)
  5. actions of P[1] upon receiving the token:
         if color[1] = blue → announce termination and halt fi;
         color[1] := blue; token := 0; send ch[2](token)

Normal computation of P[i] is not shown.

The integer value of token is used only in the reasoning of the alg.

color is a single-int local variable; we use [i] to identify the process.

Figure 7.23 Termination detection in a ring

References

  1. Gregory R. Andrews, Concurrent Programming: Principles and Practice, Benjamin/Cummings, 1991. Chapter 7: Required Reading.

Copyright © 2014 pmateti@wright.edu