# Exercises on RTSD, 5

Exercises on RTSD, 5.06.03
1. Deadlock avoidance (Habermann’s algorithm). Let’s assume that number of
available resources a=10, number of concurrent processes n=4, their current
requests c=(2,2,3,1), their maximal requests b=(4,4,5,3). Decide, whether granting
of c is safe, and if it so, show safe sequence of processes.
For deciding, whether granting of resources is safe we are to find safe sequence S of 4
processes, if it exists, granting is safe, otherwise – not safe. We start with empty
sequence S=0. Then we are to extend it by some processes step by step. If next step
will give no extension, it means that sequence is not extendable, and granting is not
safe. For extending we are to compare maximal additional requirement with available
number of free resources r (r=a- sum of all components in c = 10-2-2-3-1=2) plus sum
of resource units freed by processes preceding current in sequence S. We start with
empty S, so for the 1st process, which is to be included in S, there are no preceding
processes and we are to compare its additional resources requirements just with r:
B1-c1=4-2=2&lt;=r=2? Yes =&gt;S=(P1), we have included P1 in S. Next consider 2nd
process:
B2-c2=4-2=2&lt;=r+c1=2+2=4? Yes =&gt;S=(P1,P2). Consider 3rd process:
B3-c3=5-3=2&lt;=r+c1+c2=2+2+2=6? Yes =&gt;S=(P1,P2,P3). Consider last process:
B4-c4=3-1=2&lt;=r+c1+c2+c3=2+2+2+3=9? Yes =&gt;S=(P1,P2,P3,P4) it is full sequence
of all processes, hence, granting of resources c to processes is safe, and in the case of
actual requesting by them additional resources, we can satisfy all if we shall grant
additional resources in the sequence defined by S: at first, serve all requests of
process P1, after its termination serve P2, and so on.
2. Deadlock detection by usage of resource graph. Assume that current resource
state is depicted by the following resource graph:
R2
P2
2
R1
3
P3
P1
R3
2
P4
P5
2
R4
On the draw above we have representation of resource graph for the system of 5
processes P1,.., P5, and set of resources of 4 types R1 with 3 units of resources available,
R2 – 2 units, R3 – 2 units, R4 – 2 units. Arrow from process to resource means request
for 1 unit of respective resource type, arrow from resource to process means granted 1
unit of respective resource to respective process. Detect, whether this current resource
For deadlocks detection we shall use reduction of resource graph on processes
sequentially, step by step. For each process we shall either eliminate all incident to it
edges, either non will be eliminated. We may eliminate incident to process edges only in
the case if all its requests are already satisfied, or can be satisfied (for processes having
some resources). If process has not yet resources, it can’t affect on deadlock arising, and
all incident to it edges may be eliminated. So, process P5 is without granted to it
resources, and only 1 edge (request for resource R1) may be eliminated. If after
reduction, we shall not have edges, it will mean that there are no deadlocks. If some
edges will remain, they are incident to processes that are in the deadlock loop. So, we can
eliminate edges incident to P1. After that we may eliminate edges incident to P3 (1 unit
of resource R4 now is free). Then we may eliminate edges incident to P2 (both units of
resource R2 are now free). Then we can eliminate edges incident to P4 (2 units of
resource R2 now are free). Hence, no edges will remain in our graph after such reduction,
we may conclude, that there are no deadlocks in this situation.
3. Build tasks waiting graph using resource graph from Exercise 2. Examine it on
Tasks waiting graph (TWG) is a graph having nodes-tasks, it may be obtained from
resource graph by elimination of resource nodes, and connecting by edges nodes
which are in the relation of waiting for locked resources. So, nodes T1 and T2 are to
be connected with edge directed from T1 to T2, if T2 holds resource, necessary to T1
(in other words, T2 blocks T1 by using some critical resource requested by T1).
Resource graph has information about granted and requested resources, and TWG
may be easily obtained.
P2
P3
or
P1
P5
P4
We have that P5 is not blocked. P1 is not blocked (there is a free unit of resource R1).
P2 is blocked by P3 (pr holds 2 units of resource R2, requested by P2). P3 is blocked
by P1 or P2 (each of them holds 1 unit of resource requested by P3). P4 is blocked by
P3 (P4 requests 1 unit of R2 both units of which are captured by P2). In this picture
we have a loop (P2,P3), which corresponds to deadlock situation. So, our previous
conclusion of absence of deadlock contradicts to the description shown by TWG: we
have processes P2, P3 in a deadlock – P2 requires resources captured by P2, and P3
requires resources captured by P2. But actually, there is no deadlock, because P3
requires 1 unit of resource R4, one unit of which is used by P3, and other – by P1. So,
P3 is locked either by P1 or by P3. If any of them will release resource, P3 will be
able to develop. P2 is locked by P3, but P1 is not locked by any process, and after
releasing resources by P1, P3 will develop, and then P2 may develop. So, for
deadlock detection, in the case of alternative edges (locking), for deadlock existence,
we are to have loop in each alternative.
4. Consider priority preemptive system of 4 processes, patterns of whose work are
give in the table below
Process
Priority
Execution
Release time
sequence
A
1
EQQQQE
0
B
2
EVQE
2
C
3
EEVE
2
D
3
EQEE
4
Assume that more priority (important) process has larger value of priority.
Release time means time of issuing request for execution of respective task.
Execution sequence corresponds to the sequence of actions which are to be made by
the process in each quantum of processor time: E means just execution without usage
of critical resources, V,Q – means that in respective time slices task must critical
resource V or Q. Draw a time diagram representing execution of these processes.
D
P
E
E
b
b
b
b
b
Q
E
E
C
E
E
V
E
B
P
P
P
P
P
E
V
b
b
b
P
P
P
Q
E
A
E
Q
P
P
P
P
P
P
P
Q
Q
Q
P
P
P
P
P
E
1
2
3
4
5
1
2
3
4
5
1
2
3
4
5
1
2
3
Row at the bottom shows time units (counts up to 5). Response times for processes
are: A – 18, B- 15, C – 4, D – 11.
5. In conditions of Exercise 4, use Original Ceiling Priority Protocol (OCPP) to
draw time diagram of execution of the same system of processes.
OCPP locking policy assumes that in addition to static priority each process may have
dynamic priority depending on priorities of blocked by it processes: it inherits highest
priority of blocked by it processes. Process is blocked when it requires critical
resources, used by other processes, or may be not used but locking policy may not
allow to use some resource, if the priority of requesting process is not high enough.
This is the case of OCPP policy. It assumes that each resource has static value –
ceiling - maximum of static priorities of processes which are to use this resource.
Process can lock resource only if its dynamic priority is higher than ceiling of any
currently locked resources.
So, let’s consider execution of the system of processes from Exercise 4 under OCPP
locking policy:
D
B
B
B
P
P
E
Q
E
E
C
E
E
B
B
B
V
E
B
P
P
B
B
B
P
P
P
P
P
P
E
V
Q
E
A
E
Q
P
P
Q
Q
Q
P
P
P
P
P
P
P
P
P
P
E
1
2
3
4
5
1
2
3
4
5
1
2
3
4
5
1
2
3
At first, as previously will run process A, and it will capture resource Q (ceiling of Q
is 3, dynamic priority of A is 1, but there are no locked resources yet). At moment 2
will arise 2 requests, and A will be preempted. At moment 4 will arise new request
for task D, and also at this moment task C will require resource V, dynamic priority
of C is 3, but resource Q is now locked and its ceiling is 3, which is not less, than
priority of C, hence C will be blocked by process A. All processes, excepting A, are
blocked, so A is resumed, it works for 3 units of time with a dynamic priority 3, then
it releases resource Q, its priority falls to 1, it is preempted now, and process C is
resumed. After termination of C, D is activated, after its termination – B is activated.
After termination of B, A is resumed and terminates after 1 time unit. Response times
for processes are: A – 18, B- 15, C – 7, D – 9.
Other locking policy – Immediate Ceiling Priority Protocol (ICPP) – also uses
ceilings, associated with resources in the same way as for OCPP, also processes have
dynamic priorities, but now it is maximum of ceilings of locked by it resources.
Process with larger dynamic priority preempts processes with lower priority. For
previous example, when process A will capture resource Q, its dynamic priority will
become equal to 3, and it will not be preempted by process C at moment 2.