Professional Documents
Culture Documents
u wx672ster+os@gmail.com
1 / 29
Deadlocks
Resources Introduction to Deadlocks Deadlock Modeling Deadlock Detection and Recovery Deadlock Avoidance Deadlock Prevention The Ostrich Algorithm
2 / 29
Processes need access to resources in reasonable order Suppose a process holds resource A and requests resource B
at same time another process holds B and requests A both are blocked and remain so
3 / 29
Resources
typedef int semaphore; semaphore resource 1; semaphore resource 2; void process A(void) { down(&resource 1); down(&resource 2); use both resources( ); up(&resource 2); up(&resource 1); } void process B(void) { down(&resource 1); down(&resource 2); use both resources( ); up(&resource 2); up(&resource 1); } (a) semaphore resource 1; semaphore resource 2; void process A(void) { down(&resource 1); down(&resource 2); use both resources( ); up(&resource 2); up(&resource 1); } void process B(void) { down(&resource 2); down(&resource 1); use both resources( ); up(&resource 1); up(&resource 2); } (b)
4 / 29
Resources
typedef int semaphore; semaphore resource 1; semaphore resource 2; void process A(void) { down(&resource 1); down(&resource 2); use both resources( ); up(&resource 2); up(&resource 1); } void process B(void) { down(&resource 1); down(&resource 2); use both resources( ); up(&resource 2); up(&resource 1); } (a) semaphore resource 1; semaphore resource 2; void process A(void) { down(&resource 1); down(&resource 2); use both resources( ); up(&resource 2); up(&resource 1); } void process B(void) { down(&resource 2); down(&resource 1); use both resources( ); up(&resource 1); up(&resource 2); } (b)
4 / 29
D .
ea dl oc k!
Resources
Deadlocks occur when ...
processes are granted exclusive access to resources
devices, data records, files, ...
Preemptable resources can be taken away from a process with no ill effects e.g. memory Nonpreemptable resources will cause the process to fail if taken away e.g. CD recorder In general, deadlocks involve nonpreemptable resources
5 / 29
Resources
6 / 29
7 / 29
Introduction to Deadlocks
Deadlock 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. Usually the event is release of a currently held resource None of the processes can ...
run release resources be awakened
8 / 29
9 / 29
10 / 29
Resource-Allocation Graph
A S D
h . as
w . ants
R (a)
B (b)
C (c)
Fig. (a) Strategies3-3. Resource allocation graphs.Deadlocks for dealing (c) Deadlock. Holding a resource. (b) Requesting a resource. with
1. just ignore the problem altogether 2. detection and recovery 3. dynamic avoidance careful resource allocation 4. prevention negating one of the four necessary conditions .
11 / 29
Resource-Allocation Graph
12 / 29
Resource-Allocation Graph
Basic facts:
No cycles no deadlock If graph contains a cycle
if only one instance per resource type, then deadlock if several instances per resource type, possibility of deadlock
13 / 29
Deadlock Detection
14 / 29
Deadlock Detection
Single Instance of Each Resource Type Wait-for Graph
Pi Pj if Pi is waiting for Pj ; Periodically invoke an algorithm that searches for a cycle in the graph. If there is a cycle, there exists a deadlock. An algorithm to detect a cycle in a graph requires an order of n2 operations, where n is the number of vertices in the graph.
15 / 29
Deadlock Detection
Several Instances of a Resource Type
Resources in existence (E1, E2, E3, , Em) Current allocation matrix C11 C21 Cn1 C12 C22 Cn2 C13 C23 Cn3 C1m C2m Cnm R11 R21 Rn1 Resources available (A1, A2, A3, , Am) Request matrix R12 R22 Rn2 R13 R23 Rn3 R1m R2m Rnm
n i=1
Cij + Aj = Ej
Fig. 3-6. The four data structures needed by the deadlock detection e.g. algorithm. (C13 + C23 + . . . + Cn3 ) + A3 = E3
16 / 29
Deadlock Detection
Several Instances of a Resource Type
Deadlock Detection
Several Instances of a Resource Type
Vectors comparison
For two vectors, X and Y XY
e.g.
iff Xi Yi
[ ] 1 2 3 4 [ ] 1 2 3 4
for 0 i m
[ ] 2 3 4 4 [ ] 2 3 2 4
18 / 29
Deadlock Detection
Several Instances of a Resource Type
ive
ive
rs
rs ne an C
0)
ne
om
dr
te
dr
te
an
ot
Ta p
Ta p
ot
Pl
Sc
E=(4
1)
A=(2
Pl
Request matrix 2 0 0 1 1 0 1 0 2 1 0 0
C=
R=
Sc
D
19 / 29
om
rs
rs
Deadlock Detection
Several Instances of a Resource Type
ive
ive
rs
rs ne an C
0)
ne
om
dr
te
dr
te
an
ot
Ta p
Ta p
ot
Pl
Sc
E=(4
1)
A=(2
Pl
A . R ( . 2 1 0 0) R3 , (2 1 0 0)
Request matrix 2 0 0 1 1 0 1 0 2 1 0 0
C=
R=
Sc
om
rs
rs
Deadlock Detection
Several Instances of a Resource Type
ive
ive
rs
rs ne an C
0)
ne
om
dr
te
dr
te
an
ot
Ta p
Ta p
ot
Pl
Sc
E=(4
1)
A=(2
Pl
A . R ( . 2 1 0 0) R3 , (2 1 0 0) ( . 2 2 2 0) R2 , (1 0 1 0)
Current allocation matrix 0 0 1 0 2 0 0 1 0 1 2 0 Request matrix 2 0 0 1 1 0 1 0 2 1 0 0
C=
R=
Sc
om
rs
rs
Deadlock Detection
Several Instances of a Resource Type
ive
ive
rs
rs ne an C
0)
ne
om
dr
te
dr
te
an
ot
Ta p
Ta p
ot
Pl
Sc
E=(4
1)
A=(2
Pl
A . R ( . 2 1 0 0) R3 , (2 1 0 0) ( . 2 2 2 0) R2 , (1 0 1 0) ( . 4 2 2 0) R3 , (2 0 0 1)
Current allocation matrix 0 0 1 0 2 0 0 1 0 1 2 0 Request matrix 2 0 0 1 1 0 1 0 2 1 0 0
C=
R=
.
19 / 29
Sc
om
rs
rs
20 / 29
;; ;; ;
Resource Trajectories
B u (Both processes finished) Printer I8 I7 I6 I5 t
Deadlock Avoidance
Plotter
U . nsafe region
A
q Printer
I1
I2
I3
I4 Plotter
B is requesting a resource at point t. The system must decide whether to grant it trajectories. Fig. 3-8. Two process resource or not. .
21 / 29
Deadlock Avoidance
Safe and Unsafe States
Assuming E = 10,
Safe
Has Max A B C 3 2 2 Free: 3 (a) 9 4 7 A B C Has Max 3 4 2 Free: 1 (b) 9 4 7 A B C Has Max 3 0 2 Free: 5 (c) 9 7 A B C Has Max 3 0 7 Free: 0 (d) 9 7 A B C Has Max 3 0 0 Free: 7 (e) 9
9 7
Deadlock Avoidance
The Bankers Algorithm for a Single Resource
The bankers algorithm considers each request as it occurs, and sees if granting it leads to a safe state.
Has Max A B C D 0 0 0 0 Free: 10 (a) 6 5 4 7 A B C D Has Max 1 1 2 4 Free: 2 (b) 6 5 4 7 A B C D Has Max 1 2 2 4 Free: 1 (c) 6 5 4 7
Fig. 3-11. Three resource allocation states: (a) Safe. (b) Safe. (c) Unsafe.
23 / 29
Deadlock Avoidance
The Bankers Algorithm for a Single Resource
The bankers algorithm considers each request as it occurs, and sees if granting it leads to a safe state.
Has Max A B C D 0 0 0 0 Free: 10 (a) 6 5 4 7 A B C D Has Max 1 1 2 4 Free: 2 (b) 6 5 4 7 A B C D Has Max 1 6 5 4 7
U .
. Fig. 3-11. Three resource allocation states: (a) Safe. (b) Safe. unsafe = deadlock (c) Unsafe.
23 / 29
ns
Free: 1 (c)
af
2 4
e!
Deadlock Avoidance
The Bankers Algorithm for Multiple Resources
oc e Ta ss pe Pl dr i v ot te es Sc r s an n CD e r s RO M s oc e Ta ss pe Pl dr i v ot te es Sc r s an n CD e r s RO M s
1 0 3 0 2 1 1 1 0 1 0 1 0 1 1 0 2 0 0 0 E = (6342) P = (5322) A = (1020)
Pr
A B C D E
3 0 1 1 0
0 1 1 1 0
1 0 1 0 0
1 0 0 1 0
Resources assigned
Pr
A B C D E
Deadlock Prevention
Mutual Exclusion not required for sharable resources; must hold for nonsharable resources. Hold and Wait must guarantee that whenever a process requests a resource, it does not hold any other resources. Require process to request and be allocated all its resources before it begins execution, or allow process to request resources only when the process has none Low resource utilization; starvation possible
25 / 29
Deadlock Prevention
No Preemption If a process that is holding some resources requests another resource that cannot be immediately allocated to it, then all resources currently being held are released Preempted resources are added to the list of resources for which the process is waiting Process will be restarted only when it can regain its old resources, as well as the new ones that it is requesting
26 / 29
Deadlock Prevention
Circular Wait
Circular Wait impose a total ordering of all resource types, and require that each process requests resources in an increasing order of enumeration
i (b)
. .ts hard to find an ordering that satisfiesresource Fig. 3-13. (a) Numerically ordered resources. (b) A everyone I graph.
27 / 29
28 / 29
Reference
Operating System Concepts, 8e, Chapter 7 Modern Operating System, 3e, Chapter 6
29 / 29