Beruflich Dokumente
Kultur Dokumente
Implementing locks
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
2 / 14
Requirements for a good solution
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
3 / 14
Requirements for a good solution
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
3 / 14
Requirements for a good solution
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
3 / 14
Progress vs. Bounded waiting
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
4 / 14
Progress vs. Bounded waiting
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
4 / 14
Progress vs. Bounded waiting
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
4 / 14
Mutual exclusion: legacy solutions
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
5 / 14
Mutual exclusion: legacy solutions
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
5 / 14
Mutual exclusion: legacy solutions
Solution for 2 threads T0 and T1
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
6 / 14
Peterson’s algorithm: a few comments
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
7 / 14
Correctness (1)
Mutual exclusion
Assume both threads in CS
Would mean wants[0] == wants[1] == true, so not turn would have
blocked one thread from CS
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
8 / 14
Correctness (2)
Progress
If T1−i doesn’t want CS, wants[1-i] == false, so Ti won’t loop
If both threads try to enter, one thread is the no turn thread
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
9 / 14
Correctness (3)
Bounded waiting
If Ti wants to lock and T1−i tries to re-enter, T1−i will set not turn =
1 - i, allowing Ti in
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
10 / 14
Q1
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
11 / 14
Answer
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
12 / 14
Answer
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
12 / 14
Answer
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
12 / 14
Answer
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
12 / 14
Q2
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
13 / 14
Peterson’s algorithm: a few more comments
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
14 / 14