You are on page 1of 31

Operating

Systems
Lecture 29
Syed Mansoor Sarwar

Agenda for Today


Review

of previous lecture
Deadlock detection: resources with
single and multiple instances
Recovery from deadlocks
Process termination
Resource preemption
Recap of lecture
January 27, 2016

Copyright Virtual Univers


ity of Pakistan

Review of Lecture 28
Deadlock avoidance
Resource with single
instances
Resources with multiple
instances: Bankers algorithm
January 27, 2016

Copyright Virtual Univers


ity of Pakistan

Deadlock Detection
Allow system to enter
deadlock state
Detection algorithm
Recovery scheme
January 27, 2016

Copyright Virtual Univers


ity 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 Univers


ity 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 expensiveit
requires an order of n2 operations,
where n is the number of vertices
in
the
graph.
January
27, 2016
Copyright Virtual Univers
ity of Pakistan

RAG and Wait-for Graph

January
27, 2016
Copyright
Virtual Univers
Resource-Allocation
Graph
Corresponding wait-for graph
ity of Pakistan

Multiple Instance of
Each Resource Type
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.
January 27, 2016
Copyright Virtual Univers
ity of Pakistan

Multiple Instance of
Each Resource Type
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.
January 27, 2016

Copyright Virtual Univers


ity of Pakistan

Detection Algorithm
1. 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;
else
January Finish[i]
27, 2016
=
Copyright
Virtual Univers
true;
ity of Pakistan

Detection Algorithm
2. Find an index i such that both:

(a) Finish[i] == false


(b) Requesti Work
If no such i exists, go to step
4.
January 27, 2016

Copyright Virtual Univers


ity of Pakistan

Detection 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
deadlock state. Moreover, if
Finish[i] == false, then Pi is
deadlocked.
January 27, 2016
Copyright Virtual Univers
ity of Pakistan

Example of Detection
Algorithm
Consider the following system:
P = { P 0, P 1, P 2, P 3 , P 4 }
R = { A, B, C }
A: 7 instances
B: 2 instances
C:
January 27,
20166 instances
Copyright Virtual Univers
ity of Pakistan

Example System State


Allocation Request

Work

Process

A B C

A B C

P0
P1
P2

0 1 0

3 0 2

0 0 0
2 0 2
0 0 0

P3
P4

2 1 1
0 0 2

1 0 0
0 0 2

2 0 0

Finish
Sequence:
January 27, 2016
Copyright<>
Virtual Univers
ity of Pakistan

Example System State


Allocation Request

Work

Process

A B C

A B C

P0
P1
P2

0 1 0

0
0

0
1

0
0

3 0 2

0 0 0
2 0 2
0 0 0

P3
P4

2 1 1
0 0 2

1 0 0
0 0 2

2 0 0

Finish
Sequence:
P0>Univers
January 27, 2016
Copyright<Virtual
ity of Pakistan

Example System State


Allocation Request

Work

Process

A B C

A B C

P0
P1
P2

0 1 0
3 0 2

0 0 0
2 0 2
0 0 0

0
0
3

0
1
1

0
0
2

P3
P4

2 1 1
0 0 2

1 0 0
0 0 2

2 0 0

Finish
Sequence:
P0, Univers
P2>
January 27, 2016
Copyright<Virtual
ity of Pakistan

Example System State


Allocation Request

Work

Process

A B C

A B C

P0
P1
P2

0 1 0
3 0 2

0 0 0
2 0 2
0 0 0

0
0
3

0
1
1

0
0
2

P3
P4

2 1 1
0 0 2

1 0 0
0 0 2

2 0 0

Finish
Sequence:
P0,Univers
P2, P3>
January 27, 2016
Copyright<Virtual
ity of Pakistan

Example System State


Allocation Request

Work

Process

A B C

A B C

P0
P1
P2

0 1 0
3 0 2

0 0 0
2 0 2
0 0 0

0
0
3

0
1
1

0
0
2

P3
P4

2 1 1
0 0 2

1 0 0
0 0 2

5
5

2
2

3
5

2 0 0

Finish
Sequence:
P0,Univers
P2, P3, P4 >
January 27, 2016
Copyright<Virtual
ity of Pakistan

Example System State


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 >
January 27, 2016
Copyright Virtual Univers

ity of Pakistan

Example System State


P2 requests an additional
instance of C.
Do we have a
finish sequence?

January 27, 2016

Process Request
A B C
P0
P1
P2

0 0 0
2 0 2
0 0 1

P3
Copyright Virtual Univers P4

1 0 0
0 0 2

ity of Pakistan

Example System State


Allocation Request

Work

Process

A B C

A B C

P0
P1
P2

0 1 0

3 0 2

0 0 0
2 0 2
0 0 1

P3
P4

2 1 1
0 0 2

1 0 0
0 0 2

2 0 0

Finish
Sequence:
January 27, 2016
Copyright<>
Virtual Univers
ity of Pakistan

Example System State


Allocation Request

Work

Process

A B C

A B C

P0
P1
P2

0 1 0

0
0

0
1

0
0

3 0 2

0 0 0
2 0 2
0 0 1

P3
P4

2 1 1
0 0 2

1 0 0
0 0 2

2 0 0

Finish
Sequence:
P0>Univers
January 27, 2016
Copyright<Virtual
ity of Pakistan

Example System State


P0s 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.
January 27, 2016

Copyright Virtual Univers


ity of Pakistan

Detection Algorithm
How often should the detection
algorithm be invoked?
Every time a request for
allocation cannot be granted
immediatelyexpensive but
process causing the deadlock
is identified, along with
processes
involved
January
27, 2016
Copyright
Virtual Universin deadlock
ity of Pakistan

Detection Algorithm
Periodically, or based on CPU
utilization
Arbitrarilythere 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.
January 27, 2016
Copyright Virtual Univers
ity 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 Univers


ity of Pakistan

Recovery from Deadlock:


Process Termination
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.
January 27, 2016
Copyright Virtual Univers
ity of Pakistan

Recovery from Deadlock:


Process Termination
Resources the process has
used.
Resources the process needs to
complete.
How many processes will need
to be terminated.
Is the process interactive or
January
27, 2016
Copyright Virtual Univers
batch?
ity of Pakistan

Recovery from Deadlock:


Resource Preemption
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
January 27, 2016 of rollbacks
Copyright Virtual Univers
number
in cost factor.
ity of Pakistan

Recap of Lecture
Deadlock detection
Recovery from deadlock
Process termination
Resource preemption

January 27, 2016

Copyright Virtual Univers


ity of Pakistan

Operating
Systems
Lecture 29
Syed Mansoor Sarwar
January 27, 2016

Copyright Virtual Univers


ity of Pakistan

You might also like