Professional Documents
Culture Documents
Operating Systems
CS 340 D
Introduction
Chapter 5: Deadlocks
3
OBJECTIVES:
4
The Deadlock Problem
5
The Deadlock Problem
6
The Deadlock Problem (cont..)
Example
o System has 2 disk drives
o P1 and P2 each hold one disk drive and each needs the other
one
Note :– Most OSs do not prevent or deal with deadlocks
7
Bridge Crossing Example
Instances are
Each resource type Ri has Wi instances. identical
o E.g :
If a system has (2) CPUs, then the resource type CPU has (2) instances.
The resource type printer may have (5) instances.
9
System Model (cont.)
1. Request
>> If the request cannot be granted immediately, then the
requesting process must wait until it can acquire the resource.
2. Use
3. Release
10
System Model (cont.)
Resource forms:
Physical resources Logical resources
e.g. printers, DVD drives, e.g. files
memory space, and CPU
cycles
Example:
consider a system with one printer and one DVD drive. Suppose that
process Pi is holding the DVD and process Pj is holding the printer. If Pi
requests the printer and Pj requests the DVD drive, a deadlock occurs.
11
The Deadlock
Characterization
12
Deadlock Characterization
13
Deadlock Characterization (cont..)
4. Circular wait:
There exists a set {P0, P1, …, Pn} of waiting processes such that :
o P0 is waiting for a resource that is held by P1,
o P1 is waiting for a resource that is held by P2, …, Pn–1 is
waiting for a resource that is held by Pn,
o Pn is waiting for a resource that is held by P0.
P0 P1 P2 Pn
14
Resource-Allocation Graph
15
Resource-Allocation Graph
16
Resource-Allocation Graph (Cont.)
Process
Pi requests instance of Rj Pi
Rj
Pi is holding an instance of Rj
Pi
Rj
17
Example of a Resource Allocation Graph
18
Example of a Resource Allocation Graph
Process states:
19
How To Detect Deadlocks
Using Resource Allocation Graph
20
How To Detect Deadlocks
Using Resource Allocation Graph (as a Flowchart)
Is there is
a cycle?
y N
Is all R’s has NO D.L.
only one
instance?
y N
It is a D.L. Is cycle
breakable?
y N
NO D.L. It is a D.L.
21
Resource Allocation Graph - Example (1)
Example (1):
NO deadlock because the graph contains NO cycles
22
Resource Allocation Graph - Example (2)
Example (2):
The graph contains
(2) cycles
Cycle (1) : P1 → R1 → P2 → R3 → P3 → R2 → P1
Cycle (2) : P2 → R3 → P3 → R2 → P2
23
Resource Allocation Graph - Example (3)
Example (3):
The graph contains one cycle
Cycle : P1 → R1 → P3 → R2 → P1
25
Methods for Handling Deadlocks
26
Method (1) for Handling Deadlocks
27
Method(1) for Handling Deadlocks
28
1- Deadlock Prevention
29
Deadlock Prevention
•The process must request all its • or allow process to request resources
resources before execution. only when the process has none
3- No Preemption –
If a process that is holding some resources requests another resource that
cannot be immediately allocated to it, then all resources currently
being held are released
Process will be restarted only when it can regain its old resources, as well
as the new ones that it is requesting
It is applied to resources whose state easily saved and restored later, such
as CPU registers and memory space but it not applicable for resources
such as printers
The main advantages is more better resource utilization
Problems
The cost of removing a process's resources
starvation possible
It is not applicable for all resource types such as printers
31
Deadlock Prevention (Cont.)
4- Circular Wait -
One way to ensure that this condition never holds is to impose a
total ordering of all resource types and to require that
each process requests resources in an increasing order of
enumeration.
let R = {R1, R2, ..., Rm} be the set of resource types. We assign
to each resource type a unique integer number, which allows us
to compare two resources and to determine whether one
precedes another in our ordering.
Example : if the set of resource types R includes tape drives, disk drives,
and printers,
then the function F might be defined as follows:
F(tape drive) = 1
F(disk drive) = 5
F(printer) = 12
33
Deadlock Prevention (Cont.)
4- Circular Wait – cont..
EX: a process that wants to use the tape drive and printer at
the same time must first request the tape drive and then request
the printer.
Problems
Resources must be requested in ascending order of
resource number rather than as needed
34
Thank you
End of Chapter 5