Deadlocks

advertisement
Operating Systems
CST 352
Deadlock
4/9/2015
CST 352 - Operating Systems
1
Topics
Definitions
Deadlock Conditions
Ostrich Algorithm
Deadlock Detection
Deadlock Recovery
Deadlock Avoidance
Deadlock Prevention
4/9/2015
CST 352 - Operating Systems
2
Definitions
Resources – Parts of a system that are
required for a process to run (e.g.
memory, disk, CPU, etc.)
Preemptable – A resource that can be
taken from a process without adverse
affects (e.g. seized memory)
Non Preemtable– A resource that cannot
be taken away (e.g. a device driver
connected to printer)
4/9/2015
CST 352 - Operating Systems
3
Definitions
Deadlock – In a set of processes, each
process is waiting for a resource that
one of the other process has seized.
Remember the dining philosophers.
4/9/2015
CST 352 - Operating Systems
4
Definitions
A Traffic Deadlock
All Stop…..
Ready…..
Go!
4/9/2015
CST 352 - Operating Systems
5
Definitions
A Traffic Deadlock
Oops….
Deadlock
How is this
deadlock
avoided under
normal
circumstances?
4/9/2015
CST 352 - Operating Systems
6
Definitions
Question:
1.
2.
4/9/2015
Which of the two types of
resource can lead to a deadlock
condition?
Why?
CST 352 - Operating Systems
7
Definition
Question:
Can there be deadlock in a “firstcome, first-serve” scheduling
scheme?
4/9/2015
CST 352 - Operating Systems
8
Required Conditions
Four conditions necessary for Deadlock:
1. Mutual Exclusion – Only one process can access
a resource at any given time.
2. Hold and Wait – A process may hold a resource
while waiting for others to become available.
3. No preemption – No resource may be forcibly
taken from a holding process.
4. Circular wait – There must be at least two
processes, each waiting for a resource held by the
other.
4/9/2015
CST 352 - Operating Systems
9
Deadlock Concepts
Modeling Deadlock (grid method)
X – axis: progress of process A.
Y – axis: progress of process B.
This gives us a good feel for deadlock overlap,
however, it becomes unmanageable for
more than two processes.
4/9/2015
CST 352 - Operating Systems
10
Modeling
Deadlock
(Resource
Trajectories)
Example: Proc A
and Proc B
competing
for Resource
1 and
Resource 2.
Process B
Deadlock Concepts
Rel 1
Proc A and
Proc B want
Resource 1
Rel 2
Get 1
Deadlock
Pending
Get 2
Get 1
4/9/2015
Proc A and
Proc B want
Resource 2
Get 2
CST 352 - Operating Systems
Rel 1
Rel 2
Process A
11
1.
2.
3.
4.
5.
6.
7.
8.
Process B
Deadlock Concepts
A Executes
B Executes
Rel 1
A Executes
B acquires 2
Rel 2
B acquires 1
B releases 2
Get 1
B releases 1
A can acquire both 1Get 2
and 2
Proc A and
Proc B want
Resource 1
Deadlock
Pending
Get 1
4/9/2015
Get 2
CST 352 - Operating Systems
Proc A and
Proc B want
Resource 2
Rel 1
Rel 2
Process A
12
1.
2.
3.
4.
5.
6.
7.
8.
9.
Process B
Deadlock Concepts
A Executes
B Executes
Rel 1
A Executes
B acquires 2
Rel 2
B acquires 1
A blocks on 1
Get 1
B releases 2
B releases 1
Get 2
A can acquire both 1
and 2
Proc A and
Proc B want
Resource 1
Deadlock
Pending
Get 1
4/9/2015
Get 2
CST 352 - Operating Systems
Proc A and
Proc B want
Resource 2
Rel 1
Rel 2
Process A
13
1.
2.
3.
4.
5.
6.
7.
8.
A Executes
B Executes
A Executes
B acquires 2
A acquires 1
B blocks on 1
A blocks on 2
Deadlock
Process B
Deadlock Concepts
Rel 1
Proc A and
Proc B want
Resource 1
Rel 2
X
Get 1
Deadlock
Pending
Proc A and
Proc B want
Resource 2
Get 2
Get 1
4/9/2015
Get 2
CST 352 - Operating Systems
Rel 1
Rel 2
Process A
14
To reduce resource
contention, construct
Process A such that it
does not need both
resource 1 and
resource 2 at the
same time.
This increases the
number of valid
paths through the
“gray” areas.
Process B
Deadlock Concepts
Rel 1
Rel 2
Proc A and
Proc B
want
Resource 1
Proc A and
Proc B
want
Resource 2
Get 1
Get 2
Get 1
4/9/2015
Rel 1
CST 352 - Operating Systems
Get 2
Rel 2
Process A
15
Deadlock Concepts
Modeling Deadlock (grid method)
The resource trajectory model has
disadvantages
Unmanageable for complex systems
Dependent on resource acquisition sequencing
This model does suggest solutions to
deadlock (e.g. avoid getting into the
“Deadlock Pending” region.
4/9/2015
CST 352 - Operating Systems
16
Deadlock Concepts
Modeling Deadlock (graph method)
A circle represents a process
A square represents a resource
A directed arch from a circle to a square
represents a process requesting a resource.
P1 requesting R1
P1
R1
A directed arch from square to circle represents a
process holding a resource
P1 holding R1
P1
R1
A Deadlock is condition is represented by a
“cycle” in the graph.
4/9/2015
CST 352 - Operating Systems
17
Deadlock Concepts
Modeling Deadlock (graph method)
Example:
Processes P1, P2, and P3
Resources R1, R2, and R3
4/9/2015
P1
P2
P3
R1
R2
R3
CST 352 - Operating Systems
18
Deadlock Concepts
Modeling Deadlock (graph method)
1.
P1 requests and holds R1
2.
P2 requests and holds R2
3.
P3 requests and holds R3
4.
P1 requests R2
5.
P2 requests R3
6.
P3 requests R1
The cyclic condition
of the graph indicates
this sequence of event
will cause deadlock.
4/9/2015
P1
P2
P3
R1
R2
R3
CST 352 - Operating Systems
19
Ostrich Algorithm
Stick your head in the sand and
pretend there is no problem at
all.
Mathematically speaking – this is
unacceptable.
From an engineering point of
view, this may be ok.
4/9/2015
CST 352 - Operating Systems
20
Ostrich Algorithm
Considerations:
What is the half-life of your OS.
What resource contentions could
cause a deadlock.
How often statistically does your
OS experience a visible deadlock.
4/9/2015
CST 352 - Operating Systems
21
Ostrich Algorithm
The Ostrich Algorithm is the easiest
solution to the deadlock problem.
In standard “user” based systems (e.g.
Windows, Unix Workstations, etc.),
this is perfectly acceptable.
Would you consider the “Ostrich
Algorithm” an acceptable solution for
an enterprise management system?
4/9/2015
CST 352 - Operating Systems
22
Deadlock Detection
Graph based approach
For each resource access
Create a node representing the
process accessing the resource.
Create a node representing the
resource.
Make the appropriate directed
association.
4/9/2015
CST 352 - Operating Systems
23
Deadlock Detection
Graph based approach (example)
1.
2.
3.
4.
5.
6.
PA requests R1 and gets it.
PB requests R2 and gets it.
PC requests R2.
PD requests R3 and gets it.
PD requests R1.
PA requests R3.
Every time a resource
request is made:
1. Add the appropriate
node to the graph
2. Check the graph for
cycles.
4/9/2015
PA
R1
PB
R2
PC
R3
CST 352 - Operating Systems
PD
24
Deadlock Detection
Graph based approach
To implement the graph based
approach, the OS needs to:
Build a graph on the fly based on
resource requests.
Provide some form of cycle
detection to detect deadlock
conditions in the constructed graph.
4/9/2015
CST 352 - Operating Systems
25
Deadlock Detection
Q:
1.
4/9/2015
List the four conditions for
deadlock.
CST 352 - Operating Systems
26
Deadlock Detection
Matrix Approach
Let “n” = number of processes == (P1,
…, Pn).
Let “m” = number of resource classes.
E1 – resource of type 1
E2 – resource of type 2
…
Em – resource of type m
4/9/2015
CST 352 - Operating Systems
27
Deadlock Detection
Matrix Approach
At any point in time, the allocation
of resources can be represented by
a two vectors.
(E1, E2, …, Em) – Existing resource
vector.
(A1, A2, …, Am) – Available resource
vector.
4/9/2015
CST 352 - Operating Systems
28
Deadlock Detection
Matrix Approach
Represent allocation with a matrix
Columns represent resources
Rows represent processes
Represent requests with a matrix
Columns represent resources
Rows represent processes
4/9/2015
CST 352 - Operating Systems
29
Deadlock Detection
Matrix Approach
Current Allocation and Request
c11
c22
…
cn1
c12 … c1m
c22 … c2m
…
…
cn2 … cnm
r11
r22
…
rn1
Current Allocation
4/9/2015
CST 352 - Operating Systems
r12 … r1m
r22 … r2m
…
…
rn2 … rnm
Current Request
30
Deadlock Detection
Matrix Approach
At any point in time, the resource
state must conform to the equation:
For any j (j -> resource “column”):
n
cij+Aj = Ej
i=1
e.g. allocated instances of resource j + available
instances of resource j = number of existing instances
of resource j.
4/9/2015
CST 352 - Operating Systems
31
Deadlock Detection
Matrix Approach
Process allocation is now done by
comparing vectors.
Vector A <= B iff
For all i < m, Ai <= Bi
Initialize the processes to “r” for “run”.
As the algorithm proceeds, set the process
flags to either “r” for processes capable of
running, or “b” for processes blocked and
waiting for a resource.
4/9/2015
CST 352 - Operating Systems
32
Deadlock Detection
Matrix Approach
Every time there is a resource allocation:
Look for a process (Pi) for which the request row (Ri)
<= available resources (Ai).
Looking for resource demands that can be met.
If such a process is found, add the ith row of the c matrix
to the A vector.
Loop over next request.
Else
Terminate the detection algorithm and set Pi to “b”
All “b” processes are blocked.
4/9/2015
CST 352 - Operating Systems
33
Deadlock Detection
Matrix Approach
In practical application, keep the matrices as
a running total.
Each time there is a resource allocation,
update the matrices.
Each time there is a resource de-allocation,
update the matrices.
Deadlock occurs when two (or more)
processes have cross referenced resources.
4/9/2015
CST 352 - Operating Systems
34
Deadlock Detection
Matrix Approach (Example)
Current
0
C= 0
0
Allocation
0 0
0 0
0 0
DVD
Printer
DVD
CD Rom
Printer
E = [3 2 2]
CD Rom
Available Vector
Resource Vector
A = [3 2 2]
P1 r
P2 r
P3 r
Request
2 0 0
R= 0 0 0
0 0 0
1) P1 requests 2 Printers
4/9/2015
CST 352 - Operating Systems
35
Deadlock Detection
Matrix Approach (Example)
Current
2
C= 0
0
4/9/2015
Allocation
0 0
0 0
0 0
DVD
Printer
DVD
CD Rom
Printer
E = [3 2 2]
CD Rom
Available Vector
Resource Vector
A = [1 2 2]
P1 r
P2 r
P3 r
Request
0 0 0
R= 0 0 0
0 0 0
CST 352 - Operating Systems
36
Deadlock Detection
Matrix Approach (Example)
Current
2
C= 0
0
Allocation
0 0
0 0
0 0
DVD
Printer
DVD
CD Rom
Printer
E = [3 2 2]
CD Rom
Available Vector
Resource Vector
A = [1 2 2]
P1 r
P2 r
P3 r
Request
0 0 0
R= 1 0 2
0 0 0
2) P2 requests 1 Printer, 2 DVDs
4/9/2015
CST 352 - Operating Systems
37
Deadlock Detection
Matrix Approach (Example)
Current
2
C= 1
0
4/9/2015
Allocation
0 0
0 2
0 0
DVD
Printer
DVD
CD Rom
Printer
E = [3 2 2]
CD Rom
Available Vector
Resource Vector
A = [0 2 0]
P1 r
P2 r
P3 r
Request
0 0 0
R= 0 0 0
0 0 0
CST 352 - Operating Systems
38
Deadlock Detection
Matrix Approach (Example)
Current
2
C= 1
0
Allocation
0 0
0 2
0 0
DVD
Printer
DVD
CD Rom
Printer
E = [3 2 2]
CD Rom
Available Vector
Resource Vector
A = [0 2 0]
P1 r
P2 r
P3 r
Request
0 0 0
R= 0 0 0
1 1 0
3) P3 requests 1 Printer, 1 CD Rom
4/9/2015
CST 352 - Operating Systems
39
Deadlock Detection
Matrix Approach (Example)
Current
2
C= 1
0
Allocation
0 0
0 2
0 0
DVD
Printer
DVD
CD Rom
Printer
E = [3 2 2]
CD Rom
Available Vector
Resource Vector
A = [0 2 0]
P1 r
P2 r
P3 b
Request
0 0 0
R= 0 0 0
1 1 0
4) P3 blocked
4/9/2015
CST 352 - Operating Systems
40
Deadlock Detection
Matrix Approach (Example)
Current
2
C= 1
0
Allocation
0 0
0 2
0 0
DVD
Printer
DVD
CD Rom
Printer
E = [3 2 2]
CD Rom
Available Vector
Resource Vector
A = [0 2 0]
P1 r
P2 r
P3 b
Request
0 0 0
R= 0 0 0
1 1 0
5) P2 frees up printer
4/9/2015
CST 352 - Operating Systems
41
Deadlock Detection
Matrix Approach (Example)
Current
2
C= 0
0
4/9/2015
Allocation
0 0
0 2
0 0
DVD
Printer
DVD
CD Rom
Printer
E = [3 2 2]
CD Rom
Available Vector
Resource Vector
A = [1 2 0]
P1 r
P2 r
P3 b
Request
0 0 0
R= 0 0 0
1 1 0
CST 352 - Operating Systems
42
Deadlock Detection
Matrix Approach (Example)
Current
2
C= 0
1
Allocation
0 0
0 2
1 0
DVD
Printer
DVD
CD Rom
Printer
E = [3 2 2]
CD Rom
Available Vector
Resource Vector
A = [0 1 0]
P1 r
P2 r
P3 r
Request
0 0 0
R= 0 0 0
0 0 0
6) P3 can now run
4/9/2015
CST 352 - Operating Systems
43
Deadlock Detection
Matrix Approach (Example)
Current
2
C= 0
1
Allocation
0 0
0 2
1 0
DVD
Printer
DVD
CD Rom
Printer
E = [3 2 2]
CD Rom
Available Vector
Resource Vector
A = [0 1 0]
P1 r
P2 r
P3 r
Request
0 0 1
R= 0 0 0
0 0 0
7) P1 request DVD
4/9/2015
CST 352 - Operating Systems
44
Deadlock Detection
Matrix Approach (Example)
Current
2
C= 0
1
Allocation
0 0
0 2
1 0
DVD
Printer
DVD
CD Rom
Printer
E = [3 2 2]
CD Rom
Available Vector
Resource Vector
A = [0 1 0]
P1 b
P2 r
P3 r
Request
0 0 1
R= 0 0 0
0 0 0
8) P1 blocked
4/9/2015
CST 352 - Operating Systems
45
Deadlock Detection
Matrix Approach (Example)
Current
2
C= 0
1
Allocation
0 0
0 2
1 0
DVD
Printer
DVD
CD Rom
Printer
E = [3 2 2]
CD Rom
Available Vector
Resource Vector
A = [0 1 0]
P1 b
P2 r
P3 r
Request
0 0 1
R= 1 0 0
0 0 0
9) P2 request printer
4/9/2015
CST 352 - Operating Systems
46
Deadlock Detection
Matrix Approach (Example)
Current
2
C= 0
1
Allocation
0 0
0 2
1 0
DVD
Printer
DVD
CD Rom
Printer
E = [3 2 2]
CD Rom
Available Vector
Resource Vector
A = [0 1 0]
P1 b
P2 b
P3 r
Request
0 0 1
R= 1 0 0
0 0 0
10) P2 blocked
4/9/2015
CST 352 - Operating Systems
47
Deadlock Detection
Matrix Approach (Example)
Current
2
C= 0
1
Allocation
0 0
0 2
1 0
DVD
Printer
DVD
CD Rom
Printer
E = [3 2 2]
CD Rom
Available Vector
Resource Vector
A = [0 1 0]
P1 b
P2 b
P3 r
Request
0 0 1
R= 1 0 0
0 0 0
•
This is still not deadlock since P3 holds a resource that could allow P2 to
commence execution.
•
If P1 held all 3 printers, we would have true deadlock.
4/9/2015
CST 352 - Operating Systems
48
Deadlock Detection
Matrix Approach (Example)
Current
2
C= 0
1
Allocation
0 0
0 2
1 0
DVD
Printer
DVD
CD Rom
Printer
E = [3 2 2]
CD Rom
Available Vector
Resource Vector
A = [0 1 0]
P1 b
P2 b
P3 r
Request
0 0 1
R= 1 0 0
0 0 0
• What are the general conditions for true deadlock?
4/9/2015
CST 352 - Operating Systems
49
Deadlock Detection
Hypothesis (extra credit – 50 pts)
Prove or disprove
Deadlock can be detected by rotating the
rows and columns of the C and R matrix
(in conjunction) so that if C and R have
any inverse sub-matrices, the involved
processes are deadlocked.
E and A must be involved.
4/9/2015
CST 352 - Operating Systems
50
Deadlock Recovery
Given the above algorithm, the OS
can detect which process is waiting
for what resource.
Now the OS needs to decide what to
do when a deadlocked process is
detected.
4/9/2015
CST 352 - Operating Systems
51
Deadlock Recovery
Recovery through preemption
Take a resource away from a
process and give it to another.
Only works for preemptable
processes.
4/9/2015
CST 352 - Operating Systems
52
Deadlock Recovery
Recovery through rollback
PCB (TCB) and resource state are periodically
saved at a “checkpoint”
When a deadlock is detected, back the
deadlocked process up to the previous stable
state.
Discard the resource manipulation that occurred
after the checkpoint.
Start the process after it is determined it can run
again.
4/9/2015
CST 352 - Operating Systems
53
Deadlock Recovery
Recovery through process killing
Kill off the offending processes.
The processes that have allocated
contending resources are removed from
the system.
Kill off the lower priority process.
Kill off non-OS (user) processes.
Return their resources to the system.
4/9/2015
CST 352 - Operating Systems
54
Deadlock Recovery
Q:
1.
2.
3.
4/9/2015
How can a system be constructed
so it will never have deadlock?
Give an example.
T or F – The graph model for
deadlock only works well for 2
processes.
CST 352 - Operating Systems
55
Deadlock Avoidance
Shaded regions with
slanted lines represent
instances where both
processes gain access to a
resource at the same time.
If the resources are
mutually exclusive, it is
impossible for these areas
to be entered by a “run
time trajectory”.
4/9/2015
Process B
Process Trajectories – revisited
Rel 1
Proc A and
Proc B want
Resource 1
Rel 2
Get 1
Deadlock
Pending
Proc A and
Proc B want
Resource 2
Get 2
Get 1
CST 352 - Operating Systems
Get 2
Rel 1
Rel 2
Process A
56
Deadlock Avoidance
Bankers Algorithm
The banker at Klamath First Federal is
granting a group of customers credit.
The banker has limited cash to loan.
Each customer has a credit limit.
The banker assumes no customer will
request their entire credit limit at a
single time.
4/9/2015
CST 352 - Operating Systems
57
Deadlock Avoidance
Bankers Algorithm
The banker grants loans to the customers
based customer credit limit and available
cash reserve.
The bankers job is to avoid the situation
where cash reserves drop so low that the
FDIC shuts the bank down.
Loans that may cause inspection by the
FDIC are called “unsafe” loans.
4/9/2015
CST 352 - Operating Systems
58
Deadlock Avoidance
Bankers Algorithm (Example)
4 customers – Joe, Jay, Jim, and Jill
Initial reserve of 10 dollars (it is a very small
bank).
Each customer has a credit limit.
The goal here is to ensure that two customers
cannot be simultaneously blocked waiting
on a resource.
4/9/2015
CST 352 - Operating Systems
59
Deadlock Avoidance
Bankers Algorithm (Example)
1. Joe requests 1
Name
Has
Limit
Joe
0
6
Jay
0
5
Jim
0
4
Jill
0
7
Reserve: 10
4/9/2015
CST 352 - Operating Systems
60
Deadlock Avoidance
Bankers Algorithm (Example)
1. Joe requests 1 – granted
Name
Has
Limit
Joe
1
6
Jay
0
5
Jim
0
4
Jill
0
7
Reserve: 9
4/9/2015
CST 352 - Operating Systems
61
Deadlock Avoidance
Bankers Algorithm (Example)
1. Joe requests 1 – granted
2. Jay requests 1
Name
Has
Limit
Joe
1
6
Jay
0
5
Jim
0
4
Jill
0
7
Reserve: 9
4/9/2015
CST 352 - Operating Systems
62
Deadlock Avoidance
Bankers Algorithm (Example)
1. Joe requests 1 – granted
2. Jay requests 1 – granted
Name
Has
Limit
Joe
1
6
Jay
1
5
Jim
0
4
Jill
0
7
Reserve: 8
4/9/2015
CST 352 - Operating Systems
63
Deadlock Avoidance
Bankers Algorithm (Example)
1. Joe requests 1 – granted
2. Jay requests 1 – granted
3. Jim requests 2
Name
Has
Limit
Joe
1
6
Jay
1
5
Jim
0
4
Jill
0
7
Reserve: 8
4/9/2015
CST 352 - Operating Systems
64
Deadlock Avoidance
Bankers Algorithm (Example)
1. Joe requests 1 – granted
2. Jay requests 1 – granted
3. Jim requests 2 – granted
Name
Has
Limit
Joe
1
6
Jay
1
5
Jim
2
4
Jill
0
7
Reserve: 6
4/9/2015
CST 352 - Operating Systems
65
Deadlock Avoidance
Bankers Algorithm (Example)
1.
2.
3.
4.
Joe requests 1 – granted
Jay requests 1 – granted
Jim requests 2 – granted
Jill requests 4
Name
Has
Limit
Joe
1
6
Jay
1
5
Jim
2
4
Jill
0
7
Reserve: 6
4/9/2015
CST 352 - Operating Systems
66
Deadlock Avoidance
Bankers Algorithm (Example)
1.
2.
3.
4.
Joe requests 1 – granted
Jay requests 1 – granted
Jim requests 2 – granted
Jill requests 4 – granted
Name
Has
Limit
Joe
1
6
Jay
1
5
Jim
2
4
Jill
4
7
Reserve: 2
4/9/2015
CST 352 - Operating Systems
67
Deadlock Avoidance
Bankers Algorithm (Example)
At any point in time, in this
scenario, the states are safe
because, with at least two
units left, the banker can
delay granting any requests
until Jim pays back his loan.
Jim can ask for the
additional 2 dollars.
Name
Has
Limit
Joe
1
6
Jay
1
5
Jim
2
4
Jill
4
7
Reserve: 2
4/9/2015
CST 352 - Operating Systems
68
Deadlock Avoidance
Bankers Algorithm (Example)
When Jim pays back his loan,
the banker can lend to Jay
or Jill.
Name
Has
Limit
Joe
1
6
Jay
1
5
Jim
0
4
Jill
4
7
Reserve: 4
4/9/2015
CST 352 - Operating Systems
69
Deadlock Avoidance
Bankers Algorithm (Example)
Unsafe State Example
1. Joe requests 1 – granted
2. Jay requests 2 – granted
Name
Has
Limit
Joe
1
6
Jay
2
5
Jim
0
4
Jill
0
7
Reserve: 7
4/9/2015
CST 352 - Operating Systems
70
Deadlock Avoidance
Bankers Algorithm (Example)
Unsafe State Example
1. Joe requests 1 – granted
2. Jay requests 2 – granted
3. Jim requests 2
Name
Has
Limit
Joe
1
6
Jay
2
5
Jim
0
4
Jill
0
7
Reserve: 7
4/9/2015
CST 352 - Operating Systems
71
Deadlock Avoidance
Bankers Algorithm (Example)
Unsafe State Example
1. Joe requests 1 – granted
2. Jay requests 2 – granted
3. Jim requests 2 – granted
Name
Has
Limit
Joe
1
6
Jay
2
5
Jim
2
4
Jill
0
7
Reserve: 5
4/9/2015
CST 352 - Operating Systems
72
Deadlock Avoidance
Bankers Algorithm (Example)
Unsafe State Example
1.
2.
3.
4.
Joe requests 1 – granted
Jay requests 2 – granted
Jim requests 2 – granted
Jill requests 4
Name
Has
Limit
Joe
1
6
Jay
2
5
Jim
2
4
Jill
0
7
Reserve: 5
4/9/2015
CST 352 - Operating Systems
73
Deadlock Avoidance
Bankers Algorithm (Example)
Unsafe State Example
1.
2.
3.
4.
Joe requests 1 – granted
Jay requests 2 – granted
Jim requests 2 – granted
Jill requests 4
With a reserve of 1,
potentially, any other request
could be denied.
If the banker gives the loan
to Jill, the reserve will be down
to 1.
4/9/2015
Name
Has
Limit
Joe
1
6
Jay
2
5
Jim
2
4
Jill
0
7
Reserve: 5
CST 352 - Operating Systems
74
Deadlock Avoidance
Bankers Algorithm (Example)
1.
2.
3.
4.
Joe requests 1 – granted
Jay requests 2 – granted
Jim requests 2 – granted
Jill requests 4
Granting Jill the loan would
force the system into an unsafe
state.
If all customers demanded their
maximum loan, none of them could
be granted, forcing the FDIC to
come down on the bank (deadlock).
4/9/2015
Name
Has
Limit
Joe
1
6
Jay
2
5
Jim
2
4
Jill
0
7
Reserve: 5
CST 352 - Operating Systems
75
Deadlock Avoidance
Bankers Algorithm (multiple resource)
The bankers algorithm for multiple resource is just
a modification of the matrix approach for deadlock
detection.
The modification made relates to what the
scheduler does during the application of the
algorithm.
Rather than waiting for deadlock to occur, the
scheduler takes proactive action during resource
allocation and de-allocation.
4/9/2015
CST 352 - Operating Systems
76
Deadlock Avoidance
Bankers Algorithm (multiple resource)
The problem with the bankers algorithm for
deadlock avoidance is to operate correctly,
the resources must be pre-determined and
stable.
There are no systems where resources are all
pre-determined and infinitely stable.
4/9/2015
CST 352 - Operating Systems
77
Deadlock Prevention
Attack mutual exclusion
condition
Do not allow any resources to be
mutually exclusive.
Printer spooling was introduced
to do this.
4/9/2015
CST 352 - Operating Systems
78
Deadlock Prevention
Attack the hold and wait condition
Do not allow any process to seize a
resource and hold it.
Require all processes to request their
resources up-front.
This solution introduces
inefficiencies.
4/9/2015
CST 352 - Operating Systems
79
Deadlock Prevention
Attack the No-Preempt
condition
It is close to impossible to get rid
of all non-preemptable resources
in a system.
4/9/2015
CST 352 - Operating Systems
80
Deadlock Prevention
Attack the Circular wait
condition
Only allow a process one
resource at a time.
Require process to adhere to a
resource numbering scheme
sequential allocation
4/9/2015
CST 352 - Operating Systems
81
Download