Beruflich Dokumente
Kultur Dokumente
Gadi Taubenfeld
Chapter 1 Introduction
Thanks and enjoy! Gadi Taubenfeld All material copyright 2011 Gadi Taubenfeld, All Rights Reserved To get the most updated version of these slides go to: http://www.faculty.idc.ac.il/gadi/book.htm
Chapter 1 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld 2011 2
Synchronization
Type of synchronization Contention Coordination Why synchronization is Difficult? Easy between humans Communication between computers is restricted
reading and writing of notes sending and receiving messages
Chapter 1
The Too-Much-Milk Problem Too-MuchTime 5:00 5:05 5:10 5:15 5:20 5:25 5:30 3:40 3:45 Arrive at grocery Buy milk Arrive home; put milk in fridge Buy milk Arrive home; too much milk!
Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld 2011 4
Bob
Chapter 1
Solving the Too-Much-Milk Problem Too-MuchCorrectness properties Mutual Exclusion: Only one person buys milk at a time Progress: Someone always buys milk if it is needed Communication primitives Leave a note (set a flag) Remove a note (reset a flag) Read a note (test the flag)
Chapter 1
Important Distinction
Safety Property Nothing bad happens ever Example: mutual exclusion Liveness Property Something good happens eventually Example: progress
Chapter 1
Solution 1
Alice if (no note) { if (no milk) { leave Note buy milk remove note } }
Does it work? No, may end up with too much milk.
Chapter 1
Bob if (no note) { if (no milk) { leave Note buy milk remove note } }
' mutual exclusion
progress
7
Solution 2
Using labeled notes Alice leave note A if (no note B) { if (no milk) {buy milk} } remove note A Bob leave note B if (no note A) { if (no milk) {buy milk} } remove note B
Solution 3
Alice leave note A while (note B) {skip} if (no milk) {buy milk} remove note A
Bob leave note B if (no note A) { if (no milk) {buy milk} } remove note B
Chapter 1
Chapter 1
10
Solution 3+
Alice Bob leave note A repeat while (note B) {skip} leave note B Additional requirement [informal definition] if (no milk) {buy milk} if (note A) { Thirst-freedom: Nobody ever gets stuck forever. remove note B; (Both Alice and Bob eventually complete their programs.) remove note A while (note A) {skip} } until (note B) if (no milk) {buy milk} Does it work? remove note B
Yes, BUT
Chapter 1
BUT, Bob may forever get stuck in the repeat-until loop !!!
Solution 4
A1 A2 B2 B1
the fridges door
Chapter 1
12
Solution 4
Notations
solid line without color: we do not care if there is a note
A1 A2 B2 B2 X X Alices turn
Solution 4
A1 A2 B2 B2 X X A1 A2 B2 B1 A1 A2 B2 B1
Alices turn
Alices turn
Alices turn
B2 A2 B2 B1 X X
A1 A2
B2 B1
A1 A2 B2 B1
Bobs turn
Bobs turn
Bobs turn
Chapter 1
14
Solution 4
Alice leave A1 if B2 {leave A2} else {remove A2} while B1 and ((A2 and B2) or (no A2 and no B2)) {skip} if (no milk) {buy milk} remove A1 Bob leave B1 if (no A2) {leave B2} else {remove B2} while A1 and ((A2 and no B2) or (no A2 and B2)) {skip} if (no milk) {buy milk} remove note B1
A correct, fair, symmetric algorithm! mutual exclusion progress both Alice and Bob eventually complete their programs
Chapter 1 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld 2011 15
A variant of Solution 4
The order of the first two statements is replaced Is it correct ?
Alice if B2 {leave A2} else {remove A2} leave A1 while B1 and ((A2 and B2) or (no A2 and no B2)) {skip} if (no milk) {buy milk} MILK remove A1 Bob if (no A2) {leave B2} else {remove B2} leave B1 while A1 and ((A2 and no B2) or (no A2 and B2)) {skip} if (no milk) {buy milk} MILK remove note B1
No, may end up with two milk. ' mutual exclusion progress
A1
B2 B1
Chapter 1
16
Question
Alice and Bob have a daughter C. Make it work for all the three of them.
In Chapter 2, we solve a generalization of the too-much-milk problem, called the mutual exclusion problem, for any number of participants (not just two or three).
Chapter 1
17
Question
x is an atomic register, initially 0 Process A for i = 1 to 5 { x:=x+1 } 0 Process B for i = 1 to 5 { x:=x+1 }
Q: What are all the possible values for x after both processes terminate? A: 2 through 10 inclusive.
Chapter 1
18
0 A
1 0 B
1 2
2 5 4 3 0 1
Chapter 1
19
Question
x is an atomic register, initially 0 Process A x:=x+1; x:=x+1 Process B x:=x+1; x:=x+1 0 Process C x:=10
Q: What are all the possible values for x after the three processes terminate? A: 2,3,4,10,11,12,13,14.
Chapter 1
20
Enemy
Enemy
Chapter 1
22
Chapter 1
23
Synchronization
Contention Too-much-milk (1) mutual exclusion (2,3,4) Concurrent Data Structures (4) l-exclusion (6) multiple resources: dinning philosophers (7) readers & writer (8) group-exclusions (8) ... Coordination The coordinated attack (1) barrier synchronization (5) Concurrent Data Structures (4) producer-consumer (8) Choice Coordination (8) Consensus (data coordination) (9) ....
Chapter 1
24
Chapters 2+3
Chapter 1
25
Chapter 4
data structure
Chapter 1
26
Chapter 4
Blocking
P1 P2 P3
data structure
Chapter 1
27
Chapter 4
NonNon-blocking
P1
P2
P3
data structure
Chapter 1
28
CoarseCoarse-Grained Locking
FineFine-Grained Locking
Chapter 5
Barrier Synchronization
P1 Barrier P2 P3 P4 P1 Barrier P2 P3 P4 time four processes approach the barrier all except P4 arrive Once all arrive, they continue Barrier P1 P2 P3 P4
Chapter 1
31
time
end
Chapter 1
a+b
a+b+c
a+b+c+d
a+b+c +d+e
a+b+c +d+e+f
32
time
end
Chapter 1
a+b
a+b+c
a+b+c+d
a+b
b+c
c+d
d+e
e+f
time
a+b
a+b+c
end
Chapter 1
a+b
a+b+c
a+b+c+d
a+b+c +d+e
a+b+c +d+e+f
34
end
Chapter 1
a+b
a+b+c
a+b+c+d
a+b+c +d+e
a+b+c +d+e+f
35
Chapter 9
Consensus
1 0 1 1 0 1 0
Chapter 1
36
Communication
models of computation
Concurrent programming
Shared memory
safe registers atomic registers (read/write) test&set swap compare-and-swap load-link/store-conditional read-modify-write semaphore monitor
What is the relative power of these communication primitives? Answer: see Chapter 9.
Chapter 1 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld 2011 38
Communication
models of computation
Message passing
Chapter 1
39
Time
models of computation
This book
Synchronous
(CRCW, EREW)
Partially synchronous
. . .
. . .
C
P
C
. . .
M
P
M
Coherent Caching
Chapter 1
41
FaultFault-tolerance
models of computation What can fail? processes shared memory links Type of faults crash omission Byzantine How many can fail ? constant, n/3, ... Wait-freedom, Non-blocking, Lock-freedom what is it ? Self-stabilization what is it ?
Chapter 1
42
Chapter 1
43
Chapter 1
44
Chapter 1
45
Chapter 1
46
Model Summary
Multiple processes (or threads) Shared memory Communication via shared objects Unpredictable asynchronous delays Cover all the classical synchronization problems Focus more on principles, less on performance Look for simple elegant short algorithms
Chapter 1
47
Terminology
Hardware Processors Software Threads, processes Sometimes OK to confuse them, sometimes not. Scheduler
Chapter 1
48
User space
Threads
Kernel
Three processes each with one or more threads
Kernel
One process with three threads
Chapter 1
50
New
Ready
event occurs
Running
terminated
Blocked
Chapter 1
51
n -1
Scheduler
Non-preemptive | the scheduler can not interrupt a running process. Preemptive | the scheduler can interrupt a running process.
Chapter 1
52
Scheduling Algorithms
First-come-first-served schedules processes in the order they become ready. Shortest-job-first schedule the process with the shortest time. Priority scheduling schedule the process with the highest priority. Round robin each process is scheduled for a small number of time units (milliseconds). Many other possibilities exist.
Chapter 1
53
Scheduling
The Priority Inversion Problem Process P1 with priority 5 enters its CS, and is preempted before releasing it. Process P2 with priority 10 is scheduled but can not enter its CS and busy-waits. Process P1 wants to release its critical section (to enable P2 to enter) but never gets scheduled.
In the sequel, we will assume that processes have the same priority.
Chapter 1
54
Models
read/write
barrier
test&set
readers& writers
swap
consensus
LL/SC
Algorithms Upper and lower bounds Impossibility results Simulations & Implementations
Chapter 1 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld 2011 55
Chapter 1
56
entry code
critical section
exit code
The problem is to design the entry and exit code in a way that guarantees that the mutual exclusion and deadlock-freedom properties are satisfied.
Chapter 1 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld 2011 57
Chapter 1
58
Algorithm C
Algorithm A
Algorithm B
Chapter 1
59
Algorithm C
remainder C is entry code of A C entry code of B critical section exit code of B exit code of A
A or B satisfies mutual exclusion satisfies mutual exclusion. A is deadlock-free and B is starvation-free C is starvationfree.
Chapter 1
60
Chapter 1
61
Complexity Measures
Counting steps process step complexity process time complexity contention-free time complexity Counting time units system response time process response time Counting communications distributed shared memory coherent caching Space complexity
Chapter 1
62
E. W. Dijkstra
www.cs.utexas.edu/users/EWD/ (Photo by Hamilton Richards 2002)
Chapter 1
63
Next Chapter
In Chapter 2 we will start looking at basic solutions to the mutual exclusion problem using atomic registers. -- Gadi
Chapter 1
64