Presentations PPT Unit-4 27042019035826AM
Presentations PPT Unit-4 27042019035826AM
Unit-4
Deadlocks
Topics to be covered
Definitions
Deadlock characteristics
Deadlock ignorance
• Ostrich Algorithm
Deadlock detection and recovery
Deadlock avoidance
• Banker’s algorithm
Deadlock prevention
Unit
Unit –– 4:
4: Deadlocks
Deadlocks 22 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
What is Deadlock?
Unit
Unit –– 4:
4: Deadlocks
Deadlocks 33 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
What is Deadlock?
A set of processes is deadlocked if each process in the set is
waiting for an event that only another process in the set can
cause.
Deadlocks are a set of blocked processes each holding a resource
and waiting to acquire a resource held by another process.
Process Process
Hold P1 Request
Request Hold
R1 DEADLOCK R2
P2 Hold
Request
Resource Resource
Unit
Unit –– 4:
4: Deadlocks
Deadlocks 44 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
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 to the process.
• Example:- CD-ROM (CD recorder), Printer.
Unit
Unit –– 4:
4: Deadlocks
Deadlocks 55 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Conditions 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.
Unit
Unit –– 4:
4: Deadlocks
Deadlocks 66 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
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.
Unit
Unit –– 4:
4: Deadlocks
Deadlocks 77 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Deadlock ignorance (Ostrich Algorithm)
When storm approaches, 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
Unit
Unit –– 4:
4: Deadlocks
Deadlocks 88 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Deadlock detection for single resource
• We are starting from node D.
R A B • Empty list L = ()
• Add current node so Empty list =
C S D T E (D).
• From this node there is one
outgoing arc to T so add T to list.
F U V
• So list become L = (D, T).
• Continue this step….so we get
W G
list as below
• L = (D, T, E)………… L = (D, T, E, V,
G, U, D)
• In the above step in list the
node D appears twice, so
deadlock.
Unit
Unit –– 4:
4: Deadlocks
Deadlocks 99 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Deadlock detection for single resource
Algorithm for detecting deadlock for single resource
1. For each node, N in the graph, perform the following five steps
with N as the starting node.
i. Initialize L to the empty list, designate all arcs as unmarked.
ii. Add current node to end of L, check to see if node now appears in
L two times. If it does, graph contains a cycle (listed in L),
algorithm terminates.
iii. From given node, see if any unmarked outgoing arcs. If so, go to
step 4; if not, go to step 5.
iv. Pick an unmarked outgoing arc at random and mark it. Then
follow it to the new current node and go to step 2.
v. If this is initial node, graph does not contain any cycles, algorithm
terminates. Otherwise, dead end. Remove it, go back to previous
node, make that one current node, go to step 2.
Unit
Unit –– 4:
4: Deadlocks
Deadlocks 10
10 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Deadlock detection for multiple resource
Tape Drive
Tape Drive
Scanners
Scanners
CD Roms
CD Roms
Plotters
E= Plotters A=
4 2 3 1 2 1 0 0
total no of each resource no of resources that are
available (free)
Tape Drive
Tape Drive
Scanners
CD Roms
Scanners
CD Roms
Plotters
Plotters
Process
Process
C= R=
P1 0 0 1 0 P1 2 0 0 1
P2 2 0 0 1 P2 1 0 1 1
P3 0 1 2 0 P3 2 1 0 0
Tape Drive
Tape Drive
Scanners
Scanners
CD Roms
CD Roms
Plotters
E= Plotters A=
4 2 3 1 0 0 0 0
total no of each resource no of resources that are
available (free)
Tape Drive
Tape Drive
Scanners
CD Roms
Scanners
CD Roms
Plotters
Plotters
Process
Process
C= R=
P1 0 0 1 0 P1 2 0 0 1
P2 2 0 0 1 P2 1 0 1 1
P3 2 2 2 0 P3 0 0 0 0
Tape Drive
Tape Drive
Scanners
Scanners
CD Roms
CD Roms
Plotters
E= Plotters A=
4 2 3 1 2 2 2 0
total no of each resource no of resources that are
available (free)
Tape Drive
Tape Drive
Scanners
CD Roms
Scanners
CD Roms
Plotters
Plotters
Process
Process
C= R=
P1 0 0 1 0 P1 2 0 0 1
DEADLOCK
P2 2 0 0 1 P2 1 0 1 1
P3 0 0 0 0 P3 0 0 0 0
Hold P1 Request
Hold
R1 R2
P2 Hold
Unit
Unit –– 4:
4: Deadlocks
Deadlocks 14
14 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Deadlock recovery (cont…)
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.
A A A
F1 F2
R R
First Second B
Checkpoints Checkpoints
Unit
Unit –– 4:
4: Deadlocks
Deadlocks 15
15 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Deadlock recovery (cont…)
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
Unit
Unit –– 4:
4: Deadlocks
Deadlocks 16
16 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
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.
Total resources are 10 Process Has Max
7 resources already allocated A 3 9
Unit
Unit –– 4:
4: Deadlocks
Deadlocks 17
17 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Safe states
Process Has Max Process Has Max Process Has Max
A 3 9 A 3 9 A 3 9
B 2 4 B 4 4 B 0 -
4
2 C 2 7 C 2 7
C 2 7
Free : 3 Free : 1 Free : 5
Unit
Unit –– 4:
4: Deadlocks
Deadlocks 18
18 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Unsafe states
Process Has Max Process Has Max Process Has Max
A 3 9 A 4 9 A 4 9
B 2 4 1 B 2 4 B 4 4
2
C 2 7 C 2 7 C 2 7
Free : 3 Free : 2 Free : 0
Unit
Unit –– 4:
4: Deadlocks
Deadlocks 19
19 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
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
Unit
Unit –– 4:
4: Deadlocks
Deadlocks 20
20 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
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
Process Has Max
• then it is safe state A 0 6
• because with 10 free units B 0 5
• one by one all customers can be served. C 0 4
D 0 7
Free : 10
Unit
Unit –– 4:
4: Deadlocks
Deadlocks 21
21 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Banker’s algorithm for single resource
Process Has Max Process Has Max Process Has Max
A 1 6 A 1 6 A 1 6
B 1 5 B 1 5 B 1 5
C 2 4 C 4 4 C 0 0
D 4 7 D 4 7 D 4 7
Free : 2 Free : 0 Free : 4
Unit
Unit –– 4:
4: Deadlocks
Deadlocks 22
22 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Banker’s algorithm for single resource
Process Has Max Process Has Max Process Has Max
A 1 6 A 6 6 A 0 -
B 0 - B 0 - B 0 -
C 0 - C 0 - C 0 -
D 0 - D 0 - D 0 -
Free : 9 Free : 4 Free : 10
Unit
Unit –– 4:
4: Deadlocks
Deadlocks 23
23 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Banker’s algorithm for single resource
Process Has Max
A 1 6
B 2 5
C 2 4
D 4 7
Free : 1
Unit
Unit –– 4:
4: Deadlocks
Deadlocks 24
24 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Banker’s algorithm for multiple resource
Tape Drive
Tape Drive
Tape Drive
Scanners
Scanners
Scanners
CD Roms
CD Roms
CD Roms
Plotters
Plotters
Plotters
6 3 4 2 5 3 2 2 1 0 2 0
total no of each resource resources hold Available (free) resources
Tape Drive
Tape Drive
Scanners
CD Roms
Scanners
CD Roms
Plotters
Process
Plotters
Process
P4 1 1 0 1 P4 0 0 1 0
P5 0 0 0 0 P5 2 1 1 0
Unit
Unit –– 4:
4: Deadlocks
Deadlocks 25
25 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Banker’s algorithm for multiple resource
Tape Drive
Tape Drive
Tape Drive
Scanners
Scanners
Scanners
CD Roms
CD Roms
CD Roms
Plotters
Plotters
Plotters
6 3 4 2 5 3 2 2 1 0 1 0
total no of each resource resources hold Available (free) resources
Tape Drive
Tape Drive
Scanners
CD Roms
Scanners
CD Roms
Plotters
Process
Plotters
Process
P4 1 1 1 1 P4 0 0 0 0
P5 0 0 0 0 P5 2 1 1 0
Unit
Unit –– 4:
4: Deadlocks
Deadlocks 26
26 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Banker’s algorithm for multiple resource
Tape Drive
Tape Drive
Tape Drive
Scanners
Scanners
Scanners
CD Roms
CD Roms
CD Roms
Plotters
Plotters
Plotters
6 3 4 2 5 3 2 2 2 1 2 1
total no of each resource resources hold Available (free) resources
Tape Drive
Tape Drive
Scanners
CD Roms
Scanners
CD Roms
Plotters
Process
Plotters
Process
P4 - - - - P4 0 0 0 0
P5 0 0 0 0 P5 2 1 1 0
Unit
Unit –– 4:
4: Deadlocks
Deadlocks 27
27 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Banker’s algorithm for multiple resource
Tape Drive
Tape Drive
Tape Drive
Scanners
Scanners
Scanners
CD Roms
CD Roms
CD Roms
Plotters
Plotters
Plotters
6 3 4 2 5 3 2 2 1 0 2 1
total no of each resource resources hold Available (free) resources
Tape Drive
Tape Drive
Scanners
CD Roms
Scanners
CD Roms
Plotters
Process
Plotters
Process
P4 - - - - P4 0 0 0 0
P5 0 0 0 0 P5 2 1 1 0
Unit
Unit –– 4:
4: Deadlocks
Deadlocks 28
28 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Banker’s algorithm for multiple resource
Tape Drive
Tape Drive
Tape Drive
Scanners
Scanners
Scanners
CD Roms
CD Roms
CD Roms
Plotters
Plotters
Plotters
6 3 4 2 5 3 2 2 5 1 3 2
total no of each resource resources hold Available (free) resources
Tape Drive
Tape Drive
Scanners
CD Roms
Scanners
CD Roms
Plotters
Process
Plotters
Process
P4 - - - - P4 0 0 0 0
P5 0 0 0 0 P5 2 1 1 0
Unit
Unit –– 4:
4: Deadlocks
Deadlocks 29
29 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Banker’s algorithm for multiple resource
Tape Drive
Tape Drive
Tape Drive
Scanners
Scanners
Scanners
CD Roms
CD Roms
CD Roms
Plotters
Plotters
Plotters
6 3 4 2 5 3 2 2 5 0 2 0
total no of each resource resources hold Available (free) resources
Tape Drive
Tape Drive
Scanners
CD Roms
Scanners
CD Roms
Plotters
Process
Plotters
Process
P4 - - - - P4 0 0 0 0
P5 0 0 0 0 P5 2 1 1 0
Unit
Unit –– 4:
4: Deadlocks
Deadlocks 30
30 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Banker’s algorithm for multiple resource
Tape Drive
Tape Drive
Tape Drive
Scanners
Scanners
Scanners
CD Roms
CD Roms
CD Roms
Plotters
Plotters
Plotters
6 3 4 2 5 3 2 2 5 2 3 2
total no of each resource resources hold Available (free) resources
Tape Drive
Tape Drive
Scanners
CD Roms
Scanners
CD Roms
Plotters
Process
Plotters
Process
P4 - - - - P4 0 0 0 0
P5 0 0 0 0 P5 2 1 1 0
Unit
Unit –– 4:
4: Deadlocks
Deadlocks 31
31 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Banker’s algorithm for multiple resource
Tape Drive
Tape Drive
Tape Drive
Scanners
Scanners
Scanners
CD Roms
CD Roms
CD Roms
Plotters
Plotters
Plotters
6 3 4 2 5 3 2 2 2 1 3 2
total no of each resource resources hold Available (free) resources
Tape Drive
Tape Drive
Scanners
CD Roms
Scanners
CD Roms
Plotters
Process
Plotters
Process
P4 - - - - P4 0 0 0 0
P5 0 0 0 0 P5 2 1 1 0
Unit
Unit –– 4:
4: Deadlocks
Deadlocks 32
32 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Banker’s algorithm for multiple resource
Tape Drive
Tape Drive
Tape Drive
Scanners
Scanners
Scanners
CD Roms
CD Roms
CD Roms
Plotters
Plotters
Plotters
6 3 4 2 5 3 2 2 6 3 4 2
total no of each resource resources hold Available (free) resources
Tape Drive
Tape Drive
Scanners
CD Roms
Scanners
CD Roms
Plotters
Process
Plotters
Process
P4 - - - - P4 0 0 0 0
P5 0 0 0 0 P5 2 1 1 0
Unit
Unit –– 4:
4: Deadlocks
Deadlocks 33
33 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Banker’s algorithm for multiple resource
Tape Drive
Tape Drive
Tape Drive
Scanners
Scanners
Scanners
CD Roms
CD Roms
CD Roms
Plotters
Plotters
Plotters
6 3 4 2 5 3 2 2 4 2 3 2
total no of each resource resources hold Available (free) resources
Tape Drive
Tape Drive
Scanners
CD Roms
Scanners
CD Roms
Plotters
Process
Plotters
Process
P4 - - - - P4 0 0 0 0
P5 2 1 1 0 P5 0 0 0 0
Unit
Unit –– 4:
4: Deadlocks
Deadlocks 34
34 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Banker’s algorithm for multiple resource
Tape Drive
Tape Drive
Tape Drive
Scanners
Scanners
Scanners
CD Roms
CD Roms
CD Roms
Plotters
Plotters
Plotters
6 3 4 2 5 3 2 2 6 3 4 2
total no of each resource resources hold Available (free) resources
Tape Drive
Tape Drive
Scanners
CD Roms
Scanners
CD Roms
Plotters
Process
Plotters
Process
P4 - - - - P4 0 0 0 0
P5 - - - - P5 0 0 0 0
Unit
Unit –– 4:
4: Deadlocks
Deadlocks 35
35 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Banker’s algorithm for multiple resource
Tape Drive
Tape Drive
Tape Drive
Scanners
Scanners
Scanners
CD Roms
CD Roms
CD Roms
Plotters
Plotters
Plotters
6 3 4 2 5 3 2 2 1 0 2 0
total no of each resource resources hold Available (free) resources
Tape Drive
Tape Drive
Scanners
CD Roms
Scanners
CD Roms
Plotters
Process
Plotters
Process
P4 1 1 0 1 P4 0 0 1 1
P5 0 0 0 0 P5 2 1 1 0
Unit
Unit –– 4:
4: Deadlocks
Deadlocks 36
36 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
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
Unit
Unit –– 4:
4: Deadlocks
Deadlocks 37
37 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
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.
Unit
Unit –– 4:
4: Deadlocks
Deadlocks 38
38 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Attacking the hold and wait condition
Require processes to request all their resources 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.
Unit
Unit –– 4:
4: Deadlocks
Deadlocks 39
39 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
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 to 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.
Unit
Unit –– 4:
4: Deadlocks
Deadlocks 40
40 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Attacking the circular wait condition
Provide a global numbering of all the resources.
Now the rule is that: processes can request resources whenever
they want to, but all requests must be made 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.
Resource graph can never have cycle.
Unit
Unit –– 4:
4: Deadlocks
Deadlocks 41
41 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Questions asked in GTU
1. What is RAG? Explain briefly.
2. What is Deadlock? List the conditions that lead to deadlock. How
Deadlock can be prevented?
3. Which are the necessary conditions for Deadlock? Explain
Deadlock recovery in brief.
Unit
Unit –– 4:
4: Deadlocks
Deadlocks 42
42 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Questions asked in GTU
4. Consider the snapshot of the system with Five Processes and Four types of
resources A,B,C,D.
Allocation Max
A B C D A B C D
P0 0 0 1 2 0 0 1 2
P1 1 0 0 0 1 7 5 0
P2 1 3 5 4 2 3 5 6
P3 0 6 3 2 0 6 5 2
P4 0 0 1 4 0 6 5 6
Unit
Unit –– 4:
4: Deadlocks
Deadlocks 43
43 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology