1
BY THE NAME OF ALLAH
GROUP: MOUNTAIN OF
LIGHT
 MD. Saidur Rahman Kohinoor [142-15-3669]
 Md. Mudasser-ul Haque [142-15-3688]
 Obaidul Hassan [142-15-3559]
 Israt ZaHan Esha [142-15-3604]
 Md. Hamia [142-15-3458]
2
DEADLO
CK
Operating System
3
WHAT IS DEADLOCK?
o An example from US Kansas law:
o “When two trains approach each other at a
crossing, both shall come to a full stop and
neither shall start up again until the other
has gone.”
4
[142-15-3669]
REAL WORLD
DEADLOCKS!
5
• Truck A has to wait
for truck B to
move
• Not
deadlocked
[142-15-3669]
REAL WORLD DEADLOCKS!
6
Neither truck can proceed
[142-15-3669]
GRAPH WITHOUT A
DEADLOCK
7
R1
P1
P2
P3
R2
P4
[142-15-3669]
GRAPH WITH A DEADLOCK
8
R1
P1 P2 P3
R3
R2
R4
[142-15-3669]
DEALING WITH DEADLOCKS
o Stop a deadlock ever occuring
o deadlock prevention
o disallow at least one of the necessary
conditions
o deadlock avoidance
o see a deadlock coming and alter the
process/resource allocation strategy
9continued
[142-15-3669]
 Deadlock detection and recovery
 Ignore the problem
done by most OS, including UNIX
cheap solution
infrequent, manual reboots may be
acceptable
10
[142-15-3669]
DEADLOCK PREVENTION
 Eliminate one (or more) of:
mutual exclusion
hold and wait
no preemption (i.e. have preemption)
circular wait
11
[142-15-3688]
ELIMINATE MUTUAL
EXCLUSION
 Shared resources do not require mutual
exclusion
e.g. read-only files
 But some resources cannot be shared (at the
same time)
e.g. printers
12
[142-15-3688]
DEADLOCK AVOIDANCE
 In deadlock avoidance, the necessary
conditions are untouched.
 Instead, extra information about resources is
used by the OS to do better forward planning
of process/resource allocation
indirectly avoids circular wait
13
[142-15-3688]
SAFE STATES
• An OS is in a safe state if there is a
safe sequence of process executions
<P1, P2, …, Pn>.
• A safe state cannot lead to deadlock.
• An unsafe state may lead to deadlock.
• Deadlock is avoided by always keeping
the system in a safe state
– this may reduce resource utilization 14
Deadlock
Unsafe
Safe
[142-15-3559]
EXAMPLE 1
 Max no. of resources: 12 tape drives
 Max needs Current Allocation
P0 10 5
P1 4 2
P2 9 2
 Currently, there are 3 free tape drives
 The OS is in a safe state, since <P1, P0, P2> is a
safe sequence.
15
[142-15-3559]
EXAMPLE 2
 Same as last slide, but P2 now has 4 tape
drives allocated currently.
 Max needs Current Allocation
P0 10 5
P1 4 2
P2 9 4
 The OS is in an unsafe state.
16
[142-15-3559]
BANKER’S ALGORITHM
 Assume that:
a resource can have multiple instances
the OS has N processes, M resource types
 Initially, each process must declare the
maximum no. of resources it will need.
 Calculate a safe sequence if possible.
17
[142-15-3604]
WAIT-FOR GRAPH
• Assume that each resource has only one
instance.
• Create a wait-for graph by removing the
resource types nodes from a resource allocation
graph.
• Deadlock exists if and only if the wait-for graph
contains a cycle.
18
[142-15-3604]
EXAMPLE
19
R1
P2
R3 R4
R2 R5
P1 P3
P5
P4
P2P1 P3
P5
P4
[142-15-3604]
BANKER’S ALGORITHM
VARIATION
 If a resource type can have multiple instances,
then an algorithm very similar to the banker’s
algorithm can be used.
 The algorithm investigates every possible
allocation sequence for the processes that
remain to be completed.
20
[142-15-3604]
EXAMPLE
 Resource Type Instances
A 7
B 2
C 6
21continued
[142-15-3604]
Allocation Request Available
A B C A B C A B C
P0 0 1 0 0 0 0 0 0 0
P1 2 0 0 2 0 2
P2 3 0 3 0 0 0
P3 2 1 1 1 0 0
P4 0 0 2 0 0 2
 The OS is not in a deadlocked state since
<P0, P2, P3, P1, P4> is a safe sequence.
22
[142-15-3604]
EXAMPLE 2
 Change P2 to request 1 C instance
23
AllocationAllocation RequestRequest AvailableAvailable
A B CA B C A B CA B C A B CA B C
PP00 0 1 00 1 0 0 0 00 0 0 0 0 00 0 0
PP11 2 0 02 0 0 2 0 22 0 2
PP22 3 0 33 0 3 0 00 0 11
PP33 2 1 12 1 1 1 0 01 0 0
PP44 0 0 20 0 2 0 0 20 0 2
The OSThe OS isis deadlocked.deadlocked.
[142-15-3604]
DEADLOCK RECOVERY
 Tell the operator
 System-based recovery:
abort one or more processes in the circular
wait
preempt resources in one or more deadlocked
processes
24
[142-15-3458]
PROCESS TERMINATION
 Abort all deadlocked processes, or
 Abort one process at a time until the
deadlocked cycle disappears
not always easy to abort a process
choice should be based on minimum cost
25
[142-15-3458]
RESOURCE PREEMPTION
 Issues:
how to select a resource
(e.g. by using minimum cost)
how to rollback the process which has just
lost its resources
avoiding process starvation
26
[142-15-3458]
27
28

Operating System: Deadlock

  • 1.
    1 BY THE NAMEOF ALLAH
  • 2.
    GROUP: MOUNTAIN OF LIGHT MD. Saidur Rahman Kohinoor [142-15-3669]  Md. Mudasser-ul Haque [142-15-3688]  Obaidul Hassan [142-15-3559]  Israt ZaHan Esha [142-15-3604]  Md. Hamia [142-15-3458] 2
  • 3.
  • 4.
    WHAT IS DEADLOCK? oAn example from US Kansas law: o “When two trains approach each other at a crossing, both shall come to a full stop and neither shall start up again until the other has gone.” 4 [142-15-3669]
  • 5.
    REAL WORLD DEADLOCKS! 5 • TruckA has to wait for truck B to move • Not deadlocked [142-15-3669]
  • 6.
    REAL WORLD DEADLOCKS! 6 Neithertruck can proceed [142-15-3669]
  • 7.
  • 8.
    GRAPH WITH ADEADLOCK 8 R1 P1 P2 P3 R3 R2 R4 [142-15-3669]
  • 9.
    DEALING WITH DEADLOCKS oStop a deadlock ever occuring o deadlock prevention o disallow at least one of the necessary conditions o deadlock avoidance o see a deadlock coming and alter the process/resource allocation strategy 9continued [142-15-3669]
  • 10.
     Deadlock detectionand recovery  Ignore the problem done by most OS, including UNIX cheap solution infrequent, manual reboots may be acceptable 10 [142-15-3669]
  • 11.
    DEADLOCK PREVENTION  Eliminateone (or more) of: mutual exclusion hold and wait no preemption (i.e. have preemption) circular wait 11 [142-15-3688]
  • 12.
    ELIMINATE MUTUAL EXCLUSION  Sharedresources do not require mutual exclusion e.g. read-only files  But some resources cannot be shared (at the same time) e.g. printers 12 [142-15-3688]
  • 13.
    DEADLOCK AVOIDANCE  Indeadlock avoidance, the necessary conditions are untouched.  Instead, extra information about resources is used by the OS to do better forward planning of process/resource allocation indirectly avoids circular wait 13 [142-15-3688]
  • 14.
    SAFE STATES • AnOS is in a safe state if there is a safe sequence of process executions <P1, P2, …, Pn>. • A safe state cannot lead to deadlock. • An unsafe state may lead to deadlock. • Deadlock is avoided by always keeping the system in a safe state – this may reduce resource utilization 14 Deadlock Unsafe Safe [142-15-3559]
  • 15.
    EXAMPLE 1  Maxno. of resources: 12 tape drives  Max needs Current Allocation P0 10 5 P1 4 2 P2 9 2  Currently, there are 3 free tape drives  The OS is in a safe state, since <P1, P0, P2> is a safe sequence. 15 [142-15-3559]
  • 16.
    EXAMPLE 2  Sameas last slide, but P2 now has 4 tape drives allocated currently.  Max needs Current Allocation P0 10 5 P1 4 2 P2 9 4  The OS is in an unsafe state. 16 [142-15-3559]
  • 17.
    BANKER’S ALGORITHM  Assumethat: a resource can have multiple instances the OS has N processes, M resource types  Initially, each process must declare the maximum no. of resources it will need.  Calculate a safe sequence if possible. 17 [142-15-3604]
  • 18.
    WAIT-FOR GRAPH • Assumethat each resource has only one instance. • Create a wait-for graph by removing the resource types nodes from a resource allocation graph. • Deadlock exists if and only if the wait-for graph contains a cycle. 18 [142-15-3604]
  • 19.
    EXAMPLE 19 R1 P2 R3 R4 R2 R5 P1P3 P5 P4 P2P1 P3 P5 P4 [142-15-3604]
  • 20.
    BANKER’S ALGORITHM VARIATION  Ifa resource type can have multiple instances, then an algorithm very similar to the banker’s algorithm can be used.  The algorithm investigates every possible allocation sequence for the processes that remain to be completed. 20 [142-15-3604]
  • 21.
    EXAMPLE  Resource TypeInstances A 7 B 2 C 6 21continued [142-15-3604]
  • 22.
    Allocation Request Available AB C A B C A B C P0 0 1 0 0 0 0 0 0 0 P1 2 0 0 2 0 2 P2 3 0 3 0 0 0 P3 2 1 1 1 0 0 P4 0 0 2 0 0 2  The OS is not in a deadlocked state since <P0, P2, P3, P1, P4> is a safe sequence. 22 [142-15-3604]
  • 23.
    EXAMPLE 2  ChangeP2 to request 1 C instance 23 AllocationAllocation RequestRequest AvailableAvailable A B CA B C A B CA B C A B CA B C PP00 0 1 00 1 0 0 0 00 0 0 0 0 00 0 0 PP11 2 0 02 0 0 2 0 22 0 2 PP22 3 0 33 0 3 0 00 0 11 PP33 2 1 12 1 1 1 0 01 0 0 PP44 0 0 20 0 2 0 0 20 0 2 The OSThe OS isis deadlocked.deadlocked. [142-15-3604]
  • 24.
    DEADLOCK RECOVERY  Tellthe operator  System-based recovery: abort one or more processes in the circular wait preempt resources in one or more deadlocked processes 24 [142-15-3458]
  • 25.
    PROCESS TERMINATION  Abortall deadlocked processes, or  Abort one process at a time until the deadlocked cycle disappears not always easy to abort a process choice should be based on minimum cost 25 [142-15-3458]
  • 26.
    RESOURCE PREEMPTION  Issues: howto select a resource (e.g. by using minimum cost) how to rollback the process which has just lost its resources avoiding process starvation 26 [142-15-3458]
  • 27.
  • 28.