# Concurrent and Distributed Programming

Document Sample

```					Concurrent and Distributed
Programming
lecture 6
Pieter Hartel
Distributed Algorithms
What is the problem about Distributed Algorithms?

(non)existence of global time

local states vs global state

simultaneity vs consistency
Network Algorithms
Characteristics

 a network is a connected graph
 each node can communicate with its neighbours
(sometimes it is required that a node can
communicate with all other nodes)
 nodes have IDs, otherwise they are indistinguishable
 a network algorithm is a schema, i.e. it runs on an
arbitrary network topology, not only for a fixed one
 all nodes run the same algorithm
Network Algorithms
Examples
• Leader election and spanning trees

• Echo Algorithm

• Distributed Termination

• Consensus in Networks

• Mutual Exclusion in Networks

• Phase Synchronisation on Undirected Trees

• Distributed Self Stabilization
Network Algorithms Examples

Problem:
 elect one site (node) as leader
 if the leader crashes select a new one

Applications:
 service discovery - need for a central
 radio networks - control the message traffic
perform message routing
…
Network Algorithms Examples
Assumptions:
 each node has a unique name
 there is a total order on the names
(e.g. natural numbers)

Algorithmic idea:
 the node with the largest number will be leader
 each node has a candidate, initially itself,
 initially, and also when there is an update,
all neighbours are informed
 termination, if each node knows the leader
Network Algorithms Examples
for Minimal Spanning Trees
Algorithmic idea:
 nodes send also the information about distance from
 neighbour with minimal distance to the leader
is chosen as parent

1        2       3
try the algorithm for
this example
5        4
Network Algorithms Examples
Echo Algorithm
Problem:
 initiator sends broadcast message through the network
 termination if all sites are informed

Algorithmic idea:
 each node that receives a message passes this on to all its
neighbours, exept the one from which it has received the
message
 leaves get concurrently messages from all their neighbours
 leaf sends acknowledgements to parent node
 nodes having receiving an acknowledgement form all their
children send an acknowledgement to their parent

what, if there are many initiators?
Network Algorithms Examples
Echo Algorithm

Algorithmic idea:
 each node that receives a message passes this on to all its
neighbours, exept the one from which it has received the
message
 leaves get concurrently messages from all their neighbours
 leaf sends acknowledgements to parent node
 nodes having receiving an acknowledgement form all their
children send an acknowledgement to their parent

1        2        3
try the algorithm for
this example
5       4
Network Algorithms Examples
Distributed Termination
Problem:
detect whether all (processes in all) nodes have terminated
 topology: connected directed graph with back edges
root node (no incoming edges,
every node accessible from it)
 all nodes exept root node are initially inactive
 nodes start when they get an incoming message, process,
send possibly messages to other nodes,terminate eventually

Algorithmic idea:
 wait for reply signal for each message sent
have terminated and all expected reply signals arrived
stores the number
Network Algorithms Examples             of messages it got
per incoming edge
stores the number
Distributed Termination
of messages it sent
integer array[incoming] inDeficit  [0,…,0]
on all outgoing edges
integer inDeficit  0
integer outDeficit  0
send(message(destination, myID)
increment outDeficit                             send message

increment inDeficit[source] and inDeficit
when inDeficit>1 or
(inDeficit=1 and isTerminated and outDeficit=0)
select E with inDeficit[E]≠0
send(signal, E, myID)                            send signal
decrement inDeficit[E] and inDeficit
decrement outDeficit
Network Algorithms Examples
Distributed Termination

1                try the algorithm
for this topology
2             3

If the system terminates,
the source node eventually announces termination.

If the source node announces termination,
the system possibly has not terminated.
Network Algorithms Examples
Distributed Termination
algorithmic idea for the repaired algorithm:
 on a tree the algorithm works perfectly
 so, build implicitly a spanning tree:

 parent becomes the first node that sends a message
 the last signal is sent to the parent
 a node sends it last signal only when outDeficit is 0

1
try this version on the
same example                 2              3
Network Algorithms Examples
Consensus in Networks
Problem:
algorithm organizes consensus about some contract
or agreement among the nodes of a network
 message passing is the only communication medium

 crash failures: nodes simply stops sending messages
 byzantine failures: nodes may send arbitrary messages

Byzantine generals:
 group of Byzantine armies is surrounding an enemy city
 if they attack together, they can capture the city
 if only some attack they will be defeated
 they can retreat
 some generals are traitors, messengers are reliable
 find an algorithm for consensus following a majority vote
Network Algorithms Examples
Consensus in Networks
Algorithmic idea:
 send own plan to all othe generals
 wait for the plans of all other generals
 decide what the majority vote is
naive one-round algorithm

plan Type finalPlan
plan Type array[generals] plan

plan[myID]  chooseAttackOrRetreat
for all other generals G
send(G, myID, plan[myID])
for all other generals G
finalPlan  majority(plan)
Network Algorithms Examples
Mutual Exclusion in Networks

local mutual exclusion:
neighboring nodes are never critical at the same time
(e.g. dining philosophers)

global mutual exclusion:
in the entire network,
at most one node is in its critical section
Network Algorithms Examples
Global Mutual Exclusion in Networks

Algorithmic idea:

 choose the highest number known
 send request with number to all other nodes
 wait for reply from all other nodes
 get into the critical section

 reply to a message, if number (or ID) is lower
 store the ID of the applying node otherwise
integer myNum  0
set of node IDs deferred  empty set
Network Algorithms Examples
Distributed Mutual Exclusion

integer highestNum  0
boolean requestCS  false
two-rounds algorithm

Main
loop forever
p1: noncritical
p2: requestCS  true
p3: myNum  highestNum+1
p4: for all other nodes N
p5:     send(request, N, myID, myNum)
p6: await reply‘s from all other nodes
p7: critical section
p8: requestCS  false
p9: for all nodes N in deferred
p10: remove N from deferred
Network Algorithms Examples
Distributed Mutual Exclusion

integer myNum  0
set of node IDs deferred  empty set
integer highestNum  0
two-rounds algorithm

boolean requestCS  false
integer source, requestedNum
loop forever
p13: highestNum  max(highestNum, requestedNum)
p14: if not requestCS or requestedNUM << myNum