Académique Documents
Professionnel Documents
Culture Documents
Process
Rj
Eg of RAG
P={P1,P2,P3}
R={R1,R2,R3,R4}
E={P1->R1, P2->R3, R1->P2,R2->P2,
>P1, R3->P3 }
Resource instances
1 instance of R1
2 instances of R2
1 instance of R3
3 instances of R4
R2-
Cycles:
P1R1P2R3P3R2P1
P2R3P3R2P2
Processes P1, P2 and P3 are deadlocked:
P2 is waiting for R3, which is held by P3
There is no deadlock:
P4 may release its instance on resource type
R2. That resource can then be allocated to P3,
breaking the cycle.
What is a knot?
A knot K in a graph is a nonempty set of nodes
such that for every node x in K, all nodes in K
and only the nodes in k are reachable from x.
P1
R2
R1
P2
P3
R1
P2
P3
P1
R2
P3
R1
P2
2.
R2
P3
P1
R1
Simplified
to
P2
P3
P2
1. Deadlock Prevention :
A set of methods for ensuring that at least one of the
necessary conditions cannot hold.
2.
Deadlock avoidance :
Predicts the future state of the system for avoiding
allocations that can eventually lead to a deadlock.
3.
Deadlock detection :
If a system does not employ either a deadlock
prevention or deadlock avoidance algorithm, then a
deadlock situation may occur.
Hence an algorithm can be used to determine whether
a deadlock has occurred, and an algorithm can be
used to recover from deadlock.
1.
Mutual exclusion
For mutual exclusion to not happen, resources should
be sharable.
But printer cannot be simultaneously shared by several
processes.
Read only files are an example of sharable resource.
If several processes attempt to open a read only file at
the same time, they can be granted simultaneous
access to the file.
A process never waits for a sharable resource.
In general , however, we deadlocks cannot be
prevented by denying mutual exclusion condition.
Some resources are intrinsically non sharable.
First
A process must request all of the resources before it
begins execution.
If all the needed resources are available, they are
allocated to the process
If one of the requested resources are not available, none
will be allocated and the process would just wait.
Second
Instead of requesting all its resources before its execution
starts, a process may request a resource during its execution
if it obeys the rule that it requests resources only when it
holds no other resources.
Advantages of the second policy over the first :
1.
Processes generally do not know how many resources they
will need until they have started running.
2.
A long process may require some resources only towards
the end of execution. Hence using the first policy a process
will unnecessarily hold these resources for the entire
duration of its execution.
Disadvantages
1.
2.
3. Circular wait
Denies circular wait
4. No Preemption
Sequence <P0, P1, , Pn> is safe if for each Pi, the resources that Pi can
still request can be satisfied by currently available resources + resources
held by all the Pj with j < i
If Pis resource needs are not immediately available, then Pi can wait
until all Pj have finished
When Pj finishes, Pi can obtain needed resources, execute, return
allocated resources, and terminate
When Pi terminates, Pi+1 can obtain its needed resources, and so on
P1
P2
p3
Max
Holds
P1
P2
p3
Max
Holds
P1
P2
p3
Max
Holds
Free =2
Eg
Free =0
Free =4
P1
P2
p3
Max
Holds
Free =1
P1
P2
p3
Max
Holds
Free =0
P1
P2
p3
Max
Holds
Free =6
P1
P2
p3
Max
Holds
Free =6
4. if(found)
{
Current_Avail= Current_Avail+ Alloci
Marked[i]=true
Save the process number in SafeString[]
got to step 3
}
5. if (Marked[i]=true) for all processes, the system is in
the safe state.
Print SafeString.
Otherwise the system is unsafe state and may be
deadlocked.
R2
R3
15
Process
Max
Alloc
R1
R2
R3
R1
R2
R3
P1
P2
P3
P4
P5
Av
R1
R2
R3
Process
Alloc
Need
R1
R2
R3
R1
R2
R3
R1
R2
R3
P1
P2
P3
P4
P5
false
P2
false
P3
false
P4
false
P5
false
Process
found
Current_Avail
SafeString [ ]
Marked [ ]
P5
[3 3 2] + [1 0 1]= [4 3 3]
P5
Marked[P5]=true
P4
[4 3 3] + [3 2 0]=[7 5 3]
P5,P4
Marked[P4]=true
P1
[7 5 3] + [2 1 0]=[9 6 3]
P5,P4,P1
Marked[P1]=true
P2
[9 6 3] + [3 2 3]=[12 8 6]
P5,P4,P1,P2
Marked[P2]=true
P3
[12 8 6] + [3 0 2]=[15 8 8]
P5,P4,P1,P2,P3
Marked[P3]=true
Since Marked value for all processes is true. Therefore, the system is in
safe state.
The Safe String of processes is {P5,P4,P1,P2,P3}
i.e. The processes can be scheduled in this order, to have a safe state in
the system.
Av
R1
R2
R3
Max
Alloc
Need
R1
R2
R3
R1
R2
R3
R1
R2
R3
P1
P2
P3
P4
P5
Ex: 2
Total resources 3 14 12 12
P0
P1
P2
P3
P4
Allocation
ABCD
0012
1000
1354
0632
0014
Max
ABCD
0012
1750
2356
0652
0656
Need
ABCD
0000
0750
1002
0020
0642
Available
ABCD
1520
P0-1 5 2 0 + 0 0 1 2 = 1 5 3 2
P2-1 5 3 2 + 1 3 5 4 = 2 8 8 6
P3-2 8 8 6 + 0 6 3 2 = 2 14 11 8
P4-2 14 11 8 + 0 0 1 4 = 2 14 12 12
P1- 2 14 12 12 + 1 0 0 0 =3 14 12 12
Total resources 3 14 12 12
P0
P1
P2
P3
P4
Allocation
ABCD
0012
1000
1354
0632
0014
Max
ABCD
0012
1750
2356
0652
0656
Need
ABCD
0000
0750
1002
0020
0642
Available
ABCD
Request from P1 [ 0 4 2 0]
1520
Check if Req<=Need
[ 0 4 2 0]<= 0750
Check if Req<=Av
[ 0 4 2 0]<= 1520
Av=Av-Req1 = [1 5 2 0 ] [0 4 2 0]=[1 1 0 0]
Alloc 1 =Alloc 1 +Req1=[1 0 0 0] + [0 4 2 0]=[1 4 2 0]
Need1 = Need1 Req1=[0 7 5 0] [0 4 2 0]= [0 3 3 0]
P0
P1
P2
P3
P4
Allocation
ABCD
0012
1420
1354
0632
0014
Max
ABCD
0012
1750
2356
0652
0656
Need
ABCD
0000
0330
1002
0020
0642
Available
ABCD
1100
P0-1 1 0 0 + 0 0 1 2 = 1 1 1 2
P2-1 1 1 2 + 1 3 5 4=2 4 6 6
P3-2 4 6 6 + 0 6 3 2=2 10 9 8
P4-2 10 9 8 + 0 0 1 4 = 2 10 10 12
P1-2 10 10 12 + 1 4 2 0 =3 14 12 12
Safe sequence P0 P2 P3 P4 P1
Request can be granted
Ex :3
5 processes P0 through P4;
3 resource types:
A (10 instances), B (5instances), and C (7 instances)
Snapshot at time T0:
Allocation
ABC
P0
P1
P2
P3
P4
0
2
3
2
0
1
0
0
1
0
0
0
2
1
2
Max
Available
ABC
ABC
753
322
902
222
433
332
P0
P1
P2
P3
P4
Need
ABC
743
122
600
011
431
P1-3 3 2 + 2 0 0 = 5 3 2
P3-5 3 2 + 2 1 1 = 7 4 3
P4-7 4 3 + 0 0 2= 7 4 5
P0- 7 4 5 + 0 1 0 = 7 5 5
P2- 7 5 5 + 3 0 2 =10 5 7
Request Need
Request Available
Execute safety algorithm:
1 0 2 <= 1 2 2
1 0 2 <= 3 3 2
We pretend that this request has been fulfilled
Allocation
Need
Available
ABC
ABC
ABC
P0 0 1 0
743
230
P1 3 0 2
020
P2 3 0 2
600
P3 2 1 1
011
P4 0 0 2
431
P1- 2 3 0 + 3 0 2 = 5 3 2
P3- 5 3 2 + 2 1 1= 7 4 3
P4- 7 4 3 + 0 0 2 = 7 4 5
P0- 7 4 5 + 0 1 0 =7 5 5
P2- 7 5 5 + 3 0 2 = 10 5 7
Executing safety algorithm shows that sequence < P1, P3, P4, P0,
P2> satisfies safety requirement hence P1s request can be
granted.
When the system is in this state------
Example 4:
4 types resources R1 R2 R3 R4 with 6 8 10 and 12
instances.
5 processes(P1P5)
Max
Alloc
R1 R2 R3 R4
R1 R2 R3 R4
P1 3
P1 1
P2 3
P2 1
P3 2
P3 1
P4 5
P4 1
P5 1
P5 1
Need
R1 R2 R3 R4
P1 2
P2 2
P3 1
P4 4
P5 0
Av < 1 4 0 7 >
If P5 requests 7 instances of R4
Req <0 0 0 7>
Req<=Need5----yes
Req<=Av -----yes
Alloc
Need
R1 R2 R3 R4
P1 1
P2 1
P3 1
P4 1
P5 1
R1 R2 R3 R4
P1 2
P2 2
P3 1
P4 4
P5 0
Av < 1 4 0 0>
P3 1 4 0 0 + 1 2 3 1 = 2 6 3 1
No process can proceed after this.
System would be in an unsafe state .
Hence request cannot be granted.
Pi Pj
if
Resource-Allocation Graph
Detection algorithm
Let Current_Avail and Marked be two vectors of length n
and p respectively.
Safe string is an array to store process IDs.
1. Current_Avail=Av
2. Initialize Marked as :
for(i=1; i<=p; i++)
If Alloci 0 then Marked [i]=false; else
Marked[i]=true.
3. Find a process Pi such that
Reqi <= Current_Avail and Marked[i]=false
If no such i exists then goto step 5
4. if(found)
{
Current_Avail= Current_Avail+ Alloci
Marked[i]=true
Save the process number in SafeString[]
got to step 3
}
5. if (Marked[i]=false) for some i, then the system is in the
deadlock state.
Moreover if (Marked[i]=false) then process Pi is
deadlocked.
Note : Since Reqi<=Current_Avail for Pi, Pi is not involved in deadlock.
P0 - 0 0 0 + 0 1 0 = 0 1 0
P2 - 0 1 0 + 3 0 3 = 3 1 3
P3 - 3 1 3 + 2 1 1 = 5 2 4
P4 - 5 2 4 + 0 0 2 = 5 2 6
P1 5 2 6 + 2 0 0 = 7 2 6
Sequence <P0, P2, P3, P4 ,P1> will result in Finish[i] = true for all i
No deadlock
Ex: 2
4 resources total res=(4 2 3 1)
A= 0 0 1 0
R= 2 0 0 1
2001
1010
0120
2100
Av=( 2 1 0 0)
P3 2 1 0 0 + 0 1 2 0= 2 2 2 0
P2- 2 2 2 0 + 2 0 0 1= 4 2 2 1
P1- 4 2 2 1 + 0 0 1 0= 4 2 3 1
SAFE state
Current state is not deadlocked
Process Termination
2. Resource Preemption
To eliminate deadlocks using resource preemption, we
successively preempt some resources from processes and
give these resources to other processes until the deadlock
cycle is broken.
If preemption is required to deal with deadlocks, then three
issues need to be addressed:
1.Selection a victim.
Which resources and which processes are to be preempted?
As in process termination, we must determine the order of
preemption to minimize cost. Cost factors may include such
parameters as the number of resources a deadlocked process
is holding and the amount of time the process has thus far
consumed during its execution.
2. Rollback
If we preempt a resource from a process, what should be done
with that process? Clearly, it cannot continue with its normal
execution; it is missing some needed resource. We must roll
back the process to some safe state and restart if from that
state.
3. 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 same process?
It may happen that the same process is always picked as a
victim. As a result, this process never completes its designated
task. We must ensure that a process can be picked as a victim
only a (small) finite number of times. The most common solution
is to include the number of rollbacks in the cost factor.