Die Präsentation wird geladen. Bitte warten

Die Präsentation wird geladen. Bitte warten

Distributed Algorithms - Ricart Agrawala Mutual Exclusion 2000-06-26 Thomas Lehner, Rene Mayrhofer Distributed Algorithms - Ricart Agrawala Mutual Exclusion.

Ähnliche Präsentationen


Präsentation zum Thema: "Distributed Algorithms - Ricart Agrawala Mutual Exclusion 2000-06-26 Thomas Lehner, Rene Mayrhofer Distributed Algorithms - Ricart Agrawala Mutual Exclusion."—  Präsentation transkript:

1 Distributed Algorithms - Ricart Agrawala Mutual Exclusion 2000-06-26 Thomas Lehner, Rene Mayrhofer Distributed Algorithms - Ricart Agrawala Mutual Exclusion Datum: 26. Juni 2000 Vortragende: Thomas Lehner Rene Mayrhofer

2 Distributed Algorithms - Ricart Agrawala Mutual Exclusion 2000-06-26 Thomas Lehner, Rene Mayrhofer Problemstellung gegeben: - asynchrones Netzwerk - eine Ressource - mehrere konkurrierende Prozesse und deren User gewünscht: - immer nur ein Prozess greift zugleich auf Ressource zu Beispiel: Drucker Grundlegende Idee des Ricart Agrawala Algorithmus: - Einführung einer Critical Region - Prozesse dürfen nur aus dieser auf Ressource zugreifen - Prozesse sprechen sich untereinander ab, so dass sich immer nur einer zugleich in der Critical Region befindet  exklusiver Zugriff auf Ressource Problemlösung

3 Distributed Algorithms - Ricart Agrawala Mutual Exclusion 2000-06-26 Thomas Lehner, Rene Mayrhofer Problemlösung User-Prozess Interaktion:Einzuhaltende Bedingungen: Well-formedness: - die User-Prozess Interaktion ist wohlgeformt Mutual exclution: - es ist kein globaler Zustand erreichbar, in dem sich mehr als ein Prozess in C befindet Progress: - wenn kein Prozess in C und zumindest einer in T ist, dann muss irgendwann einer in C eintreten - wenn zumindest ein Prozess in E ist, erreicht irgendwann einer auch R Lockout-freedom: - wenn alle User C immer verlassen, dann kommt ein Prozess der in T ist auch irgendwann in C - jeder der in E ist kommt auch irgendwann in R

4 Distributed Algorithms - Ricart Agrawala Mutual Exclusion 2000-06-26 Thomas Lehner, Rene Mayrhofer Der Ricart AgrawalaME Algorithmus - Jeder Prozess besitzt eine logische Uhr. Diese wird erhöht bei jedem Empfangs-/Sendeereignis und bei jeder User-Prozess Interaktion. - Es existieren die beiden Nachrichten TRY und OK. - Ein Prozess i kann nur dann in C eintreten, wenn er ein TRY- Broadcast durchgeführt hat und von alle anderen Prozessen die dazugehörigen OK’s erhielt. - Ein Prozess i darf nur dann mit einem OK auf ein (“try”,c) von j antworten wenn: 1. P i ist in E, R oder in T und das try i wurde noch nicht versand. 2. P i ist in T und (c,j)<(c´,i) mit (c´,“try“) ist letztes TRY von P i - In allen anderen Fällen wird das OK für P j zwischengespeichert und erst in E an P j versand.

5 Distributed Algorithms - Ricart Agrawala Mutual Exclusion 2000-06-26 Thomas Lehner, Rene Mayrhofer Beweis für Safety Beweis für Mutual Exclution durch indirekte Beweisführung: Annahme: Pi und Pj sind zu gleich in C mit (c(last try i ),i)<(c(last try j ),j)  es existieren die dazugehörigen zwei TRY und OK Nachrichten, die von P j an P i bzw. von P i an P j geschickt wurden, bevor die Prozesse in C eintraten zwei Mögliche Zeitpunkte an denen das TRY von P j bei P i ankam: - i hat sein TRY noch nicht versendet: dann kann P i kein TRY mit (c,i)<(c(last try j ),j) mehr broadcasten - i hat sein TRY bereits abgeschickt: dann kann P j kein OK von P i erhalten, da ja nach Annahme gilt (c(last tryi),i)<(c(last tryj),j) und somit P i das OK zurückhält bis Region E i erreicht wird

6 Distributed Algorithms - Ricart Agrawala Mutual Exclusion 2000-06-26 Thomas Lehner, Rene Mayrhofer Beweis für Liveness ·Beweis für Progress: (für Region E trivial) - Der Beweis für die Region T durch indirekten Beweis: Annahme: Zum Zeitpunkt a ist zumindest ein Prozess in T und keiner erreicht je C --> es existiert ein Zeitpunkt b vor a an dem alle Prozesse in R oder in T bleiben --> es existiert ein Zeitpunkt c nach b an dem alle Prozesse in T ihre TRY versand haben und keine dieser Nachrichten mehr in den Nachrichtenkanälen ist --> für einen P i in T gilt nun: (c(latest Try i ),i)<(c(latest Try j ),j) für alle P j in T Da P i nicht in C eintritt, muss es mindestens einen P j geben dessen OK fehlt Die 2 Gründe, weshalb P j das OK zurückhalten kann: - P j ist in C.  Widerspruch, da P j in R oder in T ist - P j ist in T und erhielt ein Try j mit (c(Try j ),j)<(c(latest Try k ),k) für alle P k in T (k  j)  Widerspruch, da gilt: (c(latest Try i ),i)<(c(latest Try j ),j) ·Beweis für Lockout-freedom: (für Region E trivial) - Der Beweis für die Region T durch indirekten Beweis: Annahme: P i ist in T mit versendetem TRY und tritt nie in C ein  nach dem maximal n*(c(latest try i )-1) + i-1 mal ein Prozess in C eintrat, tritt kein weiterer P j (j  i)mehr in C ein, da dann gelten würde (c(latest try j ),j)>(c(latest try i ),i)  Widerspruch zu Beweis von Progress

7 Distributed Algorithms - Ricart Agrawala Mutual Exclusion 2000-06-26 Thomas Lehner, Rene Mayrhofer Node i ( i  {1,....., N} ): Signature: Input: try i exit i recieve(m) j, i, m  {"try", "ok"}  N, 1 <= j <= N Output: crit i rem i send(m) i, j, m  {"ok"}  N, j  i bcast(m) i, m  {"try"}  N State Variables: region i  {R, T, C, E}, initially R clock i  N, initially 0 bcast-buffer i, a FIFO queue of {"try"}, initially empty for every j, 1 <= j, <= N: history(j) i, a subset of {"try", "ok"}  N, initially {} for every j ¹ i: send-buffer(j) i, a FIFO queue of {"ok"}, initially empty deferred-ok(j) i, a FIFO queue of {"ok"}, initially empty Transitions: try i Effect: clock i := clock i + 1 region i := T add ("try") to bcast-buffer i recieve(m, c) j, i Effect: clock i := max(clock i, c) + 1 history(j) i := history(j) i  {(m, c)} if m = "try" and j  i then if region i  {E, R} or (region i = T and bcast-buffer i contains ("try")) then add ("ok") to send-buffer(j) i else if region i = C then add ("ok") to deferred-ok(j) i else if region i = T and not bcast-buffer i contains ("try") if (c', i) > (c, j) with ("try", c')  history(i) i and  (c'' with ("try", c'')  history(i) i and c'' > c') then add ("ok") to send-buffer(j) i else add ("ok") to deferred-ok(j) i Formale Spezifikation - 1

8 Distributed Algorithms - Ricart Agrawala Mutual Exclusion 2000-06-26 Thomas Lehner, Rene Mayrhofer exit i Effect: clock i := clock i + 1 region i := E for all j  i for all (m, c) in deferred-ok(j) i add (m, c) to send-buffer(j) i clear deferred-ok(j) i crit i Precondition: region i = T for such a c that ("try", c)  history(i) i and  (c'' with ("try", c'')  history(i) i and c'' > c) for all j  i  ("ok", c')  history(j) i with (c, i) < (c', j) Effect: clock i := clock + 1 region i := C rem i Precondition: region i = E Effect: clock i := clock i +1 region i = R send(m, c) i, j Precondition: m ist first on send-buffer(j) i c = clock i + 1 Effect: clock i := clock i +1 remove first element of send-buffer(j) i bcast(m, c) i Precondition: m ist first on bcast-buffer i c = clock i + 1 Effect: clock i := clock i +1 remove first element of bcast-buffer i Tasks: { crit i } { rem i } { bcast(m) i } for every j  i: { send(m) i, j } Formale Spezifikation - 2

9 Distributed Algorithms - Ricart Agrawala Mutual Exclusion 2000-06-26 Thomas Lehner, Rene Mayrhofer package MutualExclusion; import daj.*; import java.util.*; public class RicartAgrawala extends Application { public static final int PNUM = 6; public static final int DISTANCE = 70; public static final int BORDER = 40;.... public void construct() { Node[] nodes = new Node[PNUM]; int i, j; double phi, r = (PNUM * DISTANCE) / (2 * Math.PI); for (i=0; i<PNUM; i++) { phi = (double) i / PNUM * Math.PI * 2; nodes[i] = node(new Prog(i), new Integer(i).toString(), (int) (r * Math.cos(phi) + r + BORDER), (int) (r * Math.sin(phi) + r + BORDER)); } for (i=0; i<PNUM; i++) { for (j=0; j<PNUM; j++) { link(nodes[i], nodes[j]); }.... }

10 Distributed Algorithms - Ricart Agrawala Mutual Exclusion 2000-06-26 Thomas Lehner, Rene Mayrhofer class Prog extends Program {... /** the node number */ protected int index; /** the region can be either R, T, C or E */ protected int region = R; /** the current clock value */ protected int clock = 0; /** for every node, the message history */ protected Vector[] history = null; /** for every neighbour, the buffer for remembering deferred ok messages until critical region has been left */ protected Vector[] deferred_ok = null; /** the logical time of the last "try" message that has been sent */ protected LogicalTime lastTryTime = null; /** the default constructor initializes the index and the arrays */ public Prog(int index) { this.index = index; history = new Vector[RicartAgrawala.PNUM]; for (int i=0; i<history.length; i++) history[i] = new Vector(); deferred_ok = new Vector[RicartAgrawala.PNUM]; for (int i=0; i<deferred_ok.length; i++) if (i != index) deferred_ok[i] = new Vector(); else deferred_ok[i] = null; }

11 Distributed Algorithms - Ricart Agrawala Mutual Exclusion 2000-06-26 Thomas Lehner, Rene Mayrhofer public void tryRessource() { // single actions must be atomic --> synchronize them synchronized (this) { clock++; region = T; lastTryTime = new LogicalTime(clock, index); out().send(new Msg(Msg.TRY, lastTryTime)); } public void freeRessource() { synchronized (this) { clock++; // this does not match the formal specification - the release is // done immediately region = R; for (int i=0; i<deferred_ok.length; i++) if (i != index) { for (int j=0; j<deferred_ok[i].size(); j++) { out(i).send((Msg) deferred_ok[i].get(j)); } deferred_ok[i].removeAllElements(); } public void ressourceAvailable() { // for testing purposes there is no user to ne notified } public void ressourceFreed() { // for testing purposes there is no user to ne notified }

12 Distributed Algorithms - Ricart Agrawala Mutual Exclusion 2000-06-26 Thomas Lehner, Rene Mayrhofer public void main() { Random rand = new Random(); GlobalAssertion safety = new CriticalRegionConflict(), liveness = new CriticalRegionLockout(); while (true) { assert(safety); assert(liveness); /* first the tree construction routines */ int received = in().select(2); if (received >= 0) { synchronized (this) { Msg msg = (Msg) in(received).receive(); if (msg.time.clock > clock) clock = msg.time.clock; clock++; history[received].add(msg); // respond to try requests from other nodes if (msg.type == Msg.TRY && received != index) handleTryMessage(received, msg); // check if the critical region can be entered now if (region == T && checkOkMsgsReceived()) { clock++; region = C; // the last try is invalid now lastTryTime = null; } // simulate the user here... }

13 Distributed Algorithms - Ricart Agrawala Mutual Exclusion 2000-06-26 Thomas Lehner, Rene Mayrhofer private void handleTryMessage(int received, Msg msg) { Msg ackMsg = new Msg(Msg.OK, new LogicalTime(clock, index)); if (region == R /*|| region == E*/) out(received).send(ackMsg); else if (region == C) deferred_ok[received].add(ackMsg); else if (region == T) { if (lastTryTime != null && msg.time.lessThan(lastTryTime)) out(received).send(ackMsg); else deferred_ok[received].add(ackMsg); } private boolean checkOkMsgsReceived() { boolean receivedOkMsgs = true; for (int i=0; i<history.length; i++) { if (i != index) { int j = history[i].size() - 1; while (j >= 0 && ((Msg) history[i].get(j)).type != Msg.OK) j--; if (j >= 0) { Msg lastMsg = (Msg) history[i].get(j); if (! lastTryTime.lessThan(lastMsg.time)) receivedOkMsgs = false; } else receivedOkMsgs = false; } return receivedOkMsgs; }

14 Distributed Algorithms - Ricart Agrawala Mutual Exclusion 2000-06-26 Thomas Lehner, Rene Mayrhofer class LogicalTime { /** the clock value */ public int clock; /** for equal clock values the sender process's index is compared */ public int process; public LogicalTime(int clock, int process) { this.clock = clock; this.process = process; } public boolean equals(Object o) { if (o instanceof LogicalTime && ((LogicalTime) o).clock == clock && ((LogicalTime) o).process == process) return true; else return false; } public boolean lessThan(LogicalTime t) { if (clock < t.clock || (clock == t.clock && process < t.process)) return true; else return false; }

15 Distributed Algorithms - Ricart Agrawala Mutual Exclusion 2000-06-26 Thomas Lehner, Rene Mayrhofer class CriticalRegionConflict extends GlobalAssertion { private Vector procsInCR = new Vector(); public boolean assert(Program progs[]) { int numOfProcsInCR = 0; procsInCR.removeAllElements(); for (int i=0; i<progs.length; i++) { if (((Prog) progs[i]).region == Prog.C) { numOfProcsInCR++; procsInCR.add(new Integer(i)); } return numOfProcsInCR <= 1; } public String getText() { String text = "Processes in the critical region: "; for (int i=0; i<procsInCR.size(); i++) text += procsInCR.get(i) + " "; return text; }

16 Distributed Algorithms - Ricart Agrawala Mutual Exclusion 2000-06-26 Thomas Lehner, Rene Mayrhofer class CriticalRegionLockout extends GlobalAssertion { private LogicalTime[] tryTimes = new LogicalTime[RicartAgrawala.PNUM]; private int procInCR = -1, procTryingLonger = -1; public CriticalRegionLockout() { for (int i=0; i<tryTimes.length; i++) tryTimes[i] = null; } public boolean assert(Program progs[]) { for (int i=0; i<progs.length; i++) if (((Prog) progs[i]).region == Prog.T) tryTimes[i] = ((Prog) progs[i]).lastTryTime; // Now check when a process is in the CR, if another one is still // trying but started to try earlier. This should not happen. for (int i=0; i<progs.length; i++) if (((Prog) progs[i]).region == Prog.C) { for (int j=0; j<progs.length; j++) { if (((Prog) progs[j]).region == Prog.T && tryTimes[j].lessThan(tryTimes[i])) { procInCR = i; procTryingLonger = j; return false; } return true; }


Herunterladen ppt "Distributed Algorithms - Ricart Agrawala Mutual Exclusion 2000-06-26 Thomas Lehner, Rene Mayrhofer Distributed Algorithms - Ricart Agrawala Mutual Exclusion."

Ähnliche Präsentationen


Google-Anzeigen