![Page 1: CHAPTER 7 - DEADLOCKS - SDUimada.sdu.dk/~jamik/dm510-16/material/Chapter7.pdf · CHAPTER 7 - DEADLOCKS. 2 ... waiting for a resource that is held by P2 , …, Pn–1 is waiting for](https://reader031.vdocuments.us/reader031/viewer/2022022005/5ab61e937f8b9a156d8d865a/html5/thumbnails/1.jpg)
1
CHAPTER 7 -DEADLOCKS
![Page 2: CHAPTER 7 - DEADLOCKS - SDUimada.sdu.dk/~jamik/dm510-16/material/Chapter7.pdf · CHAPTER 7 - DEADLOCKS. 2 ... waiting for a resource that is held by P2 , …, Pn–1 is waiting for](https://reader031.vdocuments.us/reader031/viewer/2022022005/5ab61e937f8b9a156d8d865a/html5/thumbnails/2.jpg)
2
OBJECTIVESTo develop a description of deadlocks, which prevent setsof concurrent processes from completing their tasks
To present a number of different methods for preventingor avoiding deadlocks in a computer system
![Page 3: CHAPTER 7 - DEADLOCKS - SDUimada.sdu.dk/~jamik/dm510-16/material/Chapter7.pdf · CHAPTER 7 - DEADLOCKS. 2 ... waiting for a resource that is held by P2 , …, Pn–1 is waiting for](https://reader031.vdocuments.us/reader031/viewer/2022022005/5ab61e937f8b9a156d8d865a/html5/thumbnails/3.jpg)
3 . 1
SYSTEM MODEL
![Page 4: CHAPTER 7 - DEADLOCKS - SDUimada.sdu.dk/~jamik/dm510-16/material/Chapter7.pdf · CHAPTER 7 - DEADLOCKS. 2 ... waiting for a resource that is held by P2 , …, Pn–1 is waiting for](https://reader031.vdocuments.us/reader031/viewer/2022022005/5ab61e937f8b9a156d8d865a/html5/thumbnails/4.jpg)
SYSTEM MODELSystem consists of resources
Resource types R1, R2, … , Rm
CPU cycles, memory space, I/O devices
Each resource type Ri has Wi instances.
Each process utilizes a resource as follows:
request
use
release
![Page 5: CHAPTER 7 - DEADLOCKS - SDUimada.sdu.dk/~jamik/dm510-16/material/Chapter7.pdf · CHAPTER 7 - DEADLOCKS. 2 ... waiting for a resource that is held by P2 , …, Pn–1 is waiting for](https://reader031.vdocuments.us/reader031/viewer/2022022005/5ab61e937f8b9a156d8d865a/html5/thumbnails/5.jpg)
3 . 24 . 1
DEADLOCK CHARACTERIZATION
![Page 6: CHAPTER 7 - DEADLOCKS - SDUimada.sdu.dk/~jamik/dm510-16/material/Chapter7.pdf · CHAPTER 7 - DEADLOCKS. 2 ... waiting for a resource that is held by P2 , …, Pn–1 is waiting for](https://reader031.vdocuments.us/reader031/viewer/2022022005/5ab61e937f8b9a156d8d865a/html5/thumbnails/6.jpg)
DEADLOCK CHARACTERIZATIONDeadlock can arise if four conditions hold simultaneously.
Mutual exclusion
Hold and wait
No preemption
Circular wait
![Page 7: CHAPTER 7 - DEADLOCKS - SDUimada.sdu.dk/~jamik/dm510-16/material/Chapter7.pdf · CHAPTER 7 - DEADLOCKS. 2 ... waiting for a resource that is held by P2 , …, Pn–1 is waiting for](https://reader031.vdocuments.us/reader031/viewer/2022022005/5ab61e937f8b9a156d8d865a/html5/thumbnails/7.jpg)
4 . 24 . 3
MUTUAL EXCLUSIONOnly one process at a time can use a resource
![Page 8: CHAPTER 7 - DEADLOCKS - SDUimada.sdu.dk/~jamik/dm510-16/material/Chapter7.pdf · CHAPTER 7 - DEADLOCKS. 2 ... waiting for a resource that is held by P2 , …, Pn–1 is waiting for](https://reader031.vdocuments.us/reader031/viewer/2022022005/5ab61e937f8b9a156d8d865a/html5/thumbnails/8.jpg)
4 . 4
HOLD AND WAITA process holding at least one resource is waiting to acquire
additional resources held by other processes
![Page 9: CHAPTER 7 - DEADLOCKS - SDUimada.sdu.dk/~jamik/dm510-16/material/Chapter7.pdf · CHAPTER 7 - DEADLOCKS. 2 ... waiting for a resource that is held by P2 , …, Pn–1 is waiting for](https://reader031.vdocuments.us/reader031/viewer/2022022005/5ab61e937f8b9a156d8d865a/html5/thumbnails/9.jpg)
4 . 5
NO PREEMPTIONA resource can be released only voluntarily by the process
holding it, a�er that process has completed its task
![Page 10: CHAPTER 7 - DEADLOCKS - SDUimada.sdu.dk/~jamik/dm510-16/material/Chapter7.pdf · CHAPTER 7 - DEADLOCKS. 2 ... waiting for a resource that is held by P2 , …, Pn–1 is waiting for](https://reader031.vdocuments.us/reader031/viewer/2022022005/5ab61e937f8b9a156d8d865a/html5/thumbnails/10.jpg)
4 . 6
CIRCULAR WAITThere exists a set {P0 , P1 , … , Pn } of waiting processes such
that P0 is waiting for a resource that is held by P1 , P1 iswaiting for a resource that is held by P2 , … , Pn–1 is waiting
for a resource that is held by Pn, and Pn is waiting for aresource that is held by P0.
![Page 11: CHAPTER 7 - DEADLOCKS - SDUimada.sdu.dk/~jamik/dm510-16/material/Chapter7.pdf · CHAPTER 7 - DEADLOCKS. 2 ... waiting for a resource that is held by P2 , …, Pn–1 is waiting for](https://reader031.vdocuments.us/reader031/viewer/2022022005/5ab61e937f8b9a156d8d865a/html5/thumbnails/11.jpg)
DEADLOCK WITH MUTEX LOCKS/ * t h r e a d o n e r u n s i n t h i s f u n c t i o n * / v o i d * d o _ w o r k _ o n e ( v o i d * p a r a m ) { p t h r e a d _ m u t e x _ l o c k ( & f i r s t m u t e x ) ; p t h r e a d _ m u t e x _ l o c k ( & s e c o n d m u t e x ) ; / * * * D o s o m e w o r k * / p t h r e a d _ m u t e x _ u n l o c k ( & s e c o n d m u t e x ) ; p t h r e a d _ m u t e x _ u n l o c k ( & f i r s t m u t e x ) ; p t h r e a d _ e x i t ( 0 ) ; } / * t h r e a d t w o r u n s i n t h i s f u n c t i o n * / v o i d * d o _ w o r k _ t w o ( v o i d * p a r a m ) { p t h r e a d _ m u t e x _ l o c k ( & s e c o n d m u t e x ) ; p t h r e a d _ m u t e x _ l o c k ( & f i r s t m u t e x ) ; / * * * D o s o m e w o r k * / p t h r e a d _ m u t e x _ u n l o c k ( & f i r s t m u t e x ) ; p t h r e a d _ m u t e x _ u n l o c k ( & s e c o n d m u t e x ) ; p t h r e a d _ e x i t ( 0 ) ; }
![Page 12: CHAPTER 7 - DEADLOCKS - SDUimada.sdu.dk/~jamik/dm510-16/material/Chapter7.pdf · CHAPTER 7 - DEADLOCKS. 2 ... waiting for a resource that is held by P2 , …, Pn–1 is waiting for](https://reader031.vdocuments.us/reader031/viewer/2022022005/5ab61e937f8b9a156d8d865a/html5/thumbnails/12.jpg)
4 . 7
RESOURCE-ALLOCATION GRAPHA set of vertices V and a set of edges E.
V is partitioned into two types:
P = {P1 , P2 , … , Pn }, the set consisting of all theprocesses in the system
R = {R1 , R2 , … , Rn }, the set consisting of all resourcetypes in the system
request edge – directed edge Pi → Rj
assignment edge – directed edge Rj → Pi
![Page 13: CHAPTER 7 - DEADLOCKS - SDUimada.sdu.dk/~jamik/dm510-16/material/Chapter7.pdf · CHAPTER 7 - DEADLOCKS. 2 ... waiting for a resource that is held by P2 , …, Pn–1 is waiting for](https://reader031.vdocuments.us/reader031/viewer/2022022005/5ab61e937f8b9a156d8d865a/html5/thumbnails/13.jpg)
4 . 84 . 9
LEGEND
Process
![Page 14: CHAPTER 7 - DEADLOCKS - SDUimada.sdu.dk/~jamik/dm510-16/material/Chapter7.pdf · CHAPTER 7 - DEADLOCKS. 2 ... waiting for a resource that is held by P2 , …, Pn–1 is waiting for](https://reader031.vdocuments.us/reader031/viewer/2022022005/5ab61e937f8b9a156d8d865a/html5/thumbnails/14.jpg)
LEGEND
Resource Type with 3 instances
![Page 15: CHAPTER 7 - DEADLOCKS - SDUimada.sdu.dk/~jamik/dm510-16/material/Chapter7.pdf · CHAPTER 7 - DEADLOCKS. 2 ... waiting for a resource that is held by P2 , …, Pn–1 is waiting for](https://reader031.vdocuments.us/reader031/viewer/2022022005/5ab61e937f8b9a156d8d865a/html5/thumbnails/15.jpg)
4 . 10
LEGEND
P2 requests instance of R
![Page 16: CHAPTER 7 - DEADLOCKS - SDUimada.sdu.dk/~jamik/dm510-16/material/Chapter7.pdf · CHAPTER 7 - DEADLOCKS. 2 ... waiting for a resource that is held by P2 , …, Pn–1 is waiting for](https://reader031.vdocuments.us/reader031/viewer/2022022005/5ab61e937f8b9a156d8d865a/html5/thumbnails/16.jpg)
4 . 11
LEGEND
P2 is holding an instance of R
![Page 17: CHAPTER 7 - DEADLOCKS - SDUimada.sdu.dk/~jamik/dm510-16/material/Chapter7.pdf · CHAPTER 7 - DEADLOCKS. 2 ... waiting for a resource that is held by P2 , …, Pn–1 is waiting for](https://reader031.vdocuments.us/reader031/viewer/2022022005/5ab61e937f8b9a156d8d865a/html5/thumbnails/17.jpg)
4 . 124 . 13
EXAMPLE RESOURCESP = { P1, P2, P3 }
R = { R1, R2, R3, R4 }
E = { P1 → R1, P2 → R3, R1 → P2, R2 → P2, R2 → p1, R3 → P3 }
![Page 18: CHAPTER 7 - DEADLOCKS - SDUimada.sdu.dk/~jamik/dm510-16/material/Chapter7.pdf · CHAPTER 7 - DEADLOCKS. 2 ... waiting for a resource that is held by P2 , …, Pn–1 is waiting for](https://reader031.vdocuments.us/reader031/viewer/2022022005/5ab61e937f8b9a156d8d865a/html5/thumbnails/18.jpg)
RESOURCE ALLOCATION GRAPH
![Page 19: CHAPTER 7 - DEADLOCKS - SDUimada.sdu.dk/~jamik/dm510-16/material/Chapter7.pdf · CHAPTER 7 - DEADLOCKS. 2 ... waiting for a resource that is held by P2 , …, Pn–1 is waiting for](https://reader031.vdocuments.us/reader031/viewer/2022022005/5ab61e937f8b9a156d8d865a/html5/thumbnails/19.jpg)
4 . 14
GRAPH WITH A DEADLOCK
![Page 20: CHAPTER 7 - DEADLOCKS - SDUimada.sdu.dk/~jamik/dm510-16/material/Chapter7.pdf · CHAPTER 7 - DEADLOCKS. 2 ... waiting for a resource that is held by P2 , …, Pn–1 is waiting for](https://reader031.vdocuments.us/reader031/viewer/2022022005/5ab61e937f8b9a156d8d865a/html5/thumbnails/20.jpg)
4 . 15
WITH A CYCLE BUT NO DEADLOCK
![Page 21: CHAPTER 7 - DEADLOCKS - SDUimada.sdu.dk/~jamik/dm510-16/material/Chapter7.pdf · CHAPTER 7 - DEADLOCKS. 2 ... waiting for a resource that is held by P2 , …, Pn–1 is waiting for](https://reader031.vdocuments.us/reader031/viewer/2022022005/5ab61e937f8b9a156d8d865a/html5/thumbnails/21.jpg)
4 . 16
BASIC FACTSIf graph contains 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 ofdeadlock
![Page 22: CHAPTER 7 - DEADLOCKS - SDUimada.sdu.dk/~jamik/dm510-16/material/Chapter7.pdf · CHAPTER 7 - DEADLOCKS. 2 ... waiting for a resource that is held by P2 , …, Pn–1 is waiting for](https://reader031.vdocuments.us/reader031/viewer/2022022005/5ab61e937f8b9a156d8d865a/html5/thumbnails/22.jpg)
4 . 175 . 1
METHODS FOR HANDLINGDEADLOCKS
![Page 23: CHAPTER 7 - DEADLOCKS - SDUimada.sdu.dk/~jamik/dm510-16/material/Chapter7.pdf · CHAPTER 7 - DEADLOCKS. 2 ... waiting for a resource that is held by P2 , …, Pn–1 is waiting for](https://reader031.vdocuments.us/reader031/viewer/2022022005/5ab61e937f8b9a156d8d865a/html5/thumbnails/23.jpg)
METHODS FOR HANDLINGDEADLOCKS
1. Ensure that the system will never enter a deadlock state
2. Allow the system to enter a deadlock state and thenrecover
3. Ignore the problem and pretend that deadlocks neveroccur in the system; used by most operating systems
![Page 24: CHAPTER 7 - DEADLOCKS - SDUimada.sdu.dk/~jamik/dm510-16/material/Chapter7.pdf · CHAPTER 7 - DEADLOCKS. 2 ... waiting for a resource that is held by P2 , …, Pn–1 is waiting for](https://reader031.vdocuments.us/reader031/viewer/2022022005/5ab61e937f8b9a156d8d865a/html5/thumbnails/24.jpg)
5 . 26 . 1
DEADLOCK PREVENTION
![Page 25: CHAPTER 7 - DEADLOCKS - SDUimada.sdu.dk/~jamik/dm510-16/material/Chapter7.pdf · CHAPTER 7 - DEADLOCKS. 2 ... waiting for a resource that is held by P2 , …, Pn–1 is waiting for](https://reader031.vdocuments.us/reader031/viewer/2022022005/5ab61e937f8b9a156d8d865a/html5/thumbnails/25.jpg)
6 . 2
DEADLOCK PREVENTIONRestrain the ways request can be made
Mutual Exclusion – not required for sharable resources;must hold for nonsharable resources
![Page 26: CHAPTER 7 - DEADLOCKS - SDUimada.sdu.dk/~jamik/dm510-16/material/Chapter7.pdf · CHAPTER 7 - DEADLOCKS. 2 ... waiting for a resource that is held by P2 , …, Pn–1 is waiting for](https://reader031.vdocuments.us/reader031/viewer/2022022005/5ab61e937f8b9a156d8d865a/html5/thumbnails/26.jpg)
DEADLOCK PREVENTIONHold and Wait – multiple ways
1. must guarantee that whenever a process requests aresource, it does not hold any other resources.
2. Request all resources up front before execution. (all ornone)
![Page 27: CHAPTER 7 - DEADLOCKS - SDUimada.sdu.dk/~jamik/dm510-16/material/Chapter7.pdf · CHAPTER 7 - DEADLOCKS. 2 ... waiting for a resource that is held by P2 , …, Pn–1 is waiting for](https://reader031.vdocuments.us/reader031/viewer/2022022005/5ab61e937f8b9a156d8d865a/html5/thumbnails/27.jpg)
6 . 3
DEADLOCK PREVENTIONNo Preemption
If a process that is holding some resources requests anotherresource that cannot be immediately allocated to it, then all
resources currently being held are released
Preempted resources are added to the list of resources forwhich the process is waiting
Process will be restarted only when it can regain its oldresources, as well as the new ones that it is requesting
![Page 28: CHAPTER 7 - DEADLOCKS - SDUimada.sdu.dk/~jamik/dm510-16/material/Chapter7.pdf · CHAPTER 7 - DEADLOCKS. 2 ... waiting for a resource that is held by P2 , …, Pn–1 is waiting for](https://reader031.vdocuments.us/reader031/viewer/2022022005/5ab61e937f8b9a156d8d865a/html5/thumbnails/28.jpg)
6 . 46 . 5
DEADLOCK PREVENTIONCircular Wait
impose a total ordering of all resource types, and requirethat each process requests resources in an increasing order
of enumeration
![Page 29: CHAPTER 7 - DEADLOCKS - SDUimada.sdu.dk/~jamik/dm510-16/material/Chapter7.pdf · CHAPTER 7 - DEADLOCKS. 2 ... waiting for a resource that is held by P2 , …, Pn–1 is waiting for](https://reader031.vdocuments.us/reader031/viewer/2022022005/5ab61e937f8b9a156d8d865a/html5/thumbnails/29.jpg)
DEADLOCK EXAMPLE WITH LOCKORDERING
v o i d t r a n s a c t i o n ( A c c o u n t f r o m , A c c o u n t t o , d o u b l e a m o u n t ) { m u t e x l o c k 1 , l o c k 2 ; l o c k 1 = g e t _ l o c k ( f r o m ) ; l o c k 2 = g e t _ l o c k ( t o ) ; a c q u i r e ( l o c k 1 ) ; a c q u i r e ( l o c k 2 ) ; w i t h d r a w ( f r o m , a m o u n t ) ; d e p o s i t ( t o , a m o u n t ) ; r e l e a s e ( l o c k 2 ) ; r e l e a s e ( l o c k 1 ) ; }
![Page 30: CHAPTER 7 - DEADLOCKS - SDUimada.sdu.dk/~jamik/dm510-16/material/Chapter7.pdf · CHAPTER 7 - DEADLOCKS. 2 ... waiting for a resource that is held by P2 , …, Pn–1 is waiting for](https://reader031.vdocuments.us/reader031/viewer/2022022005/5ab61e937f8b9a156d8d865a/html5/thumbnails/30.jpg)
6 . 67 . 1
DEADLOCK AVOIDANCE
![Page 31: CHAPTER 7 - DEADLOCKS - SDUimada.sdu.dk/~jamik/dm510-16/material/Chapter7.pdf · CHAPTER 7 - DEADLOCKS. 2 ... waiting for a resource that is held by P2 , …, Pn–1 is waiting for](https://reader031.vdocuments.us/reader031/viewer/2022022005/5ab61e937f8b9a156d8d865a/html5/thumbnails/31.jpg)
DEADLOCK AVOIDANCERequires that the system has some additional a priori
information available
Simplest and most useful model requires that each processdeclare the maximum number of resources of each type that
it may need
![Page 32: CHAPTER 7 - DEADLOCKS - SDUimada.sdu.dk/~jamik/dm510-16/material/Chapter7.pdf · CHAPTER 7 - DEADLOCKS. 2 ... waiting for a resource that is held by P2 , …, Pn–1 is waiting for](https://reader031.vdocuments.us/reader031/viewer/2022022005/5ab61e937f8b9a156d8d865a/html5/thumbnails/32.jpg)
7 . 2
DEADLOCK AVOIDANCEThe deadlock-avoidance algorithm dynamically examines
the resource-allocation state to ensure that there can neverbe a circular-wait condition
Resource-allocation state is defined by the number ofavailable and allocated resources, and the maximum
demands of the processes
![Page 33: CHAPTER 7 - DEADLOCKS - SDUimada.sdu.dk/~jamik/dm510-16/material/Chapter7.pdf · CHAPTER 7 - DEADLOCKS. 2 ... waiting for a resource that is held by P2 , …, Pn–1 is waiting for](https://reader031.vdocuments.us/reader031/viewer/2022022005/5ab61e937f8b9a156d8d865a/html5/thumbnails/33.jpg)
7 . 3
SAFE STATEWhen a process requests an available resource, system must
decide if immediate allocation leaves the system in a safestate
System is in safe state if there exists a safe sequence
<P1, P2, … , Pn>
of ALL the processes in the systems such that for each P~i ,the resources that Pi can still request can be satisfied by
currently available resources + resources held by all the Pj ,with j < i
![Page 34: CHAPTER 7 - DEADLOCKS - SDUimada.sdu.dk/~jamik/dm510-16/material/Chapter7.pdf · CHAPTER 7 - DEADLOCKS. 2 ... waiting for a resource that is held by P2 , …, Pn–1 is waiting for](https://reader031.vdocuments.us/reader031/viewer/2022022005/5ab61e937f8b9a156d8d865a/html5/thumbnails/34.jpg)
7 . 4
SAFE STATEThat is:
If Pi resource needs are not immediately available, then Pican wait until all Pj have finished
When Pj is finished, Pi can obtain needed resources,execute, return allocated resources, and terminate
When Pi terminates, Pi+1 can obtain its needed resources,and so on
![Page 35: CHAPTER 7 - DEADLOCKS - SDUimada.sdu.dk/~jamik/dm510-16/material/Chapter7.pdf · CHAPTER 7 - DEADLOCKS. 2 ... waiting for a resource that is held by P2 , …, Pn–1 is waiting for](https://reader031.vdocuments.us/reader031/viewer/2022022005/5ab61e937f8b9a156d8d865a/html5/thumbnails/35.jpg)
7 . 57 . 6
BASIC FACTSIf a system is in safe state → no deadlocks
If a system is in unsafe state → possibility of deadlock
Avoidance → ensure that a system will never enter anunsafe state.
![Page 36: CHAPTER 7 - DEADLOCKS - SDUimada.sdu.dk/~jamik/dm510-16/material/Chapter7.pdf · CHAPTER 7 - DEADLOCKS. 2 ... waiting for a resource that is held by P2 , …, Pn–1 is waiting for](https://reader031.vdocuments.us/reader031/viewer/2022022005/5ab61e937f8b9a156d8d865a/html5/thumbnails/36.jpg)
SAFE, UNSAFE, DEADLOCK STATE
![Page 37: CHAPTER 7 - DEADLOCKS - SDUimada.sdu.dk/~jamik/dm510-16/material/Chapter7.pdf · CHAPTER 7 - DEADLOCKS. 2 ... waiting for a resource that is held by P2 , …, Pn–1 is waiting for](https://reader031.vdocuments.us/reader031/viewer/2022022005/5ab61e937f8b9a156d8d865a/html5/thumbnails/37.jpg)
7 . 77 . 8
AVOIDANCE ALGORITHMSSingle instance of a resource type → Use a resource-
allocation graph
Multiple instances of a resource type → Use the banker’salgorithm
![Page 38: CHAPTER 7 - DEADLOCKS - SDUimada.sdu.dk/~jamik/dm510-16/material/Chapter7.pdf · CHAPTER 7 - DEADLOCKS. 2 ... waiting for a resource that is held by P2 , …, Pn–1 is waiting for](https://reader031.vdocuments.us/reader031/viewer/2022022005/5ab61e937f8b9a156d8d865a/html5/thumbnails/38.jpg)
SCHEMEClaim edge Pi → Rj indicated that process Pi may requestresource Rj ; represented by a dashed line
Claim edge converts to request edge when a processrequests a resource
Request edge converted to an assignment edge when theresource is allocated to the process
When a resource is released by a process, assignmentedge reconverts to a claim edge
Resources must be claimed a priori in the system
![Page 39: CHAPTER 7 - DEADLOCKS - SDUimada.sdu.dk/~jamik/dm510-16/material/Chapter7.pdf · CHAPTER 7 - DEADLOCKS. 2 ... waiting for a resource that is held by P2 , …, Pn–1 is waiting for](https://reader031.vdocuments.us/reader031/viewer/2022022005/5ab61e937f8b9a156d8d865a/html5/thumbnails/39.jpg)
7 . 9
RESOURCE-ALLOCATION GRAPH
![Page 40: CHAPTER 7 - DEADLOCKS - SDUimada.sdu.dk/~jamik/dm510-16/material/Chapter7.pdf · CHAPTER 7 - DEADLOCKS. 2 ... waiting for a resource that is held by P2 , …, Pn–1 is waiting for](https://reader031.vdocuments.us/reader031/viewer/2022022005/5ab61e937f8b9a156d8d865a/html5/thumbnails/40.jpg)
7 . 10
UNSAFE STATE IN RESOURCE-ALLOCATION GRAPH
![Page 41: CHAPTER 7 - DEADLOCKS - SDUimada.sdu.dk/~jamik/dm510-16/material/Chapter7.pdf · CHAPTER 7 - DEADLOCKS. 2 ... waiting for a resource that is held by P2 , …, Pn–1 is waiting for](https://reader031.vdocuments.us/reader031/viewer/2022022005/5ab61e937f8b9a156d8d865a/html5/thumbnails/41.jpg)
7 . 11
RESOURCE-ALLOCATION GRAPHALGORITHM
Suppose that process Pi requests a resource Rj
The request can be granted only if converting the requestedge to an assignment edge does not result in the formation
of a cycle in the resource allocation graph
Cycle detection is O(n2)
![Page 42: CHAPTER 7 - DEADLOCKS - SDUimada.sdu.dk/~jamik/dm510-16/material/Chapter7.pdf · CHAPTER 7 - DEADLOCKS. 2 ... waiting for a resource that is held by P2 , …, Pn–1 is waiting for](https://reader031.vdocuments.us/reader031/viewer/2022022005/5ab61e937f8b9a156d8d865a/html5/thumbnails/42.jpg)
7 . 12
BANKER’S ALGORITHMMultiple instances
Each process must a priori claim maximum use
When a process requests a resource it may have to wait
When a process gets all its resources it must return themin a finite amount of time
![Page 43: CHAPTER 7 - DEADLOCKS - SDUimada.sdu.dk/~jamik/dm510-16/material/Chapter7.pdf · CHAPTER 7 - DEADLOCKS. 2 ... waiting for a resource that is held by P2 , …, Pn–1 is waiting for](https://reader031.vdocuments.us/reader031/viewer/2022022005/5ab61e937f8b9a156d8d865a/html5/thumbnails/43.jpg)
7 . 137 . 14
BANKER’S ALGORITHM SETUPLet n = number of processes,
Let m = number of resources types.
![Page 44: CHAPTER 7 - DEADLOCKS - SDUimada.sdu.dk/~jamik/dm510-16/material/Chapter7.pdf · CHAPTER 7 - DEADLOCKS. 2 ... waiting for a resource that is held by P2 , …, Pn–1 is waiting for](https://reader031.vdocuments.us/reader031/viewer/2022022005/5ab61e937f8b9a156d8d865a/html5/thumbnails/44.jpg)
DATA STRUCTURES NEEDEDAvailable: Vector of length m. If available[j] = k,there are k instances of resource type Rj available
Max: n x m matrix. If Max [i,j] = k, then process Pi mayrequest at most k instances of resource type R j
Allocation: n x m matrix. If Allocation[i,j] = kthen Pi is currently allocated k instances of Rj
Need: n x m matrix. If Need[i,j] = k, then Pi mayneed k more instances of Rj to complete its task
Need[i,j] = Max[i,j] – Allocation[i,j]
![Page 45: CHAPTER 7 - DEADLOCKS - SDUimada.sdu.dk/~jamik/dm510-16/material/Chapter7.pdf · CHAPTER 7 - DEADLOCKS. 2 ... waiting for a resource that is held by P2 , …, Pn–1 is waiting for](https://reader031.vdocuments.us/reader031/viewer/2022022005/5ab61e937f8b9a156d8d865a/html5/thumbnails/45.jpg)
7 . 15
SAFETY ALGORITHM 1Let Work and Finish be vectors of length m and n,
respectively.
Initialize:
Work = Available
Finish[i] = false for i = 0,1,… ,n-1
![Page 46: CHAPTER 7 - DEADLOCKS - SDUimada.sdu.dk/~jamik/dm510-16/material/Chapter7.pdf · CHAPTER 7 - DEADLOCKS. 2 ... waiting for a resource that is held by P2 , …, Pn–1 is waiting for](https://reader031.vdocuments.us/reader031/viewer/2022022005/5ab61e937f8b9a156d8d865a/html5/thumbnails/46.jpg)
7 . 16
SAFETY ALGORITHM 2Find an i such that both:
1. Finish[i] = false
2. Needi ≤ Work
If no such i exists, go to step 4
![Page 47: CHAPTER 7 - DEADLOCKS - SDUimada.sdu.dk/~jamik/dm510-16/material/Chapter7.pdf · CHAPTER 7 - DEADLOCKS. 2 ... waiting for a resource that is held by P2 , …, Pn–1 is waiting for](https://reader031.vdocuments.us/reader031/viewer/2022022005/5ab61e937f8b9a156d8d865a/html5/thumbnails/47.jpg)
7 . 177 . 18
SAFETY ALGORITHM 3Work = Work + Allocationi
Finish[i] = true
go to step 2
![Page 48: CHAPTER 7 - DEADLOCKS - SDUimada.sdu.dk/~jamik/dm510-16/material/Chapter7.pdf · CHAPTER 7 - DEADLOCKS. 2 ... waiting for a resource that is held by P2 , …, Pn–1 is waiting for](https://reader031.vdocuments.us/reader031/viewer/2022022005/5ab61e937f8b9a156d8d865a/html5/thumbnails/48.jpg)
7 . 19
SAFETY ALGORITHM 4If Finish [i] == true for all i, then the system is in a
safe state
![Page 49: CHAPTER 7 - DEADLOCKS - SDUimada.sdu.dk/~jamik/dm510-16/material/Chapter7.pdf · CHAPTER 7 - DEADLOCKS. 2 ... waiting for a resource that is held by P2 , …, Pn–1 is waiting for](https://reader031.vdocuments.us/reader031/viewer/2022022005/5ab61e937f8b9a156d8d865a/html5/thumbnails/49.jpg)
RESOURCE-REQUEST ALGORITHMLet Requesti be request vector for for Process Pi
If Requesti[j] = k then process Pi wants k instances ofresource type Rj
When resource request made by Pi:
![Page 50: CHAPTER 7 - DEADLOCKS - SDUimada.sdu.dk/~jamik/dm510-16/material/Chapter7.pdf · CHAPTER 7 - DEADLOCKS. 2 ... waiting for a resource that is held by P2 , …, Pn–1 is waiting for](https://reader031.vdocuments.us/reader031/viewer/2022022005/5ab61e937f8b9a156d8d865a/html5/thumbnails/50.jpg)
7 . 20
RESOURCE-REQUEST ALGORITHM1. If Requesti ≤ Needi go to step 2. Otherwise, raise error
condition, since process has exceeded its maximum claim
2. If Requesti ≤ Available, go to step 3. Otherwise Pi mustwait, since resources are not available
3. Pretend to allocate requested resources to Pi bymodifying the state as follows:
Available = Available – Request
Allocationi = Allocationi + Requesti
Needi = Needi – Requesti
![Page 51: CHAPTER 7 - DEADLOCKS - SDUimada.sdu.dk/~jamik/dm510-16/material/Chapter7.pdf · CHAPTER 7 - DEADLOCKS. 2 ... waiting for a resource that is held by P2 , …, Pn–1 is waiting for](https://reader031.vdocuments.us/reader031/viewer/2022022005/5ab61e937f8b9a156d8d865a/html5/thumbnails/51.jpg)
7 . 217 . 22
RESOURCE-REQUEST ALGORITHMIf safe → the resources are allocated to Pi
If unsafe → Pi must wait, and the old resource-allocationstate is restored
![Page 52: CHAPTER 7 - DEADLOCKS - SDUimada.sdu.dk/~jamik/dm510-16/material/Chapter7.pdf · CHAPTER 7 - DEADLOCKS. 2 ... waiting for a resource that is held by P2 , …, Pn–1 is waiting for](https://reader031.vdocuments.us/reader031/viewer/2022022005/5ab61e937f8b9a156d8d865a/html5/thumbnails/52.jpg)
EXAMPLE OF BANKER’S ALGORITHM5 processes P0 through P4
3 resource types:
A (10 instances)
B (5 instances)
C (7 instances)
![Page 53: CHAPTER 7 - DEADLOCKS - SDUimada.sdu.dk/~jamik/dm510-16/material/Chapter7.pdf · CHAPTER 7 - DEADLOCKS. 2 ... waiting for a resource that is held by P2 , …, Pn–1 is waiting for](https://reader031.vdocuments.us/reader031/viewer/2022022005/5ab61e937f8b9a156d8d865a/html5/thumbnails/53.jpg)
7 . 23
EXAMPLE OF BANKER’S ALGORITHM
![Page 54: CHAPTER 7 - DEADLOCKS - SDUimada.sdu.dk/~jamik/dm510-16/material/Chapter7.pdf · CHAPTER 7 - DEADLOCKS. 2 ... waiting for a resource that is held by P2 , …, Pn–1 is waiting for](https://reader031.vdocuments.us/reader031/viewer/2022022005/5ab61e937f8b9a156d8d865a/html5/thumbnails/54.jpg)
7 . 24
EXAMPLE OF BANKER’S ALGORITHM
The system is in a safe state since the sequence<P1,P3,P4,P2,P0> satisfies safety criteria
![Page 55: CHAPTER 7 - DEADLOCKS - SDUimada.sdu.dk/~jamik/dm510-16/material/Chapter7.pdf · CHAPTER 7 - DEADLOCKS. 2 ... waiting for a resource that is held by P2 , …, Pn–1 is waiting for](https://reader031.vdocuments.us/reader031/viewer/2022022005/5ab61e937f8b9a156d8d865a/html5/thumbnails/55.jpg)
7 . 257 . 26
EXAMPLE OF BANKER’S ALGORITHMNow a request from P1 comes for (1,0,2)
Check first that
Requesti ≤ Available
![Page 56: CHAPTER 7 - DEADLOCKS - SDUimada.sdu.dk/~jamik/dm510-16/material/Chapter7.pdf · CHAPTER 7 - DEADLOCKS. 2 ... waiting for a resource that is held by P2 , …, Pn–1 is waiting for](https://reader031.vdocuments.us/reader031/viewer/2022022005/5ab61e937f8b9a156d8d865a/html5/thumbnails/56.jpg)
EXAMPLE OF BANKER’S ALGORITHM
<P1,P3,P4,P0,P2> satisfies safety criteria
![Page 57: CHAPTER 7 - DEADLOCKS - SDUimada.sdu.dk/~jamik/dm510-16/material/Chapter7.pdf · CHAPTER 7 - DEADLOCKS. 2 ... waiting for a resource that is held by P2 , …, Pn–1 is waiting for](https://reader031.vdocuments.us/reader031/viewer/2022022005/5ab61e937f8b9a156d8d865a/html5/thumbnails/57.jpg)
7 . 277 . 28
EXERCISECan request for (3,3,0) by P4 be granted?
Can request for (0,2,0) by P0 be granted?
![Page 58: CHAPTER 7 - DEADLOCKS - SDUimada.sdu.dk/~jamik/dm510-16/material/Chapter7.pdf · CHAPTER 7 - DEADLOCKS. 2 ... waiting for a resource that is held by P2 , …, Pn–1 is waiting for](https://reader031.vdocuments.us/reader031/viewer/2022022005/5ab61e937f8b9a156d8d865a/html5/thumbnails/58.jpg)
8 . 1
DEADLOCK DETECTION
![Page 59: CHAPTER 7 - DEADLOCKS - SDUimada.sdu.dk/~jamik/dm510-16/material/Chapter7.pdf · CHAPTER 7 - DEADLOCKS. 2 ... waiting for a resource that is held by P2 , …, Pn–1 is waiting for](https://reader031.vdocuments.us/reader031/viewer/2022022005/5ab61e937f8b9a156d8d865a/html5/thumbnails/59.jpg)
8 . 2
DEADLOCK DETECTIONAllow system to enter deadlock state
Detection algorithm
Recovery scheme
![Page 60: CHAPTER 7 - DEADLOCKS - SDUimada.sdu.dk/~jamik/dm510-16/material/Chapter7.pdf · CHAPTER 7 - DEADLOCKS. 2 ... waiting for a resource that is held by P2 , …, Pn–1 is waiting for](https://reader031.vdocuments.us/reader031/viewer/2022022005/5ab61e937f8b9a156d8d865a/html5/thumbnails/60.jpg)
SINGLE INSTANCE OF EACHRESOURCE TYPE
Maintain wait-for graph
Nodes are processes
Pi → Pj if Pi is waiting for Pj
Periodically invoke an algorithm that searches for a cyclein the graph. If there is a cycle, there exists a deadlock
An algorithm to detect a cycle in a graph requires O(n2)operations, where n is the number of vertices in the graph
![Page 61: CHAPTER 7 - DEADLOCKS - SDUimada.sdu.dk/~jamik/dm510-16/material/Chapter7.pdf · CHAPTER 7 - DEADLOCKS. 2 ... waiting for a resource that is held by P2 , …, Pn–1 is waiting for](https://reader031.vdocuments.us/reader031/viewer/2022022005/5ab61e937f8b9a156d8d865a/html5/thumbnails/61.jpg)
8 . 3
GRAPHS
![Page 62: CHAPTER 7 - DEADLOCKS - SDUimada.sdu.dk/~jamik/dm510-16/material/Chapter7.pdf · CHAPTER 7 - DEADLOCKS. 2 ... waiting for a resource that is held by P2 , …, Pn–1 is waiting for](https://reader031.vdocuments.us/reader031/viewer/2022022005/5ab61e937f8b9a156d8d865a/html5/thumbnails/62.jpg)
8 . 4
SEVERAL INSTANCES OF ARESOURCE TYPE
Available: A vector of length m indicates the number ofavailable resources of each type
Allocation: An n x m matrix defines the number ofresources of each type currently allocated to each process
Request: An n x m matrix indicates the current request ofeach process. If Request[i][j] = k, then process Piis requesting k more instances of resource type Rj
![Page 63: CHAPTER 7 - DEADLOCKS - SDUimada.sdu.dk/~jamik/dm510-16/material/Chapter7.pdf · CHAPTER 7 - DEADLOCKS. 2 ... waiting for a resource that is held by P2 , …, Pn–1 is waiting for](https://reader031.vdocuments.us/reader031/viewer/2022022005/5ab61e937f8b9a156d8d865a/html5/thumbnails/63.jpg)
8 . 5
DETECTION ALGORITHM 1Let Work and Finish be vectors of length m and n,
respectively
Initialize:
Work = Available
For i = 1,2, … , n, if Allocationi ≠ 0, thenFinish[i] = false; otherwise, Finish[i] =true
![Page 64: CHAPTER 7 - DEADLOCKS - SDUimada.sdu.dk/~jamik/dm510-16/material/Chapter7.pdf · CHAPTER 7 - DEADLOCKS. 2 ... waiting for a resource that is held by P2 , …, Pn–1 is waiting for](https://reader031.vdocuments.us/reader031/viewer/2022022005/5ab61e937f8b9a156d8d865a/html5/thumbnails/64.jpg)
8 . 6
DETECTION ALGORITHM 2Find an index i such that both:
Finish[i] == false
Request i ≤ Work
If no such i exists, go to step 4
![Page 65: CHAPTER 7 - DEADLOCKS - SDUimada.sdu.dk/~jamik/dm510-16/material/Chapter7.pdf · CHAPTER 7 - DEADLOCKS. 2 ... waiting for a resource that is held by P2 , …, Pn–1 is waiting for](https://reader031.vdocuments.us/reader031/viewer/2022022005/5ab61e937f8b9a156d8d865a/html5/thumbnails/65.jpg)
8 . 78 . 8
DETECTION ALGORITHM 3Work = Work + Allocationi
Finish[i] = true
go to step 2
![Page 66: CHAPTER 7 - DEADLOCKS - SDUimada.sdu.dk/~jamik/dm510-16/material/Chapter7.pdf · CHAPTER 7 - DEADLOCKS. 2 ... waiting for a resource that is held by P2 , …, Pn–1 is waiting for](https://reader031.vdocuments.us/reader031/viewer/2022022005/5ab61e937f8b9a156d8d865a/html5/thumbnails/66.jpg)
DETECTION ALGORITHM 4If Finish[i] == false, for some i, 1 ≤ i ≤ n, then thesystem is in deadlock state.
Moreover, if Finish[i] == false, then Pi isdeadlocked
Algorithm requires an order of O(m x n2) operations todetect whether the system is in deadlocked state
![Page 67: CHAPTER 7 - DEADLOCKS - SDUimada.sdu.dk/~jamik/dm510-16/material/Chapter7.pdf · CHAPTER 7 - DEADLOCKS. 2 ... waiting for a resource that is held by P2 , …, Pn–1 is waiting for](https://reader031.vdocuments.us/reader031/viewer/2022022005/5ab61e937f8b9a156d8d865a/html5/thumbnails/67.jpg)
8 . 9
EXAMPLEFive processes P0 through P4
Three resource types
A (7 instances)
B (2 instances)
C (6 instances)
![Page 68: CHAPTER 7 - DEADLOCKS - SDUimada.sdu.dk/~jamik/dm510-16/material/Chapter7.pdf · CHAPTER 7 - DEADLOCKS. 2 ... waiting for a resource that is held by P2 , …, Pn–1 is waiting for](https://reader031.vdocuments.us/reader031/viewer/2022022005/5ab61e937f8b9a156d8d865a/html5/thumbnails/68.jpg)
8 . 10
EXAMPLESnapshot at time T0
Sequence <P0, P2, P3, P1, P4> will result in Finish[i] =true for all i
![Page 69: CHAPTER 7 - DEADLOCKS - SDUimada.sdu.dk/~jamik/dm510-16/material/Chapter7.pdf · CHAPTER 7 - DEADLOCKS. 2 ... waiting for a resource that is held by P2 , …, Pn–1 is waiting for](https://reader031.vdocuments.us/reader031/viewer/2022022005/5ab61e937f8b9a156d8d865a/html5/thumbnails/69.jpg)
8 . 11
EXAMPLEP2 requests an additional instance of type C
State of system?
![Page 70: CHAPTER 7 - DEADLOCKS - SDUimada.sdu.dk/~jamik/dm510-16/material/Chapter7.pdf · CHAPTER 7 - DEADLOCKS. 2 ... waiting for a resource that is held by P2 , …, Pn–1 is waiting for](https://reader031.vdocuments.us/reader031/viewer/2022022005/5ab61e937f8b9a156d8d865a/html5/thumbnails/70.jpg)
8 . 12
DETECTION-ALGORITHM USAGEWhen, and how o�en, to invoke depends on:
How o�en a deadlock is likely to occur?
How many processes will need to be rolled back?
→ One for each disjoint cycle
If detection algorithm is invoked arbitrarily, there may bemany cycles in the resource graph and so we would not beable to tell which of the many deadlocked processes“caused” the deadlock.
![Page 71: CHAPTER 7 - DEADLOCKS - SDUimada.sdu.dk/~jamik/dm510-16/material/Chapter7.pdf · CHAPTER 7 - DEADLOCKS. 2 ... waiting for a resource that is held by P2 , …, Pn–1 is waiting for](https://reader031.vdocuments.us/reader031/viewer/2022022005/5ab61e937f8b9a156d8d865a/html5/thumbnails/71.jpg)
8 . 139 . 1
RECOVERY FROM DEADLOCK
![Page 72: CHAPTER 7 - DEADLOCKS - SDUimada.sdu.dk/~jamik/dm510-16/material/Chapter7.pdf · CHAPTER 7 - DEADLOCKS. 2 ... waiting for a resource that is held by P2 , …, Pn–1 is waiting for](https://reader031.vdocuments.us/reader031/viewer/2022022005/5ab61e937f8b9a156d8d865a/html5/thumbnails/72.jpg)
9 . 2
PROCESS TERMINATIONAbort all deadlocked processes
Abort one process at a time until the deadlock cycle iseliminated
![Page 73: CHAPTER 7 - DEADLOCKS - SDUimada.sdu.dk/~jamik/dm510-16/material/Chapter7.pdf · CHAPTER 7 - DEADLOCKS. 2 ... waiting for a resource that is held by P2 , …, Pn–1 is waiting for](https://reader031.vdocuments.us/reader031/viewer/2022022005/5ab61e937f8b9a156d8d865a/html5/thumbnails/73.jpg)
PROCESS TERMINATIONIn which order should we choose to abort?
1. Priority of the process
2. How long process has computed, and how much longer tocompletion
3. Resources the process has used
4. Resources process needs to complete
5. How many processes will need to be terminated
6. Is process interactive or batch?
![Page 74: CHAPTER 7 - DEADLOCKS - SDUimada.sdu.dk/~jamik/dm510-16/material/Chapter7.pdf · CHAPTER 7 - DEADLOCKS. 2 ... waiting for a resource that is held by P2 , …, Pn–1 is waiting for](https://reader031.vdocuments.us/reader031/viewer/2022022005/5ab61e937f8b9a156d8d865a/html5/thumbnails/74.jpg)
9 . 3
RESOURCE PREEMPTIONSelecting a victim – minimize cost
Rollback – return to some safe state, restart process forthat state
Starvation – same process may always be picked asvictim, include number of rollback in cost factor
![Page 75: CHAPTER 7 - DEADLOCKS - SDUimada.sdu.dk/~jamik/dm510-16/material/Chapter7.pdf · CHAPTER 7 - DEADLOCKS. 2 ... waiting for a resource that is held by P2 , …, Pn–1 is waiting for](https://reader031.vdocuments.us/reader031/viewer/2022022005/5ab61e937f8b9a156d8d865a/html5/thumbnails/75.jpg)
9 . 410 . 1
QUESTIONS
![Page 76: CHAPTER 7 - DEADLOCKS - SDUimada.sdu.dk/~jamik/dm510-16/material/Chapter7.pdf · CHAPTER 7 - DEADLOCKS. 2 ... waiting for a resource that is held by P2 , …, Pn–1 is waiting for](https://reader031.vdocuments.us/reader031/viewer/2022022005/5ab61e937f8b9a156d8d865a/html5/thumbnails/76.jpg)
10 . 2
BONUSExam question number 5: Deadlocks