2.7 Deadlock Prevention
2.7 Deadlock Prevention
Module :3
Module Name : Processes and Synchronization
Topic : Deadlock Prevention
Faculty : Dr.K.Sasi Kala Rani
Topics Covered
• Deadlock characteristics
• Eliminate Mutual Exclusion
• Eliminate Hold and wait
• Eliminate No Preemption
• Eliminate Circular Wait
• Deadlock prevention
• Wait-die & Wound-wait
Deadlock Characteristics
• Mutual Exclusion
• Hold and Wait
• No preemption
• Circular wait
Deadlock Prevention - We can prevent Deadlock by
eliminating any of the above four conditions.
Eliminate Mutual Exclusion
• Mutual section 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.
Eliminate Mutual Exclusion
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. Later, Printer collects all the jobs and print
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.
Eliminate Mutual Exclusion
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.
Eliminate Hold and wait
• Allocate all required resources to the process before the start
of its execution, this way hold and wait condition is eliminated
but it will lead to low device utilization.
• Eg. If a process requires printer at a later time and we have
allocated printer before the start of its execution printer
will remain blocked till it has completed its execution.
• The process will make a new request for resources after
releasing the current set of resources. This solution may lead
to starvation.
Eliminate 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.
• 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.
Eliminate Hold and wait
• !(Hold and wait) = !hold or !wait (negation of hold and wait
is, either you don't hold or you don't wait)
• This can be implemented practically if a process declares all
the resources initially.
• Process is the set of instructions which are executed by the
CPU. Each of the instruction may demand multiple resources
at the multiple times. The need cannot be fixed by the OS.
The problem with the approach is:
• Practically not possible.
• Possibility of getting starved will be increases due to the fact
that some process may hold a resource for a very long time.
Eliminate No Preemption
• Preempt resources from the process when resources
required by other high priority processes.
• 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.
Eliminate No Preemption
• 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.
Eliminate Circular Wait(Ordered request)
• Each resource will be assigned with a numerical number.
• A process can request the resources
increasing/decreasing order of numbering.
• For Example, if P1 process is allocated R5 resources, now next
time if P1 ask for R4, R3 lesser than R5 such request will not
be granted, only request for resources more than R5 will be
granted.
Eliminate Circular Wait(Ordered request)
• Each resource will be assigned with a numerical number.
• A process can request the resources
increasing/decreasing order of numbering.
• For Example, if P1 process is allocated R5 resources, now next
time if P1 ask for R4, R3 lesser than R5 such request will not
be granted, only request for resources more than R5 will be
granted.
Deadlock prevention
Wait die Vs Wound-wait
WAIT-DIE POLICY WOUND-WAIT POLICY