7-2
7-2
Lecture seven
part2
Dr jamal altuwaijari
B- Banker's Algorithm
If Available [j] = k these are k instances of resource type Rj available.
Max: An n× m matrix defines the maximum demand of each process. If max [i , j] = k , then process p i
may request at most k instances of resource type Rj .
Allocation: An n × m the resources currently allocated to each process. If allocation [i , j]= k then process p i
is currently allocated process pi is currently allocated 1 instances of resources of resource type Rj .
Need: An n × m matrix indicates the remaining resource need of each process. If need [i , j] =k then
process pi may need k more instances of resource type Rj to complete its task.
Need [i , j] = Max [i ,j] — Allocation [i , j]
If request i ≤ Need i go to step 2 otherwise raise an error since the process has exceeded its maximum claim
If request i ≤ Available go to step 3. Otherwise pi must wait since the resources are not available.
The system pretends to have allocated the requested resources to process pi by modifying the state as
follows:
Available: = Available - Request i;
Allocation i : = Allocation i + Request i;
Need i:= Need i - Request i;
If the resulting resource allocation state is safe the transaction is completed and process pi is allocated its
resources . If the new state is unsafe the pi must wait for request i and the old resource allocation state is
restored .
C- Safety Algorithm
The algorithm for finding out whether or out a system is in a safe state
can be described as follows:
1-Let work and finish be vectors of length m and n respectively.
-Initialize work:= Available and Finish [i]:=False for i=1, 2, ..., n.
2-Find an i such that both
-Finish [i] = false
-Need i ≤ work
-If no such i exists, go to step 4.
3-Work:=work + allocation I Finish[i] :=true go to step 2.
4-If Finish [i] = true for all i then the system is in a safe state.
This algorithm may require an order of mxn2 operations to decide
whether a state is safe.
C- Safety Algorithm
This algorithm may require an order of mxn2 operations to decide
whether a state is safe.
Example:
Consider a system with five processes {p0 , p1 ,….., p4 } and three
resource types {A, B, C}. Resource type A has 10 instances. Resource
type B has 5 instances, and resource type C has 7 instance. Suppose
that at time T0 the following snapshot of the system has been taken.
Allocat Max Availa
ion ble
ABC ABC ABC
010 357 332
P0
P1 002 223
P2 203 209
C- Safety Algorithm
The content of the matrix Need is defined to be max-Allocation and is:
Need
ABC
P0 743
P1 122
P2 600
P3 011
P4 431
The system is in the safe state if the processes executed in the sequence <p1 , p3 ,
p4 , p2 ,p0 >. Suppose now that process p1 requests one additional instance of
resource type A and two instance of resources type C so request 1 = (1, 0,2).
To decide whether this request can be immediately granted we first check that
Request 1≤ Available. (that is, (1, 0, 2) ≤ (3 3 2)) which is true we then pretend that
this request has been fulfilled and we arrive at the following new state.
C- Safety Algorithm
Allocation Max Available
P1 302 020
P2 302 600
P3 211 011
P4 002 431
By execute the safety Alg. we find the sequence <p1 , p3 , p4 , p0 , p2> satisfies our safety
requirements. Hence we can immediately grant the request of process P1.
If p4 request for (3, 3, 0). The request can not granted since the resources are not available.
Request 1 > Available . If p0 request (0, 2, 0) can not granted even though the resources are
7.4.3 Deadlock Detection
If a system does not employ some protocol that ensures that no
deadlock will ever occur. Then a detection and recovery scheme must
be implemented. The system can use an Algorithm to examines the
state of the system periodically to determine whether has occurred. If
so the system must recover from the deadlock by providing:
A- Maintain information about the current allocation of resources to processes
and outstanding requests.
B- Provide an Alg. that use the above information to determine whether the
system has entered the deadlock state.
The detection Alg. employs several time - varing data structures that
are very similar to those used in the Banker's Algorithm:
- Available.
- Allocation.
Request. An n × m matrix indicating the current request of each
process. If Request [i , j]=k then p1 is requesting k more instances of
resource type rj .
The detection Alg. simply investigates every possible allocation
sequence for the processes that remain to be completed.
The detection Alg. as follows:
1- Let work and finish be vectors of length m and n respectively. Initialize
work:=Available, for i = 1, 2, .. n. If allocation ≠ 0 the finish [i]:=false;
otherwise, finish [i]:= false.
2- Find an index i such that :
- Finish [i] = false, and
- Request i ≤ work.
- If no such i exists go to step 4.
3-Work:=work + Allocation i
finish [i]= true
go to step 2.
4- If Finish[i] = false, for some i, 1≤ i ≤ n then the system is in a deadlock
state. More over, if Finish[i]-false then process p is deadlocked.
Example:-
Consider a system with five processes {p0, p1, ...., p4} and three
resources types {A=7 instance, B=2, C=6 instance} suppose that at time
T0 we the following resource allocation state.
Allocation Max Available
P1 200 202
P2 303 000
P3 211 100
P4 002 002
If we execute the detection Alg. we find the system is not in a deadlock state, and the
sequence
<p0 , p2 , p3 , p1 , p4> will result in finish [i]=true for all i.
Suppose now that process p2 makes one additional request for an instance of type C. The
Request matrix is modified as follows: Request
ABC
P0 000
P1 202
P2 001
P3 100
P4 002
We claim that the system is now deadlocked. Although we can reclaim the resources held by
process p0 the number of available resources is not sufficient to fut fill the requests of the
-Single Instance of each resource type
The Detection Alg. is of order mxn2. If all resources have only a single
instance we can define a faster Alg. we will use a variant of the
resource allocation graph called a wait—for graph. This graph is
obtained from the resource allocation graph by removing the nodes of
type resource and collapsing the appropriate edges. Where the edge
from pi to pj in a wait—for graph implies that process pi is waiting for
process pj to release a resource that it needs.
An edge (pi , pj ) exists in a wait-for graph if and only if the resource
RAG contains two edges (pi , rq ) and (rq , pi) for some resource, see
figure 7.8.
.A deadlock exists in the system if and only if the wait—for graph contains a cycle
Recovery from Deadlock
When a detection Alg. determines that a deadlock exists the system must recover from the deadlock.
There are two options for breaking a deadlock.
A- Process termination by killing a process, two methods:
- Kill all deadlocked processes.
- Kill one process at a time until the deadlock cycle is eliminated.
B- Resource preemption, to eliminate deadlocks using resource preemption we can preempt some
resources front processes and give them to other processes until the deadlock cycle is broken.
If preemption is required in order to deal with deadlocks then three issues need to be addressed:
- Selecting a Victim: Which process and which resources.
- Rollback: If we preempt a resource from a process what should be done with that
process?
- Starvation: How do we ensure that Starvation will not occur?
That is how can we guarantee that resources will not always be preempted from the some
process?