SlideShare uma empresa Scribd logo
1 de 53
7.1 Silberschatz, Galvin and Gagne ©2005
Operating System Concepts - 7th Edition, Feb 14, 2005
Deadlocks
n The Deadlock Problem
n System Model
n Deadlock Characterization
n Methods for Handling Deadlocks
n Deadlock Prevention
n Deadlock Avoidance
n Deadlock Detection
n Recovery from Deadlock
7.2 Silberschatz, Galvin and Gagne ©2005
Operating System Concepts - 7th Edition, Feb 14, 2005
.
a system consists of finite number of resources to be distributed
among a number of competing processes.
The resources may be the processor , main , secondary, devices
and data structurers such as files, databases etc.
Resource types
CPU cycles, memory space, I/O devices
A set of processes are said to be in deadlock state if every process
holding some resource and it is waiting for additional resources ,
which are acquired by other processes.
i.e DEADLOCK refers to a specific condition when two or more
processes are each waiting for each other to release a resource ,
or
Waiting for something for infinite time, in which there is NO
PROGRESS for waiting processes.
7.3 Silberschatz, Galvin and Gagne ©2005
Operating System Concepts - 7th Edition, Feb 14, 2005
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)
7.4 Silberschatz, Galvin and Gagne ©2005
Operating System Concepts - 7th Edition, Feb 14, 2005
“ A process request “ the resources, the resources are not availble
at that time , so the process enter in to the waiting state.
The requesting resources are held by another waiting process ,
both are in waiting stae. This situation is called DEADLOCK.
For example P1 and P2 are two processes, R1 and R2 are two
resources .
P1 request the resource R1,
R1 held by by process P2,
P2 request the resource R2, R2 is held by P1, then both are
entered into the waiting state.
There is no work progress for process P1 and P2. it is called
DEADLOCK.
7.5 Silberschatz, Galvin and Gagne ©2005
Operating System Concepts - 7th Edition, Feb 14, 2005
Bridge Crossing Example
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.
Starvation is possible.
7.6 Silberschatz, Galvin and Gagne ©2005
Operating System Concepts - 7th Edition, Feb 14, 2005
System Model
a system consists of finite number of resources to be distributed
among a number of competing processes.
The resources may be the processor , main , secondary, devices
and data structurers such as files, databases etc.
Resource types R1, R2, . . ., Rm
CPU cycles, memory space, I/O devices
Each process utilizes a resource as follows:
request : if the request can not granted immediately , then the
requesting process must wait until it becomes free.
Use : the process can operate on resource.
Release: process releases the resource after performing the
required task.
7.7 Silberschatz, Galvin and Gagne ©2005
Operating System Concepts - 7th Edition, Feb 14, 2005
Deadlock Characterization
Mutual exclusion: at least one resource must be held in non-
sharable mode .
A process can use only one resource at a time.
If another process requests the resource , the requesting process
must wait until the resources has been released.
Example line printers are always in non-sharable mode, only one
process can use the resource at a time.
If we share a printer between two processes simultaneously ,then
we will get , the printing as the combination of those two processes.
Hold and wait: a process holding at least one resource is waiting
to acquire additional resources held by other processes.
P1,P2,P3 are 3 processes. R1,R2,R3,R4 are resources.
Each process holding a resource waiting for another resource.
P1 holding R3 and is waiting for resource R1.
P2 holding R1 and R4, waiting for R2 and R3.
P3 holding R2 waiting for R4.
re are four conditions that must hold simultaneously for there to be a deadlock..
7.8 Silberschatz, Galvin and Gagne ©2005
Operating System Concepts - 7th Edition, Feb 14, 2005
No preemption: resources can not be preempted.
i.E you can not forcefully take any resource from a process.
a resource can be released only voluntarily by the process holding it,
after that process has completed its task.
If the process releases the resource , during the program execution then
we can say that there is no deadlock in our system,
Circular wait: there exists a set {P0, P1, …, P0} of waiting processes
such that P0 is waiting for a resource that is held by P1, P1 is waiting 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 a resource that is held by P0.
7.9 Silberschatz, Galvin and Gagne ©2005
Operating System Concepts - 7th Edition, Feb 14, 2005
Resource-Allocation Graph
A resource allocation graph is directed graph, it is used to
represent the deadlocks.
The graph consists of 2 types of nodes.
One for Process ---represented by circles.
Second is for resources. ---- by squares.
Graph consists of 2 types of edges.
One is requesting edge and the other one is assignment edge.
An edge from process to resource is said to be a requesting edge.
Edge from a resource to a process is said to be an assignment
edge.
Note: if the system is in deadlock state, the resource allocation
graph must consisting of cycles.
P1-> R1 ->P2->R2->P1 then the system may or may not in
deadlock state.
7.10 Silberschatz, Galvin and Gagne ©2005
Operating System Concepts - 7th Edition, Feb 14, 2005
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 P1  Rj
assignment edge – directed edge Rj  Pi
A set of vertices V and a set of edges E.
7.11 Silberschatz, Galvin and Gagne ©2005
Operating System Concepts - 7th Edition, Feb 14, 2005
Resource-Allocation Graph (Cont.)
Process
Resource Type with 4 instances
Pi requests instance of Rj
Pi is holding an instance of Rj
Pi
Pi
Rj
Rj
7.12 Silberschatz, Galvin and Gagne ©2005
Operating System Concepts - 7th Edition, Feb 14, 2005
Example of a Resource Allocation Graph
7.13 Silberschatz, Galvin and Gagne ©2005
Operating System Concepts - 7th Edition, Feb 14, 2005
Resource Allocation Graph With A Deadlock
7.14 Silberschatz, Galvin and Gagne ©2005
Operating System Concepts - 7th Edition, Feb 14, 2005
Graph With A Cycle But No Deadlock
7.15 Silberschatz, Galvin and Gagne ©2005
Operating System Concepts - 7th Edition, Feb 14, 2005
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.
7.16 Silberschatz, Galvin and Gagne ©2005
Operating System Concepts - 7th Edition, Feb 14, 2005
Methods for Handling Deadlocks
Ensure that the system will never enter a deadlock state.
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.
7.17 Silberschatz, Galvin and Gagne ©2005
Operating System Concepts - 7th Edition, Feb 14, 2005
Deadlock Prevention :
Violate any of the 4 necessary conditions at any time & deadlock
can never occur in the system.
Do something , deadlock will never occur in ur system.
If u design a system such a way that atleast one out of the 4
conditions can not hold in the system.
Mutual exclusion :
Atleast one resource in the system should be non-sharable.
i.e only one process can use that particular resorce at one time.
7.18 Silberschatz, Galvin and Gagne ©2005
Operating System Concepts - 7th Edition, Feb 14, 2005
General meaning of prevention is take medicine before the attack of
diseases.
Deadlock prevention is same as take the preventive methods before
attacking the deadlock.
For deadlock to occur ,each of the 4 conditions must hold by ensuring that
atleast one of these conditions can’t hold.
Mutual exclusion:
The mutual exclusion condition must hold for non sharable resources.
For example printer can not be simultaneously shared by several
processes.
Mutual exclusion means only one process can use the resource at a time.
We can deny this condition with simple protocol.
i.e convert the all non-sharable resources to sharable resource.
But it is not easy to make all devices are sharable.
Because some resources are inherently non-sharable .(like Printer)
7.19 Silberschatz, Galvin and Gagne ©2005
Operating System Concepts - 7th Edition, Feb 14, 2005
. So this condition is not satisfied by the deadlock, then we can
prevent the deadlock.
We can not apply this condition if the system consists of printers.
i.e
Mutual exclusion is not required for sharable resources; must hold
for nonsharable resources.
7.20 Silberschatz, Galvin and Gagne ©2005
Operating System Concepts - 7th Edition, Feb 14, 2005
Deadlock Prevention
Hold and Wait –
Hold and wait means each and every processes in the
deadlock state ,must hold atleast one resource and waiting
for at least one resource.
We can deny the condition with a small protocol.
1. allow process to request resources only when the
process has none
2.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,
Low resource utilization; starvation possible.
Restrain the ways request can be made.
7.21 Silberschatz, Galvin and Gagne ©2005
Operating System Concepts - 7th Edition, Feb 14, 2005
Both the protocols have some disadvantages like
Resource utilization is may be low., since the resources may be allocated
bit unused for a long period.
i.e a program requiring ten tape drives must request and receive all ten
drives before it begins executing.
If the program needs only one tape drive to begin execution and does not
need the remaining tap drives for several hours.
And staravation is possible.
No preemption:
NO preemption means you can not forcefully take any resource from a
process.
i.e if we implement
If we can forcefully take any resource from a process .
Process we are taking from Waiting process Resources not from a running
process resources.
7.22 Silberschatz, Galvin and Gagne ©2005
Operating System Concepts - 7th Edition, Feb 14, 2005
A process request some resources , we first check whether they
are available .
If they are available we allocate them. If they are not available ,we
check whether they are allocated to some other process that is
waiting for additional resource.
If so , we preempt the desired resources from the waiting process
and allocate them to the requesting process.
If the resources are not available or held by a waiting process ,
then the requesting process must wait.
While waiting some of the resources may be used by other
process.
7.23 Silberschatz, Galvin and Gagne ©2005
Operating System Concepts - 7th Edition, Feb 14, 2005
Deadlock Prevention (Cont.)
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.
Circular Wait – impose a total ordering of all resource types,
and require that each process requests resources in an
increasing order of enumeration.
7.24 Silberschatz, Galvin and Gagne ©2005
Operating System Concepts - 7th Edition, Feb 14, 2005
Deadlock Avoidance
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.
Requires that the system has some additional a priori information
available.
7.25 Silberschatz, Galvin and Gagne ©2005
Operating System Concepts - 7th Edition, Feb 14, 2005
When a process requests an available resource, system must
decide if immediate allocation leaves the system in a safe state.
If the state is safe .the system allocate the resources to the
requesting process.
Otherwise (unsafe) do not allocate the resources.
So taking care before the allocation is said to be DEADLOCK
AVOIDENCE.
Safe State means “ no deadlock will happen, even we allocate the
resources to the requesting processes.
Unsafe sate means the deadlocks may happen if grant the
resources’
7.26 Silberschatz, Galvin and Gagne ©2005
Operating System Concepts - 7th Edition, Feb 14, 2005
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.
7.27 Silberschatz, Galvin and Gagne ©2005
Operating System Concepts - 7th Edition, Feb 14, 2005
Safe, Unsafe , Deadlock State
7.28 Silberschatz, Galvin and Gagne ©2005
Operating System Concepts - 7th Edition, Feb 14, 2005
Avoidance algorithms
Single instance of a resource type. Use a resource-
allocation graph
Multiple instances of a resource type. Use the banker’s
algorithm
7.29 Silberschatz, Galvin and Gagne ©2005
Operating System Concepts - 7th Edition, Feb 14, 2005
Resource-Allocation Graph Scheme
Claim edge Pj  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.
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.
7.30 Silberschatz, Galvin and Gagne ©2005
Operating System Concepts - 7th Edition, Feb 14, 2005
Resource-Allocation Graph
7.31 Silberschatz, Galvin and Gagne ©2005
Operating System Concepts - 7th Edition, Feb 14, 2005
Unsafe State In Resource-Allocation Graph
7.32 Silberschatz, Galvin and Gagne ©2005
Operating System Concepts - 7th Edition, Feb 14, 2005
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
7.33 Silberschatz, Galvin and Gagne ©2005
Operating System Concepts - 7th Edition, Feb 14, 2005
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.
7.34 Silberschatz, Galvin and Gagne ©2005
Operating System Concepts - 7th Edition, Feb 14, 2005
To apply Banker’s algorithm , some data should be known in
advance like
i) how many instances of each resource ,
each process can Max request [Max]
Each process currently holds [Allocation]
Available instances [ Available]
7.35 Silberschatz, Galvin and Gagne ©2005
Operating System Concepts - 7th Edition, Feb 14, 2005
Data Structures for the Banker’s Algorithm
Available: A Vector of length m indicates the availble resources
of each type.. If available [j] = k, there are k instances of resource
type Rj available.
Max: an n x m matrix defines the maximum demand of each
process.. If Max [i,j] = k, then process Pi may request at most k
instances of resource type Rj.
Allocation: n x m matrix defines the number of resources of each
type currently alllocated to each process.. If Allocation[i,j] = k then
Pi is currently allocated k instances of Rj.
Need: n x m matrix indicates the remaining resource need of each
process.. 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].
Let n = number of processes, and m = number of resources types.
7.36 Silberschatz, Galvin and Gagne ©2005
Operating System Concepts - 7th Edition, Feb 14, 2005
Safety Algorithm
1. Let Work and Finish be vectors of length m and n,
respectively. Initialize:
Work = Available
Finish [i] = false for i = 0, 1, …, n- 1.
2. Find an i such that both:
(a) Finish [i] = false
(b) Needi  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] == true for all i, then the system is in a safe
state.
7.37 Silberschatz, Galvin and Gagne ©2005
Operating System Concepts - 7th Edition, Feb 14, 2005
Resource-Request Algorithm for Process Pi
Request = 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 – Request;
Allocationi = Allocationi + Requesti;
Needi = Needi – Requesti;
If safe  the resources are allocated to Pi.
If unsafe  Pi must wait, and the old resource-allocation
state is restored
7.38 Silberschatz, Galvin and Gagne ©2005
Operating System Concepts - 7th Edition, Feb 14, 2005
Or
N= no. of processes ;M= no. of resources
Input :
Processes ,Any 2 out of 3 ( Max, Need ,Allocation),Available / Total
number of resources
Algorithm:
Step1 : flag [i] =0 for i=0 to n-1 & find need [n][m] = Max [n][m]-
allocation [n][m]
Stpe2: find a process P I such that :- flag[i] =0 & Need i <=
Available
Step3: if such I exists then
 Flag [i] =1 , Available = available + allocation
 Go to step2
Step 4: if flag[i] = 1 for all I
Then the system is in safe state otherwise unsafe state.
7.39 Silberschatz, Galvin and Gagne ©2005
Operating System Concepts - 7th Edition, Feb 14, 2005
Example of Banker’s Algorithm
5 processes P0 through P4;
3 resource types:
A (10 instances), B (5instances), and C (7 instances).
Snapshot at time T0:
Allocation Max Available
A B C A B C A B C
P0 0 1 0 7 5 3 3 3 2
P1 2 0 0 3 2 2
P2 3 0 2 9 0 2
P3 2 1 1 2 2 2
P4 0 0 2 4 3 3
7.40 Silberschatz, Galvin and Gagne ©2005
Operating System Concepts - 7th Edition, Feb 14, 2005
Example (Cont.)
The content of the matrix Need is defined to be Max – Allocation.
Need
A B C
P0 7 4 3
P1 1 2 2
P2 6 0 0
P3 0 1 1
P4 4 3 1
The system is in a safe state since the sequence < P1, P3, P4, P2, P0>
satisfies safety criteria.
7.41 Silberschatz, Galvin and Gagne ©2005
Operating System Concepts - 7th Edition, Feb 14, 2005
Example: P1 Request (1,0,2)
Check that Request  Available (that is, (1,0,2)  (3,3,2)  true.
Allocation Need Available
A B C A B C A B C
P0 0 1 0 7 4 3 2 3 0
P1 3 0 2 0 2 0
P2 3 0 1 6 0 0
P3 2 1 1 0 1 1
P4 0 0 2 4 3 1
Executing safety algorithm shows 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 P0 be granted?
7.42 Silberschatz, Galvin and Gagne ©2005
Operating System Concepts - 7th Edition, Feb 14, 2005
Deadlock Detection
Allow system to enter deadlock state
Detection algorithm
Recovery scheme
7.43 Silberschatz, Galvin and Gagne ©2005
Operating System Concepts - 7th Edition, Feb 14, 2005
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.
7.44 Silberschatz, Galvin and Gagne ©2005
Operating System Concepts - 7th Edition, Feb 14, 2005
Resource-Allocation Graph and Wait-for Graph
Resource-Allocation Graph Corresponding wait-for graph
7.45 Silberschatz, Galvin and Gagne ©2005
Operating System Concepts - 7th Edition, Feb 14, 2005
Several Instances of a Resource Type
We can define a deadlock detection algorithm that uses a
variant of the resource allocation graph called as wait fro graph
when all resources have only a single instance.
an edge from Pi PJ in a wait fro graph exists if and only if the
corresponding resource allocation graph contains 2 edges Pi
Rq and Rq  Pi for some resouce Rq.
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.
7.46 Silberschatz, Galvin and Gagne ©2005
Operating System Concepts - 7th Edition, Feb 14, 2005
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.
7.47 Silberschatz, Galvin and Gagne ©2005
Operating System Concepts - 7th Edition, Feb 14, 2005
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.
7.48 Silberschatz, Galvin and Gagne ©2005
Operating System Concepts - 7th Edition, Feb 14, 2005
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
A B C A B C A B C
P0 0 1 0 0 0 0 0 0 0
P1 2 0 0 2 0 2
P2 3 0 3 0 0 0
P3 2 1 1 1 0 0
P4 0 0 2 0 0 2
Sequence <P0, P2, P3, P1, P4> will result in Finish[i] = true for all i.
7.49 Silberschatz, Galvin and Gagne ©2005
Operating System Concepts - 7th Edition, Feb 14, 2005
Example (Cont.)
P2 requests an additional instance of type C.
Request
A B C
P0 0 0 0
P1 2 0 1
P2 0 0 1
P3 1 0 0
P4 0 0 2
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.
7.50 Silberschatz, Galvin and Gagne ©2005
Operating System Concepts - 7th Edition, Feb 14, 2005
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.
7.51 Silberschatz, Galvin and Gagne ©2005
Operating System Concepts - 7th Edition, Feb 14, 2005
When a detection algorithm determines a deadlock exists
Solutions :
Inform the operator that a deadlock has occurred, the
operator deal with the deadlock manually.
or
Let the system recover from the deadlock automatically.
i.e
1 .Abort one or more processes to break the circular wait.
2. Preempt some resources from one or more of the
deadlocked processes.
7.52 Silberschatz, Galvin and Gagne ©2005
Operating System Concepts - 7th Edition, Feb 14, 2005
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?
Priority of the process.
How long process has computed, and how much longer to
completion.
How many and what type of Resources the process has used.
How many more Resources the process needs to complete.
How many processes will need to be terminated.
Is process interactive or batch?
7.53 Silberschatz, Galvin and Gagne ©2005
Operating System Concepts - 7th Edition, Feb 14, 2005
Recovery from Deadlock: Resource Preemption
To eliminate deadlocks using resource preemption, we
successively preempt some resources from processes and give
these resources to the other processes until the deadlock cycle is
broken.
If preemption is required to deal with deadlocks ,3 issues need to
be addresses.
Selecting a victim – minimize cost.
Rollback – we must rollback the process to some safe state and
restart it from that state.
Starvation – same process may always be picked as victim based
on cost factors etc ...this process never completes its task.

Mais conteúdo relacionado

Semelhante a An brief introduction to Deadlocks in OS

Semelhante a An brief introduction to Deadlocks in OS (20)

deadlock.pptx
deadlock.pptxdeadlock.pptx
deadlock.pptx
 
ch7.ppt
ch7.pptch7.ppt
ch7.ppt
 
Galvin-operating System(Ch8)
Galvin-operating System(Ch8)Galvin-operating System(Ch8)
Galvin-operating System(Ch8)
 
Ch7
Ch7Ch7
Ch7
 
Ch7
Ch7Ch7
Ch7
 
ch7- Deadlock.ppt
ch7- Deadlock.pptch7- Deadlock.ppt
ch7- Deadlock.ppt
 
Chapter 7
Chapter 7Chapter 7
Chapter 7
 
chp1_merged.pdf
chp1_merged.pdfchp1_merged.pdf
chp1_merged.pdf
 
7.Dead Locks
7.Dead Locks7.Dead Locks
7.Dead Locks
 
Deadlock : operating system ( BTECH CSE )
Deadlock : operating system ( BTECH CSE )Deadlock : operating system ( BTECH CSE )
Deadlock : operating system ( BTECH CSE )
 
Dead Lock In Operating Systems
Dead Lock In Operating SystemsDead Lock In Operating Systems
Dead Lock In Operating Systems
 
5 process synchronization
5 process synchronization5 process synchronization
5 process synchronization
 
Chapter 8 Operating Systems silberschatz : deadlocks
Chapter 8 Operating Systems silberschatz : deadlocksChapter 8 Operating Systems silberschatz : deadlocks
Chapter 8 Operating Systems silberschatz : deadlocks
 
ch07.pdf
ch07.pdfch07.pdf
ch07.pdf
 
Deadlocks 160928121516-160928183232
Deadlocks 160928121516-160928183232Deadlocks 160928121516-160928183232
Deadlocks 160928121516-160928183232
 
Operating System Deadlock Galvin
Operating System  Deadlock GalvinOperating System  Deadlock Galvin
Operating System Deadlock Galvin
 
Deadlocks
DeadlocksDeadlocks
Deadlocks
 
Deadlocks final
Deadlocks finalDeadlocks final
Deadlocks final
 
Ch8: Deadlocks
Ch8: DeadlocksCh8: Deadlocks
Ch8: Deadlocks
 
Ch8
Ch8Ch8
Ch8
 

Mais de RaviKiranVarma4

ppt-U3 - (Programming, Memory Interfacing).pptx
ppt-U3 - (Programming, Memory Interfacing).pptxppt-U3 - (Programming, Memory Interfacing).pptx
ppt-U3 - (Programming, Memory Interfacing).pptxRaviKiranVarma4
 
ppt-U2 - (Instruction Set of 8086, Simple programs).pptx
ppt-U2 - (Instruction Set of 8086, Simple programs).pptxppt-U2 - (Instruction Set of 8086, Simple programs).pptx
ppt-U2 - (Instruction Set of 8086, Simple programs).pptxRaviKiranVarma4
 
ppt-U1 - (Introduction to 8086, Instruction Set).pptx
ppt-U1 - (Introduction to 8086, Instruction Set).pptxppt-U1 - (Introduction to 8086, Instruction Set).pptx
ppt-U1 - (Introduction to 8086, Instruction Set).pptxRaviKiranVarma4
 
MPI UNIT 5 - (INTERRUPTS OF 8086, INTRODUCTION TO 8051).pptx
MPI UNIT 5 - (INTERRUPTS OF 8086, INTRODUCTION TO 8051).pptxMPI UNIT 5 - (INTERRUPTS OF 8086, INTRODUCTION TO 8051).pptx
MPI UNIT 5 - (INTERRUPTS OF 8086, INTRODUCTION TO 8051).pptxRaviKiranVarma4
 
MPI UNIT 4 - (Introduction to DMA and ADC)
MPI UNIT 4 - (Introduction to DMA and ADC)MPI UNIT 4 - (Introduction to DMA and ADC)
MPI UNIT 4 - (Introduction to DMA and ADC)RaviKiranVarma4
 
PPT ON INTRODUCTION TO AI- UNIT-1-PART-1.pptx
PPT ON INTRODUCTION TO AI- UNIT-1-PART-1.pptxPPT ON INTRODUCTION TO AI- UNIT-1-PART-1.pptx
PPT ON INTRODUCTION TO AI- UNIT-1-PART-1.pptxRaviKiranVarma4
 
PPT ON INTRODUCTION TO AI- UNIT-1-PART-2.pptx
PPT ON INTRODUCTION TO AI- UNIT-1-PART-2.pptxPPT ON INTRODUCTION TO AI- UNIT-1-PART-2.pptx
PPT ON INTRODUCTION TO AI- UNIT-1-PART-2.pptxRaviKiranVarma4
 
PPT ON INTRODUCTION TO AI- UNIT-1-PART-3.pptx
PPT ON INTRODUCTION TO AI- UNIT-1-PART-3.pptxPPT ON INTRODUCTION TO AI- UNIT-1-PART-3.pptx
PPT ON INTRODUCTION TO AI- UNIT-1-PART-3.pptxRaviKiranVarma4
 
ppt on introduction to Machine learning tools
ppt on introduction to Machine learning toolsppt on introduction to Machine learning tools
ppt on introduction to Machine learning toolsRaviKiranVarma4
 
prof Elective Technical Report Writing.ppt
prof Elective Technical Report Writing.pptprof Elective Technical Report Writing.ppt
prof Elective Technical Report Writing.pptRaviKiranVarma4
 
UNIT 3.2 -Mining Frquent Patterns (part1).ppt
UNIT 3.2 -Mining Frquent Patterns (part1).pptUNIT 3.2 -Mining Frquent Patterns (part1).ppt
UNIT 3.2 -Mining Frquent Patterns (part1).pptRaviKiranVarma4
 
Harmony in the Family- Understanding the Relationship.pptx
Harmony in the Family- Understanding the Relationship.pptxHarmony in the Family- Understanding the Relationship.pptx
Harmony in the Family- Understanding the Relationship.pptxRaviKiranVarma4
 
Basic aspiration,Contineous Happiness and Prosperous- Right Understanding.pptx
Basic aspiration,Contineous Happiness and Prosperous- Right Understanding.pptxBasic aspiration,Contineous Happiness and Prosperous- Right Understanding.pptx
Basic aspiration,Contineous Happiness and Prosperous- Right Understanding.pptxRaviKiranVarma4
 
Contineous Happiness and Method of fulfillment.pptx
Contineous Happiness and Method of fulfillment.pptxContineous Happiness and Method of fulfillment.pptx
Contineous Happiness and Method of fulfillment.pptxRaviKiranVarma4
 

Mais de RaviKiranVarma4 (14)

ppt-U3 - (Programming, Memory Interfacing).pptx
ppt-U3 - (Programming, Memory Interfacing).pptxppt-U3 - (Programming, Memory Interfacing).pptx
ppt-U3 - (Programming, Memory Interfacing).pptx
 
ppt-U2 - (Instruction Set of 8086, Simple programs).pptx
ppt-U2 - (Instruction Set of 8086, Simple programs).pptxppt-U2 - (Instruction Set of 8086, Simple programs).pptx
ppt-U2 - (Instruction Set of 8086, Simple programs).pptx
 
ppt-U1 - (Introduction to 8086, Instruction Set).pptx
ppt-U1 - (Introduction to 8086, Instruction Set).pptxppt-U1 - (Introduction to 8086, Instruction Set).pptx
ppt-U1 - (Introduction to 8086, Instruction Set).pptx
 
MPI UNIT 5 - (INTERRUPTS OF 8086, INTRODUCTION TO 8051).pptx
MPI UNIT 5 - (INTERRUPTS OF 8086, INTRODUCTION TO 8051).pptxMPI UNIT 5 - (INTERRUPTS OF 8086, INTRODUCTION TO 8051).pptx
MPI UNIT 5 - (INTERRUPTS OF 8086, INTRODUCTION TO 8051).pptx
 
MPI UNIT 4 - (Introduction to DMA and ADC)
MPI UNIT 4 - (Introduction to DMA and ADC)MPI UNIT 4 - (Introduction to DMA and ADC)
MPI UNIT 4 - (Introduction to DMA and ADC)
 
PPT ON INTRODUCTION TO AI- UNIT-1-PART-1.pptx
PPT ON INTRODUCTION TO AI- UNIT-1-PART-1.pptxPPT ON INTRODUCTION TO AI- UNIT-1-PART-1.pptx
PPT ON INTRODUCTION TO AI- UNIT-1-PART-1.pptx
 
PPT ON INTRODUCTION TO AI- UNIT-1-PART-2.pptx
PPT ON INTRODUCTION TO AI- UNIT-1-PART-2.pptxPPT ON INTRODUCTION TO AI- UNIT-1-PART-2.pptx
PPT ON INTRODUCTION TO AI- UNIT-1-PART-2.pptx
 
PPT ON INTRODUCTION TO AI- UNIT-1-PART-3.pptx
PPT ON INTRODUCTION TO AI- UNIT-1-PART-3.pptxPPT ON INTRODUCTION TO AI- UNIT-1-PART-3.pptx
PPT ON INTRODUCTION TO AI- UNIT-1-PART-3.pptx
 
ppt on introduction to Machine learning tools
ppt on introduction to Machine learning toolsppt on introduction to Machine learning tools
ppt on introduction to Machine learning tools
 
prof Elective Technical Report Writing.ppt
prof Elective Technical Report Writing.pptprof Elective Technical Report Writing.ppt
prof Elective Technical Report Writing.ppt
 
UNIT 3.2 -Mining Frquent Patterns (part1).ppt
UNIT 3.2 -Mining Frquent Patterns (part1).pptUNIT 3.2 -Mining Frquent Patterns (part1).ppt
UNIT 3.2 -Mining Frquent Patterns (part1).ppt
 
Harmony in the Family- Understanding the Relationship.pptx
Harmony in the Family- Understanding the Relationship.pptxHarmony in the Family- Understanding the Relationship.pptx
Harmony in the Family- Understanding the Relationship.pptx
 
Basic aspiration,Contineous Happiness and Prosperous- Right Understanding.pptx
Basic aspiration,Contineous Happiness and Prosperous- Right Understanding.pptxBasic aspiration,Contineous Happiness and Prosperous- Right Understanding.pptx
Basic aspiration,Contineous Happiness and Prosperous- Right Understanding.pptx
 
Contineous Happiness and Method of fulfillment.pptx
Contineous Happiness and Method of fulfillment.pptxContineous Happiness and Method of fulfillment.pptx
Contineous Happiness and Method of fulfillment.pptx
 

Último

SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...M56BOOKSTORE PRODUCT/SERVICE
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfakmcokerachita
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docxPoojaSen20
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 

Último (20)

SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdf
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docx
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 

An brief introduction to Deadlocks in OS

  • 1. 7.1 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7th Edition, Feb 14, 2005 Deadlocks n The Deadlock Problem n System Model n Deadlock Characterization n Methods for Handling Deadlocks n Deadlock Prevention n Deadlock Avoidance n Deadlock Detection n Recovery from Deadlock
  • 2. 7.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7th Edition, Feb 14, 2005 . a system consists of finite number of resources to be distributed among a number of competing processes. The resources may be the processor , main , secondary, devices and data structurers such as files, databases etc. Resource types CPU cycles, memory space, I/O devices A set of processes are said to be in deadlock state if every process holding some resource and it is waiting for additional resources , which are acquired by other processes. i.e DEADLOCK refers to a specific condition when two or more processes are each waiting for each other to release a resource , or Waiting for something for infinite time, in which there is NO PROGRESS for waiting processes.
  • 3. 7.3 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7th Edition, Feb 14, 2005 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)
  • 4. 7.4 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7th Edition, Feb 14, 2005 “ A process request “ the resources, the resources are not availble at that time , so the process enter in to the waiting state. The requesting resources are held by another waiting process , both are in waiting stae. This situation is called DEADLOCK. For example P1 and P2 are two processes, R1 and R2 are two resources . P1 request the resource R1, R1 held by by process P2, P2 request the resource R2, R2 is held by P1, then both are entered into the waiting state. There is no work progress for process P1 and P2. it is called DEADLOCK.
  • 5. 7.5 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7th Edition, Feb 14, 2005 Bridge Crossing Example 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. Starvation is possible.
  • 6. 7.6 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7th Edition, Feb 14, 2005 System Model a system consists of finite number of resources to be distributed among a number of competing processes. The resources may be the processor , main , secondary, devices and data structurers such as files, databases etc. Resource types R1, R2, . . ., Rm CPU cycles, memory space, I/O devices Each process utilizes a resource as follows: request : if the request can not granted immediately , then the requesting process must wait until it becomes free. Use : the process can operate on resource. Release: process releases the resource after performing the required task.
  • 7. 7.7 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7th Edition, Feb 14, 2005 Deadlock Characterization Mutual exclusion: at least one resource must be held in non- sharable mode . A process can use only one resource at a time. If another process requests the resource , the requesting process must wait until the resources has been released. Example line printers are always in non-sharable mode, only one process can use the resource at a time. If we share a printer between two processes simultaneously ,then we will get , the printing as the combination of those two processes. Hold and wait: a process holding at least one resource is waiting to acquire additional resources held by other processes. P1,P2,P3 are 3 processes. R1,R2,R3,R4 are resources. Each process holding a resource waiting for another resource. P1 holding R3 and is waiting for resource R1. P2 holding R1 and R4, waiting for R2 and R3. P3 holding R2 waiting for R4. re are four conditions that must hold simultaneously for there to be a deadlock..
  • 8. 7.8 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7th Edition, Feb 14, 2005 No preemption: resources can not be preempted. i.E you can not forcefully take any resource from a process. a resource can be released only voluntarily by the process holding it, after that process has completed its task. If the process releases the resource , during the program execution then we can say that there is no deadlock in our system, Circular wait: there exists a set {P0, P1, …, P0} of waiting processes such that P0 is waiting for a resource that is held by P1, P1 is waiting 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 a resource that is held by P0.
  • 9. 7.9 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7th Edition, Feb 14, 2005 Resource-Allocation Graph A resource allocation graph is directed graph, it is used to represent the deadlocks. The graph consists of 2 types of nodes. One for Process ---represented by circles. Second is for resources. ---- by squares. Graph consists of 2 types of edges. One is requesting edge and the other one is assignment edge. An edge from process to resource is said to be a requesting edge. Edge from a resource to a process is said to be an assignment edge. Note: if the system is in deadlock state, the resource allocation graph must consisting of cycles. P1-> R1 ->P2->R2->P1 then the system may or may not in deadlock state.
  • 10. 7.10 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7th Edition, Feb 14, 2005 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 P1  Rj assignment edge – directed edge Rj  Pi A set of vertices V and a set of edges E.
  • 11. 7.11 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7th Edition, Feb 14, 2005 Resource-Allocation Graph (Cont.) Process Resource Type with 4 instances Pi requests instance of Rj Pi is holding an instance of Rj Pi Pi Rj Rj
  • 12. 7.12 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7th Edition, Feb 14, 2005 Example of a Resource Allocation Graph
  • 13. 7.13 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7th Edition, Feb 14, 2005 Resource Allocation Graph With A Deadlock
  • 14. 7.14 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7th Edition, Feb 14, 2005 Graph With A Cycle But No Deadlock
  • 15. 7.15 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7th Edition, Feb 14, 2005 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.
  • 16. 7.16 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7th Edition, Feb 14, 2005 Methods for Handling Deadlocks Ensure that the system will never enter a deadlock state. 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.
  • 17. 7.17 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7th Edition, Feb 14, 2005 Deadlock Prevention : Violate any of the 4 necessary conditions at any time & deadlock can never occur in the system. Do something , deadlock will never occur in ur system. If u design a system such a way that atleast one out of the 4 conditions can not hold in the system. Mutual exclusion : Atleast one resource in the system should be non-sharable. i.e only one process can use that particular resorce at one time.
  • 18. 7.18 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7th Edition, Feb 14, 2005 General meaning of prevention is take medicine before the attack of diseases. Deadlock prevention is same as take the preventive methods before attacking the deadlock. For deadlock to occur ,each of the 4 conditions must hold by ensuring that atleast one of these conditions can’t hold. Mutual exclusion: The mutual exclusion condition must hold for non sharable resources. For example printer can not be simultaneously shared by several processes. Mutual exclusion means only one process can use the resource at a time. We can deny this condition with simple protocol. i.e convert the all non-sharable resources to sharable resource. But it is not easy to make all devices are sharable. Because some resources are inherently non-sharable .(like Printer)
  • 19. 7.19 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7th Edition, Feb 14, 2005 . So this condition is not satisfied by the deadlock, then we can prevent the deadlock. We can not apply this condition if the system consists of printers. i.e Mutual exclusion is not required for sharable resources; must hold for nonsharable resources.
  • 20. 7.20 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7th Edition, Feb 14, 2005 Deadlock Prevention Hold and Wait – Hold and wait means each and every processes in the deadlock state ,must hold atleast one resource and waiting for at least one resource. We can deny the condition with a small protocol. 1. allow process to request resources only when the process has none 2.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, Low resource utilization; starvation possible. Restrain the ways request can be made.
  • 21. 7.21 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7th Edition, Feb 14, 2005 Both the protocols have some disadvantages like Resource utilization is may be low., since the resources may be allocated bit unused for a long period. i.e a program requiring ten tape drives must request and receive all ten drives before it begins executing. If the program needs only one tape drive to begin execution and does not need the remaining tap drives for several hours. And staravation is possible. No preemption: NO preemption means you can not forcefully take any resource from a process. i.e if we implement If we can forcefully take any resource from a process . Process we are taking from Waiting process Resources not from a running process resources.
  • 22. 7.22 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7th Edition, Feb 14, 2005 A process request some resources , we first check whether they are available . If they are available we allocate them. If they are not available ,we check whether they are allocated to some other process that is waiting for additional resource. If so , we preempt the desired resources from the waiting process and allocate them to the requesting process. If the resources are not available or held by a waiting process , then the requesting process must wait. While waiting some of the resources may be used by other process.
  • 23. 7.23 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7th Edition, Feb 14, 2005 Deadlock Prevention (Cont.) 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. Circular Wait – impose a total ordering of all resource types, and require that each process requests resources in an increasing order of enumeration.
  • 24. 7.24 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7th Edition, Feb 14, 2005 Deadlock Avoidance 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. Requires that the system has some additional a priori information available.
  • 25. 7.25 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7th Edition, Feb 14, 2005 When a process requests an available resource, system must decide if immediate allocation leaves the system in a safe state. If the state is safe .the system allocate the resources to the requesting process. Otherwise (unsafe) do not allocate the resources. So taking care before the allocation is said to be DEADLOCK AVOIDENCE. Safe State means “ no deadlock will happen, even we allocate the resources to the requesting processes. Unsafe sate means the deadlocks may happen if grant the resources’
  • 26. 7.26 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7th Edition, Feb 14, 2005 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.
  • 27. 7.27 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7th Edition, Feb 14, 2005 Safe, Unsafe , Deadlock State
  • 28. 7.28 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7th Edition, Feb 14, 2005 Avoidance algorithms Single instance of a resource type. Use a resource- allocation graph Multiple instances of a resource type. Use the banker’s algorithm
  • 29. 7.29 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7th Edition, Feb 14, 2005 Resource-Allocation Graph Scheme Claim edge Pj  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. 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.
  • 30. 7.30 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7th Edition, Feb 14, 2005 Resource-Allocation Graph
  • 31. 7.31 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7th Edition, Feb 14, 2005 Unsafe State In Resource-Allocation Graph
  • 32. 7.32 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7th Edition, Feb 14, 2005 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
  • 33. 7.33 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7th Edition, Feb 14, 2005 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.
  • 34. 7.34 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7th Edition, Feb 14, 2005 To apply Banker’s algorithm , some data should be known in advance like i) how many instances of each resource , each process can Max request [Max] Each process currently holds [Allocation] Available instances [ Available]
  • 35. 7.35 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7th Edition, Feb 14, 2005 Data Structures for the Banker’s Algorithm Available: A Vector of length m indicates the availble resources of each type.. If available [j] = k, there are k instances of resource type Rj available. Max: an n x m matrix defines the maximum demand of each process.. If Max [i,j] = k, then process Pi may request at most k instances of resource type Rj. Allocation: n x m matrix defines the number of resources of each type currently alllocated to each process.. If Allocation[i,j] = k then Pi is currently allocated k instances of Rj. Need: n x m matrix indicates the remaining resource need of each process.. 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]. Let n = number of processes, and m = number of resources types.
  • 36. 7.36 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7th Edition, Feb 14, 2005 Safety Algorithm 1. Let Work and Finish be vectors of length m and n, respectively. Initialize: Work = Available Finish [i] = false for i = 0, 1, …, n- 1. 2. Find an i such that both: (a) Finish [i] = false (b) Needi  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] == true for all i, then the system is in a safe state.
  • 37. 7.37 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7th Edition, Feb 14, 2005 Resource-Request Algorithm for Process Pi Request = 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 – Request; Allocationi = Allocationi + Requesti; Needi = Needi – Requesti; If safe  the resources are allocated to Pi. If unsafe  Pi must wait, and the old resource-allocation state is restored
  • 38. 7.38 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7th Edition, Feb 14, 2005 Or N= no. of processes ;M= no. of resources Input : Processes ,Any 2 out of 3 ( Max, Need ,Allocation),Available / Total number of resources Algorithm: Step1 : flag [i] =0 for i=0 to n-1 & find need [n][m] = Max [n][m]- allocation [n][m] Stpe2: find a process P I such that :- flag[i] =0 & Need i <= Available Step3: if such I exists then  Flag [i] =1 , Available = available + allocation  Go to step2 Step 4: if flag[i] = 1 for all I Then the system is in safe state otherwise unsafe state.
  • 39. 7.39 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7th Edition, Feb 14, 2005 Example of Banker’s Algorithm 5 processes P0 through P4; 3 resource types: A (10 instances), B (5instances), and C (7 instances). Snapshot at time T0: Allocation Max Available A B C A B C A B C P0 0 1 0 7 5 3 3 3 2 P1 2 0 0 3 2 2 P2 3 0 2 9 0 2 P3 2 1 1 2 2 2 P4 0 0 2 4 3 3
  • 40. 7.40 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7th Edition, Feb 14, 2005 Example (Cont.) The content of the matrix Need is defined to be Max – Allocation. Need A B C P0 7 4 3 P1 1 2 2 P2 6 0 0 P3 0 1 1 P4 4 3 1 The system is in a safe state since the sequence < P1, P3, P4, P2, P0> satisfies safety criteria.
  • 41. 7.41 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7th Edition, Feb 14, 2005 Example: P1 Request (1,0,2) Check that Request  Available (that is, (1,0,2)  (3,3,2)  true. Allocation Need Available A B C A B C A B C P0 0 1 0 7 4 3 2 3 0 P1 3 0 2 0 2 0 P2 3 0 1 6 0 0 P3 2 1 1 0 1 1 P4 0 0 2 4 3 1 Executing safety algorithm shows 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 P0 be granted?
  • 42. 7.42 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7th Edition, Feb 14, 2005 Deadlock Detection Allow system to enter deadlock state Detection algorithm Recovery scheme
  • 43. 7.43 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7th Edition, Feb 14, 2005 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.
  • 44. 7.44 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7th Edition, Feb 14, 2005 Resource-Allocation Graph and Wait-for Graph Resource-Allocation Graph Corresponding wait-for graph
  • 45. 7.45 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7th Edition, Feb 14, 2005 Several Instances of a Resource Type We can define a deadlock detection algorithm that uses a variant of the resource allocation graph called as wait fro graph when all resources have only a single instance. an edge from Pi PJ in a wait fro graph exists if and only if the corresponding resource allocation graph contains 2 edges Pi Rq and Rq  Pi for some resouce Rq. 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.
  • 46. 7.46 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7th Edition, Feb 14, 2005 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.
  • 47. 7.47 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7th Edition, Feb 14, 2005 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.
  • 48. 7.48 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7th Edition, Feb 14, 2005 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 A B C A B C A B C P0 0 1 0 0 0 0 0 0 0 P1 2 0 0 2 0 2 P2 3 0 3 0 0 0 P3 2 1 1 1 0 0 P4 0 0 2 0 0 2 Sequence <P0, P2, P3, P1, P4> will result in Finish[i] = true for all i.
  • 49. 7.49 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7th Edition, Feb 14, 2005 Example (Cont.) P2 requests an additional instance of type C. Request A B C P0 0 0 0 P1 2 0 1 P2 0 0 1 P3 1 0 0 P4 0 0 2 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.
  • 50. 7.50 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7th Edition, Feb 14, 2005 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.
  • 51. 7.51 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7th Edition, Feb 14, 2005 When a detection algorithm determines a deadlock exists Solutions : Inform the operator that a deadlock has occurred, the operator deal with the deadlock manually. or Let the system recover from the deadlock automatically. i.e 1 .Abort one or more processes to break the circular wait. 2. Preempt some resources from one or more of the deadlocked processes.
  • 52. 7.52 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7th Edition, Feb 14, 2005 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? Priority of the process. How long process has computed, and how much longer to completion. How many and what type of Resources the process has used. How many more Resources the process needs to complete. How many processes will need to be terminated. Is process interactive or batch?
  • 53. 7.53 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7th Edition, Feb 14, 2005 Recovery from Deadlock: Resource Preemption To eliminate deadlocks using resource preemption, we successively preempt some resources from processes and give these resources to the other processes until the deadlock cycle is broken. If preemption is required to deal with deadlocks ,3 issues need to be addresses. Selecting a victim – minimize cost. Rollback – we must rollback the process to some safe state and restart it from that state. Starvation – same process may always be picked as victim based on cost factors etc ...this process never completes its task.