Deadlock in OS

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

Classes By: K.K.

Singh, The Launcher Academy, City Centre, Opp- Gossner College, Club Road
Ranchi. Contact- 8877155769, 7903154392

DEADLOCKS
In a multiprogramming environment, several processes may compete for a finite number of
resources. A process requests resources; and if the resources are not available at that time, the
process enters a waiting state. Sometimes, a waiting process is never again able to change state,
because the resources it has requested are held by other waiting processes. This situation is called
a deadlock.

A set of process is in a deadlock state when every process in the set is waiting for an
event that can only be caused by another process in the set.
OR
A set of blocked processes each holding a resource and waiting to acquire a resource held by
another process in the set.

System Model

• System resources are utilized in the following way:


o Request: If a process makes a request to use a system resource which cannot be
granted immediately, then the requesting process blocks until it can acquire the
resource.
o Use: The process can operate on the resource.
o Release: The process releases the resource.

Deadlock: Necessary Conditions


• For a deadlock to occur, each of the following four conditions must hold.
o Mutual exclusion: only one process at a time can use a resource.
o Hold and wait: a process holding at least one resource is waiting to acquire
additional resources held by other processes.
o No preemption: a resource can be released only voluntarily by the process
holding it, after that process has completed its task.
o 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.

Resource-Allocation Graph
A set of vertices V and a set of edges E.

• V is partitioned into two types:


Classes By: K.K. Singh, The Launcher Academy, City Centre, Opp- Gossner College, Club Road
Ranchi. Contact- 8877155769, 7903154392

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

Request Edges - A set of directed arcs from Pi to Rj, indicating that process Pi has requested Rj,
and is currently waiting for that resource to become available. Ex- Request edge – directed edge
Pi → Rj
Assignment Edges - A set of directed arcs from Rj to Pi indicating that resource Rj has been
allocated to process Pi, and that Pi is currently holding resource Rj. Ex- Assignment edge –
directed edge Rj → Pi

Note that a request edge can be converted into an assignment edge by reversing the direction of
the arc when the request is granted.

Process

Resource Type with 4 instances

Pi requests instance of Rj

Pi is holding an instance of Rj
Classes By: K.K. Singh, The Launcher Academy, City Centre, Opp- Gossner College, Club Road
Ranchi. Contact- 8877155769, 7903154392

Handling Deadlocks
Deadlock Prevention and Avoidance: Make sure deadlock can never happen.

• Prevention: Ensure one of the four conditions fails.


• Avoidance: The OS needs more information so that it can determine if the current request
can be satisfied or delayed.
• Deadlock: Allow a system to enter a deadlock situation, detect it, and recover.
• Ignore Deadlock: Pretend deadlocks never occur in the system.

Deadlock Prevention
By ensuring that at least one of the four conditions cannot hold, we can prevent the occurrence of
a deadlock.

Mutual Exclusion: Some sharable resources don’t require mutually exclusive and cannot be
involved in deadlock. Read only files are good example a sharable resource. But some sharable
resources must be accessed exclusively (e.g., printer), which means we cannot deny the mutual
exclusion condition.

Hold and Wait


• No process can hold some resources and then request for other resources.
Two strategies are possible:
o A process must acquire all resources before it runs. Resource utilization may be
low, since many resources will be held and unused for a long time.
o When a process requests for resources, it must hold none (i.e., returning resources
before requesting for more).

Starvation is possible. A process that needs some popular resources may have to
wait indefinitely.
Classes By: K.K. Singh, The Launcher Academy, City Centre, Opp- Gossner College, Club Road
Ranchi. Contact- 8877155769, 7903154392

No Preemption
o 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.
The 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.

o 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. If the resources are
neither available nor held by a waiting process, the requesting process must wait. While it
is waiting, some of its resources may be preempted, but only if another process requests
them. A process can be restarted only when it is allocated the new resources it is
requesting and recovers any resources that were preempted while it was waiting.

This protocol is often applied to resources whose state can be easily saved and restored
later, such as CPU registers and memory space. It cannot generally be applied to such
resources as printers and tape drives.

Circular Waiting
o To break the circular waiting condition, we can order all resource types (e.g.,
tapes, printers).
o A process can only request resources higher than the resource types it holds.
o Suppose the ordering of tapes, disks, and printers are 1, 4, and 8. If a process
holds a disk (4), it can only ask a printer (8) and cannot request a tape (1).
o A process must release some lower order resources to request a higher order
resource. To get tapes (1), a process must release its disk (4).

Deadlock Avoidance
Each process provides the maximum number of resources of each type it needs. With this
information, there are algorithms that can ensure the system will never enter a deadlock state.
This is deadlock avoidance.
A sequence of processes <P1, P2, …, Pn> is a safe sequence if for each process Pi in the
sequence, its resource requests can be satisfied by the remaining resources and the sum of all
resources that are being held by P1, P2, …, Pi-1. This means we can suspend Pi and run P1, P2,
…, Pi-1 until they complete. Then, Pi will have all resources to run.

Ex:
Classes By: K.K. Singh, The Launcher Academy, City Centre, Opp- Gossner College, Club Road
Ranchi. Contact- 8877155769, 7903154392

A system has 12 tapes and three processes A, B, C. At time t0, we have:

Max need Current holding will need


A 10 5 5
B 4 2 2
C 9 2 7

Then, <B, A, C> is a safe sequence (safe state).

Max need Current holding will need


A 10 5 5
B 4 2 2
C 9 3 6

Not a safe state.

Deadlock avoidance algorithms.


There are two popular algorithms are available for deadlock avoidance.
1. Resource allocation graph algorithm (suited for each resources having single instance)
2. Banker’s algorithms (suited for each resources having multiple instances)

Resource allocation graph algorithm


If resource categories have only single instances of their resources, then deadlock states can be
detected by cycles in the resource-allocation graphs. In this case, unsafe states can be recognized
and avoided by augmenting the resource-allocation graph with claim edges, noted by dashed
lines, which point from a process to a resource that it may request in the future.
In order for this technique to work, all claim edges must be added to the graph for any particular
process before that process is allowed to request any resources.
When a process makes a request, the claim edge Pi->Rj is converted to a request edge. Similarly
when a resource is released, the assignment reverts back to a claim edge.
This approach works by denying requests that would produce cycles in the resource-allocation
graph, taking claim edges into effect.

Example- in the following resource allocation graph

When process P2 requests resource R2 then the resulting resource-allocation graph would have a
cycle in it, and so the request cannot be granted
Classes By: K.K. Singh, The Launcher Academy, City Centre, Opp- Gossner College, Club Road
Ranchi. Contact- 8877155769, 7903154392

2. Banker’s algorithms
The resource-allocation-graph algorithm is not applicable to a resource allocation system with multiple
instances of each resource type. The banker’s algorithm is applicable to such a system.
The name was chosen because the algorithm could be used in a banking system to ensure that the bank
never allocated its available cash in such a way that it could no longer satisfy the needs of all its
customers.
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. If it will, the resources are allocated; otherwise, the process
must wait until some other process releases enough resources.

Example-
Consider a system with five processes P0 through P4 and three resource types A, B, and C. Resource type
A has 10 instances, resource type B has 5 instances, and resource type C has 7 instances. Suppose that, at
time T0, the following snapshot of the system has been taken:

Processes 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

Answer the following questions using the banker's algorithm:


a. What is the content of the matrix Need?
b. Is the system in a safe state? If it is then write the safe sequence.

Solution-
Calculate the need matrix using the formula (need=max-allocation)

Processes Allocation Max Available Need Matrix


A B C A B C A B C A B C
P0 0 1 0 7 5 3 3 3 2 7 4 3
P1 2 0 0 3 2 2 5 3 2 1 2 2
P2 3 0 2 9 0 2 6 0 0
P3 2 1 1 2 2 2 0 1 1
P4 0 0 2 4 3 3 4 3 1
Classes By: K.K. Singh, The Launcher Academy, City Centre, Opp- Gossner College, Club Road
Ranchi. Contact- 8877155769, 7903154392

Now check whether the request of each process can be satisfied by current available resources or not.

For p0 need is (7,4,3) which is not <= available resources (3,3,2). So this request can’t be satisfied and
must be delayed.

For p1 need is (1,2,2) which is <= available resources (3,3,2). So this request can be satisfied and p1 is
completed and released its allocated resources now, available resources are (3,3,2)+(2,0,0)=(5,3,2).

For p2 need is (6,0,0) which is not <= available resources (5,3,2). So this request can’t be satisfied and
must be delayed.

For p3 need is (0,1,1) which is <= available resources (5,3,2). So this request can be satisfied and p3 is
completed and released its allocated resources now, available resources are (5,3,2)+(2,1,1)=(7,4,3).

For p4 need is (4,3,1) which is <= available resources (7,4,3). So this request can be satisfied and p4 is
completed and released its allocated resources now, available resources are (7,4,3)+(0,0,2)=(7,4,5).

Safe sequence (p1,p3,p4,…)Now, again checks for remaining processes.

For p0 need is (7,4,3) which is not <= available resources (7,4,5). So this request can be satisfied and p0
is completed and released its allocated resources now, available resources are (7,4,5)+(0,1,0)=(7,5,5).

For p2 need is (6,0,0) which is <= available resources (7,5,5). So this request can be satisfied and p2 is
completed and released its allocated resources now, available resources are (7,5,5)+(3,0,2)=(10,5,7).

Now the safe sequence is p1,p3,p4,p0,p2. After the completion and released resources by all processes no
of resources must be matched with the resources given at beginning of the question.

Recovery from deadlock


When a detection algorithm determines a deadlock has occurred, the algorithm may inform the
system administrator to deal with it. And, allow the system to recover from a deadlock.

There are two options.

1. Process Termination
2. Resource Preemption

Process Termination
Classes By: K.K. Singh, The Launcher Academy, City Centre, Opp- Gossner College, Club Road
Ranchi. Contact- 8877155769, 7903154392

To eliminate deadlocks by aborting a process, we use one of two methods. In both methods, the
system reclaims all resources allocated to the terminated processes.

• Abort all deadlocked processes. This method clearly will break the deadlock cycle, but at
great expense; the deadlocked processes may have computed for a long time, and the results
of these partial computations must be discarded and probably will have to be recomputed
later.
• Abort one process at a time until the deadlock cycle is eliminated. This method incurs
considerable overhead, since, after each process is aborted, a deadlock-detection algorithm
must be invoked to determine whether any processes are still deadlocked.

Aborting a process may not be easy. If the process was in the midst of updating a file,
terminating it will leave that file in an incorrect state. Similarly, if the process was in the midst of
printing data on a printer, the system must reset the printer to a correct state before printing the
next job.

If the partial termination method is used, then we must determine which deadlocked process (or
processes) should be terminated. This determination is a policy decision, similar to CPU-
scheduling decisions. We should abort those processes whose termination will incur the
minimum cost.

Many factors may affect which process is chosen, including:

1. What the priority of the process is

2. How long the process has computed and how much longer the process will compute before
completing its designated task

3. How many and what type of resources the process has used (for example, whether the
resources are simple to preempt)

4. How many more resources the process needs in order to complete

5. How many processes will need to be terminated?

6. Whether the process is interactive or batch

Resource Preemption
Classes By: K.K. Singh, The Launcher Academy, City Centre, Opp- Gossner College, Club Road
Ranchi. Contact- 8877155769, 7903154392

To eliminate deadlocks using resource preemption, we successively preempt some resources


from processes and give these resources to other processes until the deadlock cycle is broken. If
preemption is required to deal with deadlocks, then three issues need to be addressed:

1. Selecting a victim. Which resources and which processes are to be preempted? As in process
termination, we must determine the order of preemption to minimize cost. Cost factors may
include such parameters as the number of resources a deadlocked process is holding and the
amount of time the process has thus far consumed during its execution.

2. Rollback. If we preempt a resource from a process, what should be done with that process?
Clearly, it cannot continue with its normal execution; it is missing some needed resource. We
must roll back the process to some safe state and restart it from that state. Since, in general, it is
difficult to determine what a safe state is, the simplest solution is a total rollback: Abort the
process and then restart it. Although it is more effective to roll back the process only as far as
necessary to break the deadlock, this method requires the system to keep more information about
the state of all running processes.

3. Starvation. How do we ensure that starvation will not occur? That is, how can we guarantee
that resources will not always be preempted from the same process? In a system where victim
selection is based primarily on cost factors, it may happen that the same process is always picked
as a victim. As a result, this process never completes its designated task, a starvation situation
that must be dealt with in any practical system. Clearly, we must ensure that a process can be
picked as a victim only a (small) finite number of times. The most common solution is to include
the number of rollbacks in the cost factor.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy