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

Os 08

The document discusses deadlocks in operating systems. It describes the four necessary conditions for deadlock, which are mutual exclusion, hold and wait, no preemption, and circular wait. It also discusses different methods for handling deadlocks, including deadlock prevention, avoidance, and detection and recovery. Deadlock prevention methods restrain the allocation of resources to ensure deadlock conditions cannot occur, while avoidance uses safe state algorithms like the Banker's Algorithm to dynamically monitor the system and prevent entering unsafe states.

Uploaded by

jrahulroy
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views

Os 08

The document discusses deadlocks in operating systems. It describes the four necessary conditions for deadlock, which are mutual exclusion, hold and wait, no preemption, and circular wait. It also discusses different methods for handling deadlocks, including deadlock prevention, avoidance, and detection and recovery. Deadlock prevention methods restrain the allocation of resources to ensure deadlock conditions cannot occur, while avoidance uses safe state algorithms like the Banker's Algorithm to dynamically monitor the system and prevent entering unsafe states.

Uploaded by

jrahulroy
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 32

Deadlocks

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

Prof. D.S.R. Murthy OS-8 Deadlocks 1


The Deadlock Problem
 
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 tape drives.
Two Processes (P1 and P2),
each hold one tape drive and each needs another one.

Prof. D.S.R. Murthy OS-8 Deadlocks 2


System Model
Resource types
R1, R2, . . ., Rm.

CPU cycles
Memory space
I/O devices

Each resource type Ri has Wi instances.

Each process utilizes a resource:


Request
Use
Release

Prof. D.S.R. Murthy OS-8 Deadlocks 3


Necessary Conditions for Deadlock
Deadlock can arise if four conditions hold simultaneously.

Mutual exclusion
Hold and wait
No preemption
Circular wait

Prof. D.S.R. Murthy OS-8 Deadlocks 4


Necessary Conditions for Deadlock

Mutual exclusion
Only one process at a time
can use a resource.

Hold and wait


A process holding at least one resource
is waiting to acquire additional resources held
by other processes.

Prof. D.S.R. Murthy OS-8 Deadlocks 5


Necessary Conditions for Deadlock
No preemption
A resource can be released only voluntarily by
the process holding it,
after that process has completed its task.

Circular wait
There exists a set {P0, P1, …, Pn} 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
Prof. D.S.R. Murthy OS-8 Deadlocks 6
Pn is waiting for a resource that is held by P0.
Resource–Allocation Graph
Set of vertices V
Set of edges E

V is partitioned into two types:


P = {P1, P2, …, Pn}
Set consisting of all the processes in the system.
R = {R1, R2, …, Rm}
Set consisting of all resource types in the system.

Request edge
Directed edge Pi  Rj

Assignment edge
Directed edge Rj  Pi
Prof. D.S.R. Murthy OS-8 Deadlocks 7
Example of Resource Allocation Graph

Prof. D.S.R. Murthy OS-8 Deadlocks 8


Resource Allocation Graph with Deadlock

Prof. D.S.R. Murthy OS-8 Deadlocks 9


Resource Allocation Graph with a Cycle but No Deadlock

Prof. D.S.R. Murthy OS-8 Deadlocks 10


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.

Prof. D.S.R. Murthy OS-8 Deadlocks 11


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.

Prof. D.S.R. Murthy OS-8 Deadlocks 12


Deadlock Prevention
 

Set of methods for ensuring that


atleast one of the necessary conditions cannot hold.
Restrain the ways request can be made.

Mutual Exclusion
Not required for sharable resources.
Must hold for non–sharable resources.

Prof. D.S.R. Murthy OS-8 Deadlocks 13


Deadlock Prevention
 

Hold and Wait


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,
or allow process to request resources
only when the process has none.

Low Resource utilization.


Starvation possible.

Prof. D.S.R. Murthy OS-8 Deadlocks 14


Deadlock Prevention
 

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.

Prof. D.S.R. Murthy OS-8 Deadlocks 15


Deadlock Prevention
 

Circular Wait
Imposes a total ordering of all resource types,
and require that each process requests resources
in an increasing order of enumeration.

Prof. D.S.R. Murthy OS-8 Deadlocks 16


Deadlock Avoidance
Requires that
the system have some additional a priori information
available.

Simplest and most useful model requires that


each process declare the maximum no. of resources of each
type
that it may need.

Deadlock–avoidance algorithm
Dynamically examines the resource-allocation state
to ensure that there can never be a circular-wait
condition.

Resource–allocation state
Defined
Prof. by the no. of available
D.S.R. Murthy and allocated resources, 17
OS-8 Deadlocks
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 safe sequence of all processes.

Sequence <P1, P2, …, Pn> is safe


if 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.
Prof. D.S.R. Murthy OS-8 Deadlocks 18
Safe State
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.
Prof. D.S.R. Murthy OS-8 Deadlocks 19
Safe, Unsafe, Deadlock State

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.

Prof. D.S.R. Murthy OS-8 Deadlocks 20


Resource–Allocation Graph for Deadlock Avoidance

Prof. D.S.R. Murthy OS-8 Deadlocks 21


Unsafe State in Resource–Allocation Graph

Prof. D.S.R. Murthy OS-8 Deadlocks 22


Banker’s Algorithm
When a new process enters the system,
it must declare maximum no. of instances of each resource
type,
it may need.
This no. may not exceed the total no. of resources in the
system.

When a user requests a set of resources,


the system must determine
whether the allocation of the resources,
leave the system in a safe state.
If it will, the resources are allocated,
otherwise, the process must wait until
some other process releases enough resources.

Prof. D.S.R. Murthy OS-8 Deadlocks 23


Data Structures for Banker’s Algorithm

Available No. of available resources of each type.


Max Maximum demand of each process.
Allocation No. of resources currently allocated to each process.
Need Remaining resource need of each process.

Need = Max – Allocation

Prof. D.S.R. Murthy OS-8 Deadlocks 24


Example of Banker’s Algorithm
Consider the following snapshot of a system:
Allocation Max Available
ABC ABC ABC
P0 010 753 332
P1 200 322
P2 302 902
P3 211 222
P4 002 433

a. What is the content of the matrix Need?

b. Is the system in a Safe state?

c. If a request from process P1 arrive for (1, 0, 2) can the request be


granted immediately?
Prof. D.S.R. Murthy OS-8 Deadlocks 25
Example of Banker’s Algorithm
a. Content of the matrix Need

Need = Max – Allocation

Allocation Max Need


ABC ABC ABC
P0 010 753 743
P1 200 322 122
P2 302 902 602
P3 211 222 011
P4 002 433 431

Prof. D.S.R. Murthy OS-8 Deadlocks 26


b. Is the system in a safe state?

If (Need ≤ Available)
Available = Available + Allocated

Allocation Need Available


ABC ABC ABC
P0 010 743 332 755
P1 200 122 532
P2 302 602 10 5 7
P3 211 011 743
P4 002 431 745

Sequence is P1, P3, P4, P0, P2 and the System is in Safe State.

Prof. D.S.R. Murthy OS-8 Deadlocks 27


c. If a request from process P1 arrive for (1, 0, 2) can the request be
granted immediately?
If (Requesti ≤ Needi), if (Requesti ≤ Available)
Available = Available – Requesti
Allocationi = Allocationi + Requesti
Needi = Needi – Requesti i = 1, Request1 = 1 0 2
Allocation Need Available
ABC ABC ABC
P0 010 743 3 3 2 (2 3 2) 7 5 5
P1 2 0 0 (3 0 2) 1 2 2 (0 2 0) 5 3 2
P2 302 602 10 5 7
P3 211 011 743
P4 002 431 745
Sequence P1, P3, P4, P0, P2. and the system is in Safe State.
Hence,
Prof. D.S.R. We
Murthycan immediately grant the request of process P281.
OS-8 Deadlocks
Deadlock Detection
 
Allow system to enter deadlock state.

Detection algorithm.

Recovery scheme.

Prof. D.S.R. Murthy OS-8 Deadlocks 29


Single Instance of Each Resource Type
 

Maintain wait-for graph.


Nodes are processes.
Pi  Pj if Pi is waiting for Pj.
Resource-Allocation Graph and Wait-for Graph

Prof. D.S.R. Murthy OS-8 Deadlocks 30


Recovery from Deadlock: Process Termination
 
Abort all deadlocked processes.

Abort one process at a time until


the deadlock cycle is eliminated.

Prof. D.S.R. Murthy OS-8 Deadlocks 31


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 no. of rollback in cost factor.

Prof. D.S.R. Murthy OS-8 Deadlocks 32

You might also like