Module3 2 OS
Module3 2 OS
Introduction
In a computer system, we have a finite number of
resources to be distributed among a number of
competing processes.
• Physical Resources
• Logical Resources
Introduction continue…
In a computer system, we have a finite number of
resources to be distributed among a number of
competing processes.
• Printers
• Tape drivers
System resources are classified into:
• Memory
space
• Physical Resources
• CPU cycles
• Logical Resources
Introduction continue…
In a computer system, we have a finite number of
resources to be distributed among a number of
competing processes.
• Request
• Use
The requesting process can operate
• Release on the resource.
Introduction continue…
In a normal operation, a process may utilize a resource
in the following sequence:
• Request
• Use
The process releases the resource
• Release after using it.
The OS is responsible for making sure that the
requesting process has been allocated the
resource. Request and release of resources can
be accomplished through the wait and signal
operations on semaphores. A system table
records whether each resource is free or
allocated, and if allocated, to which process. If a
process requests a resource that is currently
allocated to another process, it can be added to a
queue of processes waiting for this resource.
Deadlock Situation
In a multi-programming environment, several
processes may compete for a fixed number of
resources. A process requests resources and if the
resources are not available at that time, it enters a wait
state. Waiting processes may never again change state,
because the resources they have requested are held by
other waiting processes. This situation is called
deadlock.
Deadlock Situation
P1
Printer
P2
Scanner
Consider a system with one printer and one scanner.
Process P1 request the printer and process P2 requests
the scanner. Both requests are granted. Now P1
requests the scanner (without giving up the printer)
and P2 requests the printer (without giving up the
scanner). Neither request can be granted so both
processes enter a deadlock situation.
Deadlock Situation
P1
Printer
P2
Scanner
Deadlock Characterization
❖ Necessary Conditions
• Mutual Exclusion
• No Preemption
• Circular Wait
Deadlock Characterization
❖ Necessary Conditions Continue…
Mutual Exclusion
At least one resource must be held in a non-sharable mode;
that is, only one process at a time can use the resource. If
another process requests that resource, the requesting
process must be delayed until the resource has been
released.
Deadlock Characterization
❖ Necessary Conditions Continue…
No Preemption
Resources already allocated to a process cannot be
preempted; that is, a resource can be released only
voluntarily by the process holding it, after process has
completed its task.
Deadlock Characterization
❖ Necessary Conditions Continue…
Circular Wait
A set {P0, P1, …, Pn} of waiting processes must exist 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.
The processes in the system form a circular list or chain where each process
in the list is waiting for a resource held by the next process in the list.
Deadlock Situation
P1 P2
P1
R1 i i R2
R1 i i R2 P2
P1
R1
For each instances of a resource type, there is a dot in the
resource node rectangle. i
i
R2
Resource-Allocation Graph continue…
A request edge points to only the square , where as an
assignment edge must also designate one of the dots in the
square.
R1 i i R3
P1 P2 P3
i i
R2
i i R4
i
Resource-Allocation Graph continue…
When process Pi requests an instance of resource type Rj, a
request edge is inserted in the resource allocation graph.
R1 i i R3
P1 P2 P3
i i
R2
i i R4
i
Resource-Allocation Graph continue…
When this requests can be fulfilled the request edge is
instantaneously transformed to an assignment edge.
R1 i i R3
P1 P2 P3
i i
R2
i i R4
i
Resource-Allocation Graph continue…
When the process no longer needs access to the resource it
releases the resource, and as a result the assignment edge is
deleted.
R1 i i R3
P1 P2 P3
i i
R2
i i R4
i
Resource-Allocation Graph continue…
The resource-allocation graph depicts the following:
R1 i i R3
P1 P2 P3
P1 P2 P3
Resource instances
• One instance of resource type R1
• Two instances of resource type R2 i i
R2
i i R4
• One instance of resource type R3 i
• Three instances of resource type R4
Resource-Allocation Graph continue…
The resource-allocation graph depicts the following:
Process states
R1 i i R3
Process P1 is holding an
instance of resource type R2 ,
and is waiting for an
instance of resource type R1 . P1 P2 P3
i i
R2
i i R4
i
Resource-Allocation Graph continue…
The resource-allocation graph depicts the following:
Process states
R1 i i R3
Process P1 is holding an
instance of resource type R2,
Process
and P2 is holding
is waiting for anan
instance
instance of of resource
resource types
type R1. R1 P1 P2 P3
and R2, and is waiting for an
instance of resource type R3 .
i i
R2
i i R4
i
Resource-Allocation Graph continue…
The resource-allocation graph depicts the following:
Process states
R1 i i R3
Process P1 is holding an
instance of resource type R2,
Process
and P2 is holding
is waiting for anan
instance
instance of of resource
resource types
type R1. R1 P1 P2 P3
and R2, and is waiting for an
Processof resource
instance P3 is holding
type R3 .an
instance of resource type R3 .
i i
R2
i i R4
i
Resource-Allocation Graph continue…
Tips to check deadlock:
R1 i i R3
If no cycle exists in the
resource allocation graph,
there is no deadlock. P1 P2 P3
i i
R2
i i R4
i
Resource-Allocation Graph continue…
Tips to check deadlock:
R1 i i R3
If there is a cycle in the
graph and each resource
has only one instance, then P1 P2 P3
there is a deadlock. Here a
cycle is a necessary and
sufficient condition for i
R2 i i R4
deadlock.
i
Handling Deadlocks
Possible strategies to deal with deadlocks:
❖ Deadlock Prevention
❖ Deadlock Avoidance
❖ Deadlock Prevention
Is a set methods for ensuring
that at least one of the necessary
❖ Deadlock Avoidance conditions cannot hold. These
methods prevent deadlocks by
❖ Deadlock Detection and Recovery
constraining how requests for
resources can be made.
Handling Deadlocks continue…
Possible strategies to deal with deadlocks:
❖ Deadlock Prevention
DA requires that the operating
system be given in advance
❖ Deadlock Avoidance additional information concerning
which resources a process will
❖ Deadlock Detection and Recovery
request and use during its lifetime.
With this knowledge, we can
decide for each request whether or
not the process should wait.
Handling Deadlocks continue…
Possible strategies to deal with deadlocks:
Here the system can
❖ Deadlock Prevention
provide an algorithm
that examines the
❖ Deadlock Avoidance state of the system to
determine whether a
❖ Deadlock Detection and Recovery deadlock has
occurred and an
algorithm to recover
from the deadlock.
Deadlock Prevention
We can prevent the occurrence of a deadlock by ensuring
that at least one of the four necessary conditions can not
hold.
❖ Mutual Exclusion
❖ No Preemption
❖ Circular Wait
Deadlock Prevention continue…
Mutual Exclusion:
o Request edge
i
o Assignment edge
i
o Claim edge
i
A claim edge Pi gRj indicates that the process Pi may
request resource Rj at some time in the future.
When process Pi request resource Rj, the claim
edge Pi gRj is converted to a request edge. When
the resource Rj is released by Pi, the assignment
edge Rj gPi is converted to a claim edge Pi gRj.
R2 i
R2 i
Banker’s Algorithm
Resource-Allocation Graph Algorithm is not applicable to a
resource allocation system with multiple instances of each
resource type.
may need. This number may not exceed the total number
❖ Available
❖ Max
❖ Allocation
❖ Need
Banker’s Algorithm continue…
To implement Banker’s algorithm, we need the following
data structures:
❖ Need
Banker’s Algorithm continue…
To implement Banker’s algorithm, we need the following
data structures:
❖ Need
Banker’s Algorithm continue…
To implement Banker’s algorithm, we need the following
data structures:
restored.
Deadlock Detection
If a system does not employ either a deadlock-prevention or
a deadlock avoidance algorithm, then it should provide:
❖ Available Resources
❖ Current Allocation
❖ Request
Deadlock Detection continue…
Multiple Instances of a Resource Type
The following data structures are used:
❖ Current Allocation
❖ Request
Deadlock Detection continue…
Multiple Instances of a Resource Type
The following data structures are used:
❖ Available Resources
❖ Request
Deadlock Detection continue…
Multiple Instances of a Resource Type
The following data structures are used:
❖ Available Resources
❖ Current Allocation
3. Prevent starvation.