1 Chapter 7 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014...
-
Upload
warren-garrett -
Category
Documents
-
view
232 -
download
2
Transcript of 1 Chapter 7 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014...
1Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
Chapter 7
Synchronization Algorithms and Concurrent Programming
Gadi TaubenfeldChapter 7
Multiple resourcesThe dinning philosophers
problem
Version: June 2014
2Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
Chapter 7
A note on the use of these ppt slides:I am making these slides freely available to all (faculty, students, readers). They are in PowerPoint form so you can add, modify, and delete slides and slide content to suit your needs. They obviously represent a lot of work on my part. In return for use, I only ask the following:
That you mention their source, after all, I would like people to use my
book!
That you note that they are adapted from (or perhaps identical to) my slides, and note my copyright of this material.
Thanks and enjoy! Gadi Taubenfeld
All material copyright 2014Gadi Taubenfeld, All Rights Reserved
A note on the use of these ppt slides:I am making these slides freely available to all (faculty, students, readers). They are in PowerPoint form so you can add, modify, and delete slides and slide content to suit your needs. They obviously represent a lot of work on my part. In return for use, I only ask the following:
That you mention their source, after all, I would like people to use my
book!
That you note that they are adapted from (or perhaps identical to) my slides, and note my copyright of this material.
Thanks and enjoy! Gadi Taubenfeld
All material copyright 2014Gadi Taubenfeld, All Rights Reserved
Synchronization Algorithms and Concurrent Programming
ISBN: 0131972596, 1st edition
To get the most updated version of these slides go to: http://www.faculty.idc.ac.il/gadi/book.htm
3Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
Chapter 7
2.1 Deadlocks2.2 Deadlock Prevention2.3 Deadlock Avoidance2.4 The Dining Philosophers2.5 Hold and Wait Strategy2.5 Wait and Release
Strategy2.6 Randomized algorithms
2.1 Deadlocks2.2 Deadlock Prevention2.3 Deadlock Avoidance2.4 The Dining Philosophers2.5 Hold and Wait Strategy2.5 Wait and Release
Strategy2.6 Randomized algorithms
Chapter 7 Multiple Resources
Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
4
Deadlocks
Section 7.1
Chapter 7
5Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
Chapter 7
A set of processes is deadlocked if each process in the set is waiting for an event that only another process in the set can cause.
Deadlocks
6Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
Chapter 7
Multiple resourcesHow to avoid deadlock?
account Aaccount A account Baccount B
Transferring money between two bank accounts
P0 P1
down(A); down(B)down(B); down(A)
semaphores A and B, initialized to 1
deadlock
7Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
Chapter 7
Multiple resourcesHow to avoid deadlock?
Bridge crossing
On the bridge traffic only in one direction.
The resources are the two entrances.
8Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
Chapter 7
Two Simple Questions
Question: A system has 2 processes and 3 identical resources. Each process needs a maximum of 2 resources. Is deadlock possible?
Question: A system has 2 processes and 3 identical resources. Each process needs a maximum of 2 resources. Is deadlock possible?
Question: Consider a system with X identical resources. The system has 15 processes each needing a maximum of 15 resources. What is the smallest value for X which makes the system deadlock-free (without the need to use a deadlock avoidance algorithm)?
Question: Consider a system with X identical resources. The system has 15 processes each needing a maximum of 15 resources. What is the smallest value for X which makes the system deadlock-free (without the need to use a deadlock avoidance algorithm)?
No
15×14+1 = 211
9Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
Chapter 7
Question
Question: Two processes, P1 and P2 each need to hold five records 1,2,3,4 and 5 in a database to complete. If P1 asks for them in the order 1,2,3,4,5 and P2 asks them in the same order, deadlock is not possible. However, if P2 asks for them in the order 5,4,3,2,1 then deadlock is possible. With five resources, there are 5! or 120 possible combinations each process can request the resources. Hence there are 5!×5! different algorithms. What is the exact number of algorithms (out of 5!×5!) that is guaranteed to be deadlock free?
Question: Two processes, P1 and P2 each need to hold five records 1,2,3,4 and 5 in a database to complete. If P1 asks for them in the order 1,2,3,4,5 and P2 asks them in the same order, deadlock is not possible. However, if P2 asks for them in the order 5,4,3,2,1 then deadlock is possible. With five resources, there are 5! or 120 possible combinations each process can request the resources. Hence there are 5!×5! different algorithms. What is the exact number of algorithms (out of 5!×5!) that is guaranteed to be deadlock free?
5!(4!×4!) = (5!×5!)/5
10Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
Chapter 7
Strategies for dealing with Deadlocks
Just ignore the problem altogether UNIX and Windows take this approach.
Detection and recovery Allow the system to enter a deadlock state and
then recover. Avoidance
By careful resource allocation, ensure that the system will never enter a deadlock state.
Prevention The programmer should write programs that
never deadlock. This is achieved by negating one of the four necessary conditions for deadlock to occur (mentioned in the next slide.)
Just ignore the problem altogether UNIX and Windows take this approach.
Detection and recovery Allow the system to enter a deadlock state and
then recover. Avoidance
By careful resource allocation, ensure that the system will never enter a deadlock state.
Prevention The programmer should write programs that
never deadlock. This is achieved by negating one of the four necessary conditions for deadlock to occur (mentioned in the next slide.)
Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
11
Deadlock Prevention
Section 7.2
Chapter 7
12Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
Chapter 7
Mutual exclusion condition one process at a time can use the resource.
Hold and wait condition a process can request (and wait for) a
resource while holding another resource. No preemption condition
A resource can be released only voluntarily by the process holding it.
Circular wait condition must be a cycle involving several processes,
each waiting for a resource held by the next one.
Mutual exclusion condition one process at a time can use the resource.
Hold and wait condition a process can request (and wait for) a
resource while holding another resource. No preemption condition
A resource can be released only voluntarily by the process holding it.
Circular wait condition must be a cycle involving several processes,
each waiting for a resource held by the next one.
Deadlock PreventionAttacking one of the following conditions for deadlock
13Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
Chapter 7
Attacking the mutual exclusion condition
Some devices (such as printer) can be spooled only the printer
daemon uses printer resource, thus deadlock for printer eliminated
Not all devices can be spooled
attack is not useful in general
Some devices (such as printer) can be spooled only the printer
daemon uses printer resource, thus deadlock for printer eliminated
Not all devices can be spooled
attack is not useful in general
Attacking the no preemption condition Many resources (such as
printer) should not be preempted can not take the
printer from a process that has not finished printing yet
attack is not useful in general
Many resources (such as printer) should not be preempted can not take the
printer from a process that has not finished printing yet
attack is not useful in general
14Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
Chapter 7
Attacking the Hold and Wait Condition
Problems May not know all required resources in
advance. Inefficient : ties up resources other processes
could be using. Starvation is possible.
Problems May not know all required resources in
advance. Inefficient : ties up resources other processes
could be using. Starvation is possible.
Processes may request all the resources they need in advance.
15Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
Chapter 7
Two-Phase Locking(Notice similarity to requesting all resources at once)
Phase one The process tries to lock all the resources
it currently needs, one at a time if needed record is not avaliable, release
and start over Phase two: when phase one succeeds,
performing updates releasing locks
Phase one The process tries to lock all the resources
it currently needs, one at a time if needed record is not avaliable, release
and start over Phase two: when phase one succeeds,
performing updates releasing locks
“livelock” is possible.
16Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
Chapter 7
Time stamps: Before a process starts locking a unique
new timestamp is associated with that process.
If a process has been assigned timestamp Ti and later a new process has assigned timestamp Tj then Ti <Tj.
We associate with each resource a timestamp value, which is the timestamp of the process that is currently holding that resource.
Time stamps: Before a process starts locking a unique
new timestamp is associated with that process.
If a process has been assigned timestamp Ti and later a new process has assigned timestamp Tj then Ti <Tj.
We associate with each resource a timestamp value, which is the timestamp of the process that is currently holding that resource.
The time-stamping ordering technique
17Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
Chapter 7
Phase one: the process tries to lock all the resources it currently needs, one at a time.
If a needed resource is not available and the timestamp value is smaller than that of the process, release all the resources, waits until the resource with the smaller
timestamp is released, and starts over.
Otherwise, if the timestamp of the resource is not smaller, waits until the resource is released and locks
it.Phase two: when phase one succeeds, performing updates; releasing locks.
Phase one: the process tries to lock all the resources it currently needs, one at a time.
If a needed resource is not available and the timestamp value is smaller than that of the process, release all the resources, waits until the resource with the smaller
timestamp is released, and starts over.
Otherwise, if the timestamp of the resource is not smaller, waits until the resource is released and locks
it.Phase two: when phase one succeeds, performing updates; releasing locks.
The time-stamping ordering technique
Prevents deadlock and starvation.
18Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
Chapter 7
Attacking the Circular Wait Condition
Impose a total ordering of all resource types, and require that each process requests resources in an increasing order of enumeration.
11 22 33 44 55 66 77
We will see other interesting usage of this observation
time
account A account B
Solves transferring money between two bank accounts
Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
19
Deadlock Avoidance
Section 7.3
Chapter 7
20Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
Chapter 7
Safe and Unsafe States
safeunsafedeadloc
k
time
All terminated!
Deadlock Avoidance
21Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
Chapter 7
Basic Facts
If a system is in safe state no deadlock.
If a system is in unsafe state deadlock now or in the future.
Deadlock Avoidance ensure that a system will never enter an unsafe state.
If a system is in safe state no deadlock.
If a system is in unsafe state deadlock now or in the future.
Deadlock Avoidance ensure that a system will never enter an unsafe state.
22Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
Chapter 7
Maxim
um
Allo
catio
n
available : 2
P1
P2
P3
1 9
4 5
2 8
Example: Prove that the state below is safe
23Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
Chapter 7
Maxim
um
Allo
catio
n
Proof
time
available : 8
P1
P2
P3
1 9
0 -
0 -
Maxim
um
Allo
catio
n
available : 0
P1
P2
P3
1 9
0 -
8 8
Maxim
um
Allo
catio
n
available : 6
P1
P2
P3
1 9
0 -
2 8
Maxim
um
Allo
catio
n
available : 1
P1
P2
P3
1 9
5 5
2 8
Maxim
um
Allo
catio
n
available : 2
P1
P2
P3
1 9
4 5
2 8
available : 0
P1
P2
P3
9 9
0 --
0 --
available : 9
P1
P2
P3
0 --
0 --
0 --
24Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
Chapter 7
Example: safe and unsafe
Maxim
um
Allo
catio
n
available : 2
P1
P2
P3
1 9
4 5
2 8
If process P1 requests one (out of the 2 avaliable), resources the Banker will not
allocated it.
available : 1
P1
P2
P3
2 9
4 5
2 8
unsafestate
25Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
Chapter 7
The Banker’s Algorithm
When there is a request for an available resource, the banker must decide if immediate allocation leaves the system in a safe state.
If the answer is positive, the resource is allocated, otherwise the request is temporarily denied.
When there is a request for an available resource, the banker must decide if immediate allocation leaves the system in a safe state.
If the answer is positive, the resource is allocated, otherwise the request is temporarily denied.
A state is safe if there exists a sequence of all processes <P1, P2, …,
Pn> such that for each Pi,
the resources that Pi can
still request can be satisfied by currently available resources + resources held by all the Pj, with j < i.
A state is safe if there exists a sequence of all processes <P1, P2, …,
Pn> such that for each Pi,
the resources that Pi can
still request can be satisfied by currently available resources + resources held by all the Pj, with j < i.
26Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
Chapter 7
Can handle Multiple instances. Each process must a priori claim
maximum use -- a disadvantage. When a process requests a resource it
may have to wait. When a process gets all its resources it
must return them in a finite amount of time.
Can handle Multiple instances. Each process must a priori claim
maximum use -- a disadvantage. When a process requests a resource it
may have to wait. When a process gets all its resources it
must return them in a finite amount of time.
The Banker’s Algorithm: commensts
Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
27
The Dinning Philosophers Problem
Section 7.4
Chapter 7
28Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
Chapter 7
Dining Philosophers Philosophers
think take forks eat put forks
Eating needs 2 forks
Pick one fork at a time
How to prevent deadlock
Philosophers think take forks eat put forks
Eating needs 2 forks
Pick one fork at a time
How to prevent deadlock
29Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
Chapter 7
( means “waiting for this forks”)An incorrect solution
L
L
L
LL
L
30Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
Chapter 7
An inefficient solutionusing mutual exclusion
31Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
Chapter 7
Proving deadlock-freedom & starvation-freedom
1 2
3
45
6
R
R
R
R
R
L
Impose a total ordering of all forks, and require thateach philosopher requests resources in an increasing
order.
Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
32
The Hold and Wait Strategy
Section 7.5
Chapter 7
33Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
Chapter 7
R
L
L
RR
L
The LR Solution
34Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
Chapter 7
1 4
2
53
6
R
L
R
L
R
L
The LR Solution
Proving deadlock-freedom & starvation-freedom
35Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
Chapter 7
Concurrency How many can eat simultaneously?
36Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
Chapter 7
At most half can eat simultaneously
37Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
Chapter 7
Only one third can eat simultaneouslyAny algorithm is at most n/3-concurrent
38Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
Chapter 7
In LR only one forth can eat simultaneouslyThe LR algorithm is at most n/4-concurrent
39Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
Chapter 7
If all want to eat, there is a case where only n/4 will be able to eat simultaneously.
R
free
RL
L
LR
( means “waiting for this forks”)
40Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
Chapter 7
Robustness
k-robust: if all except k consecutive
philosophers fail, then one will not
starve. Any algorithm is at most
n/3-robust. The LR algorithm is not 4-
robust. The LR algorithm is 5-robust iff
n is even.
There is no 4-robust algorithm using a hold and wait strategy.
k-robust: if all except k consecutive
philosophers fail, then one will not
starve. Any algorithm is at most
n/3-robust. The LR algorithm is not 4-
robust. The LR algorithm is 5-robust iff
n is even.
There is no 4-robust algorithm using a hold and wait strategy.
41Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
Chapter 7
L
L
R
LR
L
The LLR Solution
42Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
Chapter 7
LLR
3 2
1
65
0
L
L
R
L
L
R
Proving deadlock-freedom & starvation-freedom
43Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
Chapter 7
In LLR one third can eat simultaneously
A tight bound
The LLR algorithm is n/3-concurrent
44Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
Chapter 7
Robustness
k-robust: if all except k consecutive
philosophers fail, then one will not
starve. The LLR algorithm is not 4-
robust. The LLR algorithm is 5-robust
iff n 0 mod 3.
k-robust: if all except k consecutive
philosophers fail, then one will not
starve. The LLR algorithm is not 4-
robust. The LLR algorithm is 5-robust
iff n 0 mod 3.
Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
45
The Hold and Release Strategy
Section 7.6
Chapter 7
46Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
Chapter 7
The LR wait/release Algorithm
R
L
L
RR
L
The LR wait/release algorithm is: deadlock-free but not
starvation-free. n/3-concurrent. 3-robust iff n is even.
Recall: There is no 4-robust algorithm using a hold and wait strategy.
The LR wait/release algorithm is: deadlock-free but not
starvation-free. n/3-concurrent. 3-robust iff n is even.
Recall: There is no 4-robust algorithm using a hold and wait strategy.
Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
47
Randomized Algorithms
Section 7.7
Chapter 7
48Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
Chapter 7
Two Randomized Algorithms
The Free Philosophers algorithm is:
deadlock-free with probability 1, but is not starvation-free and is not 2-concurrent.
3-robust with probability 1.
The Free Philosophers algorithm is:
deadlock-free with probability 1, but is not starvation-free and is not 2-concurrent.
3-robust with probability 1.
The Courteous Philosophers algorithm is:
starvation-free with probability 1, but is not 2-concurrent and is not (n-1)-robust.
The Courteous Philosophers algorithm is:
starvation-free with probability 1, but is not 2-concurrent and is not (n-1)-robust.