0% found this document useful (0 votes)
15 views

Lecture 7

Uploaded by

franktalla577
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Lecture 7

Uploaded by

franktalla577
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

COSC4740-01 Operating Systems Design, Fall 2004, Byunggu Yu

Chapter 7 Deadlock

1. Deadlock Problem
A set of blocked processes each holding a resource and waiting to acquire a resource held
by another process in the set.
• Example #1: System has 2 tape drives. P1 and P2 each hold one tape drive and
each needs another one.
• Example #2: semaphores A and B, initialized to 1
P0 P1
wait (A); wait(B)
wait (B); wait(A)

2. Assumptions
• Resource types R1, R2, . . ., Rm (e.g., CPU cycles, memory space, I/O devices)
• Each resource type Ri has Wi instances.
• Each process utilizes a resource as follows:
o request
o use
o release

3. Deadlock Situation
Deadlock can arise if the following four conditions hold simultaneously:
• Mutual exclusion: only one process at a time can use a resource.
• Hold and wait: a process holding at least one resource is waiting to acquire
additional resources held by other processes.
• No preemption: a resource can be released only voluntarily by the process
holding it, after that process has completed its task.
• Circular wait: there exists a set {P1, P2, …, Pn} of waiting processes such that
P1 is waiting for a resource that is held by P2, P2 is waiting for a resource that is
held by P3, …, Pn–1 is waiting for a resource that is held by Pn, and Pn is waiting
for a resource that is held by P1.

4. Resource Allocation Graph (RAG)


• RAG: a set of vertices (V) and edges (E)
• V is partitioned into two types:
o P = {P1, P2, …, Pn} (i.e., processes); R = {R1, R2, …, Rm} (resource
types).
• request edge: directed edge P1 Rj
• assignment edge: directed edge Rj Pi
R vertex (a resource type with 4 instances)

P vertex (a process)

Pi Request edge: Pi requests instance of Rj

Rj

Pi Assignment edge: Pi is holding an instance of Rj

Rj

Figure 7.1 Example of RAG


5. RAG and Deadlock

(a) (b)

Figure 7.2 (a) RAG with deadlock and (b) RAG with a cycle but no deadlock

• If graph contains no cycles no deadlock.


• If graph contains a cycle
o if only one instance per resource type, then deadlock.
o if several instances per resource type, possibility of deadlock.

6. Method for Handling Deadlocks


Choice #1: Ensure that the system will never enter a deadlock state
a. Deadlock Prevention
b. Deadlock Avoidance

Choice #2: Allow the system to enter a deadlock state and then recover
c. Deadlock Detection & Recovery

Choice #3: Ignore and don’t care


6.1 Deadlock Prevention: ensure that the system will never enter a deadlock state.

Technique #1: Ensure that “Mutual Exclusion” condition never holds


• Impossible

Technique #2: Ensure that “Hold-and-Wait” condition never holds


• Protocol #1: A new process requests every required resources before it begins
execution, OR
• Protocol #2: OS allows a process to request resources only when it has no
resource
• Problems: low resource utilization, starvation

Technique #3: Ensure that “No Preemption” condition never holds


• Protocol #1: Pi requests a resource at point in time. If the resource is not available
then all resources currently being held by Pi are preempted, OR
• Protocol #2: Pi request a resource at anytime. If the resource is held by Pj waiting
for another resource then preempt the resource and allocate it to Pi.
• Problem: for preemptable resources such as CPU registers and memory space.
Not applied to such resources as printers and tape drives.

Technique #4: Ensure that “Circular Wait” condition never holds


• Pi can request Rj only after Ri has been allocated to Pi (note, F(Rj) > F(Ri))
• Pi release Ri before it requests Rj (note, F(Rj) >= F(Ri)).
• Problem: Resource ordering

6.2 Deadlock Avoidance: Requires that the system has some additional a priori
information available.

• Requires each process to declare the maximum number of resources of each type
that it may need.

• Safe State:
o System is in safe state if there exists a safe sequence of all processes (i.e.,
the system can allocate all the requested resources in some order).
o Safe Sequence for the current allocation state: <P1, P2, …, Pn>, for each
Pi, Pi resource request can be satisfied by currently available resources +
resources held by all the Pj, with j<i.

• Unsafe State: There is no safe sequence

• Deadlock State: the deadlock state is a subset of the unsafe state.

• Deadlock Avoidance ensures that the system will never enter the unsafe state
Figure 7.3 Safe/Unsafe/Deadlock State Spaces

• When every resource type has only one instance (RAG Algorithm)
o Before Pi starts executing, all its claim edges are generated and added to
the RAG.
o Claim edge Pi Rj indicated that process Pj may request resource Rj;
represented by a dashed line.
Claim edge converts to request edge when a process requests a
resource.
When a resource is released by a process, assignment edge
reconverts to a claim edge.
o Resource request is granted only if converting the request edge to an
assignment edge does not result in the formation of a circle in the RAG.
Unsafe State In A Resource-Allocation Graph Resource-Allocation Graph For Deadlock Avoidance

Fugure 7.4 Unsafe Stage in RAG and Deadlock Avoidance


In the single resource instance case, circle in RAG is both a necessary and a sufficient
condition.
Note that the algorithm, to detect a cycle in a graph, requires an order of n2 operations,
where n is the number of vertices in the graph.

• When resource types have multiple instances (Banker’s Algorithm)


o Before Pi starts executing, it must declare the maximum number of
instances of each resource type that it may need..

Data Structures for the Banker’s Algorithm

Let n = number of processes, and m = number of resources types.

• Available: 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 may request at
most k instances of resource type Rj.
• Allocation: n x m matrix. If Allocation[i,j] = k then Pi is currently
allocated k instances of Rj.
• Need: n x m matrix. If Need[i,j] = k, then Pi may need k more
instances of Rj to complete its task.

Need [i,j] = Max[i,j] – Allocation [i,j].

Safety Algorithm
Local Vectors:
Work: a vector of length m.
1. Let Work and Finish be vectors of length m and n, respectively.
Finish: a vector of length n
Initialize:
Work := Available
Step 1. Work = Available;
Finish
Finish[i] = false, for[i]all
= false for i - 1,3, …, n.
i=1,2,...,n;
2. Find and i such that both:
Step 2. Find(a)i such that
Finish [i] (Finish[i]=false
= false && Need[i] <= Work);
If no such i exist then the system is in an unsafe state; return;
(b) Needi ≤ Work
If no such i exists, go to step 4.
Step 3. Work = Work+Allocation[i];
3.Finish[i]
Work :==Work
true;+ Allocationi
Finish[i] := true
Step 4. Ifgo to step =2.true for all i then the system is in a safe state; return;
Finish[i]
Else goto [i]
4. If Finish Step 2; for all i, then the system is in a safe state.
= true
Resource-Request Algorithm for Process Pi

Local Vector:
Request: m-dimensional
Request vector. r[j] is the number of instances of jth resource type Pi is
i = request vector for process Pi. If Requesti [j] = k then
currently requesting.
process P wants k instances of resource type R
i j.
1. If Requesti ≤ Needi go to step 2. Otherwise, raise error
Step 1. If Requestcondition,
<= Need[i] thenprocess
since goto Step has2;exceeded its maximum claim.
Else, raise an error; return;
2. If Requesti ≤ Available, go to step 3. Otherwise Pi must
wait, since resources are not available.
Step 2. If Request<= Available[i] then goto Step 3;
3. Pretend to allocate
Else, block the current processrequested
Pi; return;resources to Pi by modifying
the state as follows:
Step 3. Available
Backup the current values := Available
of Available, = Request
Need[i], i;
and Allocation[i];
Allocation
Available = Available-Request;i := Allocation i + Request i;
Allocation[i] = Allocation[i]+Request;
Needi := Needi – Requesti;;
Need[i] = Need[i]-Request;
• If safe the resources are allocated to Pi.
• If unsafe Pi must wait, and the old resource-allocation
Step 4. Call the Safetystate
Algorithm;
is restored
If it is safe then physically allocate requested resources; return;
Else restore Available, Allocation[i], and Need[i] and block the process Pi; return;
Example of Banker’s Algorithm

• 5 processes P0through P4; 3 resource types A(10 instances),


B(5instances, and C(7 instances).
• Snapshot at time T0:
Allocation Max Available
AB C ABC ABC
P0 01 0 753 332
P1 20 0 322
P2 30 2 902
P3 21 1 222
P4 00 2 433

• The content of the matrix. Need is defined to be Max


– Allocation
.
Need
ABC
P0 743
P1 122
P2 600
P3 011
P4 431
• The system is in a safe state since there
P1, Pare
3, P4,sequences
P2, P0> (e.g., <P1 P3 P4 P0 P2>).
satisfies safety criteria.

P1 requests
(1,0,2)
• Check that Request ≤Available (that is, (1,0,2) ≤(3,3,2) true.
Allocation Need Available
AB C ABC ABC
P0 010 743 230
P1 302 020
P2 301 600
P3 211 011
P4 002 431
• Executing safety algorithmshows that sequence <P1, P3, P4, P0, P2>
satisfies safety requirement.
• Can request for (3,3,0) by P4 be granted?
• Can request for (0,2,0) by P0be granted?
6.3 Deadlock Detection and Recovery: In this method, OS allows the system to enter
deadlock state. Then OS detects the deadlock with a detection algorithm, and runs
a recovery scheme.

6.3.1 Deadlock Detection Algorithms


• Single instance case
o Maintain wait-for graph
Nodes are processes.
Pi Pj if Pi is waiting for Pj.
o Periodically invoke an algorithm that searches for a cycle in the graph.

Note that in the single resource instance case, circle in RAG is both a
necessary and a sufficient condition.
Note that the algorithm to detect a cycle in a graph requires an order of n2
operations, where n is the number of vertices of the graph.

Resource-Allocation Graph Corresponding wait-for graph

Figure 7.8 Wait-for graph

• Multiple instance case


o Data Structures:
Available: A vector of length m indicates the number of available
resources of each type.
Allocation: An n x m matrix defines the number of resources of
each type currently allocated to each process.
Request: An n x m matrix indicates the current request of each
process. If Request [ij] = k, then process Pi is requesting k more
instances of resource type. Rj.
o Algorithm:
1. Let Work and Finish be vectors of length m and n, respectively Initialize:
(a) Work = Available
(b) For i = 1,2, …, n, if Allocationi != 0, then Finish[i] = false;
otherwise, Finish[i] = true.
2. Find an index i such that both:
(a) Finish[i] = false
(c) Requesti <= Work
If no such i exists, go to step 4.

3. Work = Work + Allocationi


Finish[i] := true
go to step 2.

4. If Finish[i] = false, for some i, 1 <= i <= n, then the system is in deadlock
state.
Moreover, if Finish[i] = false, then Pi is deadlocked.

Note, the algorithm requires an order of m x n2 operations to detect


whether the system is in deadlocked state.

• Usage of Deadlock Detection Algorithms


o When, and how often, to invoke the detection algorithm depends on:
How often a deadlock is likely to occur?
How many processes will need to be rolled back?
o If the detection algorithm is invoked at arbitrarily points in time, there
may be many cycles in the resource graph and so we would not be able to
tell which of the many deadlocked processes “caused” the deadlock.

6.3.2 Deadlock Recovery


• Process Termination
o Abort all deadlocked processes, or
o Abort one deadlocked process at a time until every deadlock cycle is
eliminated.
o In which order should we choose processes to abort?
Priority of the process.
Process age and remaining time
Resources the process has used.
Resources the process needs to complete.
How many processes will need to terminate.
Is the process interactive or batch?

• Resource Preemption
o Selecting a victim – minimize cost.
o Rollback – return to some safe state, restart process fro that state.
o Starvation – same process is always picked as a victim, include the
number of rollback in cost factor.

6.4 Combined Approach

• Combine the three basic approaches


o Prevention
o Avoidance
o Detection
• Partition resources into hierarchically ordered classes.
• Use most appropriate technique for handling deadlocks within each class.

You might also like