• Keine Ergebnisse gefunden

SynchronisationTeilI:ModellannahmenunddasMutual-Exclusion-Problem Nebenl¨aufigenProgrammierung

N/A
N/A
Protected

Academic year: 2021

Aktie "SynchronisationTeilI:ModellannahmenunddasMutual-Exclusion-Problem Nebenl¨aufigenProgrammierung"

Copied!
254
0
0

Wird geladen.... (Jetzt Volltext ansehen)

Volltext

(1)

Prinzipien, Modelle und Algorithmen der

Nebenl¨aufigen Programmierung

Wintersemester 2019/20

Synchronisation Teil I: Modellannahmen und das Mutual-Exclusion-Problem

Prof. Dr. David Sabel

LFE Theoretische Informatik

Letzte ¨Anderung der Folien: 27. November 2019

(2)

Ubersicht ¨

1

Modell

Nebenl¨ aufiges Programm Interleaving

Fairness Weitere

2

Das Mutual-Exclusion-Problem f¨ ur 2 Prozesse Einleitendes Beispiel: Too much milk

Formalisierung des Mutual-Exclusion-Problems

Algorithmen f¨ ur 2 Prozesse mit atomarem Read & Write

3

Das Mutual-Exclusion-Problem f¨ ur n Prozesse Tournament-Algorithmen

Lamports Algorithmus Bakery-Algorithmus

4

Drei Komplexit¨ atsresultate

Eine genaue Schranke f¨ ur den Platzbedarf

Ein Resultat zur Laufzeit

(3)

Modellannahmen: Nebenl¨ aufiges Programm

Nebenl¨aufiges Programm

Nebenl¨aufiges Programm:Endliche Menge von Prozessen Prozess:Sequentielles Programm aus atomaren

Berechnungsschritten

Annahme f¨ ur unsere s¨ amtlichen Modelle

Definition von

Berechnungsschritt

modellabh¨ angig!

TCS | 03 Synchronisation I | WS 2019/20 3/94 Modell Mutual-Exclusion-Problem nProzesse Komplexit¨at

(4)

Modellannahmen: Interleaving-Annahme

Interleaving-Annahme

Ausf¨uhrung eines nebenl¨aufigen Programms:

Sequenz der atomaren Berechnungsschritte der Prozesse, diebeliebig durchmischt sein k¨onnen.

Nur ein Berechnungsschritt

gleichzeitig,

d.h. zwei Schritte

¨uberlappen nie

(Fast) beliebiges Durchmischen (Interleaving) von Berechnungsschritten der einzelnen Prozesse (Fast) alle Ausf¨ uhrungsreihenfolgen erlaubt

” Fast“ wird sp¨ ater erkl¨ art

(5)

Warum ist die Interleaving-Annahme sinnvoll?

Granularit¨at

Berechnungsschritte k¨ onnen beliebig definiert werden (d.h. beliebig klein)

Multitasking-Systeme

Einprozessor-Systeme mit Multitasking machen Interleaving

Allerdings: Reale Implementierungen benutzen meist Verfahren (z.B.

Zeitscheiben), die nicht jede Reihenfolge zulassen.

“Prozess f¨ uhrt eine Zeit lang Schritte aus, dann wird gewechselt”

Aber: “eine Zeit lang” problematisch:

Formal schwer zu fassen, kompliziert

Neue (schnellere) Hardware erfordert Modellanpassung

Ber¨ucksichtigung von Umwelteinfl¨ussen (z.B. Phasenschwankungen)

Alle Reihenfolgen umfassen auch die kleinere Menge der realen Reihenfolgen (Korrektheit aller impliziert Korrektheit eines Teils)

TCS | 03 Synchronisation I | WS 2019/20 5/94 Modell Mutual-Exclusion-Problem nProzesse Komplexit¨at

(6)

Warum ist die Interleaving-Annahme sinnvoll? (2)

Multiprozessor-Systeme

Annahme scheint unrealistisch, parallele und ¨ uberlappende Berechnungsschritte m¨ oglich

Aber: Gemeinsame Ressourcen (z.B. Speicher), sind auf Hardwareebene vor parallelen Zugriffen gesch¨ utzt und erzwingen Sequentialisierung

Ressourcen-autonome Schritte sind immer noch parallel

Das ist aber nicht sichtbar!

(7)

Modellannahmen: Die Fairness-Annahme

vorhin: . . .

Fast

beliebiges Interleaving . . .

Die Einschr¨ ankung kommt durch die Fairness-Annahme Verschieden starke Annahme von Fairness

Unser Begriff relativ schwach

Fairness-Annahme

Jeder Prozess f¨ ur den ein Berechnungsschritt m¨ oglich ist, f¨ uhrt in der Gesamt-Auswertungssequenz diesen Schritt nach endlich vielen Berechnungsschritten durch.

TCS | 03 Synchronisation I | WS 2019/20 7/94 Modell Mutual-Exclusion-Problem nProzesse Komplexit¨at

(8)

Beispiel zur Fairness

(X am Anfang 0)

Prozess

P

: Prozess

Q:

(P1)

if X 6=

10

then goto

(P1); (Q1)

X:=10;

Auswertungssequenz die keine Fairness beachtet

(P1), (P1), (P1), (P1), (P1), (P1), . . . unendlich so weiter Beachte: Die Fairness-Annahme macht nur eine Aussage ¨ uber

unendliche

Auswertungsreihenfolgen

Auswertungssequenzen unter Beachtung von Fairness (P1),(P1), . . . ,(P1)

| {z }

n-mal

(Q1)(P1)

f¨ ur beliebiges

n∈N0

(9)

Beispiel zur Fairness (2)

(X am Anfang 0)

Prozess

P

: Prozess

Q:

(P1)

if X 6=

10

then goto

(P1); (Q1)

X:=10;

Folgerung

Unter Einhaltung der Fairness-Annahme:

Obiges Programm

terminiert immer

(Da (Q1) irgendwann ausgef¨ uhrt werden

muss)

TCS | 03 Synchronisation I | WS 2019/20 9/94 Modell Mutual-Exclusion-Problem nProzesse Komplexit¨at

(10)

Noch ein Beispiel zur Fairness

(Der Wert von

X

vor der Ausf¨ uhrung sei 0)

Prozess

P

: Prozess

Q:

(P1)

if

X

6=

10

then goto

(P1); (Q1) X:=10;

(P2)

goto

(P1)

Erlaubte Sequenzen (P1),(P1), . . . ,(P1)

| {z }

n-mal

(Q1) (P1),(P2),(P1),(P2), . . .

| {z }

unendlich oft

mit

n∈N0

Verbotene Sequenzen

(P1),(P1),(P1), . . .

unendlich lange

(11)

Weitere Annahmen

Bekannte Prozesse

Ein Programm besteht aus einer

festen Anzahl

von Prozessen Es werden keine Prozesse vom Programm neu erzeugt

Prozesse sind identifizierbar

Achtung!

Diese Annahme werden wir nicht f¨ ur die ganze Vorlesung beibehalten!

TCS | 03 Synchronisation I | WS 2019/20 11/94 Modell Mutual-Exclusion-Problem nProzesse Komplexit¨at

(12)

Weitere Annahmen (2)

Programmiersprache

Zun¨ achst gehen wir von einer kleinen

Pseudo-Programmiersprache aus, die imperativ ist.

Befehle:

if-then,goto, ZuweisungenX := 10, while-Schleifen usw.

Jede nummerierte Zeile wird

atomar

ausgef¨ uhrt.

erlaubte Operationen auf dem Speicher werden wir festlegen

(13)

Das Problem vom wechselseitigen Ausschluss

TCS | 03 Synchronisation I | WS 2019/20 13/94 Modell Mutual-Exclusion-Problem nProzesse Komplexit¨at

(14)

Das Mutual-Exclusion Problem

Einleitendes Beispiel: Too much milk

Alice und Bob teilen sich einen K¨ uhlschrank Sie wollen, dass stets genug Milch im K¨ uhlschrank Weder zuviel noch zu wenig Milch.

Naive L¨osung

Wenn jemand sieht, dass keine Milch im K¨ uhlschrank ist, geht er

Milch kaufen.

(15)

Naive L¨ osung funktioniert nicht

Alice Bob

17:00 kommt nach hause

17:05 bemerkt: keine Milch im K¨uhl- schrank

17:10 geht zum Supermarkt

17:15 kommt nach hause

17:20 bemerkt: keine Milch im K¨uhl-

schrank

17:25 geht zum Supermarkt

17:30 kommt vom Supermarkt zur¨uck, packt Milch in den K¨uhlschrank

17:40 kommt vom Supermarkt zur¨uck,

packt Milch in den K¨uhlschrank

TCS | 03 Synchronisation I | WS 2019/20 15/94 Modell Mutual-Exclusion-Problem nProzesse Komplexit¨at

(16)

Naive L¨ osung funktioniert nicht

Alice Bob

17:00 kommt nach hause

17:05 bemerkt: keine Milch im K¨uhl- schrank

17:10 geht zum Supermarkt

17:15 kommt nach hause

17:20 bemerkt: keine Milch im K¨uhl-

schrank

17:25 geht zum Supermarkt

17:30 kommt vom Supermarkt zur¨uck, packt Milch in den K¨uhlschrank

17:40 kommt vom Supermarkt zur¨uck,

packt Milch in den K¨uhlschrank

(17)

Naive L¨ osung funktioniert nicht

Alice Bob

17:00 kommt nach hause

17:05 bemerkt: keine Milch im K¨uhl- schrank

17:10 geht zum Supermarkt

17:15 kommt nach hause

17:20 bemerkt: keine Milch im K¨uhl-

schrank

17:25 geht zum Supermarkt

17:30 kommt vom Supermarkt zur¨uck, packt Milch in den K¨uhlschrank

17:40 kommt vom Supermarkt zur¨uck,

packt Milch in den K¨uhlschrank

TCS | 03 Synchronisation I | WS 2019/20 15/94 Modell Mutual-Exclusion-Problem nProzesse Komplexit¨at

(18)

Naive L¨ osung funktioniert nicht

Alice Bob

17:00 kommt nach hause

17:05 bemerkt: keine Milch im K¨uhl- schrank

17:10 geht zum Supermarkt

17:15 kommt nach hause

17:20 bemerkt: keine Milch im K¨uhl-

schrank

17:25 geht zum Supermarkt

17:30 kommt vom Supermarkt zur¨uck, packt Milch in den K¨uhlschrank

17:40 kommt vom Supermarkt zur¨uck,

packt Milch in den K¨uhlschrank

(19)

Naive L¨ osung funktioniert nicht

Alice Bob

17:00 kommt nach hause

17:05 bemerkt: keine Milch im K¨uhl- schrank

17:10 geht zum Supermarkt

17:15 kommt nach hause

17:20 bemerkt: keine Milch im K¨uhl-

schrank

17:25 geht zum Supermarkt

17:30 kommt vom Supermarkt zur¨uck, packt Milch in den K¨uhlschrank

17:40 kommt vom Supermarkt zur¨uck,

packt Milch in den K¨uhlschrank

TCS | 03 Synchronisation I | WS 2019/20 15/94 Modell Mutual-Exclusion-Problem nProzesse Komplexit¨at

(20)

Naive L¨ osung funktioniert nicht

Alice Bob

17:00 kommt nach hause

17:05 bemerkt: keine Milch im K¨uhl- schrank

17:10 geht zum Supermarkt

17:15 kommt nach hause

17:20 bemerkt: keine Milch im K¨uhl-

schrank

17:25 geht zum Supermarkt

17:30 kommt vom Supermarkt zur¨uck, packt Milch in den K¨uhlschrank

17:40 kommt vom Supermarkt zur¨uck,

packt Milch in den K¨uhlschrank

(21)

Naive L¨ osung funktioniert nicht

Alice Bob

17:00 kommt nach hause

17:05 bemerkt: keine Milch im K¨uhl- schrank

17:10 geht zum Supermarkt

17:15 kommt nach hause

17:20 bemerkt: keine Milch im K¨uhl-

schrank

17:25 geht zum Supermarkt

17:30 kommt vom Supermarkt zur¨uck, packt Milch in den K¨uhlschrank

17:40 kommt vom Supermarkt zur¨uck,

packt Milch in den K¨uhlschrank

TCS | 03 Synchronisation I | WS 2019/20 15/94 Modell Mutual-Exclusion-Problem nProzesse Komplexit¨at

(22)

Naive L¨ osung funktioniert nicht

Alice Bob

17:00 kommt nach hause

17:05 bemerkt: keine Milch im K¨uhl- schrank

17:10 geht zum Supermarkt

17:15 kommt nach hause

17:20 bemerkt: keine Milch im K¨uhl-

schrank

17:25 geht zum Supermarkt

17:30 kommt vom Supermarkt zur¨uck, packt Milch in den K¨uhlschrank

17:40 kommt vom Supermarkt zur¨uck,

packt Milch in den K¨uhlschrank

(23)

Naive L¨ osung funktioniert nicht

Alice Bob

17:00 kommt nach hause

17:05 bemerkt: keine Milch im K¨uhl- schrank

17:10 geht zum Supermarkt

17:15 kommt nach hause

17:20 bemerkt: keine Milch im K¨uhl-

schrank

17:25 geht zum Supermarkt

17:30 kommt vom Supermarkt zur¨uck, packt Milch in den K¨uhlschrank

17:40 kommt vom Supermarkt zur¨uck,

packt Milch in den K¨uhlschrank

=⇒ Zuviel Milch im K¨uhlschrank

TCS | 03 Synchronisation I | WS 2019/20 15/94 Modell Mutual-Exclusion-Problem nProzesse Komplexit¨at

(24)

Too much milk: Spezifikation

Alice und Bob vereinbaren,

Notizen am K¨uhlschrank

zu hinterlassen, die beide lesen k¨ onnen.

Dadurch soll sichergestellt werden, dass:

H¨ ochstens eine der beiden Personen geht Milch kaufen, wenn keine Milch im K¨ uhlschrank ist.

Eine der beiden Personen geht Milch kaufen, wenn keine Milch im K¨ uhlschrank ist.

Nicht erlaubt:

Nur Bob kauft Milch. Weil: Alice dann u.U.

unendlich lange auf Milch wartet. (Verletzt 2. Bedingung)

Annahme:

Alice und Bob k¨ onnen sich nicht sehen, d.h. sie

kommunizieren nur ¨ uber die Notizen

(25)

1. L¨ osungsversuch

Wenn keine Notiz am K¨ uhlschrank und keine Milch im

K¨ uhlschrank ist, dann schreibe Notiz an den K¨ uhlschrank, gehe Milch kaufen, stelle die Milch in den K¨ uhlschrank und entferne danach die Notiz am K¨ uhlschrank.

Als Prozesse:

Programm f¨ur Alice: Programm f¨ur Bob:

(A1)ifkeine Notizthen (B1)ifkeine Notizthen (A2) ifkeine Milchthen (B2) ifkeine Milchthen (A3) schreibe Notiz; (B3) schreibe Notiz;

(A4) kaufe Milch; (B4) kaufe Milch;

(A5) entferne Notiz; (B5) entferne Notiz;

Notiz

TCS | 03 Synchronisation I | WS 2019/20 17/94 Modell Mutual-Exclusion-Problem nProzesse Komplexit¨at

(26)

1. L¨ osungsversuch

Wenn keine Notiz am K¨ uhlschrank und keine Milch im

K¨ uhlschrank ist, dann schreibe Notiz an den K¨ uhlschrank, gehe Milch kaufen, stelle die Milch in den K¨ uhlschrank und entferne danach die Notiz am K¨ uhlschrank.

Als Prozesse:

Programm f¨ur Alice: Programm f¨ur Bob:

(A1)ifkeine Notizthen (B1)ifkeine Notizthen (A2) ifkeine Milchthen (B2) ifkeine Milchthen (A3) schreibe Notiz; (B3) schreibe Notiz;

(A4) kaufe Milch; (B4) kaufe Milch;

(A5) entferne Notiz; (B5) entferne Notiz;

Notiz

(27)

1. L¨ osungsversuch

Wenn keine Notiz am K¨ uhlschrank und keine Milch im

K¨ uhlschrank ist, dann schreibe Notiz an den K¨ uhlschrank, gehe Milch kaufen, stelle die Milch in den K¨ uhlschrank und entferne danach die Notiz am K¨ uhlschrank.

Als Prozesse:

Programm f¨ur Alice: Programm f¨ur Bob:

(A1)ifkeine Notizthen (B1)ifkeine Notizthen (A2) ifkeine Milchthen (B2) ifkeine Milchthen (A3) schreibe Notiz; (B3) schreibe Notiz;

(A4) kaufe Milch; (B4) kaufe Milch;

(A5) entferne Notiz; (B5) entferne Notiz;

Notiz

TCS | 03 Synchronisation I | WS 2019/20 17/94 Modell Mutual-Exclusion-Problem nProzesse Komplexit¨at

(28)

1. L¨ osungsversuch

Wenn keine Notiz am K¨ uhlschrank und keine Milch im

K¨ uhlschrank ist, dann schreibe Notiz an den K¨ uhlschrank, gehe Milch kaufen, stelle die Milch in den K¨ uhlschrank und entferne danach die Notiz am K¨ uhlschrank.

Als Prozesse:

Programm f¨ur Alice: Programm f¨ur Bob:

(A1)ifkeine Notizthen (B1)ifkeine Notizthen (A2) ifkeine Milchthen (B2) ifkeine Milchthen (A3) schreibe Notiz; (B3) schreibe Notiz;

(A4) kaufe Milch; (B4) kaufe Milch;

(A5) entferne Notiz; (B5) entferne Notiz;

Notiz

(29)

1. L¨ osungsversuch

Wenn keine Notiz am K¨ uhlschrank und keine Milch im

K¨ uhlschrank ist, dann schreibe Notiz an den K¨ uhlschrank, gehe Milch kaufen, stelle die Milch in den K¨ uhlschrank und entferne danach die Notiz am K¨ uhlschrank.

Als Prozesse:

Programm f¨ur Alice: Programm f¨ur Bob:

(A1)ifkeine Notizthen (B1)ifkeine Notizthen (A2) ifkeine Milchthen (B2) ifkeine Milchthen (A3) schreibe Notiz; (B3) schreibe Notiz;

(A4) kaufe Milch; (B4) kaufe Milch;

(A5) entferne Notiz; (B5) entferne Notiz;

Notiz

TCS | 03 Synchronisation I | WS 2019/20 17/94 Modell Mutual-Exclusion-Problem nProzesse Komplexit¨at

(30)

1. L¨ osungsversuch

Wenn keine Notiz am K¨ uhlschrank und keine Milch im

K¨ uhlschrank ist, dann schreibe Notiz an den K¨ uhlschrank, gehe Milch kaufen, stelle die Milch in den K¨ uhlschrank und entferne danach die Notiz am K¨ uhlschrank.

Als Prozesse:

Programm f¨ur Alice: Programm f¨ur Bob:

(A1)ifkeine Notizthen (B1)ifkeine Notizthen (A2) ifkeine Milchthen (B2) ifkeine Milchthen (A3) schreibe Notiz; (B3) schreibe Notiz;

(A4) kaufe Milch; (B4) kaufe Milch;

(A5) entferne Notiz; (B5) entferne Notiz;

Notiz

(31)

1. L¨ osungsversuch

Wenn keine Notiz am K¨ uhlschrank und keine Milch im

K¨ uhlschrank ist, dann schreibe Notiz an den K¨ uhlschrank, gehe Milch kaufen, stelle die Milch in den K¨ uhlschrank und entferne danach die Notiz am K¨ uhlschrank.

Als Prozesse:

Programm f¨ur Alice: Programm f¨ur Bob:

(A1)ifkeine Notizthen (B1)ifkeine Notizthen (A2) ifkeine Milchthen (B2) ifkeine Milchthen (A3) schreibe Notiz; (B3) schreibe Notiz;

(A4) kaufe Milch; (B4) kaufe Milch;

(A5) entferne Notiz; (B5) entferne Notiz;

Notiz

TCS | 03 Synchronisation I | WS 2019/20 17/94 Modell Mutual-Exclusion-Problem nProzesse Komplexit¨at

(32)

1. L¨ osungsversuch

Wenn keine Notiz am K¨ uhlschrank und keine Milch im

K¨ uhlschrank ist, dann schreibe Notiz an den K¨ uhlschrank, gehe Milch kaufen, stelle die Milch in den K¨ uhlschrank und entferne danach die Notiz am K¨ uhlschrank.

Als Prozesse:

Programm f¨ur Alice: Programm f¨ur Bob:

(A1)ifkeine Notizthen (B1)ifkeine Notizthen (A2) ifkeine Milchthen (B2) ifkeine Milchthen (A3) schreibe Notiz; (B3) schreibe Notiz;

(A4) kaufe Milch; (B4) kaufe Milch;

(A5) entferne Notiz; (B5) entferne Notiz;

Notiz

(33)

1. L¨ osungsversuch

Wenn keine Notiz am K¨ uhlschrank und keine Milch im

K¨ uhlschrank ist, dann schreibe Notiz an den K¨ uhlschrank, gehe Milch kaufen, stelle die Milch in den K¨ uhlschrank und entferne danach die Notiz am K¨ uhlschrank.

Als Prozesse:

Programm f¨ur Alice: Programm f¨ur Bob:

(A1)ifkeine Notizthen (B1)ifkeine Notizthen (A2) ifkeine Milchthen (B2) ifkeine Milchthen (A3) schreibe Notiz; (B3) schreibe Notiz;

(A4) kaufe Milch; (B4) kaufe Milch;

(A5) entferne Notiz; (B5) entferne Notiz;

Notiz

TCS | 03 Synchronisation I | WS 2019/20 17/94 Modell Mutual-Exclusion-Problem nProzesse Komplexit¨at

(34)

1. L¨ osungsversuch

Wenn keine Notiz am K¨ uhlschrank und keine Milch im

K¨ uhlschrank ist, dann schreibe Notiz an den K¨ uhlschrank, gehe Milch kaufen, stelle die Milch in den K¨ uhlschrank und entferne danach die Notiz am K¨ uhlschrank.

Als Prozesse:

Programm f¨ur Alice: Programm f¨ur Bob:

(A1)ifkeine Notizthen (B1)ifkeine Notizthen (A2) ifkeine Milchthen (B2) ifkeine Milchthen (A3) schreibe Notiz; (B3) schreibe Notiz;

(A4) kaufe Milch; (B4) kaufe Milch;

(A5) entferne Notiz; (B5) entferne Notiz;

Notiz

(35)

1. L¨ osungsversuch

Wenn keine Notiz am K¨ uhlschrank und keine Milch im

K¨ uhlschrank ist, dann schreibe Notiz an den K¨ uhlschrank, gehe Milch kaufen, stelle die Milch in den K¨ uhlschrank und entferne danach die Notiz am K¨ uhlschrank.

Als Prozesse:

Programm f¨ur Alice: Programm f¨ur Bob:

(A1)ifkeine Notizthen (B1)ifkeine Notizthen (A2) ifkeine Milchthen (B2) ifkeine Milchthen (A3) schreibe Notiz; (B3) schreibe Notiz;

(A4) kaufe Milch; (B4) kaufe Milch;

(A5) entferne Notiz; (B5) entferne Notiz;

Notiz

TCS | 03 Synchronisation I | WS 2019/20 17/94 Modell Mutual-Exclusion-Problem nProzesse Komplexit¨at

(36)

2. L¨ osungsversuch

Hinterlasse als erstes eine Notiz am K¨ uhlschrank, dann pr¨ ufe ob eine Notiz des anderen vorhanden ist. Nur wenn keine weitere Notiz vorhanden ist, pr¨ ufe ob Milch vorhanden ist und gehe Milch kaufen, wenn keine Milch vorhanden ist. Danach entferne die Notiz.

Als Prozesse:

Programm f¨ur Alice: Programm f¨ur Bob:

(A1)schreibe Notiz “Alice”; (B1) schreibe Notiz “Bob”;

(A2)ifkeine Notiz von Bobthen (B2) ifkeine Notiz von Alice then (A3) ifkeine Milchthen (B3) ifkeine Milchthen

(A4) kaufe Milch; (B4) kaufe Milch;

(A5) entferne Notiz “Alice”; (B5) entferne Notiz “Bob”;

Alice Bob

(37)

2. L¨ osungsversuch

Hinterlasse als erstes eine Notiz am K¨ uhlschrank, dann pr¨ ufe ob eine Notiz des anderen vorhanden ist. Nur wenn keine weitere Notiz vorhanden ist, pr¨ ufe ob Milch vorhanden ist und gehe Milch kaufen, wenn keine Milch vorhanden ist. Danach entferne die Notiz.

Als Prozesse:

Programm f¨ur Alice: Programm f¨ur Bob:

(A1) schreibe Notiz “Alice”; (B1) schreibe Notiz “Bob”;

(A2)ifkeine Notiz von Bobthen (B2) ifkeine Notiz von Alice then (A3) ifkeine Milchthen (B3) ifkeine Milchthen

(A4) kaufe Milch; (B4) kaufe Milch;

(A5) entferne Notiz “Alice”; (B5) entferne Notiz “Bob”;

Alice

Bob

TCS | 03 Synchronisation I | WS 2019/20 18/94 Modell Mutual-Exclusion-Problem nProzesse Komplexit¨at

(38)

2. L¨ osungsversuch

Hinterlasse als erstes eine Notiz am K¨ uhlschrank, dann pr¨ ufe ob eine Notiz des anderen vorhanden ist. Nur wenn keine weitere Notiz vorhanden ist, pr¨ ufe ob Milch vorhanden ist und gehe Milch kaufen, wenn keine Milch vorhanden ist. Danach entferne die Notiz.

Als Prozesse:

Programm f¨ur Alice: Programm f¨ur Bob:

(A1) schreibe Notiz “Alice”; (B1) schreibe Notiz “Bob”;

(A2)ifkeine Notiz von Bobthen (B2) ifkeine Notiz von Alice then (A3) ifkeine Milchthen (B3) ifkeine Milchthen

(A4) kaufe Milch; (B4) kaufe Milch;

(A5) entferne Notiz “Alice”; (B5) entferne Notiz “Bob”;

Alice Bob

(39)

2. L¨ osungsversuch

Hinterlasse als erstes eine Notiz am K¨ uhlschrank, dann pr¨ ufe ob eine Notiz des anderen vorhanden ist. Nur wenn keine weitere Notiz vorhanden ist, pr¨ ufe ob Milch vorhanden ist und gehe Milch kaufen, wenn keine Milch vorhanden ist. Danach entferne die Notiz.

Als Prozesse:

Programm f¨ur Alice: Programm f¨ur Bob:

(A1) schreibe Notiz “Alice”; (B1) schreibe Notiz “Bob”;

(A2)ifkeine Notiz von Bobthen (B2) ifkeine Notiz von Alice then (A3) ifkeine Milchthen (B3) ifkeine Milchthen

(A4) kaufe Milch; (B4) kaufe Milch;

(A5)entferne Notiz “Alice”; (B5) entferne Notiz “Bob”;

Alice Bob

TCS | 03 Synchronisation I | WS 2019/20 18/94 Modell Mutual-Exclusion-Problem nProzesse Komplexit¨at

(40)

2. L¨ osungsversuch

Hinterlasse als erstes eine Notiz am K¨ uhlschrank, dann pr¨ ufe ob eine Notiz des anderen vorhanden ist. Nur wenn keine weitere Notiz vorhanden ist, pr¨ ufe ob Milch vorhanden ist und gehe Milch kaufen, wenn keine Milch vorhanden ist. Danach entferne die Notiz.

Als Prozesse:

Programm f¨ur Alice: Programm f¨ur Bob:

(A1) schreibe Notiz “Alice”; (B1) schreibe Notiz “Bob”;

(A2)ifkeine Notiz von Bobthen (B2) ifkeine Notiz von Alice then (A3) ifkeine Milchthen (B3) ifkeine Milchthen

(A4) kaufe Milch; (B4) kaufe Milch;

(A5)entferne Notiz “Alice”; (B5) entferne Notiz “Bob”;

Alice Bob

(41)

2. L¨ osungsversuch

Hinterlasse als erstes eine Notiz am K¨ uhlschrank, dann pr¨ ufe ob eine Notiz des anderen vorhanden ist. Nur wenn keine weitere Notiz vorhanden ist, pr¨ ufe ob Milch vorhanden ist und gehe Milch kaufen, wenn keine Milch vorhanden ist. Danach entferne die Notiz.

Als Prozesse:

Programm f¨ur Alice: Programm f¨ur Bob:

(A1) schreibe Notiz “Alice”; (B1) schreibe Notiz “Bob”;

(A2)ifkeine Notiz von Bobthen (B2) ifkeine Notiz von Alice then (A3) ifkeine Milchthen (B3) ifkeine Milchthen

(A4) kaufe Milch; (B4) kaufe Milch;

(A5) entferne Notiz “Alice”; (B5) entferne Notiz “Bob”;

Alice

Bob

TCS | 03 Synchronisation I | WS 2019/20 18/94 Modell Mutual-Exclusion-Problem nProzesse Komplexit¨at

(42)

2. L¨ osungsversuch

Hinterlasse als erstes eine Notiz am K¨ uhlschrank, dann pr¨ ufe ob eine Notiz des anderen vorhanden ist. Nur wenn keine weitere Notiz vorhanden ist, pr¨ ufe ob Milch vorhanden ist und gehe Milch kaufen, wenn keine Milch vorhanden ist. Danach entferne die Notiz.

Als Prozesse:

Programm f¨ur Alice: Programm f¨ur Bob:

(A1) schreibe Notiz “Alice”; (B1) schreibe Notiz “Bob”;

(A2)ifkeine Notiz von Bobthen (B2) ifkeine Notiz von Alice then (A3) ifkeine Milchthen (B3) ifkeine Milchthen

(A4) kaufe Milch; (B4) kaufe Milch;

(A5) entferne Notiz “Alice”; (B5) entferne Notiz “Bob”;

Keine Milch!

(43)

3. L¨ osungsversuch

Alice

Wie vorher: Schreibe Notiz, wenn keine Notiz von Bob, pr¨ufe ob Milch vorhanden und gehe Milch kaufen.

Entferne Notiz.

Bob

Schreibe Notiz, warte bis keine Notiz von Alice am K¨uhlschrank h¨angt, dann pr¨ufe, ob Milch leer ist, gehe Milch kaufen, entferne eigene Notiz.

Als Prozesse:

Programm f¨ur Alice: Programm f¨ur Bob:

(A1)schreibe Notiz “Alice”; (B1)schreibe Notiz “Bob”;

(A2)ifkeine Notiz von Bobthen (B2)whileNotiz von Alicedo skip;

(A3) ifkeine Milchthen (B3)ifkeine Milchthen

(A4) kaufe Milch; (B4) kaufe Milch;

(A5) entferne Notiz “Alice”; (B5) entferne Notiz “Bob”;

Alice Bob

TCS | 03 Synchronisation I | WS 2019/20 19/94 Modell Mutual-Exclusion-Problem nProzesse Komplexit¨at

(44)

3. L¨ osungsversuch

Alice

Wie vorher: Schreibe Notiz, wenn keine Notiz von Bob, pr¨ufe ob Milch vorhanden und gehe Milch kaufen.

Entferne Notiz.

Bob

Schreibe Notiz, warte bis keine Notiz von Alice am K¨uhlschrank h¨angt, dann pr¨ufe, ob Milch leer ist, gehe Milch kaufen, entferne eigene Notiz.

Als Prozesse:

Programm f¨ur Alice: Programm f¨ur Bob:

(A1) schreibe Notiz “Alice”; (B1)schreibe Notiz “Bob”;

(A2)ifkeine Notiz von Bobthen (B2)whileNotiz von Alicedo skip;

(A3) ifkeine Milchthen (B3)ifkeine Milchthen

(A4) kaufe Milch; (B4) kaufe Milch;

(A5) entferne Notiz “Alice”; (B5) entferne Notiz “Bob”;

Alice

Bob

(45)

3. L¨ osungsversuch

Alice

Wie vorher: Schreibe Notiz, wenn keine Notiz von Bob, pr¨ufe ob Milch vorhanden und gehe Milch kaufen.

Entferne Notiz.

Bob

Schreibe Notiz, warte bis keine Notiz von Alice am K¨uhlschrank h¨angt, dann pr¨ufe, ob Milch leer ist, gehe Milch kaufen, entferne eigene Notiz.

Als Prozesse:

Programm f¨ur Alice: Programm f¨ur Bob:

(A1) schreibe Notiz “Alice”; (B1) schreibe Notiz “Bob”;

(A2)ifkeine Notiz von Bobthen (B2)whileNotiz von Alicedo skip;

(A3) ifkeine Milchthen (B3)ifkeine Milchthen

(A4) kaufe Milch; (B4) kaufe Milch;

(A5) entferne Notiz “Alice”; (B5) entferne Notiz “Bob”;

Alice Bob

TCS | 03 Synchronisation I | WS 2019/20 19/94 Modell Mutual-Exclusion-Problem nProzesse Komplexit¨at

(46)

3. L¨ osungsversuch

Alice

Wie vorher: Schreibe Notiz, wenn keine Notiz von Bob, pr¨ufe ob Milch vorhanden und gehe Milch kaufen.

Entferne Notiz.

Bob

Schreibe Notiz, warte bis keine Notiz von Alice am K¨uhlschrank h¨angt, dann pr¨ufe, ob Milch leer ist, gehe Milch kaufen, entferne eigene Notiz.

Als Prozesse:

Programm f¨ur Alice: Programm f¨ur Bob:

(A1) schreibe Notiz “Alice”; (B1) schreibe Notiz “Bob”;

(A2)ifkeine Notiz von Bobthen (B2)whileNotiz von Alicedo skip;

(A3) ifkeine Milchthen (B3)ifkeine Milchthen

(A4) kaufe Milch; (B4) kaufe Milch;

(A5) entferne Notiz “Alice”; (B5) entferne Notiz “Bob”;

Alice Bob

(47)

3. L¨ osungsversuch

Alice

Wie vorher: Schreibe Notiz, wenn keine Notiz von Bob, pr¨ufe ob Milch vorhanden und gehe Milch kaufen.

Entferne Notiz.

Bob

Schreibe Notiz, warte bis keine Notiz von Alice am K¨uhlschrank h¨angt, dann pr¨ufe, ob Milch leer ist, gehe Milch kaufen, entferne eigene Notiz.

Als Prozesse:

Programm f¨ur Alice: Programm f¨ur Bob:

(A1) schreibe Notiz “Alice”; (B1) schreibe Notiz “Bob”;

(A2)ifkeine Notiz von Bobthen (B2)whileNotiz von Alicedo skip;

(A3) ifkeine Milchthen (B3)ifkeine Milchthen

(A4) kaufe Milch; (B4) kaufe Milch;

(A5)entferne Notiz “Alice”; (B5) entferne Notiz “Bob”;

Alice Bob

TCS | 03 Synchronisation I | WS 2019/20 19/94 Modell Mutual-Exclusion-Problem nProzesse Komplexit¨at

(48)

3. L¨ osungsversuch

Alice

Wie vorher: Schreibe Notiz, wenn keine Notiz von Bob, pr¨ufe ob Milch vorhanden und gehe Milch kaufen.

Entferne Notiz.

Bob

Schreibe Notiz, warte bis keine Notiz von Alice am K¨uhlschrank h¨angt, dann pr¨ufe, ob Milch leer ist, gehe Milch kaufen, entferne eigene Notiz.

Als Prozesse:

Programm f¨ur Alice: Programm f¨ur Bob:

(A1)schreibe Notiz “Alice”; (B1) schreibe Notiz “Bob”;

(A2)ifkeine Notiz von Bobthen (B2)whileNotiz von Alicedo skip;

(A3) ifkeine Milchthen (B3)ifkeine Milchthen

(A4) kaufe Milch; (B4) kaufe Milch;

(A5) entferne Notiz “Alice”; (B5) entferne Notiz “Bob”;

Alice

Bob

(49)

3. L¨ osungsversuch

Alice

Wie vorher: Schreibe Notiz, wenn keine Notiz von Bob, pr¨ufe ob Milch vorhanden und gehe Milch kaufen.

Entferne Notiz.

Bob

Schreibe Notiz, warte bis keine Notiz von Alice am K¨uhlschrank h¨angt, dann pr¨ufe, ob Milch leer ist, gehe Milch kaufen, entferne eigene Notiz.

Als Prozesse:

Programm f¨ur Alice: Programm f¨ur Bob:

(A1) schreibe Notiz “Alice”; (B1) schreibe Notiz “Bob”;

(A2)ifkeine Notiz von Bobthen (B2)whileNotiz von Alicedo skip;

(A3) ifkeine Milchthen (B3)ifkeine Milchthen

(A4) kaufe Milch; (B4) kaufe Milch;

(A5) entferne Notiz “Alice”; (B5) entferne Notiz “Bob”;

Alice Bob

TCS | 03 Synchronisation I | WS 2019/20 19/94 Modell Mutual-Exclusion-Problem nProzesse Komplexit¨at

(50)

3. L¨ osungsversuch

Alice

Wie vorher: Schreibe Notiz, wenn keine Notiz von Bob, pr¨ufe ob Milch vorhanden und gehe Milch kaufen.

Entferne Notiz.

Bob

Schreibe Notiz, warte bis keine Notiz von Alice am K¨uhlschrank h¨angt, dann pr¨ufe, ob Milch leer ist, gehe Milch kaufen, entferne eigene Notiz.

Als Prozesse:

Programm f¨ur Alice: Programm f¨ur Bob:

(A1) schreibe Notiz “Alice”; (B1) schreibe Notiz “Bob”;

(A2)ifkeine Notiz von Bobthen (B2)whileNotiz von Alicedo skip;

(A3) ifkeine Milchthen (B3)ifkeine Milchthen

(A4) kaufe Milch; (B4) kaufe Milch;

(A5) entferne Notiz “Alice”; (B5) entferne Notiz “Bob”;

Alice Bob

(51)

3. L¨ osungsversuch

Alice

Wie vorher: Schreibe Notiz, wenn keine Notiz von Bob, pr¨ufe ob Milch vorhanden und gehe Milch kaufen.

Entferne Notiz.

Bob

Schreibe Notiz, warte bis keine Notiz von Alice am K¨uhlschrank h¨angt, dann pr¨ufe, ob Milch leer ist, gehe Milch kaufen, entferne eigene Notiz.

Als Prozesse:

Programm f¨ur Alice: Programm f¨ur Bob:

(A1) schreibe Notiz “Alice”; (B1) schreibe Notiz “Bob”;

(A2)ifkeine Notiz von Bobthen (B2)whileNotiz von Alicedo skip;

(A3) ifkeine Milchthen (B3)ifkeine Milchthen

(A4) kaufe Milch; (B4) kaufe Milch;

(A5)entferne Notiz “Alice”; (B5) entferne Notiz “Bob”;

Alice Bob

TCS | 03 Synchronisation I | WS 2019/20 19/94 Modell Mutual-Exclusion-Problem nProzesse Komplexit¨at

(52)

3. L¨ osungsversuch

Alice

Wie vorher: Schreibe Notiz, wenn keine Notiz von Bob, pr¨ufe ob Milch vorhanden und gehe Milch kaufen.

Entferne Notiz.

Bob

Schreibe Notiz, warte bis keine Notiz von Alice am K¨uhlschrank h¨angt, dann pr¨ufe, ob Milch leer ist, gehe Milch kaufen, entferne eigene Notiz.

Als Prozesse:

Programm f¨ur Alice: Programm f¨ur Bob:

(A1)schreibe Notiz “Alice”; (B1) schreibe Notiz “Bob”;

(A2)ifkeine Notiz von Bobthen (B2)whileNotiz von Alicedo skip;

(A3) ifkeine Milchthen (B3)ifkeine Milchthen

(A4) kaufe Milch; (B4) kaufe Milch;

(A5) entferne Notiz “Alice”; (B5) entferne Notiz “Bob”;

Alice

Bob

(53)

3. L¨ osungsversuch

Alice

Wie vorher: Schreibe Notiz, wenn keine Notiz von Bob, pr¨ufe ob Milch vorhanden und gehe Milch kaufen.

Entferne Notiz.

Bob

Schreibe Notiz, warte bis keine Notiz von Alice am K¨uhlschrank h¨angt, dann pr¨ufe, ob Milch leer ist, gehe Milch kaufen, entferne eigene Notiz.

Als Prozesse:

Programm f¨ur Alice: Programm f¨ur Bob:

(A1) schreibe Notiz “Alice”; (B1) schreibe Notiz “Bob”;

(A2)ifkeine Notiz von Bobthen (B2)whileNotiz von Alicedo skip;

(A3) ifkeine Milchthen (B3)ifkeine Milchthen

(A4) kaufe Milch; (B4) kaufe Milch;

(A5) entferne Notiz “Alice”; (B5) entferne Notiz “Bob”;

Alice Bob

TCS | 03 Synchronisation I | WS 2019/20 19/94 Modell Mutual-Exclusion-Problem nProzesse Komplexit¨at

(54)

3. L¨ osungsversuch

Alice

Wie vorher: Schreibe Notiz, wenn keine Notiz von Bob, pr¨ufe ob Milch vorhanden und gehe Milch kaufen.

Entferne Notiz.

Bob

Schreibe Notiz, warte bis keine Notiz von Alice am K¨uhlschrank h¨angt, dann pr¨ufe, ob Milch leer ist, gehe Milch kaufen, entferne eigene Notiz.

Als Prozesse:

Programm f¨ur Alice: Programm f¨ur Bob:

(A1) schreibe Notiz “Alice”; (B1) schreibe Notiz “Bob”;

(A2)ifkeine Notiz von Bobthen (B2)whileNotiz von Alicedo skip;

(A3) ifkeine Milchthen (B3)ifkeine Milchthen

(A4) kaufe Milch; (B4) kaufe Milch;

(A5) entferne Notiz “Alice”; (B5) entferne Notiz “Bob”;

Alice Bob

(55)

3. L¨ osungsversuch

Alice

Wie vorher: Schreibe Notiz, wenn keine Notiz von Bob, pr¨ufe ob Milch vorhanden und gehe Milch kaufen.

Entferne Notiz.

Bob

Schreibe Notiz, warte bis keine Notiz von Alice am K¨uhlschrank h¨angt, dann pr¨ufe, ob Milch leer ist, gehe Milch kaufen, entferne eigene Notiz.

Als Prozesse:

Programm f¨ur Alice: Programm f¨ur Bob:

(A1) schreibe Notiz “Alice”; (B1) schreibe Notiz “Bob”;

(A2)ifkeine Notiz von Bobthen (B2)whileNotiz von Alicedo skip;

(A3) ifkeine Milchthen (B3)ifkeine Milchthen

(A4) kaufe Milch; (B4) kaufe Milch;

(A5)entferne Notiz “Alice”; (B5) entferne Notiz “Bob”;

Alice Bob

TCS | 03 Synchronisation I | WS 2019/20 19/94 Modell Mutual-Exclusion-Problem nProzesse Komplexit¨at

(56)

3. L¨ osungsversuch

Alice

Wie vorher: Schreibe Notiz, wenn keine Notiz von Bob, pr¨ufe ob Milch vorhanden und gehe Milch kaufen.

Entferne Notiz.

Bob

Schreibe Notiz, warte bis keine Notiz von Alice am K¨uhlschrank h¨angt, dann pr¨ufe, ob Milch leer ist, gehe Milch kaufen, entferne eigene Notiz.

Als Prozesse:

Programm f¨ur Alice: Programm f¨ur Bob:

(A1) schreibe Notiz “Alice”; (B1) schreibe Notiz “Bob”;

(A2)ifkeine Notiz von Bobthen (B2)whileNotiz von Alicedo skip;

(A3) ifkeine Milchthen (B3)ifkeine Milchthen

(A4) kaufe Milch; (B4) kaufe Milch;

(A5) entferne Notiz “Alice”; (B5) entferne Notiz “Bob”;

Alice

Bob

(57)

3. L¨ osungsversuch

Alice

Wie vorher: Schreibe Notiz, wenn keine Notiz von Bob, pr¨ufe ob Milch vorhanden und gehe Milch kaufen.

Entferne Notiz.

Bob

Schreibe Notiz, warte bis keine Notiz von Alice am K¨uhlschrank h¨angt, dann pr¨ufe, ob Milch leer ist, gehe Milch kaufen, entferne eigene Notiz.

Als Prozesse:

Programm f¨ur Alice: Programm f¨ur Bob:

(A1) schreibe Notiz “Alice”; (B1) schreibe Notiz “Bob”;

(A2)ifkeine Notiz von Bobthen (B2)whileNotiz von Alicedo skip;

(A3) ifkeine Milchthen (B3)ifkeine Milchthen

(A4) kaufe Milch; (B4) kaufe Milch;

(A5) entferne Notiz “Alice”; (B5) entferne Notiz “Bob”;

L¨osung funktioniert nur, wenn die relative Geschwindigkeit passt. Sonst:

(A1),(B1),(B2),(A2),(A5),(A1),(B2)

| {z }

unendlich oft

ohne Milch zu kaufen

TCS | 03 Synchronisation I | WS 2019/20 19/94 Modell Mutual-Exclusion-Problem nProzesse Komplexit¨at

(58)

Eine korrekte L¨ osung

Programm f¨ur Alice (A1) schreibe a1;

(A2)ifb2

(A3) thenschreibe a2;

(A4) elseentferne a2;

(A5)while

b1∧((a2∧b2)∨(¬a2∧ ¬b2)) (A6) do skip;

(A7)ifkeine Milch (A8) thenkaufe Milch;

(A9) entferne a1;

Programm f¨ur Bob (B1) schreibe b1;

(B2)if¬a2

(B3) thenschreibe b2;

(B4) elseentferne b2 (B5)while

a1∧((a2∧ ¬b2)∨(¬a2∧b2)) (B6) do skip;

(B7)ifkeine Milch (B8) thenkaufe Milch;

(B9) entferne b1;

a1 a2 b2 b1 pr¨ uft Milch

X

dc dc

×

Alice

×

dc dc

X

Bob

dc = don’t care

a1 a2 b2 b1 pr¨ uft Milch

X × X X

Alice

X X × X

Alice

X X X X

Bob

X × × X

Bob

(59)

Eine korrekte L¨ osung

Programm f¨ur Alice (A1) schreibe a1;

(A2)ifb2

(A3) thenschreibe a2;

(A4) elseentferne a2;

(A5)while

b1∧((a2∧b2)∨(¬a2∧ ¬b2)) (A6) do skip;

(A7)ifkeine Milch (A8) thenkaufe Milch;

(A9) entferne a1;

Programm f¨ur Bob (B1) schreibe b1;

(B2)if¬a2

(B3) thenschreibe b2;

(B4) elseentferne b2 (B5)while

a1∧((a2∧ ¬b2)∨(¬a2∧b2)) (B6) do skip;

(B7)ifkeine Milch (B8) thenkaufe Milch;

(B9) entferne b1;

a1 a2 b2 b1 pr¨ uft Milch

X

dc dc

×

Alice

×

dc dc

X

Bob

dc = don’t care

a1 a2 b2 b1 pr¨ uft Milch

X × X X

Alice

X X × X

Alice

X X X X

Bob

X × × X

Bob

TCS | 03 Synchronisation I | WS 2019/20 20/94 Modell Mutual-Exclusion-Problem nProzesse Komplexit¨at

(60)

Eine korrekte L¨ osung

Programm f¨ur Alice (A1) schreibe a1;

(A2)ifb2

(A3) thenschreibe a2;

(A4) elseentferne a2;

(A5)while

b1∧((a2∧b2)∨(¬a2∧ ¬b2)) (A6) do skip;

(A7)ifkeine Milch (A8) thenkaufe Milch;

(A9) entferne a1;

Programm f¨ur Bob (B1) schreibe b1;

(B2)if¬a2

(B3) thenschreibe b2;

(B4) elseentferne b2 (B5)while

a1∧((a2∧ ¬b2)∨(¬a2∧b2)) (B6) do skip;

(B7)ifkeine Milch (B8) thenkaufe Milch;

(B9) entferne b1;

a1 a2 b2 b1 pr¨ uft Milch

X

dc dc

×

Alice

×

dc dc

X

Bob

dc = don’t care

a1 a2 b2 b1 pr¨ uft Milch

X × X X

Alice

X X × X

Alice

X X X X

Bob

X × × X

Bob

bedeutet “... will an den K¨uhlschrank”

(61)

Eine korrekte L¨ osung

Programm f¨ur Alice (A1) schreibe a1;

(A2)ifb2

(A3) thenschreibe a2;

(A4) elseentferne a2;

(A5)while

b1∧((a2∧b2)∨(¬a2∧ ¬b2)) (A6) do skip;

(A7)ifkeine Milch (A8) thenkaufe Milch;

(A9) entferne a1;

Programm f¨ur Bob (B1) schreibe b1;

(B2)if¬a2

(B3) thenschreibe b2;

(B4) elseentferne b2 (B5)while

a1∧((a2∧ ¬b2)∨(¬a2∧b2)) (B6) do skip;

(B7)ifkeine Milch (B8) thenkaufe Milch;

(B9) entferne b1;

a1 a2 b2 b1 pr¨ uft Milch

X

dc dc

×

Alice

×

dc dc

X

Bob

dc = don’t care

a1 a2 b2 b1 pr¨ uft Milch

X × X X

Alice

X X × X

Alice

X X X X

Bob

X × × X

Bob

TCS | 03 Synchronisation I | WS 2019/20 20/94 Modell Mutual-Exclusion-Problem nProzesse Komplexit¨at

versucht Vorrang an Bob zu geben

(62)

Eine korrekte L¨ osung

Programm f¨ur Alice (A1) schreibe a1;

(A2)ifb2

(A3) thenschreibe a2;

(A4) elseentferne a2;

(A5)while

b1∧((a2∧b2)∨(¬a2∧ ¬b2)) (A6) do skip;

(A7)ifkeine Milch (A8) thenkaufe Milch;

(A9) entferne a1;

Programm f¨ur Bob (B1) schreibe b1;

(B2)if¬a2

(B3) thenschreibe b2;

(B4) elseentferne b2 (B5)while

a1∧((a2∧ ¬b2)∨(¬a2∧b2)) (B6) do skip;

(B7)ifkeine Milch (B8) thenkaufe Milch;

(B9) entferne b1;

a1 a2 b2 b1 pr¨ uft Milch

X

dc dc

×

Alice

×

dc dc

X

Bob

dc = don’t care

a1 a2 b2 b1 pr¨ uft Milch

X × X X

Alice

X X × X

Alice

X X X X

Bob

X × × X

Bob

versucht Vorrang an Alice zu geben

(63)

Im Folgenden:

Mutual-Exclusion Problem:

Formalisierung

Definition des Problems und einer L¨ osung Annahmen ¨ uber das Modell Deadlock- bzw. Starvation-Freiheit

TCS | 03 Synchronisation I | WS 2019/20 21/94 Modell Mutual-Exclusion-Problem nProzesse Komplexit¨at

(64)

Das Mutual-Exclusion-Problem

Mutual-Exclusion-Problem =

Garantie, dass der

exklusive Zugriff

auf eine gemeinsam genutzte Ressource sichergestellt wird

Zugriffscode wird als

kritischer Abschnitt

bezeichnet.

Race Condition: Situationen in denen mehrere Prozesse auf

eine gemeinsame Ressource zugreifen und der Wert der Ressource h¨ angt von Ausf¨ uhrungsreihenfolge ab.

Ziel des Mutual-Exclusion-Problems:

Vermeide Race Conditions.

(65)

Das Mutual-Exclusion-Problem, formales Modell

Code-Struktur jedes Prozesses

loop forever

restlicher Code Initialisierungscode Kritischer Abschnitt Abschlusscode

end loop

TCS | 03 Synchronisation I | WS 2019/20 23/94 Modell Mutual-Exclusion-Problem nProzesse Komplexit¨at

(66)

Das Mutual-Exclusion-Problem, formales Modell (2)

Annahmen

Im restlichen Code kann ein Prozess keinen Einfluss auf andere Prozesse nehmen. Ansonsten ist dort alles erlaubt (Endlosschleifen usw.)

Ressourcen (Programmvariablen), die im Initialisierungscode oder Abschlusscode benutzt werden, werden durch den Code im Kritischen Abschnitt und den restlichen Code nicht ver¨ andert.

loop forever restlicher Code Initialisierungscode Kritischer Abschnitt Abschlusscode end loop

(67)

Das Mutual-Exclusion-Problem, formales Modell (3)

Annahmen (Fortsetzung)

Keine Fehler bei Ausf¨ uhrung des Initialisierungscode, des Codes im kritischen Abschnitt und im Abschlusscode Code im kritschen Abschnitt und im Abschlusscode: Nur endlich viele Ausf¨ uhrungsschritte (keine Schleifen!).

Impliziert (wegen Fairness): Prozess verl¨ asst nach Betreten des kritischen Abschnitt diesen und f¨ uhrt Abschlusscode durch.

loop forever restlicher Code Initialisierungscode Kritischer Abschnitt Abschlusscode end loop

TCS | 03 Synchronisation I | WS 2019/20 25/94 Modell Mutual-Exclusion-Problem nProzesse Komplexit¨at

(68)

Korrektheitskriterien

L¨osung des Mutual-Exclusion-Problems

F¨ ulle Initialisierungs- und Abschlusscode, so dass die folgenden Anforderungen erf¨ ullt sind:

Wechselseitiger Ausschluss: Es sind niemals zwei oder mehr

Prozesse zugleich in ihrem kritischen Abschnitt.

Deadlock-Freiheit: Wenn ein Prozess

seinen kritischen

Abschnitt betreten m¨ ochte, dann betritt

irgendein

Prozess

schließlich den kritischen Abschnitt.

(69)

Korrektheitskriterien (2)

St¨ arkere Forderung gegen¨ uber Deadlock-Freiheit:

Starvation-Freiheit

Starvation = Verhungern

Starvation-Freiheit: Wennein Prozess

seinen kritischen Abschnitt betreten m¨ ochte, dann muss

er

ihn nach endlich vielen Berechnungsschritten betreten.

Ein Starvation-freier Algorithmus ist auch Deadlock-frei.

TCS | 03 Synchronisation I | WS 2019/20 27/94 Modell Mutual-Exclusion-Problem nProzesse Komplexit¨at

(70)

Formale Beweistechniken

Wechselseitiger Ausschluss ist eine sog.

Sicherheitseigenschaft

(Safety Property), eine Eigenschaft die

immer

erf¨ ullt sein muss.

Deadlock- bzw. Starvation-Freiheit sind sog.

Lebendigkeitseigenschaften

(Liveness Property), Eigenschaften die irgendwann erf¨ ullt sein m¨ ussen.

Mittels Temporallogik l¨ asst sich formal nachweisen, dass diese Eigenschaften erf¨ ullt sind.

Z.B. kann Model-Checking f¨ ur den Beweis verwendet werden

Wir: Korrektheitsbeweis eher informell.

(71)

Wir betrachten:

L¨ osungen zum Mutual-Exclusion Problem bei 2 Prozessen

und atomarem Lesen und Schreiben

TCS | 03 Synchronisation I | WS 2019/20 29/94 Modell Mutual-Exclusion-Problem nProzesse Komplexit¨at

(72)

Algorithmen f¨ ur 2 Prozesse mit atomarem Read+Write

Im Folgenden:

Einige Algorithmen zum Mutual-Exclusion Problem Nebenl¨ aufiges Programm mit genau

zwei

Prozessen Annahmen:

Als atomare Speicheroperationen des gemeinsamen Speichers nur

Lesen

und

Schreiben

Z.B. verboten swap(X,Y), X := Y , etc.

Abk¨ urzung:

await Bedingung;

anstelle von

while ¬Bedingung do skip;

(73)

Dekkers Algorithmus

Historisch die erste korrekte L¨ osung

Theodorus Jozef Dekker = Holl¨ andischer Mathematiker Erw¨ ahnung des Dekker-Algorithmus: Dijkstra 1965 Nicht ganz einfache L¨ osung

TCS | 03 Synchronisation I | WS 2019/20 31/94 Modell Mutual-Exclusion-Problem nProzesse Komplexit¨at

(74)

Algorithmus von Dekker

Initial:wantp = False, wantq = False, turn =1

ProzessP: ProzessQ:

loop forever

(P1) restlicher Code (P2) wantp := True;

(P3) whilewantqdo (P4) ifturn=2then (P5) wantp := False;

(P6) awaitturn=1;

(P7) wantp := True;

(P8) Kritischer Abschnitt (P9) turn := 2;

(P10) wantp := False;

end loop

loop forever

(Q1) restlicher Code (Q2) wantq := True;

(Q3) whilewantpdo (Q4) ifturn=1then (Q5) wantq := False (Q6) awaitturn=2 (Q7) wantq := True;

(Q8) Kritischer Abschnitt (Q9) turn := 1;

(Q10) wantq :=False;

end loop

Durchlauf: NurP will in den kritischen Abschnitt wantp=False, wantq =False , turn =1

(75)

Algorithmus von Dekker

Initial: wantp = False, wantq = False, turn =1

ProzessP: ProzessQ:

loop forever

(P1) restlicher Code (P2) wantp := True;

(P3) whilewantqdo (P4) ifturn=2then (P5) wantp := False;

(P6) awaitturn=1;

(P7) wantp := True;

(P8) Kritischer Abschnitt (P9) turn := 2;

(P10) wantp := False;

end loop

loop forever

(Q1) restlicher Code (Q2) wantq := True;

(Q3) whilewantpdo (Q4) ifturn=1then (Q5) wantq := False (Q6) awaitturn=2 (Q7) wantq := True;

(Q8) Kritischer Abschnitt (Q9) turn := 1;

(Q10) wantq :=False;

end loop

Durchlauf: NurP will in den kritischen Abschnitt wantp=False, wantq =False , turn =1

TCS | 03 Synchronisation I | WS 2019/20 32/94 Modell Mutual-Exclusion-Problem nProzesse Komplexit¨at

(76)

Algorithmus von Dekker

Initial: wantp = False, wantq = False, turn =1

ProzessP: ProzessQ:

loop forever

(P1) restlicher Code (P2) wantp := True;

(P3) whilewantqdo (P4) ifturn=2then (P5) wantp := False;

(P6) awaitturn=1;

(P7) wantp := True;

(P8) Kritischer Abschnitt (P9) turn := 2;

(P10) wantp := False;

end loop

loop forever

(Q1) restlicher Code (Q2) wantq := True;

(Q3) whilewantpdo (Q4) ifturn=1then (Q5) wantq := False (Q6) awaitturn=2 (Q7) wantq := True;

(Q8) Kritischer Abschnitt (Q9) turn := 1;

(Q10) wantq :=False;

end loop

Durchlauf: NurP will in den kritischen Abschnitt wantp=True, wantq =False , turn =1

(77)

Algorithmus von Dekker

Initial: wantp = False, wantq = False, turn =1

ProzessP: ProzessQ:

loop forever

(P1) restlicher Code (P2) wantp := True;

(P3) whilewantqdo (P4) ifturn=2then (P5) wantp := False;

(P6) awaitturn=1;

(P7) wantp := True;

(P8) Kritischer Abschnitt (P9) turn := 2;

(P10) wantp := False;

end loop

loop forever

(Q1) restlicher Code (Q2) wantq := True;

(Q3) whilewantpdo (Q4) ifturn=1then (Q5) wantq := False (Q6) awaitturn=2 (Q7) wantq := True;

(Q8) Kritischer Abschnitt (Q9) turn := 1;

(Q10) wantq :=False;

end loop

Durchlauf: NurP will in den kritischen Abschnitt wantp=True, wantq =False , turn =1

TCS | 03 Synchronisation I | WS 2019/20 32/94 Modell Mutual-Exclusion-Problem nProzesse Komplexit¨at

(78)

Algorithmus von Dekker

Initial: wantp = False, wantq = False, turn =1

ProzessP: ProzessQ:

loop forever

(P1) restlicher Code (P2) wantp := True;

(P3) whilewantqdo (P4) ifturn=2then (P5) wantp := False;

(P6) awaitturn=1;

(P7) wantp := True;

(P8) Kritischer Abschnitt (P9) turn := 2;

(P10) wantp := False;

end loop

loop forever

(Q1) restlicher Code (Q2) wantq := True;

(Q3) whilewantpdo (Q4) ifturn=1then (Q5) wantq := False (Q6) awaitturn=2 (Q7) wantq := True;

(Q8) Kritischer Abschnitt (Q9) turn := 1;

(Q10) wantq :=False;

end loop

Durchlauf: NurP will in den kritischen Abschnitt wantp=True, wantq =False , turn =1

(79)

Algorithmus von Dekker

Initial: wantp = False, wantq = False, turn =1

ProzessP: ProzessQ:

loop forever

(P1) restlicher Code (P2) wantp := True;

(P3) whilewantqdo (P4) ifturn=2then (P5) wantp := False;

(P6) awaitturn=1;

(P7) wantp := True;

(P8) Kritischer Abschnitt (P9) turn := 2;

(P10) wantp := False;

end loop

loop forever

(Q1) restlicher Code (Q2) wantq := True;

(Q3) whilewantpdo (Q4) ifturn=1then (Q5) wantq := False (Q6) awaitturn=2 (Q7) wantq := True;

(Q8) Kritischer Abschnitt (Q9) turn := 1;

(Q10) wantq :=False;

end loop

Durchlauf: NurP will in den kritischen Abschnitt wantp=True, wantq =False , turn =2

TCS | 03 Synchronisation I | WS 2019/20 32/94 Modell Mutual-Exclusion-Problem nProzesse Komplexit¨at

(80)

Algorithmus von Dekker

Initial: wantp = False, wantq = False, turn =1

ProzessP: ProzessQ:

loop forever

(P1) restlicher Code (P2) wantp := True;

(P3) whilewantqdo (P4) ifturn=2then (P5) wantp := False;

(P6) awaitturn=1;

(P7) wantp := True;

(P8) Kritischer Abschnitt (P9) turn := 2;

(P10) wantp := False;

end loop

loop forever

(Q1) restlicher Code (Q2) wantq := True;

(Q3) whilewantpdo (Q4) ifturn=1then (Q5) wantq := False (Q6) awaitturn=2 (Q7) wantq := True;

(Q8) Kritischer Abschnitt (Q9) turn := 1;

(Q10) wantq :=False;

end loop

Durchlauf: NurP will in den kritischen Abschnitt wantp=False, wantq =False , turn =1

(81)

Algorithmus von Dekker

Initial: wantp = False, wantq = False, turn =1

ProzessP: ProzessQ:

loop forever

(P1) restlicher Code (P2) wantp := True;

(P3) whilewantqdo (P4) ifturn=2then (P5) wantp := False;

(P6) awaitturn=1;

(P7) wantp := True;

(P8) Kritischer Abschnitt (P9) turn := 2;

(P10) wantp := False;

end loop

loop forever

(Q1) restlicher Code (Q2) wantq := True;

(Q3) whilewantpdo (Q4) ifturn=1then (Q5) wantq := False (Q6) awaitturn=2 (Q7) wantq := True;

(Q8) Kritischer Abschnitt (Q9) turn := 1;

(Q10) wantq :=False;

end loop

Durchlauf: P zuerst im kritischen Abschnitt wantp=False, wantq =False , turn =1

TCS | 03 Synchronisation I | WS 2019/20 32/94 Modell Mutual-Exclusion-Problem nProzesse Komplexit¨at

(82)

Algorithmus von Dekker

Initial: wantp = False, wantq = False, turn =1

ProzessP: ProzessQ:

loop forever

(P1) restlicher Code (P2) wantp := True;

(P3) whilewantqdo (P4) ifturn=2then (P5) wantp := False;

(P6) awaitturn=1;

(P7) wantp := True;

(P8) Kritischer Abschnitt (P9) turn := 2;

(P10) wantp := False;

end loop

loop forever

(Q1) restlicher Code (Q2) wantq := True;

(Q3) whilewantpdo (Q4) ifturn=1then (Q5) wantq := False (Q6) awaitturn=2 (Q7) wantq := True;

(Q8) Kritischer Abschnitt (Q9) turn := 1;

(Q10) wantq :=False;

end loop

Durchlauf: P zuerst im kritischen Abschnitt wantp=True, wantq =False , turn =1

(83)

Algorithmus von Dekker

Initial: wantp = False, wantq = False, turn =1

ProzessP: ProzessQ:

loop forever

(P1) restlicher Code (P2) wantp := True;

(P3) whilewantqdo (P4) ifturn=2then (P5) wantp := False;

(P6) awaitturn=1;

(P7) wantp := True;

(P8) Kritischer Abschnitt (P9) turn := 2;

(P10) wantp := False;

end loop

loop forever

(Q1) restlicher Code (Q2) wantq := True;

(Q3) whilewantpdo (Q4) ifturn=1then (Q5) wantq := False (Q6) awaitturn=2 (Q7) wantq := True;

(Q8) Kritischer Abschnitt (Q9) turn := 1;

(Q10) wantq :=False;

end loop

Durchlauf: P zuerst im kritischen Abschnitt wantp=True, wantq =False , turn =1

TCS | 03 Synchronisation I | WS 2019/20 32/94 Modell Mutual-Exclusion-Problem nProzesse Komplexit¨at

(84)

Algorithmus von Dekker

Initial: wantp = False, wantq = False, turn =1

ProzessP: ProzessQ:

loop forever

(P1) restlicher Code (P2) wantp := True;

(P3) whilewantqdo (P4) ifturn=2then (P5) wantp := False;

(P6) awaitturn=1;

(P7) wantp := True;

(P8) Kritischer Abschnitt (P9) turn := 2;

(P10) wantp := False;

end loop

loop forever

(Q1) restlicher Code (Q2) wantq := True;

(Q3) whilewantpdo (Q4) ifturn=1then (Q5) wantq := False (Q6) awaitturn=2 (Q7) wantq := True;

(Q8) Kritischer Abschnitt (Q9) turn := 1;

(Q10) wantq :=False;

end loop

Durchlauf: P zuerst im kritischen Abschnitt wantp=True, wantq =False , turn =1

(85)

Algorithmus von Dekker

Initial: wantp = False, wantq = False, turn =1

ProzessP: ProzessQ:

loop forever

(P1) restlicher Code (P2) wantp := True;

(P3) whilewantqdo (P4) ifturn=2then (P5) wantp := False;

(P6) awaitturn=1;

(P7) wantp := True;

(P8) Kritischer Abschnitt (P9) turn := 2;

(P10) wantp := False;

end loop

loop forever

(Q1) restlicher Code (Q2) wantq := True;

(Q3) whilewantpdo (Q4) ifturn=1then (Q5) wantq := False (Q6) awaitturn=2 (Q7) wantq := True;

(Q8) Kritischer Abschnitt (Q9) turn := 1;

(Q10) wantq :=False;

end loop

Durchlauf: P zuerst im kritischen Abschnitt wantp=True, wantq =False , turn =1

TCS | 03 Synchronisation I | WS 2019/20 32/94 Modell Mutual-Exclusion-Problem nProzesse Komplexit¨at

(86)

Algorithmus von Dekker

Initial: wantp = False, wantq = False, turn =1

ProzessP: ProzessQ:

loop forever

(P1) restlicher Code (P2) wantp := True;

(P3) whilewantqdo (P4) ifturn=2then (P5) wantp := False;

(P6) awaitturn=1;

(P7) wantp := True;

(P8) Kritischer Abschnitt (P9) turn := 2;

(P10) wantp := False;

end loop

loop forever

(Q1) restlicher Code (Q2) wantq := True;

(Q3) whilewantpdo (Q4) ifturn=1then (Q5) wantq := False (Q6) awaitturn=2 (Q7) wantq := True;

(Q8) Kritischer Abschnitt (Q9) turn := 1;

(Q10) wantq :=False;

end loop

Durchlauf: P zuerst im kritischen Abschnitt wantp=True, wantq =False , turn =1

(87)

Algorithmus von Dekker

Initial: wantp = False, wantq = False, turn =1

ProzessP: ProzessQ:

loop forever

(P1) restlicher Code (P2) wantp := True;

(P3) whilewantqdo (P4) ifturn=2then (P5) wantp := False;

(P6) awaitturn=1;

(P7) wantp := True;

(P8) Kritischer Abschnitt (P9) turn := 2;

(P10) wantp := False;

end loop

loop forever

(Q1) restlicher Code (Q2) wantq := True;

(Q3) whilewantpdo (Q4) ifturn=1then (Q5) wantq := False (Q6) awaitturn=2 (Q7) wantq := True;

(Q8) Kritischer Abschnitt (Q9) turn := 1;

(Q10) wantq :=False;

end loop

Durchlauf: P zuerst im kritischen Abschnitt wantp=True, wantq =True , turn =1

TCS | 03 Synchronisation I | WS 2019/20 32/94 Modell Mutual-Exclusion-Problem nProzesse Komplexit¨at

(88)

Algorithmus von Dekker

Initial: wantp = False, wantq = False, turn =1

ProzessP: ProzessQ:

loop forever

(P1) restlicher Code (P2) wantp := True;

(P3) whilewantqdo (P4) ifturn=2then (P5) wantp := False;

(P6) awaitturn=1;

(P7) wantp := True;

(P8) Kritischer Abschnitt (P9) turn := 2;

(P10) wantp := False;

end loop

loop forever

(Q1) restlicher Code (Q2) wantq := True;

(Q3) whilewantpdo (Q4) ifturn=1then (Q5) wantq := False (Q6) awaitturn=2 (Q7) wantq := True;

(Q8) Kritischer Abschnitt (Q9) turn := 1;

(Q10) wantq :=False;

end loop

Durchlauf: P zuerst im kritischen Abschnitt wantp=True, wantq =True , turn =1

(89)

Algorithmus von Dekker

Initial: wantp = False, wantq = False, turn =1

ProzessP: ProzessQ:

loop forever

(P1) restlicher Code (P2) wantp := True;

(P3) whilewantqdo (P4) ifturn=2then (P5) wantp := False;

(P6) awaitturn=1;

(P7) wantp := True;

(P8) Kritischer Abschnitt (P9) turn := 2;

(P10) wantp := False;

end loop

loop forever

(Q1) restlicher Code (Q2) wantq := True;

(Q3) whilewantpdo (Q4) ifturn=1then (Q5) wantq := False (Q6) awaitturn=2 (Q7) wantq := True;

(Q8) Kritischer Abschnitt (Q9) turn := 1;

(Q10) wantq :=False;

end loop

Durchlauf: P zuerst im kritischen Abschnitt wantp=True, wantq =True , turn =1

TCS | 03 Synchronisation I | WS 2019/20 32/94 Modell Mutual-Exclusion-Problem nProzesse Komplexit¨at

(90)

Algorithmus von Dekker

Initial: wantp = False, wantq = False, turn =1

ProzessP: ProzessQ:

loop forever

(P1) restlicher Code (P2) wantp := True;

(P3) whilewantqdo (P4) ifturn=2then (P5) wantp := False;

(P6) awaitturn=1;

(P7) wantp := True;

(P8) Kritischer Abschnitt (P9) turn := 2;

(P10) wantp := False;

end loop

loop forever

(Q1) restlicher Code (Q2) wantq := True;

(Q3) whilewantpdo (Q4) ifturn=1then (Q5) wantq := False (Q6) awaitturn=2 (Q7) wantq := True;

(Q8) Kritischer Abschnitt (Q9) turn := 1;

(Q10) wantq :=False;

end loop

Durchlauf: P zuerst im kritischen Abschnitt wantp=True, wantq =False , turn =1

(91)

Algorithmus von Dekker

Initial: wantp = False, wantq = False, turn =1

ProzessP: ProzessQ:

loop forever

(P1) restlicher Code (P2) wantp := True;

(P3) whilewantqdo (P4) ifturn=2then (P5) wantp := False;

(P6) awaitturn=1;

(P7) wantp := True;

(P8) Kritischer Abschnitt (P9) turn := 2;

(P10) wantp := False;

end loop

loop forever

(Q1) restlicher Code (Q2) wantq := True;

(Q3) whilewantpdo (Q4) ifturn=1then (Q5) wantq := False (Q6) awaitturn=2 (Q7) wantq := True;

(Q8) Kritischer Abschnitt (Q9) turn := 1;

(Q10) wantq :=False;

end loop

Durchlauf: P zuerst im kritischen Abschnitt wantp=True, wantq =False , turn =1

TCS | 03 Synchronisation I | WS 2019/20 32/94 Modell Mutual-Exclusion-Problem nProzesse Komplexit¨at

Referenzen

ÄHNLICHE DOKUMENTE

k kle klett kletter klettern: Das Kind klettert auf den Kasten.. Kann ein

wenn ein Buchstabe angesagt wird, muss ihn das Kind sofort niederschreiben können, ohne

1) Level 1 b data are (internally) calibrated instrument time series, while Level 2 data are products which are generated in the framework of HPF, such as precise GOCE

[r]

Sulasemeelsuse tuhinas unustab Nietzsche täiesti, et tema otsekohesus „isandate" tuludele kõige suuremat kahju saadab, just niisamati kui Macchiavelli

Mit welcher mittleren Leistung P muss das Aggregat mindestens betrieben werden, wenn die W¨armekapazit¨at des Sekts pro Liter C L = 4.2 kJ/L K betr¨agt und der K¨ uhlschrank

Vielleicht haben die Schul- meister eine professionelle Affinität zum Dezimalsystem, obwohl dies im Kontext der Aufgabe keine Rolle spielt.. 2 Die

Der Funktionsgraf hat keine Symmet- rien, ist aber von einer Achsensymmetrie nicht weit entfernt.. 9: Acht nicht beinahe