Deadlocks

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 20

Never be in

deadlock !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Deadlocks
What is deadlock??

 A deadlock is a situation in which two computer programs sharing the


same resource are effectively preventing each other from accessing
the resource, resulting in both programs ceasing to function.
 Each process needs what an other process has
 The earliest computer operating systems ran only one program at a
time.
 A set of blocked processes each holding a resource and waiting to
acquire a resource held by another process in the set.“
 Under a normal operation a resource allocation proceed like this
 Request resource
 Use the resource
 Release the resource
What is deadlock??
• Bridge Crossing Example2

• Traffic only in one direction."


• ■ Each section of a bridge can be viewed as a resource."
• ■ If a deadlock occurs, it can be resolved if one car backs
up
• (preempt resources and rollback).“
• Starvation is possible."
Necessary Conditions
A deadlock situation can arise if the following four
conditions hold simultaneously in a system:
Mutual exclusion: only 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 holds a resource while
waiting for another resource.
Necessary Conditions
 No preemption:
• Resources cannot be preempted; that is, a resource can be released
only voluntarily by the process holding it after that process has
completed its task.
• Once a process is holding a resource ( i.e. once its request has been
granted ), then that resource cannot be taken away from that
process until the process voluntarily releases it.
 Circular wait: process A waits process B waits for process C…
waits for process A.
There must exist a set {P0,P1P2,……..,Pn} of waiting processes such
that P0 is waiting for a resource that is held by P 1, 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 P 0.
Mechanisms for handling deadlocks
There are three methods:

 Ignore Deadlocks: Most Operating systems do this!!

 Ensure deadlock never occurs using either

Prevention Prevent any one of the 4 conditions from happening.

Avoidance Allow all deadlock conditions, but calculate cycles about to


happen and stop dangerous operations..

 Allow deadlock to happen. This requires using both:

Detection Know a deadlock has occurred.

Recovery Regain the resources.


Deadlock Prevention

• Deadlock prevention is a set of methods for


ensuring that at least one of the necessary
conditions cannot hold. However most
prevention algorithms have poor resource
utilization, and hence result in reduced
throughputs.
Deadlock prevention
• Mutual Exclusion: Not always possible to prevent
deadlock by preventing mutual exclusion (making all
resources shareable) as certain resources are cannot be
shared safely.
• Hold and Wait – must guarantee that whenever a process
requests a resource, it does not hold any other resources.
Require process to request and be allocated all its
resources before it begins execution,
allow process to request resources only when the process
has none.
Low resource utilization; starvation possible.
Deadlock Prevention
 No Preemption –We will see two approaches here.
If a process request for a resource which is held by
another waiting resource, then the resource may be
preempted from the other waiting resource.
In the second approach, if a process request for a resource
which are not readily available, all other resources that it
holds are preempted.
The challenge here is that the resources can be preempted
only if we can save the current state can be saved and
processes could be restarted later from the saved state.
Deadlock Prevention

 Circular Wait –
impose a total ordering of all resource
types, and require that each process
requests resources in an increasing order
of enumeration.
The algorithm may itself increase
complexity and may also lead to poor
resource utilization.
Deadlock avoidance
• Allow all deadlock conditions. But calculate cycles about to happen
and stop dangerous operation
• we can try to avoid deadlocks by making use prior knowledge about
the usage of resources by processes including resources available,
resources allocated, future requests and future releases by processes.
• If a system is in safe state  no deadlocks.
• If a system is in unsafe state  possibility of deadlock.
• Avoidance  ensures that a system will never enter an unsafe state.
• System is in safe state if there exists a safe sequence of all
processes.
Banker’s Algorithm

• The banker’s algorithm is a resource


allocation and deadlock avoidance
algorithm that tests for safety by simulating
the allocation for predetermined maximum
possible amounts of all resources, then
makes an “s-state” check to test for
possible activities, before deciding whether
allocation should be allowed to continue.
Conn…
• Several data structures must be maintained to implement the
banker’s algorithm. These data structures encode the state of
the resource-allocation system. We need the following data
structures, where n is the number of processes in the system
and m is the number of resource types:
• Available: the number of available resources of each type.
• Max: the maximum demand of each process.
• Allocation: the number of resources of each type currently
allocated to each process.
• Need: the remaining resource need of each process.
• Note that Need[i][j] =Max[i][j]− Allocation[i][j].
banker’s algorithm
banker’s algorithm

solution :Need [i, j] = Max [i, j] – Allocation [i, j]


Banker algorithm

• Question2. Is the system in safe


state? If Yes, then what is the
safe sequence?
banker’s algorithm
banker algorithm
Question3. What will happen if process P1 requests
one additional instance of resource type A and two
instances of resource type C?
This question will be solve using resource request
algorithm using the following steps.
1. Request<=need, go to (2)
2. Request <= available
3. Available=available –request
allocation =allocation +request
need=need-request
4. Check if new state is safe or not (using bankers algorithm)
Banker algorithm
Banker algorithm
• We must determine whether this new system state is
safe. To do so, we again execute Safety algorithm on
the above data structures.
Hence the new system state is safe, so we can
immediately grant the request for process P1 .

You might also like