Professional Documents
Culture Documents
Operating Systems - CS604 Power Point Slides Lecture 28
Operating Systems - CS604 Power Point Slides Lecture 28
Systems
Lecture 28
Syed Mansoor Sarwar
of previous lecture
Deadlock avoidance
Bankers algorithms
Safety algorithm
Safe Sequence
Recap of lecture
January 27, 2016
Review of Lecture 27
Deadlock handling
Deadlock prevention
Deadlock avoidance
RAG Algorithm
Claim edge Pi Rj indicates
that process Pi may request
an instance of resource Rj;
represented by a dashed line.
Claim edge converts to
request edge when a process
requests
a
resource.
January 27, 2016
Copyright Virtual Univers
ity of Pakistan
RAG Algorithm
When a resource is assigned to a
process, request edge reconverts
to an assignment edge.
When a resource is released by a
process, assignment edge
reconverts to a claim edge.
Resources must be claimed a
priori
in
the
system.
January 27, 2016
Copyright Virtual Univers
ity of Pakistan
Bankers Algorithm
Multiple instances of resources.
Each process must a priori claim
maximum use.
When a process requests a
resource it may have to wait.
When a process gets all its
resources it must return them in
a finite
of time.
January
27, 2016 amount
Copyright Virtual
Univers
ity of Pakistan
Allocation: n x m matrix. If
Allocation[i,j] = k then Pi is currently
January
27, 2016
Copyright Virtual Univers
allocated
kinstances
of Rj.
ity of Pakistan
Need[i,j] =
Copyright Virtual Univers
Max[i,j] Allocation
[i,j]
ity of Pakistan
Safety Algorithm
1. Let Work and Finish be
vectors of length m and n,
respectively. Initialize:
Work = Available
Finish[i] = false; i = 1, 2, , n.
January 27, 2016
Safety Algorithm
2. Find and i such that both:
(a) Finish[i] = false
(b) Needi Work
If no such i exists, go to step 4.
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.
January 27, 2016
Resource-Request
Algorithm for Pi
Requesti = request vector for Pi.
If Requesti [j] = k then process Pi
wants k instances of resource type
Rj.
1. If Requesti Needi go to step 2.
Otherwise, report error since
process has exceeded its
maximum
claim.
January
27, 2016
Copyright
Virtual Univers
ity of Pakistan
Resource-Request
Algorithm for Pi
2. If Requesti Available, go to
step 3. Otherwise Pi must wait,
since resources are not
available.
3. Pretend to allocate requested
resources to Pi by modifying the
state
January
27, 2016 as follows:
Copyright Virtual Univers
ity of Pakistan
Resource-Request
Algorithm for Pi
Available = Available Requestj;
Allocationi = Allocationi + Requesti;
Needi
= Needi Requesti;
If safe the resources are
allocated to Pi.
If unsafe Pi must wait, and the
old resource-allocation state is
restored
January 27, 2016
Copyright Virtual Univers
ity of Pakistan
Example System
Five processes: P0 P4
Three resource types:
A (10 instances)
B (5 instances)
C (7 instances)
January 27, 2016
Example System
System state: Is it safe?
Max
Process A B C
P0
P1
P2
Allocation
Available
A B C
7 5 3
3 2 2
0 1 0
2 0 0
9 0 2
3 0 2
2 1 1
2 2 2
P3
0Virtual
0 Univers
2
3
P4 27, 2016 4 3 Copyright
January
ity of Pakistan
Example System
Needi = Maxi Allocationi
Need matrix
Process A B
P0
7 4
P1
1 2
P2
6 0
P3
0 1
January 27, 2016 P Copyright4
Virtual Univers
3
4
ity of Pakistan
C
3
2
0
1
1
Example System
Need
Process A B C
Allocation
Work
A B C
P0
P1
P2
7 4 3
1 2 2
6 0 0
0 1 0
P3
P4
0 1 1
4 3 1
2 1 1
0 0 2
2 0 0
3 0 2
Safe
Sequence:
<>Virtual Univers
January 27,
2016
Copyright
ity of Pakistan
Example System
Need
Process A B C
Allocation
Work
A B C
3
5
3
3
2
2
P0
P1
P2
7 4 3
1 2 2
6 0 0
0 1 0
P3
P4
0 1 1
4 3 1
2 1 1
0 0 2
2 0 0
3 0 2
Safe
Sequence:
<P
1> Univers
January 27,
2016
Copyright
Virtual
ity of Pakistan
Example System
Need
Process A B C
Allocation
Work
A B C
3
5
7
3
3
4
2
2
3
P0
P1
P2
7 4 3
1 2 2
6 0 0
0 1 0
P3
P4
0 1 1
4 3 1
2 1 1
0 0 2
2 0 0
3 0 2
Safe
Sequence:
<P
1, P
3>
January 27,
2016
Copyright
Virtual
Univers
ity of Pakistan
Example System
Need
Process A B C
Allocation
Work
A B C
3
5
7
3
3
4
2
2
3
P0
P1
P2
7 4 3
1 2 2
6 0 0
0 1 0
P3
P4
0 1 1
4 3 1
2 1 1
0 0 2
2 0 0
3 0 2
Safe
January 27,
2016
Copyright
Virtual
Univers
Sequence:
<P
1, P
3, P4 >
ity of Pakistan
Example System
Need
Process A B C
Allocation
Work
A B C
3
3
4
2
2
3
4
5
5
5
P0
P1
P2
7 4 3
1 2 2
6 0 0
0 1 0
3 0 2
3
5
7
P3
P4
0 1 1
4 3 1
2 1 1
0 0 2
7
7
2 0 0
Safe
January 27,
2016
Copyright
Sequence:
<PVirtual
1, PUnivers
3, P4, P0>
ity of Pakistan
Example System
Final safe sequence:
<P1, P3, P4, P0, P2>
Not a unique sequence
Possible safe sequences for the this
example:
<P1,P3,P4,P0,P2>, <P1,P3,P4,P2,P0>,
<P1,P3,P2,P0,P4>, <P1,P3,P2,P4,P0>,
<P
1,P27,
3,P
0,P2,P
4>, <P
1,PUnivers
3,P0,P4,P2>
January
2016
Copyright
Virtual
ity of Pakistan
Example
P1 requests (1,0,2). Can this
request be granted
immediately?
1. Is Request1 Need1?
(1,0,2) (1,2,2) true
2. Is Request1 Available?
Copyright
(3,3,2)
true
January(1,0,2)
27, 2016
Virtual Univers
ity of Pakistan
Example
Need
Process A B C
Allocation
A B C
P0
P1
P2
7 4 3
1 2 2
6 0 0
0 1 0
P3
P4
0 1 1
4 3 1
2 1 1
0 0 2
Available
2 0 0
3 0 2
Example
Need
Process A B C
Allocation
Work
A B C
P0
P1
P2
7 4 3
0 2 0
6 0 0
0 1 0
P3
P4
0 1 1
4 3 1
2 1 1
0 0 2
3 0 2
3 0 2
Safe
Sequence:
<>Virtual Univers
January 27,
2016
Copyright
ity of Pakistan
Example
Need
Process A B C
Allocation
Work
A B C
2
5
3
3
0
2
P0
P1
P2
7 4 3
0 2 0
6 0 0
0 1 0
P3
P4
0 1 1
4 3 1
2 1 1
0 0 2
3 0 2
3 0 2
Safe
Sequence:
<P
1 >Univers
January 27,
2016
Copyright
Virtual
ity of Pakistan
Example
Need
Process A B C
Allocation
Work
A B C
2
5
7
3
3
4
0
2
3
P0
P1
P2
7 4 3
0 2 0
6 0 0
0 1 0
P3
P4
0 1 1
4 3 1
2 1 1
0 0 2
3 0 2
3 0 2
Safe
Sequence:
<P
1, P
3 >
January 27,
2016
Copyright
Virtual
Univers
ity of Pakistan
Example
Need
Process A B C
Allocation
Work
A B C
2
5
7
3
3
4
0
2
3
P0
P1
P2
7 4 3
0 2 0
6 0 0
0 1 0
P3
P4
0 1 1
4 3 1
2 1 1
0 0 2
3 0 2
3 0 2
Safe
January 27,
2016
Copyright
Virtual
Univers
Sequence:
<P
1, P
3, P4 >
ity of Pakistan
Example
Need
Process A B C
Allocation
Work
A B C
3
3
4
0
2
3
4
4
5
5
P0
P1
P2
7 4 3
0 2 0
6 0 0
0 1 0
3 0 2
2
5
7
P3
P4
0 1 1
4 3 1
2 1 1
0 0 2
7
7
3 0 2
Safe
January 27,
2016
Copyright
Sequence:
<PVirtual
1, PUnivers
3, P4, P0>
ity of Pakistan
Example
Executing safety algorithm
shows that sequence
<P1,P3,P4,P0,P2> satisfies
safety requirement.
Yes, P1s request may be
granted immediately.
January 27, 2016
More Examples
Can request for (0,2,0) by P0 be
granted?
1. Is Request0 Need0?
(0,2,0) (7,4,3) true
2. Is Request1 Available?
(0,2,0)
(3,3,2)
true
January 27, 2016
Copyright Virtual Univers
ity of Pakistan
Example
Need
Process A B C
Allocation
A B C
P0
P1
P2
7 4 3
1 2 2
6 0 0
0 1 0
P3
P4
0 1 1
4 3 1
2 1 1
0 0 2
Available
2 0 0
3 0 2
Example
Need
Process A B C
Allocation
Work
A B C
P0
P1
P2
7 2 3
1 2 2
6 0 0
0 3 0
P3
P4
0 1 1
4 3 1
2 1 1
0 0 2
2 0 0
3 0 2
Safe
Sequence:
<>Virtual Univers
January 27,
2016
Copyright
ity of Pakistan
Example
Need
Process A B C
Allocation
Work
A B C
3
5
1
2
2
3
P0
P1
P2
7 2 3
1 2 2
6 0 0
0 3 0
P3
P4
0 1 1
4 3 1
2 1 1
0 0 2
2 0 0
3 0 2
Safe
Sequence:
<P
3 >Univers
January 27,
2016
Copyright
Virtual
ity of Pakistan
Example
Need
Process A B C
Allocation
Work
A B C
3
5
7
1
2
2
2
3
3
P0
P1
P2
7 2 3
1 2 2
6 0 0
0 3 0
P3
P4
0 1 1
4 3 1
2 1 1
0 0 2
2 0 0
3 0 2
Safe
Sequence:
<P
3,PUnivers
1 >
January 27,
2016
Copyright
Virtual
ity of Pakistan
Example
Need
Process A B C
Allocation
Work
A B C
3
5
7
1
2
2
2
3
3
10
P0
P1
P2
7 2 3
1 2 2
6 0 0
0 3 0
P3
P4
0 1 1
4 3 1
2 1 1
0 0 2
2 0 0
3 0 2
Safe
Sequence:
<P
3,PUnivers
1,P2 >
January 27,
2016
Copyright
Virtual
ity of Pakistan
Example
Need
Process A B C
Allocation
Work
A B C
1
2
2
2
3
3
2
5
5
5
P0
P1
P2
7 2 3
1 2 2
6 0 0
0 3 0
3 0 2
3
5
7
P3
P4
0 1 1
4 3 1
2 1 1
0 0 2
10
10
2 0 0
Safe
Sequence:
<PVirtual
3,PUnivers
1,P2,P0,P4>
January 27,
2016
Copyright
ity of Pakistan
Example
Executing safety algorithm
shows that sequence
<P3,P1,P2,P0,P4> satisfies
safety requirement.
Yes, P0s request may be
granted immediately.
January 27, 2016
More Examples
Can request for (3,3,0) by P4
be granted?
Do it yourself!
Recap of Lecture
Deadlock avoidance
Bankers algorithm
Safety algorithm
Safe state
Safe sequence
Operating
Systems
Lecture 28
Syed Mansoor Sarwar