Prinzipien, Modelle & Algorithmen der Nebenl¨aufigen Programmierung Wintersemester 2020/21
Das Mutual-Exclusion-Problem
Prof. Dr. David Sabel
LFE Theoretische Informatik
Ubersicht ¨
1 Einleitendes Beispiel: Too much milk
2 Formalisierung des Mutual-Exclusion-Problems
3 Algorithmen f¨ur 2 Prozesse mit atomarem Read & Write Dekkers Algorithmus
Petersons Algorithmus Kessels’ Algorithmus
TCS | 03 Das Mutual-Exclusion-Problem | WS 2020/21 2/35 Too much milk Formales Modell Atomic read/write
Einleitendes Beispiel: Too much milk
Alice und Bob teilen sich einen K¨ uhlschrank
Sie wollen, dass stets genug Milch im K¨ uhlschrank ist Weder zuviel noch zu wenig Milch.
Naive L¨osung
Wenn jemand sieht, dass keine Milch im K¨ uhlschrank ist, geht er Milch kaufen.
Naive L¨ osung funktioniert nicht
Alice Bob
17:00 kommt nach Hause
17:05 bemerkt: keine Milch im K¨ uhlschrank 17:10 geht zum Supermarkt
17:15 kommt nach Hause
17:20 bemerkt: keine Milch im K¨ uhlschrank
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 Das Mutual-Exclusion-Problem | WS 2020/21 4/35 Too much milk Formales Modell Atomic read/write
Naive L¨ osung funktioniert nicht
Alice Bob
17:00 kommt nach Hause
17:05 bemerkt: keine Milch im K¨ uhlschrank
17:10 geht zum Supermarkt
17:15 kommt nach Hause
17:20 bemerkt: keine Milch im K¨ uhlschrank
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
Naive L¨ osung funktioniert nicht
Alice Bob
17:00 kommt nach Hause
17:05 bemerkt: keine Milch im K¨ uhlschrank 17:10 geht zum Supermarkt
17:15 kommt nach Hause
17:20 bemerkt: keine Milch im K¨ uhlschrank
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 Das Mutual-Exclusion-Problem | WS 2020/21 4/35 Too much milk Formales Modell Atomic read/write
Naive L¨ osung funktioniert nicht
Alice Bob
17:00 kommt nach Hause
17:05 bemerkt: keine Milch im K¨ uhlschrank 17:10 geht zum Supermarkt
17:15 kommt nach Hause
17:20 bemerkt: keine Milch im K¨ uhlschrank
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
Naive L¨ osung funktioniert nicht
Alice Bob
17:00 kommt nach Hause
17:05 bemerkt: keine Milch im K¨ uhlschrank 17:10 geht zum Supermarkt
17:15 kommt nach Hause
17:20 bemerkt: keine Milch im K¨ uhlschrank
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 Das Mutual-Exclusion-Problem | WS 2020/21 4/35 Too much milk Formales Modell Atomic read/write
Naive L¨ osung funktioniert nicht
Alice Bob
17:00 kommt nach Hause
17:05 bemerkt: keine Milch im K¨ uhlschrank 17:10 geht zum Supermarkt
17:15 kommt nach Hause
17:20 bemerkt: keine Milch im K¨ uhlschrank
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
Naive L¨ osung funktioniert nicht
Alice Bob
17:00 kommt nach Hause
17:05 bemerkt: keine Milch im K¨ uhlschrank 17:10 geht zum Supermarkt
17:15 kommt nach Hause
17:20 bemerkt: keine Milch im K¨ uhlschrank
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 Das Mutual-Exclusion-Problem | WS 2020/21 4/35 Too much milk Formales Modell Atomic read/write
Naive L¨ osung funktioniert nicht
Alice Bob
17:00 kommt nach Hause
17:05 bemerkt: keine Milch im K¨ uhlschrank 17:10 geht zum Supermarkt
17:15 kommt nach Hause
17:20 bemerkt: keine Milch im K¨ uhlschrank
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
Naive L¨ osung funktioniert nicht
Alice Bob
17:00 kommt nach Hause
17:05 bemerkt: keine Milch im K¨ uhlschrank 17:10 geht zum Supermarkt
17:15 kommt nach Hause
17:20 bemerkt: keine Milch im K¨ uhlschrank
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 Das Mutual-Exclusion-Problem | WS 2020/21 4/35 Too much milk Formales Modell Atomic read/write
Too much milk: Spezifikation
Alice und Bob vereinbaren,
Notizen am K¨uhlschrankzu hinterlassen, die beide lesen k¨ onnen.
Dadurch soll sichergestellt werden, dass:
H¨ ochstens einer von beiden geht Milch kaufen, wenn keine Milch im K¨ uhlschrank ist.
Einer von beiden 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
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 Das Mutual-Exclusion-Problem | WS 2020/21 6/35 Too much milk Formales Modell Atomic read/write
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
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 Das Mutual-Exclusion-Problem | WS 2020/21 6/35 Too much milk Formales Modell Atomic read/write
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
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 Das Mutual-Exclusion-Problem | WS 2020/21 6/35 Too much milk Formales Modell Atomic read/write
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
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 Das Mutual-Exclusion-Problem | WS 2020/21 6/35 Too much milk Formales Modell Atomic read/write
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
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 Das Mutual-Exclusion-Problem | WS 2020/21 6/35 Too much milk Formales Modell Atomic read/write
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
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 Das Mutual-Exclusion-Problem | WS 2020/21 6/35 Too much milk Formales Modell Atomic read/write
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
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 Das Mutual-Exclusion-Problem | WS 2020/21 7/35 Too much milk Formales Modell Atomic read/write
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
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 Das Mutual-Exclusion-Problem | WS 2020/21 7/35 Too much milk Formales Modell Atomic read/write
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
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 Das Mutual-Exclusion-Problem | WS 2020/21 7/35 Too much milk Formales Modell Atomic read/write
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!
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 Das Mutual-Exclusion-Problem | WS 2020/21 8/35 Too much milk Formales Modell Atomic read/write
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
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 Das Mutual-Exclusion-Problem | WS 2020/21 8/35 Too much milk Formales Modell Atomic read/write
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
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 Das Mutual-Exclusion-Problem | WS 2020/21 8/35 Too much milk Formales Modell Atomic read/write
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
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 Das Mutual-Exclusion-Problem | WS 2020/21 8/35 Too much milk Formales Modell Atomic read/write
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
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 Das Mutual-Exclusion-Problem | WS 2020/21 8/35 Too much milk Formales Modell Atomic read/write
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
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 Das Mutual-Exclusion-Problem | WS 2020/21 8/35 Too much milk Formales Modell Atomic read/write
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
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 Das Mutual-Exclusion-Problem | WS 2020/21 8/35 Too much milk Formales Modell Atomic read/write
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
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 Das Mutual-Exclusion-Problem | WS 2020/21 8/35 Too much milk Formales Modell Atomic read/write
Eine korrekte L¨ osung
Programm f¨ur Alice (A1) schreibe a1;
(A2)ifb2
(A3) thenschreibe a2;
(A4) elseentferne a2;
(A5)whileb1∧((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)whilea1∧((a2∧ ¬b2)∨(¬a2∧b2)) (B6) do skip;
(B7)ifkeine Milch (B8) thenkaufe Milch;
(B9) entferne b1;
a1 a2 b2 b1 pr¨ uft Milch
Xdc dc
×Alice
×
dc dc
XBob
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 × × XBob
Eine korrekte L¨ osung
Programm f¨ur Alice (A1) schreibe a1;
(A2)ifb2
(A3) thenschreibe a2;
(A4) elseentferne a2;
(A5)whileb1∧((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)whilea1∧((a2∧ ¬b2)∨(¬a2∧b2)) (B6) do skip;
(B7)ifkeine Milch (B8) thenkaufe Milch;
(B9) entferne b1;
a1 a2 b2 b1 pr¨ uft Milch
Xdc dc
×Alice
×
dc dc
XBob
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 × × XBob
TCS | 03 Das Mutual-Exclusion-Problem | WS 2020/21 9/35 Too much milk Formales Modell Atomic read/write
Eine korrekte L¨ osung
Programm f¨ur Alice (A1) schreibe a1;
(A2)ifb2
(A3) thenschreibe a2;
(A4) elseentferne a2;
(A5)whileb1∧((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)whilea1∧((a2∧ ¬b2)∨(¬a2∧b2)) (B6) do skip;
(B7)ifkeine Milch (B8) thenkaufe Milch;
(B9) entferne b1;
a1 a2 b2 b1 pr¨ uft Milch
Xdc dc
×Alice
×
dc dc
XBob
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 × × XBob
bedeutet “... will an den K¨uhlschrank”Eine korrekte L¨ osung
Programm f¨ur Alice (A1) schreibe a1;
(A2)ifb2
(A3) thenschreibe a2;
(A4) elseentferne a2;
(A5)whileb1∧((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)whilea1∧((a2∧ ¬b2)∨(¬a2∧b2)) (B6) do skip;
(B7)ifkeine Milch (B8) thenkaufe Milch;
(B9) entferne b1;
a1 a2 b2 b1 pr¨ uft Milch
Xdc dc
×Alice
×
dc dc
XBob
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 × × XBob
TCS | 03 Das Mutual-Exclusion-Problem | WS 2020/21 9/35 Too much milk Formales Modell Atomic read/write
versucht Vorrang an Bob zu geben
Eine korrekte L¨ osung
Programm f¨ur Alice (A1) schreibe a1;
(A2)ifb2
(A3) thenschreibe a2;
(A4) elseentferne a2;
(A5)whileb1∧((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)whilea1∧((a2∧ ¬b2)∨(¬a2∧b2)) (B6) do skip;
(B7)ifkeine Milch (B8) thenkaufe Milch;
(B9) entferne b1;
a1 a2 b2 b1 pr¨ uft Milch
Xdc dc
×Alice
×
dc dc
XBob
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 × × XBob
versucht Vorrang an Alice zu gebenIm Folgenden:
Mutual-Exclusion-Problem:
Formalisierung
Definition des Problems und einer L¨ osung Annahmen ¨ uber das Modell Deadlock- bzw. Starvation-Freiheit
TCS | 03 Das Mutual-Exclusion-Problem | WS 2020/21 10/35 Too much milk Formales Modell Atomic read/write
Das Mutual-Exclusion-Problem
Mutual-Exclusion-Problem informell:
Garantie, dass der
exklusive Zugriffauf eine gemeinsam genutzte Ressource sichergestellt wird
Zugriffscode wird als
kritischer Abschnittbezeichnet.
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 ConditionsDas Mutual-Exclusion-Problem, formales Modell
Code-Struktur jedes Prozesses
loop forever
restlicher Code Initialisierungscode Kritischer Abschnitt Abschlusscode
end loopTCS | 03 Das Mutual-Exclusion-Problem | WS 2020/21 12/35 Too much milk Formales Modell Atomic read/write
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
Das Mutual-Exclusion-Problem, formales Modell (3)
Annahmen (Fortsetzung)
Keine Fehler bei Ausf¨ uhrung des Initialisierungscodes, 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 Das Mutual-Exclusion-Problem | WS 2020/21 14/35 Too much milk Formales Modell Atomic read/write
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
irgendeinProzess schließlich den kritischen Abschnitt.
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
erihn nach endlich vielen Berechnungsschritten betreten.
Ein Starvation-freier Algorithmus ist auch Deadlock-frei.
TCS | 03 Das Mutual-Exclusion-Problem | WS 2020/21 16/35 Too much milk Formales Modell Atomic read/write
Formale Beweistechniken
Wechselseitiger Ausschluss ist eine sog.
Sicherheitseigenschaft(Safety Property), eine Eigenschaft die
immererf¨ 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 machen die Beweise direkt (eher informell).
Wir betrachten:
L¨ osungen zum Mutual-Exclusion-Problem bei 2 Prozessen
und atomarem Lesen und Schreiben
TCS | 03 Das Mutual-Exclusion-Problem | WS 2020/21 18/35 Too much milk Formales Modell Atomic read/write
Algorithmen f¨ ur 2 Prozesse mit atomarem Read+Write
Im Folgenden:
Einige Algorithmen zum Mutual-Exclusion-Problem Nebenl¨ aufiges Programm mit genau
zweiProzessen Annahmen:
Als atomare Speicheroperationen des gemeinsamen Speichers nur
Lesenund
SchreibenZ.B. verboten swap(X,Y), X := Y , etc. f¨ ur gemeinsame Variablen Abk¨ urzung:
await Bedingung;
anstelle von
while ¬Bedingung do skip;
Dekkers Algorithmus
Historisch die erste korrekte L¨ osung
Theodorus Jozef Dekker = Niederl¨ andischer Mathematiker Erw¨ ahnung des Dekker-Algorithmus: Dijkstra 1965
Nicht ganz einfache L¨ osung
TCS | 03 Das Mutual-Exclusion-Problem | WS 2020/21 20/35 Too much milk Formales Modell Atomic read/write
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
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 Das Mutual-Exclusion-Problem | WS 2020/21 21/35 Too much milk Formales Modell Atomic read/write
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
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 Das Mutual-Exclusion-Problem | WS 2020/21 21/35 Too much milk Formales Modell Atomic read/write
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
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 Das Mutual-Exclusion-Problem | WS 2020/21 21/35 Too much milk Formales Modell Atomic read/write
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
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 Das Mutual-Exclusion-Problem | WS 2020/21 21/35 Too much milk Formales Modell Atomic read/write
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
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 Das Mutual-Exclusion-Problem | WS 2020/21 21/35 Too much milk Formales Modell Atomic read/write
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
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 Das Mutual-Exclusion-Problem | WS 2020/21 21/35 Too much milk Formales Modell Atomic read/write
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
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 Das Mutual-Exclusion-Problem | WS 2020/21 21/35 Too much milk Formales Modell Atomic read/write
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
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 Das Mutual-Exclusion-Problem | WS 2020/21 21/35 Too much milk Formales Modell Atomic read/write
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
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 Das Mutual-Exclusion-Problem | WS 2020/21 21/35 Too much milk Formales Modell Atomic read/write
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
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 =2
TCS | 03 Das Mutual-Exclusion-Problem | WS 2020/21 21/35 Too much milk Formales Modell Atomic read/write
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
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 =2
TCS | 03 Das Mutual-Exclusion-Problem | WS 2020/21 21/35 Too much milk Formales Modell Atomic read/write
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
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 =True , turn =2
TCS | 03 Das Mutual-Exclusion-Problem | WS 2020/21 21/35 Too much milk Formales Modell Atomic read/write
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
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 =True , turn =2
TCS | 03 Das Mutual-Exclusion-Problem | WS 2020/21 21/35 Too much milk Formales Modell Atomic read/write
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
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 Das Mutual-Exclusion-Problem | WS 2020/21 21/35 Too much milk Formales Modell Atomic read/write