Professional Documents
Culture Documents
Deadlock Prevention
COURSE : OPERATING SYSTEMS (CSSE-404) Deadlock Avoidance
Deadlock Detection
DEPARTMENT OF SOFTWARE ENGINEERING Recovery from Deadlock
1 2
3 4
5 6
Process
Pi requests instance of Rj
Pi Rj
Pi is holding an instance of Rj
Pi Rj
7 8
Resource Allocation Graph With A Deadlock Graph With A Cycle But No Deadlock
9 10
If graph contains no cycles no deadlock Ensure that the system will never enter a deadlock state:
If graph contains a cycle Deadlock prevention
if only one instance per resource type, then deadlock Deadlock avoidance
if several instances per resource type, possibility of deadlock Allow the system to enter a deadlock state and then recover
Ignore the problem and pretend that deadlocks never occur in the
system; used by most operating systems, including UNIX
11 12
No Preemption –
Restrain the ways request can be made If a process that is holding some resources requests another resource that
cannot be immediately allocated to it, then all resources currently being
Mutual Exclusion – not required for sharable resources (e.g., read- held are released
only files); must hold for non-sharable resources
Preempted resources are added to the list of resources for which the
Hold and Wait – must guarantee that whenever a process requests process is waiting
a resource, it does not hold any other resources Process will be restarted only when it can regain its old resources, as well
Require process to request and be allocated all its resources before it as the new ones that it is requesting
begins execution, or allow process to request resources only when the
Circular Wait – impose a total ordering of all resource types, and
process has none allocated to it.
require that each process requests resources in an increasing order
Low resource utilization; starvation possible of enumeration
13 14
15 16
17 18
Single instance of a resource type Claim edge Pi Rj indicated that process Pj may request resource
Rj; represented by a dashed line
Use a resource-allocation graph
Claim edge converts to request edge when a process requests a
resource
Multiple instances of a resource type
Request edge converted to an assignment edge when the
Use the banker’s algorithm resource is allocated to the process
When a resource is released by a process, assignment edge
reconverts to a claim edge
Resources must be claimed a priori in the system
19 20
21 22
Multiple instances
Suppose that process Pi requests a resource Rj
Each process must a priori claim maximum use
The request can be granted only if converting the request edge to
an assignment edge does not result in the formation of a cycle in
the resource allocation graph 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
23 24
Let n = number of processes, and m = number of resources types. 1. Let Work and Finish be vectors of length m and n, respectively.
Initialize:
Available: Vector of length m. If available [j] = k, there are k instances Work = Available
of resource type Rj available Finish [i] = false for i = 0, 1, …, n- 1
25 26
27 28
The content of the matrix Need is defined to be Max – Allocation Check that Request Available (that is, (1,0,2) (3,3,2) true
Allocation Need Available
ABC ABC ABC
Need
P0 010 743 230
ABC
P1 302 020
P0 743 P2 302 600
P1 122 P3 211 011
P2 600 P4 002 431
P3 011 Executing safety algorithm shows that sequence < P1, P3, P4, P0, P2> satisfies safety
P4 431 requirement
29 30
Detection algorithm
Periodically invoke an algorithm that searches for a cycle in the
graph. If there is a cycle, there exists a deadlock
Recovery scheme
31 32
33 34
35 36
Five processes P0 through P4; three resource types P2 requests an additional instance of type C
A (7 instances), B (2 instances), and C (6 instances)
Request
ABC
Snapshot at time T0:
Allocation Request Available P0 000
ABC ABC ABC P1 202
P0 010 000 000 P2 001
P1 200 202 P3 100
P2 303 000 P4 002
P3 211 100
P4 002 002 State of system?
Can reclaim resources held by process P0, but insufficient resources to fulfill
other processes; requests
Sequence <P0, P2, P3, P1, P4> will result in Finish[i] = true for all i
Deadlock exists, consisting of processes P1, P2, P3, and P4
37 38
39 40
Question
Selecting a victim – minimize cost
A system is composed of four processes, {P1, P2, P3, P4} and three types
of serial reusable resources, {S1, S2, S3}. The number of units (instances)
Rollback – return to some safe state, restart process for that state for resources are t1=3, t2=2 and t3=2.
Process P1 holds 1 unit of S1 and requests for 1 unit of S2. P2 holds 2
Starvation – same process may always be picked as victim, units of S2 and requests 1 unit each of S1 and S3. P3 holds 1 unit of S1
include number of rollback in cost factor and requests 1 unit of S2. P4 holds 2 units of S3 and requests for 1 unit of
S1. Show the resource allocation graph and wait-for-graph. And also
shows could a deadlock exist or not.
41 42
Class Activity # 02
Question
Consider the following snapshot of the system has five processes (P1, P2,
P3, P4 and P5) and four types of resources (A, B, C and D). There are
End of Chapter 7
multiple resources of each type. Is the following state safe or not? If it is,
show how the processes can complete. If not, show how they can
deadlock. Thank you
Processes Current Loan Maximum Current Claim Resource
Need Available
A B C D A B C D A B C D A B C D
P1 1 0 2 0 3 2 4 2 2 2 2 2 3 4 0 1
P2 0 3 1 2 3 5 1 2 3 2 0 0
P3 2 4 5 1 2 7 7 5 0 3 2 4 Total Resources
P4 3 0 0 6 5 5 0 8 2 5 0 2 A B C D
P5 4 2 1 3 6 2 1 4 2 0 0 1 13 13 9 13
43 44