0% found this document useful (0 votes)
10 views32 pages

OS PPT Unit 2.3

The document discusses deadlocks, a situation where processes are unable to proceed because each is waiting for resources held by others. It outlines necessary conditions for deadlocks, methods for handling them, including prevention and avoidance strategies, and algorithms like the Banker’s Algorithm for ensuring safe resource allocation. Additionally, it covers deadlock detection and recovery methods, emphasizing the importance of managing resource allocation to avoid deadlock situations.

Uploaded by

vinod.pq
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views32 pages

OS PPT Unit 2.3

The document discusses deadlocks, a situation where processes are unable to proceed because each is waiting for resources held by others. It outlines necessary conditions for deadlocks, methods for handling them, including prevention and avoidance strategies, and algorithms like the Banker’s Algorithm for ensuring safe resource allocation. Additionally, it covers deadlock detection and recovery methods, emphasizing the importance of managing resource allocation to avoid deadlock situations.

Uploaded by

vinod.pq
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 32

Deadlocks

• Deadlock is a situation in which each process is waiting for a


resources never changes its state. Because the resource is
held by other waiting processes.
• Let P1 & P2 are two processes and R1&R2 are two resources.
• Here P1 & P2 will never change its state from waiting. This
situation is called Deadlock.
System Model
• A system consists of a finite number of resources to be
distributed among a number of competing processes.
• If a system has two CPUs, then the resource type CPU has two
instances. Similarly, the resource type printer may have five
instances.
• If a process requests an instance, the allocation should satisfy
the request.
• A process must request a resource, use resource and release
the after using it.
• A process may request as many resources as it requires to
carry out its task.
• A process may utilize a resource in only the following
sequence:
• Request. The process requests the resource. If the request
cannot be granted immediately (for example, if the resource is
being used by another process), then the requesting process
must wait until it can acquire the resource.
• Use. The process can operate on the resource.
• Release. The process releases the resource.
• The request and release of resources may be system calls
Deadlock Characterization

The features that characterize deadlock are

• 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

• This system is in safe state, no cycles in the graph


• Let us assign R2 to P2

• This system is in unsafe state, it consists of cycle


Banker’s Algorithm
• The name was chosen because the algorithm could be used in
a banking system
• When a new process enters the system, it must declare the
maximum number of instances of each resource type that it
may need.
• This number may not exceed the total number of resources in
the system.
• When a user requests a set of resources, the system must
determine whether the allocation of these resources will
leave the system in a safe state.
• The resources are allocated if the system in safe state
otherwise, the process must wait
Data structures for banker’s algorithm
• Let n is the number of processes in the system and m is the
number of resource types
• Available: If Available[j] = k, then k instances of resource type
Rj are available.
• Max: An nxm 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: An n m matrix defines the number of resources of
each type currently allocated to each process. If Allocation[i]
[j] =k, then process Pi is currently allocated k instances of
resource type Rj .
• Need: An n m matrix indicates the remaining resource need of
each process. If Need[i][j] equals k, then process Pi may need
k more instances of resource type Rj to complete its task.
Safety Algorithm
• This algorithm for finding out whether or not a system is in a
safe state. This algorithm can be described as follows:

• This algorithm may require an order of m × n2 operations to


determine whether a state is safe.
Resource-Request Algorithm
• Next, we describe the algorithm for determining whether
requests can be safely granted.
• Let Requesti be the request vector for process Pi
• If Requesti [ j ] k, then process Pi wants k instances of resource
type Rj .
• When a request for resources is made by process Pi , the
following actions are taken:
1. If Requesti ≤ Needi , go to step 2. Otherwise, raise an error
condition
2. If Requesti ≤ Available, go to step 3. Otherwise, Pi must wait
3. System pretend to have allocated the requested resources to
process Pi by modifying the state as follows
• Available = Available – Requesti ;
• Allocationi = Allocationi + Requesti ;
• Needi = Needi – Requesti ;

• If the resulting resource-allocation state is safe, Pi is allocated


its resources.
• If the state is unsafe, then Pi must wait
An Illustrative Example - Banker’s algorithm

• Consider a system with five processes P0 through P4 and three


resource types A, B, and C.
• Resource type A has ten instances, resource type B has five
instances, and resource type C has seven instances.
• The following is the recourse allocation state.
• The system is currently in a safe state, since the sequence
• <P1, P3, P4, P2, P0> satisfies the safety criteria.
• Now that process P1 requests one additional instance of
resource type A and two instances of resource type C,
• so Request1 = (1,0,2)
• P1 releases all its resources after completion. Then available
resources are (5,3,2)
• P3 also request & acquires the resources (0 1 1). Releases after
completion. Then available resources are (7 4 3)
• Similarly P4 Then available resources are (7 4 5)
• P2 Then available resources are (10 4 7)
• Finally P0 Then available resources are (10 5 7)

Deadlock Detection
• If a system does not employ either a deadlock-prevention or a
deadlock- avoidance algorithm, then a deadlock situation may
occur. In this environment, the system may provide:
• An algorithm that examines the state of the system to
determine whether a deadlock has occurred
• An algorithm to recover from the deadlock
Types of detection algorithm
• Single Instance of Each Resource Type
– This uses resource allocation graph called wait-for-graph. A wait-for-
graph is obtained by removing the resource nodes & collapsing the
appropriate edge.
• Several Instances of a Resource Type
– This algorithm uses the Data structure similar to those used in bankers
algorithm.
Recovery from Deadlock
• When a detection algorithm determines that a deadlock
exists, then the system may inform operator that a deadlock
has occurred & let the operator deal with the deadlock
manually.
• Another possibility is to let the system recover from the
deadlock automatically.
• One is simply to abort one or more processes to break the
circular wait.
• The other is to preempt some resources from one or more of
the deadlocked processes.
• Process Termination
– Abort all deadlocked processes
– Abort one process at a time until the deadlock cycle is eliminated
• Resource Preemption
– Selecting a victim
– Rollback
– Starvation

You might also like