Operating Systems - CS604 Power Point Slides Lecture 29
description
Transcript of Operating Systems - CS604 Power Point Slides Lecture 29
Operating
Systems Lecture 29
Syed Mansoor Sarwar
January 27, 2016 © Copyright Virtual University of Pakistan
Agenda for TodayReview of previous lectureDeadlock detection: resources with
single and multiple instancesRecovery from deadlocks
Process terminationResource preemption
Recap of lecture
January 27, 2016 © Copyright Virtual University of Pakistan
Review of Lecture 28Deadlock avoidanceResource with single
instancesResources with multiple
instances: Banker’s algorithm
January 27, 2016 © Copyright Virtual University of Pakistan
Deadlock DetectionAllow system to enter
deadlock state
Detection algorithm
Recovery scheme
January 27, 2016 © Copyright Virtual University of Pakistan
Single Instance of Each Resource Type
Maintain a wait-for graph
Nodes are processes.
Pi Pj if Pi is waiting for Pj.
January 27, 2016 © Copyright Virtual University of Pakistan
Single Instance of Each Resource Type
Periodically invoke an algorithm that searches for a cycle in the wait-for graph.
The algorithm is expensive—it requires an order of n2 operations, where n is the number of vertices in the graph.
January 27, 2016 © Copyright Virtual University of Pakistan
RAG and Wait-for Graph
Resource-Allocation Graph Corresponding wait-for graph
January 27, 2016 © Copyright Virtual University of Pakistan
Available: A vector of length m indicates the number of available resources of each type.
Allocation: An n x m matrix defines the number of resources of each type currently allocated to each process.
Multiple Instance of Each Resource Type
January 27, 2016 © Copyright Virtual University of Pakistan
Request: An n x m matrix indicates the current request of each process. If Request[i,j]=k, then process Pi is requesting k more instances of resource type. Pj.
Multiple Instance of Each Resource Type
January 27, 2016 © Copyright Virtual University of Pakistan
Detection Algorithm1. Let Work and Finish be vectors of
length m and n, respectively Initialize: Work = Available; for i = 1, 2, …, n
if Allocationi 0 then Finish[i] = false;elseFinish[i] = true;
January 27, 2016 © Copyright Virtual University of Pakistan
2. Find an index i such that both:
(a) Finish[i] == false
(b) Requesti Work
If no such i exists, go to step 4.
Detection Algorithm
January 27, 2016 © Copyright Virtual University of Pakistan
3. Work = Work + Allocationi
Finish[i] = truego to step 2.
4. If Finish[i] == false, for some i, 1 i n, then the system is in deadlock state. Moreover, if Finish[i] == false, then Pi is deadlocked.
Detection Algorithm
January 27, 2016 © Copyright Virtual University of Pakistan
Example of Detection Algorithm
Consider the following system:
P = { P0, P1, P2, P3, P4 }
R = { A, B, C }
A: 7 instances
B: 2 instances
C: 6 instances
January 27, 2016 © Copyright Virtual University of Pakistan
Finish Sequence: <>
Process
P0
P1
P2
P3
P4
Work
A B C
0 0 0
Allocation
A B C
0 1 0
2 0 0
3 0 2
2 1 1
0 0 2
Request
A B C
0 0 0
2 0 2
0 0 0
1 0 0
0 0 2
Example System State
January 27, 2016 © Copyright Virtual University of Pakistan
Finish Sequence: <P0>
Process
P0
P1
P2
P3
P4
Work
A B C
0 0 0
0 1 0
Allocation
A B C
0 1 0
2 0 0
3 0 2
2 1 1
0 0 2
Request
A B C
0 0 0
2 0 2
0 0 0
1 0 0
0 0 2
Example System State
January 27, 2016 © Copyright Virtual University of Pakistan
Finish Sequence: <P0, P2>
Process
P0
P1
P2
P3
P4
Work
A B C
0 0 0
0 1 0
3 1 2
Allocation
A B C
0 1 0
2 0 0
3 0 2
2 1 1
0 0 2
Request
A B C
0 0 0
2 0 2
0 0 0
1 0 0
0 0 2
Example System State
January 27, 2016 © Copyright Virtual University of Pakistan
Finish Sequence: < P0, P2, P3>
Process
P0
P1
P2
P3
P4
Work
A B C
0 0 0
0 1 0
3 1 2
5 2 3
Allocation
A B C
0 1 0
2 0 0
3 0 2
2 1 1
0 0 2
Request
A B C
0 0 0
2 0 2
0 0 0
1 0 0
0 0 2
Example System State
January 27, 2016 © Copyright Virtual University of Pakistan
Finish Sequence: < P0, P2, P3, P4 >
Process
P0
P1
P2
P3
P4
Work
A B C
0 0 0
0 1 0
3 1 2
5 2 3
5 2 5
Allocation
A B C
0 1 0
2 0 0
3 0 2
2 1 1
0 0 2
Request
A B C
0 0 0
2 0 2
0 0 0
1 0 0
0 0 2
Example System State
January 27, 2016 © Copyright Virtual University of Pakistan
Final finish sequence:
< P0, P2, P3, P4 , P1 >
Other possible finish sequences:
< P0, P2, P3, P1 , P4 >
< P0, P2, P4, P1 , P3 >
< P0, P2, P4, P3 , P1 >…
Example System State
January 27, 2016 © Copyright Virtual University of Pakistan
P2 requests an additional instance of C.
Do we have a
finish sequence?
Example System State
Request
A B C
0 0 0
2 0 2
0 0 1
1 0 0
0 0 2
Process
P0
P1
P2
P3
P4
January 27, 2016 © Copyright Virtual University of Pakistan
Finish Sequence: <>
Process
P0
P1
P2
P3
P4
Work
A B C
0 0 0
Allocation
A B C
0 1 0
2 0 0
3 0 2
2 1 1
0 0 2
Request
A B C
0 0 0
2 0 2
0 0 1
1 0 0
0 0 2
Example System State
January 27, 2016 © Copyright Virtual University of Pakistan
Finish Sequence: <P0>
Process
P0
P1
P2
P3
P4
Work
A B C
0 0 0
0 1 0
Allocation
A B C
0 1 0
2 0 0
3 0 2
2 1 1
0 0 2
Request
A B C
0 0 0
2 0 2
0 0 1
1 0 0
0 0 2
Example System State
January 27, 2016 © Copyright Virtual University of Pakistan
P0’s request can be satisfied with currently available resources, but request for no other process can be satisfied after that.
Deadlock exists, consisting of processes P1, P2, P3, and P4.
Example System State
January 27, 2016 © Copyright Virtual University of Pakistan
Detection AlgorithmHow often should the detection
algorithm be invoked?Every time a request for allocation cannot be granted immediately—expensive but process causing the deadlock is identified, along with processes involved in deadlock
January 27, 2016 © Copyright Virtual University of Pakistan
Periodically, or based on CPU utilization
Arbitrarily—there may be many cycles in the resource graph and we would not be able to tell which of the many deadlocked processes “caused” the deadlock.
Detection Algorithm
January 27, 2016 © Copyright Virtual University of Pakistan
Recovery from Deadlock: Process Termination
Abort all deadlocked processes.
Abort one process at a time until the deadlock cycle is eliminated.
January 27, 2016 © Copyright Virtual University of Pakistan
In which order should we choose to abort processes?
Priority of a process.
How long the process has computed, and how much longer to completion.
Recovery from Deadlock: Process Termination
January 27, 2016 © Copyright Virtual University of Pakistan
Resources the process has used.
Resources the process needs to complete.
How many processes will need to be terminated.
Is the process interactive or batch?
Recovery from Deadlock: Process Termination
January 27, 2016 © Copyright Virtual University of Pakistan
Selecting a victim – minimize cost.
Rollback – return to some safe state, restart process from that state.
Starvation – same process may always be picked as victim; include number of rollbacks in cost factor.
Recovery from Deadlock: Resource Preemption
January 27, 2016 © Copyright Virtual University of Pakistan
Recap of Lecture
Deadlock detection Recovery from deadlock
Process termination Resource preemption
January 27, 2016 © Copyright Virtual University of Pakistan
Operating Systems
Lecture 29
Syed Mansoor Sarwar