150 likes | 254 Views
Chapter 11 Detecting Termination and Deadlocks. Motivation – Diffusing computation. Started by a special process, the environment environment sends messages to some processes Each process is either active or passive passive process can become active only on receiving a message.
E N D
Motivation – Diffusing computation • Started by a special process, the environment • environment sends messages to some processes • Each process is either active or passive • passive process can become active only on receiving a message
Diffusing computation- Finding the shortest path • Problem : find the shortest path from process P0 to all other processes • Each process knows only the delay of its incoming links • Process Pi maintains: • cost of currently known shortest path to P0 • parent of Pi in the shortest path known
Diffusing computation- Finding the shortest path • Coordinator P0 starts diffusing computation by sending cost = 0 to neighbors • Pi on receiving a cost c from Pj determines if c + cost(link between Pi and Pj) is less than the current cost • If yes: update cost = c and • parent = Pj
A diffusing algorithm for the shortest path publicclass ShortestPath extends Process { int parent = -1; int cost = -1; int edgeWeight[] = null; public ShortestPath(Linker initComm, int initCost[]) { super(initComm); edgeWeight = initCost; } publicvoid initiate() { if (myId == Symbols.coordinator) { parent = myId; cost = 0; sendToNeighbors("path", cost); } } publicsynchronizedvoid handleMsg(Message m, int source, String tag) { if (tag.equals("path")) { int dist = m.getMessageInt(); if ((parent == -1) || (dist + edgeWeight[source] < cost)) { parent = source; cost = dist + edgeWeight[source]; System.out.println("New cost is " + cost); sendToNeighbors("path", cost); } } } }
Dijkstra and Scholten’s Algorithm • A process is in a green state if it is passive and all its outgoing channels are empty, otherwise, it is in a red state. • The computation has terminated if all processes are green
Dijkstra and Scholten’s Algorithm • Maintain a set T such that • I0: All red processes are part of T • Initially only the environment Pe2 T • If Pj turns Pkred and Pk was not in T then add Pk to T • Maintain a directed graph (T,E) on set T such that if Pk was added to T due to Pj then add an edge from Pj to Pk (Pj s the parent of Pk) • I1: The edges E form a spanning tree rooted at Pe • Remove Pk from T and the edge to Pk from E only if Pk is a green leaf node
An Optimization • To detect if channel is empty (to determine if the process is green) we could send a signal message (acknowledgements) for every message received • Optimization: A node does send the signal message to the process that made it active until it is ready to leave the tree
publicclass DSTerm extends Process implements TermDetector { int state = passive; int D = 0; int parent = -1; boolean envtFlag; . . . publicsynchronizedvoid handleMsg(Msg m, int src, String tag) { if (tag.equals("signal")) { D = D - 1; if (D == 0) { if (envtFlag) System.out.println("Termination Detected"); elseif (state == passive) { sendMsg(parent, "signal"); parent = -1; } } } else { // application message state = active; if ((parent == -1) && !envtFlag) { parent = src; } else sendMsg(src, "signal"); } } publicsynchronizedvoid sendAction() { D = D + 1; } publicsynchronizedvoid turnPassive() { state = passive; if ((D == 0) && (parent != -1)) { sendMsg(parent, "signal"); parent = -1; } } }
Termination detection without Acknowledgements • Token based algorithm • Each process maintains • state: active or passive. • color: black / white • white - it has not received any message since the last visit of the token • c: number of messages sent by the process minus the number of messages received
Termination detection without Acknowledgements • Token • color: • Records if the token has seen any black process • count: • Records the sum of all c variables in a round • A process P0 is responsible for detecting termination • System has terminated if • (color P0 is white) and (it is passive) and (token is white) and (count + value c at P0 = 0)
Locally Stable Predicates • A local predicate B is locally stable if no process involved in the predicate can change its state relative to B once B holds • E.g. Predicate “the distributed computation has terminated” • E.g. A stable predicate which is not locally predicate : “There is at most one token in the system” (if generation of new tokens is not possible in the system)
Consistent Interval • Computation of a consistent global state is not necessary for locally stable predicates • Consistent interval: • An interval [X,Y] is a pair of cuts X,Y such that X µ Y • An interval of cuts [X,Y] is consistent if there exists a consistent cut G such that X µ G µ Y • An interval is consistent iff
Algorithm outline • Repeatedly compute consistent intervals [X,Y] • If a predicate is true in cut Y and • The values of the variables in predicate B have not changed during the interval Then B is true (though X,Y may be inconsistent cuts)
Application : Deadlock Detection (Contd.) • Coordinator P_0 requests report periodically from all processes • P_i send its WFG if changed_i is true otherwise it send the message notChanged_i • If the combined WFG has a cycle P_0 requests reports from all processes again • If everyone sends notChanged then a deadlock is detected