More Related Content
Similar to Deadlocks (20)
More from Dwight Sabio (20)
Deadlocks
- 2. INTROOS Slides - ccsmwt
Copyright© De La Salle University
Professional Schools, Inc.
College of Computer Studies
Introduction
Several processes may compete for a
finite number of resources
If a process requests for a resource but
it is not available at the time, the
process enters into a wait state
If the resource is held by another
process who is also waiting, the
situation is called a deadlock
- 3. INTROOS Slides - ccsmwt
Copyright© De La Salle University
Professional Schools, Inc.
College of Computer Studies
System Model
Types of Resources:
1. Memory space
2. CPU cycles
3. Files
4. I/O devices – printers, tape drives,
etc
- 4. INTROOS Slides - ccsmwt
Copyright© De La Salle University
Professional Schools, Inc.
College of Computer Studies
System Model
If a process requests for an instance of a
resource type, the allocation of any
instance of the type will satisfy the request
A process must request a resource before
using it.
A process must release the resource after
using it
A process’ number of requests cannot
exceed the total number of resources
available in the system
- 5. INTROOS Slides - ccsmwt
Copyright© De La Salle University
Professional Schools, Inc.
College of Computer Studies
Using a resource
A process may utilize a resource only
in the following sequence:
1. Request … wait if not available
2. Use
3. Release
- 6. INTROOS Slides - ccsmwt
Copyright© De La Salle University
Professional Schools, Inc.
College of Computer Studies
When do Deadlock occur?
A set of processes is in a deadlock state when
every process in the set is waiting for an event
that can be caused by only another process in
the set.
P1
P2
P3
P2
P3
P1
- 7. INTROOS Slides - ccsmwt
Copyright© De La Salle University
Professional Schools, Inc.
College of Computer Studies
Deadlock Characterization
In a deadlock, processes never finish
executing
System resources are tied up
To detect a deadlock, all of the
necessary conditions must hold
simultaneously
- 8. INTROOS Slides - ccsmwt
Copyright© De La Salle University
Professional Schools, Inc.
College of Computer Studies
Necessary Conditions
1. Mutual Exclusion – only one process at a
time can use the resource. It can only be
used if the resource has been released.
2. Hold and Wait –There must exist a
process that is holding at least one
resource and is waiting to acquire
additional resources that are currently
being held by other processes.
- 9. INTROOS Slides - ccsmwt
Copyright© De La Salle University
Professional Schools, Inc.
College of Computer Studies
Necessary Conditions
3. No Preemption – Resources cannot
be preempted
4. Circular Wait
P0 P1 P2 … Pn
- 10. INTROOS Slides - ccsmwt
Copyright© De La Salle University
Professional Schools, Inc.
College of Computer Studies
Resource Allocation Graph
The graph consists of a set of vertices V
and a set of edges E. V is partitioned into
2 types:
P = {P1, P2, …, Pn}, the set consisting
of all processes in the system;
R = {R1, R2,…,Rm}, the set containing
all resource types in the system
Pi
Rj
.
.
Rj
- 11. INTROOS Slides - ccsmwt
Copyright© De La Salle University
Professional Schools, Inc.
College of Computer Studies
Resource Allocation Graph
(RAG)
A directed edge from process Pi
to resource type Rj is denoted by
PiRj; it signifies that process Pi
requested an instance of
resource type Rj. (Request Edge)
A directed edge from resource
type Rj to process Pi is denoted
by RjPi; it signifies that Rj has
been allocated to process Pi
(Assignment Edge)
Pi
Pi
Rj
Rj
- 12. INTROOS Slides - ccsmwt
Copyright© De La Salle University
Professional Schools, Inc.
College of Computer Studies
Example RAG
Given: Sets P, R, and E
P = {P1, P2, P3}
R = {R1, R2, R3}
E = {P1R1, P2R3, R1 P2, R2
P2, R2P1, R3 P3}
Resource Instances:
1 R1; 2 R2; 1 R3
- 13. INTROOS Slides - ccsmwt
Copyright© De La Salle University
Professional Schools, Inc.
College of Computer Studies
Example RAG
Process States:
Process P1 is holding an
instance of resource type R2, and is
waiting for an instance resource type R1
Process P2 is holding an instance of R1
and R2, and is waiting for an instance of
resource type R3
Process P3 is holding an instance of R3
- 14. INTROOS Slides - ccsmwt
Copyright© De La Salle University
Professional Schools, Inc.
College of Computer Studies
Is there a deadlock?
If the graph contains no cycles, then no
process in the system is deadlocked.
If a graph with multiple instances of
resources contains a cycle, then there
may be a deadlock
If there is only 1 instance of each
resource, then a deadlock has occurred
- 15. INTROOS Slides - ccsmwt
Copyright© De La Salle University
Professional Schools, Inc.
College of Computer Studies
Methods for Handling Deadlocks
1. Use a protocol to ensure that the system
will never enter a deadlock state
a. Deadlock Prevention
b. Deadlock Avoidance
2. Allow the system to enter a deadlock state
and then recover
a. Deadlock Detection
b. Deadlock Recovery
3. Ignore the problem and pretend that
deadlock never occurred
- 16. INTROOS Slides - ccsmwt
Copyright© De La Salle University
Professional Schools, Inc.
College of Computer Studies
Deadlock Prevention
For a deadlock to occur,
each of the four necessary
condition holds
By ensuring that at least one
of the necessary conditions
cannot hold, deadlock is
prevented
- 17. INTROOS Slides - ccsmwt
Copyright© De La Salle University
Professional Schools, Inc.
College of Computer Studies
Deadlock Prevention
1. Mutual Exclusion
Make the resources sharable so that it
cannot be involved in a deadlock
A process never needs to wait for a
sharable resource
However, some resources are
unsharable
- 18. INTROOS Slides - ccsmwt
Copyright© De La Salle University
Professional Schools, Inc.
College of Computer Studies
Deadlock Prevention
2. Hold and Wait
Ensure that whenever a process requests
a resource, it doesn’t hold any other
resources
Protocol:
1. All resources are requested and must be
allocated before execution begins
2. All resources being held at present are
released before the new request is made
- 19. INTROOS Slides - ccsmwt
Copyright© De La Salle University
Professional Schools, Inc.
College of Computer Studies
Deadlock Prevention
3. Preemption
Resources already allocated can be preempted
If a process that is holding some resources
request for another that cannot be allocated
immediately, then all resources currently held
are preempted/released
The preempted resources are added to the
resources it is waiting
Restart only when it regains its old resources, as
well as the new ones it is requesting
Some resources cannot be released
- 20. INTROOS Slides - ccsmwt
Copyright© De La Salle University
Professional Schools, Inc.
College of Computer Studies
Deadlock Prevention
4. Circular Wait
Prevent circular wait to happen
Impose a total ordering of all resource types and
require that each process requests resources in
an increasing order of enumeration
A process may only request a new resource
provided that the new resource’s value is the
largest value among all currently held resources
- 21. INTROOS Slides - ccsmwt
Copyright© De La Salle University
Professional Schools, Inc.
College of Computer Studies
Deadlock Prevention
Example: Circular Wait
F (tape drive) = 1
F (disk drive) = 5
F (printer) = 12
If Process 1 holds the disk drive and requests
for the tape drive, it has to relinquish the disk
drive first. However, if process 1 requests for
the printer, it is allocated to it immediately.
- 22. INTROOS Slides - ccsmwt
Copyright© De La Salle University
Professional Schools, Inc.
College of Computer Studies
Deadlock Avoidance
OS will be given, in advance, additional
information concerning which resources a
process will request and use during its
lifetime
Using this information, the OS can decide
for each request whether the process
should wait or not
Avoidance is performed by monitoring the
system and try to avoid an unsafe state
- 23. INTROOS Slides - ccsmwt
Copyright© De La Salle University
Professional Schools, Inc.
College of Computer Studies
Safe State
A state is safe if the system can allocate
resources to each process (up to the
maximum) in some order and still avoid
a deadlock
A state is safe only if there exists a safe
sequence
- 24. INTROOS Slides - ccsmwt
Copyright© De La Salle University
Professional Schools, Inc.
College of Computer Studies
Safe State
A sequence of processes <P1
.. Pn
> is a
safe sequence if for each Pi
, the
resources that Pi
can still request can
be satisfied by the currently available
resources plus the resources held by all
the Pj
, with j<i
- 25. INTROOS Slides - ccsmwt
Copyright© De La Salle University
Professional Schools, Inc.
College of Computer Studies
Safe State
If the resources that process Pi
needs
are not immediately available, then Pi
can wait until all Pj
have finished. When
they finished, Pi
can obtain all its
needed resources. When Pi
terminates,
Pi+1
can obtain its needed resources
deadlock state is an unsafe state
not all unsafe states are in deadlock
- 26. INTROOS Slides - ccsmwt
Copyright© De La Salle University
Professional Schools, Inc.
College of Computer Studies
Deadlock Avoidance
Example: Given 12 magnetic tape drives
Process Max
P0 10
P1 4
P2 9
1.) t0:
Process Holds
P0 5
P1 2
P2 2
2.) Suppose that P2 requested and is allocated 1
more tape drive
• deadlock avoidance ensures that the system will always
remain in a safe state
- 27. INTROOS Slides - ccsmwt
Copyright© De La Salle University
Professional Schools, Inc.
College of Computer Studies
Resource Allocation Graph
For single instances of resource types
a claim edge Pi Rj indicates that process
Pi may request resource Rj at some time in
the future. (using a dashed line)
When process Pi requests resource Rj, the
claim edge Pi Rj is converted to a request
edge
When a resource Rj is released by Pi, the
assignment edge Rj Pi is reconverted to a
claim edge Pi Rj
- 28. INTROOS Slides - ccsmwt
Copyright© De La Salle University
Professional Schools, Inc.
College of Computer Studies
Resource Allocation Graph
Before process Pi starts executing, all its
claim edges must already appear in the RAG
Allow a claim edge Pi Rj to be added to the
graph only if all the edges associated with
process Pi are claim edges
If Pi requests resource Rj, the request can be
granted only if converting the request edge Pi
Rj to an assignment edge Rj Pi does
not result in the formation of a cycle
- 29. INTROOS Slides - ccsmwt
Copyright© De La Salle University
Professional Schools, Inc.
College of Computer Studies
Resource Allocation Graph
If no cycle exists, then the allocation of
the resource will leave the system in a
safe state
If a cycle is found, then the
allocation will put the
system in an unsafe
state
- 30. INTROOS Slides - ccsmwt
Copyright© De La Salle University
Professional Schools, Inc.
College of Computer Studies
Resource Allocation Graph
R1
R2
P1 P2
- 31. INTROOS Slides - ccsmwt
Copyright© De La Salle University
Professional Schools, Inc.
College of Computer Studies
Banker’s Algorithm
applicable to multiple instances of
resource types
when a new process enters the system, it
must declare the maximum number of
instances of each resource type that it may
need
when a user requests, OS must determine if it
will result to an unsafe state
- 32. INTROOS Slides - ccsmwt
Copyright© De La Salle University
Professional Schools, Inc.
College of Computer Studies
Data Structures Needed
Let n be the number of processes in the system
and m be the number of resource types
Available - number of available resources of
each type
Max - defines the maximum demand of
each process
Allocation- defines the number of resources of
each type currently allocated to
each process
Need - indicates the remaining resource
need of each process
- 33. INTROOS Slides - ccsmwt
Copyright© De La Salle University
Professional Schools, Inc.
College of Computer Studies
Data Structures Needed
Available [j] = k Rj has k instances available
Max [i,j] = k Pi may request at most K instances of Rj
Allocation [i,j] = k Process Pi is currently allocated
k instances of Rj
Need [ i,j] = k Pi may need k more instances of Rj
Need [i,j] = Max[i,j] – Allocation[i,j]
- 34. INTROOS Slides - ccsmwt
Copyright© De La Salle University
Professional Schools, Inc.
College of Computer Studies
Notations
Let X & Y be vectors of length n. We
say that X <= Y if and only if X[i] <=
Y[i] for all i = 1,2,3,..n
We can treat each row in the matrices
Allocation & Need as vectors & refer to them as
Allocationi
& Needi
, respectively
Allocationi
specifies the resources currently
allocated to Pi
Needi
specifies the additional resources that
process Pi may still request to complete its task
- 35. INTROOS Slides - ccsmwt
Copyright© De La Salle University
Professional Schools, Inc.
College of Computer Studies
Safety Algorithm
1. Let Work & Finish be vectors of length
m & n, respectively. Initialize Work :=
Available & Finish [i] := false, for all
processes Pi
2. Find an i such that
a. Finish [i] = false, and
b. Needi <= Work
if no such i exists, go to step 4
- 36. INTROOS Slides - ccsmwt
Copyright© De La Salle University
Professional Schools, Inc.
College of Computer Studies
Safety Algorithm
3. Work := Work + Allocationi
Finish [i] = true
go to step 2
4. If Finish[i] = true for all i, then the
system is in a safe state
- 37. INTROOS Slides - ccsmwt
Copyright© De La Salle University
Professional Schools, Inc.
College of Computer Studies
Resource Request Algorithm
Let Requesti be the request vector for process
Pi. If Requesti[j] = k, then Pi wants k instances
of resource type Rj
When a request for resources is made by
process Pi, the ff actions are taken:
1. If Requesti <= Needi, go to step 2.
Otherwise, raise an error condition, since
the process has exceeded its max. claim
- 38. INTROOS Slides - ccsmwt
Copyright© De La Salle University
Professional Schools, Inc.
College of Computer Studies
Resource Request Algorithm
2. If Requesti <= Available, go to step 3.
Otherwise, Pi must wait, since the resources
are not available
3. Have the system pretend to have allocated
the requested resources to process Pi by
modifying the state as follows:
Available := Available - Requesti
Allocation := Allocationi + Requesti
Needi := Needi - Requesti
- 39. INTROOS Slides - ccsmwt
Copyright© De La Salle University
Professional Schools, Inc.
College of Computer Studies
Resource Request Algorithm
If the resulting resource
allocation state is safe, the
transaction is completed and
process Pi is allocated its
resources. However, if the
new state is unsafe, then Pi
must wait for Requesti and
the old resource-allocation
state is restored.
- 40. INTROOS Slides - ccsmwt
Copyright© De La Salle University
Professional Schools, Inc.
College of Computer Studies
Example
PROCESS Allocation Max Available
A B C A B C A B C
P0 0 1 0 7 5 3 3 3 2
P1 2 0 0 3 2 2
P2 3 0 2 9 0 2
P3 2 1 1 2 2 2
P4 0 0 2 4 3 3
Given 5 processes
3 resources A, B, C
A = 10 instances
B = 5 instances
C = 7 instances
1. Request of P1 = (1,0,2)
2. Request of P4 = (3,3,0)
3. Request of P0 = (0,2,0)
- 41. INTROOS Slides - ccsmwt
Copyright© De La Salle University
Professional Schools, Inc.
College of Computer Studies
Deadlock Detection
If no deadlock prevention or avoidance
algorithm is available, the system may
have a deadlock situation
System must provide:
Algorithm that examines the state of the
system to determine whether a deadlock
has occurred
Algorithm to recover from deadlock
- 42. INTROOS Slides - ccsmwt
Copyright© De La Salle University
Professional Schools, Inc.
College of Computer Studies
Single Instance Resource
Type
Wait-for graph – same as RAG but remove
the nodes of type resource & collapse the
appropriate edges
An edge from Pi to Pj in a WFG implies that
process Pi is waiting for process Pj to release
a resource that Pi needs
An edge from Pi Pj exists in a WFG iff the
corresponding RAG contains 2 edges Pi Rq
& Rq Pj for some resource Rq
- 43. INTROOS Slides - ccsmwt
Copyright© De La Salle University
Professional Schools, Inc.
College of Computer Studies
Example
R3R1
P5
P3
R4
P2P1
P4 R5R2
Note:
Deadlock exists iff
the WFG contains a
cycle
- 44. INTROOS Slides - ccsmwt
Copyright© De La Salle University
Professional Schools, Inc.
College of Computer Studies
Several Instances Resource
Type
Data Structures
Available - # of available resource type
Allocation - # of resources of each type
currently allocated to each
process
Request - n X m matrix that indicates
the current request of each
process
- 45. INTROOS Slides - ccsmwt
Copyright© De La Salle University
Professional Schools, Inc.
College of Computer Studies
Algorithm
1. Let Work and Finish be vectors of length m
& n, respectively. Initialize Work :=
Available. For i = 1,2,..,n, if Allocationi
≠ 0,
then Finish[i]:= false; otherwise,
Finish[i]:=true
2. Find an index i such that
a. Finish [i] := false, and
b. Requesti
<= Work
If no such i exists, go to step 4
- 46. INTROOS Slides - ccsmwt
Copyright© De La Salle University
Professional Schools, Inc.
College of Computer Studies
Algorithm
3. Work := Work + Allocationi
Finish[i] := true
Go to step 2
4. If Finish[i] = false, for some i, 1 <= i <=
n, then the system is in a Deadlock
state. Moreover, if Finish[i] = false,
then process Pi is deadlocked
- 47. INTROOS Slides - ccsmwt
Copyright© De La Salle University
Professional Schools, Inc.
College of Computer Studies
Example 1
PROCES
S
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
Given 5 processes
3 resource types A,B,C
A = 7 instances
B = 2 instances
C = 6 instances
- 48. INTROOS Slides - ccsmwt
Copyright© De La Salle University
Professional Schools, Inc.
College of Computer Studies
Example 2
PROCES
S
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 1
P3 2 1 1 1 0 0
P4 0 0 2 0 0 2
Given 5 processes
3 resource types A,B,C
A = 7 instances
B = 2 instances
C = 6 instances
- 49. INTROOS Slides - ccsmwt
Copyright© De La Salle University
Professional Schools, Inc.
College of Computer Studies
Recovery from Deadlock
Options:
1. Abort one or more processes
2. Preempt some resources from one or
more of the deadlocked processes
Process Termination
1. Abort all deadlocked processes
2. Abort one process at a time until the
deadlock cycle is eliminated
- 50. INTROOS Slides - ccsmwt
Copyright© De La Salle University
Professional Schools, Inc.
College of Computer Studies
Recovery from Deadlock
Resource Preemption
Preempt some resources from processes &
given theses resources to other processes until
the deadlock cycle is broken
Issues
1. Selecting a Victim
2. Rollback
3. Starvation