Handling Deadlock Deadlock Prevention: Lecture - 14
Handling Deadlock Deadlock Prevention: Lecture - 14
Deadlock prevention
Lecture - 14
Strategies for handling Deadlock
• 1. Deadlock Ignorance
• Deadlock Ignorance is the most widely used approach among all the
mechanism.
• This is being used by many operating systems mainly for end user uses.
• In this approach, the Operating system assumes that deadlock never
occurs. It simply ignores deadlock.
• This approach is best suitable for a single end user system where User
uses the system only for browsing and all other normal stuff.
• There is always a tradeoff between Correctness and performance. The
operating systems like Windows and Linux mainly focus upon
performance.
• However, the performance of the system decreases if it uses deadlock
handling mechanism all the time if deadlock happens 1 out of 100 times
then it is completely unnecessary to use the deadlock handling mechanism
all the time.
• In these types of systems, the user has to simply restart the computer in the
case of deadlock.
Cont..
• 2. Deadlock prevention
• Deadlock happens only when Mutual Exclusion, hold and wait, No
preemption and circular wait holds simultaneously. If it is possible to
violate one of the four conditions at any time then the deadlock can
never occur in the system.
• The idea behind the approach is very simple that we have to fail one
of the four conditions but there can be a big argument on its physical
implementation in the system.
• 3. Deadlock avoidance
• In deadlock avoidance, the operating system checks whether the
system is in safe state or in unsafe state at every step which the
operating system performs.
• The process continues until the system is in safe state. Once the
system moves to unsafe state, the OS has to backtrack one step.
• In simple words, The OS reviews each allocation so that the
allocation doesn't cause the deadlock in the system.
Cont..
• 4. Deadlock detection and recovery
• This approach let the processes fall in deadlock and then
periodically check whether deadlock occur in the system or
not. If it occurs then it applies some of the recovery methods
to the system to get rid of deadlock.
• Deadlock Prevention
• For deadlock, if we can be able to violate one of the four
necessary conditions and don't let them occur together then we
can prevent the deadlock.
• Let's see how we can prevent each of the conditions.
Cont..
• 1. Mutual Exclusion
• Mutual exclusion from the resource point of view is the fact that
a resource can never be used by more than one process
simultaneously which is fair enough but that is the main reason
behind the deadlock.
• If a resource could have been used by more than one process at
the same time then the process would have never been waiting
for any resource.
• However, if we can be able to violate resources behaving in the
mutually exclusive manner then the deadlock can be prevented.
• Spooling
• For a device like printer, spooling can work.
• There is a memory associated with the printer which stores
jobs from each of the process into it.
Cont..
• Later, Printer collects all the jobs and prints each one of them according to
FCFS.
• By using this mechanism, the process doesn't have to wait for the printer
and it can continue whatever it was doing. Later, it collects the output
when it is produced.
Cont..
• Although, Spooling can be an effective approach to
violate mutual exclusion but it suffers from two
kinds of problems:
• This cannot be applied to every resource.
• After some point of time, there may arise a race
condition between the processes to get space in that
spool.
• We cannot force a resource to be used by more than
one process at the same time since it will not be fair
enough and some serious problems may arise in the
performance. Therefore, we cannot violate mutual
exclusion for a process practically.
Cont..
• 2. Hold and Wait
• Hold and wait condition lies when a process holds a resource
and waiting for some other resource to complete its task.
Deadlock occurs because there can be more than one process
which are holding one resource and waiting for other in the
cyclic order.
• However, we have to find out some mechanism by which a
process either doesn't hold any resource or doesn't wait. That
means, a process must be assigned all the necessary resources
before the execution starts. A process must not wait for any
resource once the execution has been started.
• This can be implemented practically if a process declares all
the resources initially. However, this sounds very practical
but can't be done in the computer system because a process
can't determine necessary resources initially.
Cont..
• 3. No Preemption
• Deadlock arises due to the fact that a process can't be
stopped once it starts. However, if we take the resource
away from the process which is causing deadlock then we
can prevent deadlock.
• This is not a good approach at all since if we take a
resource away which is being used by the process then all
the work which it has done till now can become
inconsistent.
• Consider a printer is being used by any process. If we take
the printer away from that process and assign it to some
other process then all the data which has been printed can
become inconsistent and ineffective and also the fact that
the process can't start printing again from where it has left
which causes performance inefficiency.
Cont..
• 4. Circular Wait
• To violate circular wait, we can assign a priority number to each of the
resource. A process can't request for a lesser priority resource. This ensures
that not a single process can request a resource which is being utilized by
some other process and no cycle will be formed.
• Among all the methods, violating Circular wait is the only approach that
can be implemented practically.
Cont..
• Deadlock avoidance
• In deadlock avoidance, the request for any resource will be granted if
the resulting state of the system doesn't cause deadlock in the system.
The state of the system will continuously be checked for safe and
unsafe states.
• In order to avoid deadlocks, the process must tell OS, the maximum
number of resources a process can request to complete its execution.
• The simplest and most useful approach states that the process should
declare the maximum number of resources of each type it may ever
need. The Deadlock avoidance algorithm examines the resource
allocations so that there can never be a circular wait condition.
• Safe and Unsafe States:
• The resource allocation state of a system can be defined by the
instances of available and allocated resources, and the maximum
instance of the resources demanded by the processes.
• A state of a system recorded at some random time is shown in the
next slide:
Cont..
Cont..
• Above tables and vector E, P and A describes the resource allocation state of a
system. There are 4 processes and 4 types of the resources in a system. Table 1
shows the instances of each resource assigned to each process.
• Table 2 shows the instances of the resources, each process still needs. Vector E
is the representation of total instances of each resource in the system.
• Vector P represents the instances of resources that have been assigned to
processes. Vector A represents the number of resources that are not in use.
• A state of the system is called safe if the system can allocate all the resources
requested by all the processes without entering into deadlock.
• If the system cannot fulfill the request of all processes then the state of the
system is called unsafe.
• The key of Deadlock avoidance approach is when the request is made for
resources then the request must only be approved in the case if the resulting
state is also a safe state.