0% found this document useful (0 votes)
24 views57 pages

OS-Chap-7 Deadlocks

Uploaded by

earthlinggamer
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)
24 views57 pages

OS-Chap-7 Deadlocks

Uploaded by

earthlinggamer
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/ 57

Chapter 7: Deadlocks

Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne ©2013
Chapter 7: Deadlocks

System Model
Deadlock Characterization
Methods for Handling Deadlocks
Deadlock Prevention
Deadlock Avoidance
Deadlock Detection
Recovery from Deadlock

Operating System Concepts – 9th Edition 7.2 Silberschatz, Galvin and Gagne ©2013
Chapter Objectives

To develop a description of deadlocks, which prevent sets of


concurrent processes from completing their tasks.
To present a number of different methods for preventing or
avoiding deadlocks in a computer system.

Operating System Concepts – 9th Edition 7.3 Silberschatz, Galvin and Gagne ©2013
The 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:
System has 2 disk drives.
P1 and P2 each hold one disk drive, and each needs another one.
Example:
semaphores A and B, initialized to 1
P0 P1
wait (A); wait (B)
wait (B); wait (A)

Operating System Concepts – 9th Edition 7.4 Silberschatz, Galvin and Gagne ©2013
Bridge Crossing Example

S1 S2

Traffic only in one direction.


Each section of a bridge can be viewed as a resource.
If a deadlock occurs, it can be resolved if one car backs up
(preempt resources and rollback).
Several cars may have to be backed up if a deadlock occurs.
(If a process keeps backing up and get
Starvation is possible. preempted, it will never finish).
Note – Most OSs do not prevent or deal with deadlocks.

Operating System Concepts – 9th Edition 7.5 Silberschatz, Galvin and Gagne ©2013
System Model

System consists of resources.


Resource types R1, R2, . . ., Rm
CPU cycles, memory space, I/O devices
Each resource type Ri has Wi instances.
Example: Tape is a resource.
Example: There might be 4 tape instances (4 tapes).
Each process utilizes a resource as follows:
request
use
release

Operating System Concepts – 9th Edition 7.6 Silberschatz, Galvin and Gagne ©2013
Deadlock Characterization
Deadlock can arise if four conditions hold simultaneously:

o Mutual exclusion: Only one process at a time can use a


resource.
o Hold and wait: A process holding at least one resource is Hold and Wait:
waiting to acquire additional resources held by other A process is holding
some instances of
processes. resources. It also
o No preemption: A resource can be released only voluntarily needs (requesting)
other instances of
by the process holding it, after that process has completed other resources.
its task.
Then, the held
o Circular wait: There exists a set {P0, P1, …, Pn} of waiting resources cannot be
processes such that P0 is waiting for a resource that is held released before the
by P1, P1 is waiting for a resource that is held by P2, …, Pn–1 process acquires all
resources it needs
is waiting for a resource that is held by Pn, and Pn is waiting and uses them then
for a resource that is held by P0. release them.

Operating System Concepts – 9th Edition 7.7 Silberschatz, Galvin and Gagne ©2013
Deadlock with Mutex Locks
Deadlocks can occur via system calls, locking, etc.
See example box in text page 318 for mutex deadlock.

Operating System Concepts – 9th Edition 7.8 Silberschatz, Galvin and Gagne ©2013
Resource-Allocation Graph
A 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 the processes
in the system.

R = {R1, R2, …, Rm}, the set consisting of all resource


types in the system.

request edge – directed edge Pi → Rj

assignment edge – directed edge Rj → Pi

Operating System Concepts – 9th Edition 7.9 Silberschatz, Galvin and Gagne ©2013
Resource-Allocation Graph (Cont.)
Process:

Resource type with 4 instances:

Pi requests instance of Rj (wait)

Pi
Rj
Pi is holding an instance of Rj (Hold)

Pi
Rj

Operating System Concepts – 9th Edition 7.10 Silberschatz, Galvin and Gagne ©2013
Example of a Resource Allocation Graph

• P3 can finish only when it holds and uses R3.


• P2 can finish only when it holds R1, R2, and R3
and use them.
• P1 can finish only when it holds R1 and R2 and
use them.

No deadlock here. Why?


• P3 is holding R3 instance. It uses it and finishes.
• Now R3 instance is available.
• P2 is holding an instance of R2 and R1. Now, it holds
R3 instance. It uses all three resources and finishes.
• Now, R1 and R3 instances and one of R2 instances
are available.
• P1 is holding an instance of R2. Now, it holds R1 instance.
It uses both and finishes.
• So, deadlock conditions are not met.
• So, this is a deadlock free situation.
Operating System Concepts – 9th Edition 7.11 Silberschatz, Galvin and Gagne ©2013
Resource Allocation Graph With A Deadlock

• P3 can finish only when it holds both R2 and R3


and uses them.
• P2 can finish only when it holds R1, R2 and R3
and use them.
• P1 can finish only when it holds both R1 and R2
and use them.

We have deadlock here. Why?


• P3 cannot hold an instance of R2 because
both instances are being held by P1 and P2.
• P2 cannot hold R3 instance because it is
being held by P3.
• P1 cannot hold R1 instance because it is
being held by P2.
• Therefore, no process can finish its work.

Operating System Concepts – 9th Edition 7.12 Silberschatz, Galvin and Gagne ©2013
Graph With A Cycle But No Deadlock

Operating System Concepts – 9th Edition 7.13 Silberschatz, Galvin and Gagne ©2013
Basic Facts

If 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 of deadlock.
If there is a deadlock, then there must be a cycle.

Operating System Concepts – 9th Edition 7.14 Silberschatz, Galvin and Gagne ©2013
Methods for Handling Deadlocks
1. Ensure that the system will never enter a deadlock state (using a
protocol to prevent or avoid deadlocks).
Deadlock prevention ensures that at least one of the
necessary conditions cannot hold.
 This way deadlock will never happen.
Deadlock avoidance requires that the operating system be
given additional information in advance concerning which
resources a process will request and use during its lifetime.
 Request is granted if it is not going to cause deadlock.
 Request is rejected if it is going to cause deadlock.
2. Allow the system to enter a deadlock state and then recover.
3. Ignore the problem and pretend that deadlocks never occur in the
system; used by most operating systems, including UNIX.

Operating System Concepts – 9th Edition 7.15 Silberschatz, Galvin and Gagne ©2013
Deadlock Prevention
Restrain (control) the ways request can be made. Example: Read-
Only files are
How to break Mutual Exclusion – not required for sharable sharable. So, two
resources (e.g., read-only files); must hold for non-sharable processes can use a
resources. read-only file at the
same time. In this
How to break Hold and Wait – must guarantee that whenever a
case, we do not
process requests a resource, it does not hold any other resources. need “mutual
Case 1: Require process to request and be allocated all its exclusion”. So, no
resources before it begins execution. deadlock on using
read-only files.
Case 2: or allow process to request resources only when the
process has none allocated to it.
Example: Printer is
Low resource utilization; starvation possible. not sharable. So,
we must have
• Example of Case 1 • Example of Case 2 “mutual
• A process needs to copy data from • A process needs to copy data from exclusion” here.
tape to disk, then it prints data tape to disk, then it prints data So, deadlock is
from disk to printer. from disk to printer. possible in this case.
• Here, this process can only start if it • First, hold tape and disk and make
is given all 3 resources at the same copy.
time. • Second, release tape and disk and
• Disadvantage: Low resource then
utilization. Why holding the printer • Third, request disk and printer to
from the start while we really need it make print.
at the end.
Operating System Concepts – 9th Edition 7.16 Silberschatz, Galvin and Gagne ©2013
Deadlock Prevention (Cont.)
How to break 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.
How to break Circular Wait – impose a total ordering of all resource types and
require that each process requests resources in an increasing order of enumeration.

Example of preventing circular wait:


• Assign number “1” for Tape Drive
• Assign number “5” for Disk Drive
• Assign number “12” for Printer
• If a process needs to use tape and printer at the same time, then it should
request tape then request printer
• Lowest in order must be requested first
• If a process is currently using printer, then it cannot use the tape or disk until
it releases the printer.
• Highest in order must be released before requesting a lower
order resource

Operating System Concepts – 9th Edition 7.17 Silberschatz, Galvin and Gagne ©2013
Deadlock Example
/* thread one runs in this function */
void *do_work_one(void *param)
{
pthread_mutex_lock(&first_mutex);
Suppose sequence of execution:
pthread_mutex_lock(&second_mutex); 1- Thread 1 obtaining lock on first mutex.
/** * Do some work */ 2- Thread 2 obtaining lock on second mutex.
pthread_mutex_unlock(&second_mutex); 3- Thread 1 try to obtain lock on second mutex.
4- Thread 2 try to obtain lock on first mutex.
pthread_mutex_unlock(&first_mutex);
pthread_exit(0);
• Thread 1 cannot obtain lock on second mutex
} because the lock is being held by thread 2.
/* thread two runs in this function */ • Thread 2 cannot obtain lock on first mutex
because the lock is being held by thread 1.
void *do_work_two(void *param)
{

pthread_mutex_lock(&second_mutex);
pthread_mutex_lock(&first_mutex);
/** * Do some work */
pthread_mutex_unlock(&first_mutex);
pthread_mutex_unlock(&second_mutex);
pthread_exit(0);
}

Operating System Concepts – 9th Edition 7.18 Silberschatz, Galvin and Gagne ©2013
Deadlock Example with Lock Ordering
void transaction(Account from, Account to, double amount)
{
mutex lock1, lock2;
lock1 = get_lock(from); Suppose sequence of execution:
lock2 = get_lock(to); 1- Thread 1 obtaining lock on “from” (Account A).
2- Thread 2 obtaining lock on “from” (Account B).
acquire(lock1); 3- Thread 1 try to obtain lock on ”To” (Account B).
acquire(lock2); 3- Thread 2 try to obtain lock on ”To” (Account A).

withdraw(from, amount);
deposit(to, amount); • Thread 1 cannot obtain lock on Account B because
the lock is being held by thread 2.
release(lock2);
• Thread 2 cannot obtain lock on Account A because
release(lock1); the lock is being held by thread 1.
}

▪ Transactions 1 and 2 execute concurrently.


▪ Transaction 1 transfers $25 from account A to account B, and Transaction 2
transfers $50 from account B to account A.

Operating System Concepts – 9th Edition 7.19 Silberschatz, Galvin and Gagne ©2013
Deadlock Avoidance
Requires that the system has some additional a priori information
available:

Simplest and most useful model requires that each process


declare the maximum number of resources of each type
that it may need.
The deadlock-avoidance algorithm dynamically examines
the resource-allocation state to ensure that there can never
be a circular-wait condition.
Resource-allocation state is defined by the number of
available and allocated resources, and the maximum
demands of the processes.

Operating System Concepts – 9th Edition 7.20 Silberschatz, Galvin and Gagne ©2013
Safe State

When a process requests an available resource, system must decide if


immediate allocation leaves the system in a safe state.
System is in safe state if there exists a sequence <P1, P2, …, Pn> of all
the processes in the systems 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.
That is:
If Pi resource needs are not immediately available, then Pi can 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.

Operating System Concepts – 9th Edition 7.21 Silberschatz, Galvin and Gagne ©2013
Example of Safe State
Number of existing tape drives = 12
P0, P1, P2 are currently holding 5, 2, 2 tape drives, respectively.

Process Maximum Need Current Need


---------- --------------------- ---------- --------
P0 10 5 So, P0 needs 10-5=5 more tapes to finish
P1 4 2 So, P1 needs 4-2=2 more tapes to finish
P2 9 2 So, P2 needs 9-2=7 more tapes to finish
Is the system in a safe state? (In other words, given the current allocations, can
all processes finish their work?).
Current remaining tape drives = 12 – (5+2+2) = 12-9 = 3
P1 can hold 2 more tape drives and finishes its work.
• Now, remaining tape drives = 3 – 2 + 4 = 5
Now, P0 can hold 5 more tape drives and finishes its work.
• Now, remaining tape drives = 5 – 5 + 10 = 10
Now, P2 can hold 7 more tape drives and finishes its work.
So, Yes, the system is in safe state.

Operating System Concepts – 9th Edition 7.22 Silberschatz, Galvin and Gagne ©2013
Example of Unsafe State
Suppose P2 requests 1 more tape than previous example and it is given the
tape drive. Is the system in a safe state now?

Process Maximum Need Current Need


---------- --------------------- ----------- --------
P0 10 5 So, P0 needs 10-5=5 more tapes to finish
P1 4 2 So, P1 needs 4-2=2 more tapes to finish
P2 9 3 So, P2 needs 9-3=6 more tapes to finish
Current remaining tape drives = 12 – (5+2+3) = 12-10 = 2.
P1 can hold 2 more tape drives and finishes its work.
• Now, remaining tape drives = 2 – 2 + 4 = 4.
Now, P0 cannot hold 5 more tape drives because 4 is only available.
Now, P2 cannot hold 6 more tape drives because 4 is only available.
So, P0 and P2 will wait for tape drives for ever (Deadlock).
So, the system is in unsafe state.
Our mistake was in allocating 1 more tape drive for P2.
If P2 is not given the additional tape drive, we could have avoided
deadlock.
Operating System Concepts – 9th Edition 7.23 Silberschatz, Galvin and Gagne ©2013
Basic Facts

If 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 an unsafe state.

Operating System Concepts – 9th Edition 7.24 Silberschatz, Galvin and Gagne ©2013
Safe, Unsafe, Deadlock State

Operating System Concepts – 9th Edition 7.25 Silberschatz, Galvin and Gagne ©2013
Avoidance Algorithms

Single instance of a resource type:


Use a resource-allocation graph

Multiple instances of a resource type:


Use the Banker’s algorithm

Operating System Concepts – 9th Edition 7.26 Silberschatz, Galvin and Gagne ©2013
Resource-Allocation Graph Scheme
Claim edge Pi Rj indicated that process Pi may request resource Rj;
represented by a dashed line.
Claim edge converts to request edge when a process requests a
resource.
Request edge converted to an assignment edge when the 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.

• Suppose process Pi requests resource Rj. Should request be granted?


• First, we pretend to give process Pi resource Rj by converting
request edge to assignment edge.
• Second, if that leads to a cycle in the graph, then
• Pi request should be rejected
• Else, Pi request is granted

Operating System Concepts – 9th Edition 7.27 Silberschatz, Galvin and Gagne ©2013
Resource-Allocation Graph Algorithm

Suppose that process Pi requests a resource Rj.


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.

Operating System Concepts – 9th Edition 7.28 Silberschatz, Galvin and Gagne ©2013
Resource-Allocation Graph

Assign Edge
R1 is assigned
Request Edge
to P1. So, it can
P2 is requesting
Use it.
Resource R1.

Claim Edge
P1 may use
R2 in future. • Suppose P2 requests R2.
• Should R2 be assigned to P2?
• Avoiding deadlocks using • Look Next Slide.
resource allocation graph only
works when each resource in the
graph have only one instance.

Operating System Concepts – 9th Edition 7.29 Silberschatz, Galvin and Gagne ©2013
Unsafe State in Resource-Allocation Graph

1 2 3 4

1) P2 requests R2. • If P2 request is granted and P1


2) Claim edge turns to request edge. requests R2 in future, a
deadlock occur.
3) Pretend to give R2 to P2 by converting request
edge to assignment edge. • Because of that R2 is not
assigned to P2.
4) Do we have a cycle?
Yes, we do (Unsafe State).
5) Therefore, P2 request should be rejected, and
the assignment edge goes back to claim edge.

Operating System Concepts – 9th Edition 7.30 Silberschatz, Galvin and Gagne ©2013
Banker’s Algorithm
Multiple 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 them in


a finite amount of time.

Operating System Concepts – 9th Edition 7.31 Silberschatz, Galvin and Gagne ©2013
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]

Operating System Concepts – 9th Edition 7.32 Silberschatz, Galvin and Gagne ©2013
Data Structures for the Banker’s Algorithm
Five processes P0 through P4; Three resource types:
A (10 instances), B (5 instances, and C (7 instances).
Snapshot at time T0:
Allocation Max Need Available
ABC ABC ABC ABC
P0 010 753 743 332
P1 200 322 122
P2 302 902 600
P3 211 222 011
P4 002 433 431

Allocation[0,1] = 1; means P0 is currently holding and using 1 instance of resource B.

Max[0,1] = 5; means P0 needs maximum of 5 instances of resource B to finish its job.

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


Example: Need[0,1] = Max[0,1] – Allocation[0,1] = 5 – 1 = 4.
This means that P0 needs 4 instances of resource B.
Operating System Concepts – 9th Edition 7.33 Silberschatz, Galvin and Gagne ©2013
Safety Algorithm
Work is a temporary variable
1. Let Work and Finish be vectors of length m and n, that holds the number of
respectively. Initialize: available instances of each
resource type.
Work = Available
Finish[i] = false for i = 0, 1, …, n- 1 We initially assume all
processes did not finish their
2. Find an i such that both: work.
(a) Finish[i] = false
If these two conditions are
(b) Needi  Work
true, this means that the
If no such i exists, go to step 4 process can be allocated what
it needs to finish its job.
3. Work = Work + Allocationi
Finish[i] = true Since process finishes its work, then it
go to step 2 releases its allocated resources.

4. If Finish[i] == true for all i, then the system is in a safe state.

When we say Needi <= work, the answer is true only if Need[i, j] <= Work[j] for all j.
Example: Suppose Need1 = {1, 2, 2} and work = {3, 3, 2}
Need1 <= Work because 1 <= 3 and 2 <= 3 and 2 <= 2.
Operating System Concepts – 9th Edition 7.34 Silberschatz, Galvin and Gagne ©2013
Resource-Request Algorithm for Process Pi
Requesti = request vector for process Pi. If Requesti [j] = k then process Pi
wants k instances of resource type Rj
1. 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 must wait, since
resources are not available.
3. Pretend to allocate requested resources to Pi by modifying the state as
follows:
Available = Available – Requesti Important: If the algorithm
does not lead to safe state,
Allocationi = Allocationi + Requesti then, “Available”, “Allocation”,
Needi = Needi – Requesti and “Need” should get their
old values.
4. If safe  The resources are allocated to Pi
If unsafe  Pi must wait, and the old resource-allocation state is restored.
The idea behind this algorithm is simple.
When a process request resource, then:
1- Pretend that you allocated the requested resources for the process.
2- Is the system in safe state?
3- If yes, then the process can be given resources it wants.
4- If no, then the process cannot be given resources it wants.
Operating System Concepts – 9th Edition 7.35 Silberschatz, Galvin and Gagne ©2013
Example of Banker’s Algorithm
Need[i, j] = Max[i, j] – Allocation[i, j] Initially, Work = Available
If (Finish[i]==false and Needi <= Work) Work = Work + Allocationi
5 processes P0 through P4;
3 resource types: A B C
A (10 instances), B (5 instances), and C (7 instances) 10 5 7
Snapshot at time T0: Need
<=
Allocation Max Available Finish Work
Work
A B C
ABC ABC ABC
3 3 2 P0 010 753 332 F T N Y
5 3 2 F T Y
7 4 3
P1 200 322
7 4 5 P2 302 902 F T N Y
7 5 5 T Y
P3 211 222 F
10 5 7
P4 002 433 F T Y

The system is in a safe state since the sequence < P1, P3, P4, P0, P2>
satisfies safety criteria.
Operating System Concepts – 9th Edition 7.36 Silberschatz, Galvin and Gagne ©2013
Example (Cont.)
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 the sequence < P1, P3, P4, P0, P2>
satisfies safety criteria.
We knew the system is in safe state by following
the safety algorithm (Look Next Slide).

Finding only one sequence in which all processes finish


their work is enough to say the system is in safe state.

Operating System Concepts – 9th Edition 7.37 Silberschatz, Galvin and Gagne ©2013
Example (Cont.)
1) Finish = {false, false, false, false, false} All these also lead to safe state
• <P1, P3, P4, P2, P0> or
2) Work = Available = {3, 3, 2} • <P1, P3, P2, P4, P0> or
3) P0, Need0 = {7, 4, 3} is Need0 <= work ? No • <P3, P1, P4, P2, P0> or
• <P3, P1, P2, P4, P0>
P1, Need1 = {1, 2, 2} is Need1 <= work ? Yes
Finish = {false, true, false, false, false} and Work = {5, 3, 2}
P2, Need2 = {6, 0, 0} is Need2 <= work ? No
P3, Need3 = {0, 1, 1} is Need3 <= work ? Yes
Finish = {false, true, false, true, false} and Work = {7, 4, 3} Notice that when all
P4, Need4 = {4, 3, 1} is Need4 <= work ? Yes processes finish their
work, then the number of
Finish = {false, true, false, true, true} and Work = {7, 4, 5}
available resources
P0, Need0 = {7, 4, 3} is Need0 <= work ? Yes before and after applying
Finish = {true, true, false, true, true} and Work = {7, 5, 5} algorithm must be equal.
Otherwise, you know you
P2, Need2 = {6, 0, 0} is Need2 <= work ? Yes
did something wrong.
Finish = {true, true, true, true, true} and Work = {10, 5, 7}
Now, all values in Finish vector is true, so, the system is in safe state
In other words, all processes can finish their work. The sequence is:
P1, P3, P4, P0, P2
Operating System Concepts – 9th Edition 7.38 Silberschatz, Galvin and Gagne ©2013
Example (Cont.)

Suppose the following operations are needed:


1) P1 requests (1, 0, 2)
2) P4 requests (1, 2, 0)
3) P4 requests (3, 3, 0) Self Study Homework
4) P0 requests (0, 2, 0) Self Study Homework

Note: If a given request is granted, its effect is taken into consideration in


further requests.

Operating System Concepts – 9th Edition 7.39 Silberschatz, Galvin and Gagne ©2013
Example: P1 Request (1,0,2)
Initially, Work = Available
If (Finish[i]==false and Needi <= Work) Work = Work + Allocationi
1. Check that Request  Need (that is, (1,0,2)  (1,2,2)  true
2. Check that Request  Available (that is, (1,0,2)  (3,3,2)  true
3. Available = Available – Requesti | Allocationi = Allocationi + Requesti | Needi = Needi – Requesti

122– Allocation Need Available Need


200+
102 Finish <= Pretend that
102
= ABC ABC ABC Work request is
=
302 020 granted.
P0 010 743 230
F T N Y Old Available
Work P1 302 020 = {3, 3, 2}
A B C F T Y Old P1
2 3 0 P2 302 600
332– F T N Y Allocation =
5 3 2 P3 211 011 102 {2, 0, 0}
7 4 3 = F T Y Old P1 Need
7 4 5 P4 002 431 230
F T Y = {1, 2, 2}
7 5 5
10 5 7

4. Executing safety algorithm shows that sequence < P1, P3, P4, P0, P2> satisfies safety
requirement so request can be granted. What about <P1,P4,P3,P0,P2>,
<P1,P4,P3,P2, P0 >?
Operating System Concepts – 9th Edition 7.40 Silberschatz, Galvin and Gagne ©2013
Example P1 Request (1,0,2) (Cont.)
Notice that when all processes
finish their work, then the number
1) Finish = {false, false, false, false, false}
of available resources after
2) Work = Available = {2, 3, 0} applying algorithm must be equal
to original number of resources.
3) P0, Need0 = {7, 4, 3} is Need0 <= work ? No Otherwise, you know you did
P1, Need1 = {0, 2, 0} is Need1 <= work ? Yes something wrong.
Finish = {false, true, false, false, false} and Work = {5, 3, 2}
P2, Need2 = {6, 0, 0} is Need2 <= work ? No
P3, Need3 = {0, 1, 1} is Need3 <= work ? Yes
Finish = {false, true, false, true, false} and Work = {7, 4, 3}
P4, Need4 = {4, 3, 1} is Need4 <= work ? Yes
Result is safe
Finish = {false, true, false, true, true} and Work = {7, 4, 5}
state, So, P1
P0, Need0 = {7, 4, 3} is Need0 <= work ? Yes request is
granted.
Finish = {true, true, false, true, true} and Work = {7, 5, 5}
P2, Need2 = {6, 0, 0} is Need2 <= work ? Yes
Finish = {true, true, true, true, true} and Work = {10, 5, 7}
Operating System Concepts – 9th Edition 7.41 Silberschatz, Galvin and Gagne ©2013
Example: P4 Request (1,2,0)
Initially, Work = Available
If (Finish[i]==false and Needi <= Work) Work = Work + Allocationi
1. Check that Request  Need (that is, (1,2,0)  (4,3,1)  true
2. Check that Request  Available (that is, (1,2,0)  (2,3,0)  true
3. Available = Available – Requesti | Allocationi = Allocationi + Requesti | Needi = Needi – Requesti

431– Allocation Need Available Need


002+
120 Finish <= Pretend that
120
= ABC ABC ABC Work request is
=
122 311 granted.
P0 010 743 110
F N Old Available
Work P1 302 020 = {2, 3, 0}
F N
A B C P2 302 600 Old P4
1 1 0 230–
F N Allocation =
P3 211 011 120 {0, 0, 2}
= F N Old P4 Need
P4 122 311 110
F N = {4, 3, 1}

4. Executing safety algorithm shows that we can’t find a sequence in which all
processes finish their work. The system is not in safe state.

Operating System Concepts – 9th Edition 7.42 Silberschatz, Galvin and Gagne ©2013
Example P4 Request (1,2,0) (Cont.)

1) Finish = {false, false, false, false, false}


2) Work = Available = {1, 1, 0}
3) P0, Need0 = {7, 4, 3} is Need0 <= Work ? No
P1, Need1 = {0, 2, 0} is Need1 <= Work ? No
P2, Need2 = {6, 0, 0} is Need2 <= Work ? No
P3, Need3 = {0, 1, 1} is Need3 <= Work ? No
P4, Need4 = {3, 1, 1} is Need4 <= Work ? No

Could not find a sequence in which all processes finish their work.
Unsafe system state. So, restore allocation, need, and available values:
• Need4 = {4, 3, 1}
• Allocation4 = {0, 0, 2}
• Available = {2, 3, 0}

Operating System Concepts – 9th Edition 7.43 Silberschatz, Galvin and Gagne ©2013
Example of Banker’s Algorithm
Need [i,j] = Max[i,j] – Allocation [i,j] Initially, Work = Available
If (Finish[i]==false and Needi <= Work) Work = Work + Allocationi
5 processes P0 through P4;
3 resource types: A B C
10 5 7
A (10 instances), B (5instances), and C (7 instances)
Snapshot at time T0:
Need
Allocation Max Available Finish <=
Work Work
A B C
ABC ABC ABC
5 3 2 P0 010 953 332 F N
7 4 3
P1 200 322 F T Y
7 4 5
P2 302 11 0 2 F N
P3 211 222 F T Y
P4 002 433 F T Y
• Check if the system is in a safe state?
<P1, P3, P4> unsafe state (Next Slide)
<P1, P4, P3> is also unsafe
Operating System Concepts – 9th Edition 7.44 Silberschatz, Galvin and Gagne ©2013
Example of Banker’s Algorithm (Cont.)
Notice that when all
processes finish their work,
1) Finish = {false, false, false, false, false}
then the number of available
2) Work = Available = {3, 3, 2} resources before and after
applying algorithm must be
3) P0, Need0 = {9, 4, 3} is Need0 <= work ? No equal. Otherwise, you know
P1, Need1 = {1, 2, 2} is Need1 <= work ? Yes you did something wrong.
Finish = {false, true, false, false, false} and Work = {5, 3, 2}
With this
P2, Need2 = {8, 0, 0} is Need2 <= work ? No sequence, the
P3, Need3 = {0, 1, 1} is Need3 <= work ? Yes system is not in
safe state.
Finish = {false, true, false, true, false} and Work = {7, 4, 3}
P4, Need4 = {4, 3, 1} is Need4 <= work ? Yes Try to find any
Finish = {false, true, false, true, true} and Work = {7, 4, 5} other sequence
that leaves the
P0, Need0 = {9, 4, 3} is Need0 <= work ? No system in safe
P2, Need2 = {8, 0, 0} is Need2 <= work ? No state. None
can be found.

Operating System Concepts – 9th Edition 7.45 Silberschatz, Galvin and Gagne ©2013
Deadlock Detection

Allow system to enter deadlock state

Detection algorithm

Recovery scheme

Operating System Concepts – 9th Edition 7.46 Silberschatz, Galvin and Gagne ©2013
Single Instance of Each Resource Type

Maintain wait-for graph


Nodes are processes
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

Operating System Concepts – 9th Edition 7.47 Silberschatz, Galvin and Gagne ©2013
Resource-Allocation Graph and Wait-for Graph

Resource-Allocation Graph Corresponding wait-for graph

Operating System Concepts – 9th Edition 7.48 Silberschatz, Galvin and Gagne ©2013
Several Instances of a Resource Type
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 [i][j] = k, then process Pi is
requesting k more instances of resource type Rj.

Operating System Concepts – 9th Edition 7.49 Silberschatz, Galvin and Gagne ©2013
Detection 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
(b) Requesti  Work

If no such i exists, go to step 4

Operating System Concepts – 9th Edition 7.50 Silberschatz, Galvin and Gagne ©2013
Detection Algorithm (Cont.)
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

Algorithm requires an order of O(m x n2) operations to detect


whether the system is in deadlocked state

Operating System Concepts – 9th Edition 7.51 Silberschatz, Galvin and Gagne ©2013
Example of Detection Algorithm
Five processes P0 through P4; three resource types
A (7 instances), B (2 instances), and C (6 instances)

Snapshot at time T0:


Allocation Request Available
ABC ABC ABC
P0 010 000 000
P1 200 202
P2 303 000
P3 211 100
P4 002 002

Sequence <P0, P2, P3, P1, P4> will result in Finish[i] = true for all i

Operating System Concepts – 9th Edition 7.52 Silberschatz, Galvin and Gagne ©2013
Example (Cont.)

P2 requests an additional instance of type C


Request
ABC
P0 000
P1 202
P2 001
P3 100
P4 002

State of system?
Can reclaim resources held by process P0, but insufficient
resources to fulfill other processes; requests
Deadlock exists, consisting of processes P1, P2, P3, and P4

Operating System Concepts – 9th Edition 7.53 Silberschatz, Galvin and Gagne ©2013
Detection-Algorithm Usage
When, and how often, to invoke depends on:
How often 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 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.

Operating System Concepts – 9th Edition 7.54 Silberschatz, Galvin and Gagne ©2013
Recovery from Deadlock: Process Termination

Abort all deadlocked processes

Abort one process at a time until the deadlock cycle is eliminated

In which order should we choose to abort?


1. Priority of the process
2. How long process has computed, and how much longer to
completion
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?

Operating System Concepts – 9th Edition 7.55 Silberschatz, Galvin and Gagne ©2013
Recovery from Deadlock: Resource Preemption

Selecting a victim – minimize cost

Rollback – return to some safe state, restart process for that


state

Starvation – same process may always be picked as victim,


include number of rollback in cost factor

Operating System Concepts – 9th Edition 7.56 Silberschatz, Galvin and Gagne ©2013
End of Chapter 7

Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne ©2013

You might also like