OS PPT Unit 2.3
OS PPT Unit 2.3
• Necessary Conditions
• Resource-Allocation Graph
Necessary Conditions
• A deadlock situation can arise if the following four conditions
occur simultaneously in a system:
• Mutual exclusion. 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.
• Hold and wait. A process must be holding at least one resource and
waiting to acquire additional resources that are currently being held by
other processes.
• No preemption. Resources cannot be preempted; that is, a resource can
be released only after that process has completed its task.
• Circular wait. A set P0, P1, ..., Pn of waiting processes must exist such that
P0 is waiting for a resource held by P1, P1 is waiting for a resource held by
P2, ..., Pn−1 is waiting for a resource held by Pn, and Pn is waiting for a
resource held by P0.
Resource-Allocation Graph
• Deadlocks can be described more precisely in terms of a
directed graph called a system resource-allocation graph.
• This graph consists of a set of vertices V and a set of edges E.
• The set of vertices V is partitioned into two different types
• P = P1, P2, ..., Pn , the set consisting of all the active processes
in the system
• R = R1, R2, ..., Rm , the set consisting of all resource types in the
system.
• Pi Rj ; it signifies that process Pi has requested an instance of
resource type Rj. It is called Request edge.
• Rj Pi ; it signifies that an instance of resource type Rj has been
allocated to process Pi. It is called Assignment edge.
• Pictorially, we represent process Pi as a circle and each
resource type Rj as a rectangle.
• At this point, two minimal cycles exist in the system:
• P1 → R 1 → P2 → R 3 → P3 → R 2 → P1
• P2 → R 3 → P3 → R 2 → P2
Methods for Handling Deadlocks
• To ensure that deadlocks never occur, the system can use
either a
• Deadlock- prevention – Ensures that the deadlock is
prevented occurring in the system.
• Deadlock-avoidance – Avoid deadlock i.e. do not allow the
resource request to the process if it leads to deadlock.
• If a system does not employ either a deadlock-prevention or a
deadlock- avoidance algorithm, then a deadlock situation may
arise. In this case following can be done
• Deadlock detection & recovery – Allows the system to enter
deadlock state & then recover
Deadlock Prevention
• For a deadlock to occur, each of the four necessary conditions
must hold.
• By ensuring that at least one of these conditions cannot hold,
we can prevent the occurrence of a deadlock.
• Mutual Exclusion
– The mutual exclusion condition must hold. That is, at least one
resource must be nonsharable. Such as printer cannot be
simultaneously shared by several processes.
– Shared resources such as read only files do not require mutual
exclusion condition.
– Several process can attempt to open a read only file
• Hold and Wait
– To ensure that the hold-and-wait condition never occurs in the system,
we must guarantee that, whenever a process requests a resource, it
does not hold any other resources.
– One protocol that we can use requires each process to request and be
allocated all its resources before it begins execution.
– An alternative protocol allows a process to request resources only
when it has none of the resources.
• Disadvantages
– Resource utilization may be low – Processes need to know in advance
what resources they need
– Starvation is possible - A process that needs several popular resources
may have to wait indefinitely
• No Preemption
– If a process is holding some resources and requests another resource
that cannot be immediately allocated to it (that is, the process must
wait), then all resources the process is currently holding are
preempted.
– Alternatively, if a process requests some resources, we first check
whether they are available. If they are, we allocate them. If they are
not, we check whether they are allocated to some other process that
is waiting for additional resources.
– If so, we preempt the desired resources from the waiting process and
allocate them to the requesting process.
• Circular wait
– let R = R1, R2, ..., Rm be the set of resource types.
– We assign unique integer number to each resource type, which allows
us to compare two resources and to determine whether one precedes
another
– Each process can request resources only in a ring order of the resource
type R.
– A process has requested for Ri & then request for Rj
– If F(Rj) > F(Ri)
– Whenever a process request an instances of resources type Rj it must
have released resources Ri such that F(Rj) >= F(Ri)
Deadlock Avoidance
• It is to grant only those request for available resources that
cannot possibly result in state of deadlock.
• This algorithm checks the resources allocation state to make
use the circular wait condition never exist.
• Safe State
• The critical part of deadlock avoidance is the safe state.
• A state is safe if the system can allocate resources to each
process in some order and still avoid a deadlock.
• A system is in a safe state only if there exists a safe sequence.
• A sequence of processes <P1, P2, ..., Pn> is a safe sequence,
where for each Pi the resources that Pi can still request can
satisfy by the currently available resources plus the resources
held by Pj.
• If the resource that process Pi needs is not immediately
available then Pi has wait until Pj has finished.
• A Safe state is not a deadlock state
• A deadlock state is an unsafe state
• Not all unsafe state are deadlock, however unsafe state may
lead to a deadlock
Deadlock avoidance algorithm
• Resource-Allocation-Graph Algorithm
• Banker’s Algorithm
– Safety Algorithm
– Resource-Request Algorithm
Resource-Allocation-Graph Algorithm
• If all resources in the system has only one instance then the
RAG can be used for deadlock avoidance.
• In addition to the request & assignment edge, it consist of a
new type of edge called claim edge.
• A claim edge is represented in the graph by - - - -> (dashed)
• When a process Pi requests resource Rj , the claim edge
Pi - - - > Rj is converted to a request edge.
• Similarly, when a resource Rj is released by Pi , the assignment
edge Rj 🡪 Pi is converted to a claim edge Pi - - - > Rj
• Suppose that process Pi requests resource Rj . The request can
be granted only if converting the request edge Pi 🡪 Rj to an
assignment edge Rj 🡪 Pi does not result in the formation of a
cycle in the resource-allocation graph.
• If no cycle exists, then the allocation of the resource will leave
the system in a safe state.
• If a cycle is found, then the allocation will put the system in an
unsafe state.
• In that case, process Pi will have to wait for its requests to be
satisfied.
• Consider the following RAG