0% found this document useful (0 votes)
8 views38 pages

Deadlock

Deadlock occurs when a set of processes are blocked, each waiting for resources held by another process in the set. There are four necessary conditions for deadlock: mutual exclusion, hold and wait, no preemption, and circular wait. Strategies to handle deadlock include ignoring the problem, detection and recovery, dynamic avoidance, and prevention by negating one of the four conditions.

Uploaded by

Surya Basnet
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views38 pages

Deadlock

Deadlock occurs when a set of processes are blocked, each waiting for resources held by another process in the set. There are four necessary conditions for deadlock: mutual exclusion, hold and wait, no preemption, and circular wait. Strategies to handle deadlock include ignoring the problem, detection and recovery, dynamic avoidance, and prevention by negating one of the four conditions.

Uploaded by

Surya Basnet
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 38

What is Deadlock

A set of Processes is deadlock if each process in the set is waiting for an event that only
another process in the set can cause.
Deadlock are a set of blocked processes each holding a resource and waiting to acquire a
resource held by another process.
Preemptable and non-preemptable resource
• Preemptable:- Preemptive resources are those which can be taken
away from a process without causing any ill effects to the process.
Example: Memory
Non-preemptable:- Non pre-emptive resources are those which cannot
be taken away from the process without causing any ill effects.
Example:- CD-ROM (CD recorder), Printer
Condition that lead to deadlock
1 Mutual exclusion
• Each resource is either currently assigned to exactly one process or is
available.
2. Hold and Wait
• Process currently holding resources granted earlier can request more
resources.
3. No Preemption
• Previously granted resources cannot be forcibly taken away from Process.
4. Circular Wait
• There must be a circular chain of 2 or more processes. Each process is
waiting for resource that is held by next member of the chain.
All four of these conditions must be present for a deadlock to occur.
Deadlock ignorance

Strategies for dealing with deadlock:


• 1. just ignore the problem
• 2. Detection and recovery
• Let deadlocks occur, detect them and take action
• 3. Dynamic avoidance by careful resource allocation
• 4. prevention, by structurally negating(killing) one of the four required
conditions.
Deadlock ignorance(Ostrich Algorithm)

- When storm approach, an ostrich puts his head in the sand (ground)
and pretend(imagine) that there is no problem at all.
- ignore the deadlock and pretend that deadlock never occur.
• Reasonable if
- deadlocks occur very rarely
- difficult to detect
- cost of prevention is high
- Unix and windows takes this approach
Deadlock Recovery
1. Recovery through pre-emption
In this method resources are temporarily taken away from its current
owner and give it to another process.
The ability to take a resource away from a process, have another
process use it, and then give it back without the process noticing
it is highly dependent on the nature of the resource.
Recovering this way is frequently difficult or impossible.
Deadlock Recovery
• 2. Recovery through rollback
• - PCB (Process Control Block) and resource state are periodically saved
at "checkpoint".
• - When deadlock is detected, rollback the preempted process up to
the previous safe state before it acquired that resource.
• Discard the resource manipulation that occurred after that
checkpoint.
• -Start the process after it is determined it can run again.
3. Recovery through killing processes

The simplest way to break a deadlock is to kill one or more processes.


• - Kill all the process involved in deadlock
• - kill process one by one
• - After killing each process check for deadlock
• - if deadlock recovered then stop killing more process
• - Otherwise kill another process
Safe and Unsafe State
• A state is said to be safe if it is not deadlocked and there is some
scheduling order in which every process can run to completion even if
all of them suddenly request their maximum number of resources
immediately.
• Total resources are 10
• 7 resources already allocated
• So there are 3 still free
• A needs 6 resources more to complete it.
• B need 2 resources more to complete it.
• C need 5 resources more to complete it.
Safe and Unsafe States
• A state is said to be safe if it is not deadlocked and there is some
scheduling order in which every process can run to completion even if
all of them suddenly request their maximum number of resources
immediately.
Deadlock Avoidance
• Deadlock can be avoided by allocating resources carefully.
• Carefully analyze each resource request to see if it can be safely
granted.
• Need an algorithm that can always avoid deadlock by making right
choice all the time(Banker’s algorithm)
• Banker’s algorithm for single resource
• Banker’s algorithm for multiple resource
Banker’s algorithm for single resource
• What the algorithm does is check to see if granting the request leads
to an unsafe state. If it does, the request is denied.
• If granting the request leads to a safe state, it is carried out.
• If we have situation as per figure.
• Then it is safe state
• Because with 10 free units
• One by one all customers can be served
• The order of execution is C,D,B,A. So if we can find proper order of
execution then there is no deadlock.
Unsafe state
Banker’s Algorithm for Multiple Resource
Unsafe State of Banker’s Algorithm
Deadlock prevention

• Deadlock can be prevented by attacking the one of the four


conditions that leads to deadlock.
• 1. Attacking the mutual Exclusion condition
• 2. Attacking the Hold and Wait Condition
• 3. Attacking the No Preemption Condition
• 4. Attacking the Circular Wait Condition
Attacking the mutual exclusion condition

• No deadlock if each resource can be assigned to more than one


process.
• We can not assign some resources to more than one process at a time
such as CD-Recorder, Printer etc.
• So this solution is not feasible
Attacking the hold and Wait condition

• Require processes to request all their resource before starting


execution.
• A process is allowed to run if all resources it needed is available.
Otherwise nothing will be allocated and it will just wait.
• Problem with this strategy is that a process may not know required
resources at start of run.
• Resource will not be used optimally
Attacking the no preemption condition
• When a process P0 request some resource R which is held by another
process P1 then resource R is forcibly taken away from the process P1
and allocated P0;
• Consider a process holds the printer, halfway through its job, taking
the printer away from this process without having any ill effect is not
possible.
• This is not a possible option.
Attacking the circular wait condition
• Provide a global numbering of all the resources
• Now the rule is this: processes can request resources whenever they want
to, but all requests must be make in numerical order.
• A process need not acquire them all at once.
• Circular wait is prevented if a process holding resource n cannot wait for
resource m, if m>n
• 1. Printer
• 2. Scanner
• 3. Plotter
• 4. Tape Drive
• 5. CD Rom
• A process may request 1st a CD ROM, then tape drive. But it may not
request 1st a tape drive, then CD ROM.

You might also like