Deadlock in OS
Deadlock in OS
Deadlock in OS
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
Resource-Allocation Graph
A set of vertices V and a set of edges E.
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
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.
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.
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
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:
Solution-
Calculate the need matrix using the formula (need=max-allocation)
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).
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.
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.
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)
Resource Preemption
Classes By: K.K. Singh, The Launcher Academy, City Centre, Opp- Gossner College, Club Road
Ranchi. Contact- 8877155769, 7903154392
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.