0% found this document useful (0 votes)
17 views

Deadlocks Final

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Deadlocks Final

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 84

Neither truck can proceed.

 A system consists of a finite number of resources to be


distributed among a number of competing processes.

 The resources are partitioned into several types each of


which consists of some number of identical instances.

 Eg of resources : CPU, memory space, files, I/O


devices(printers, disk, tape drive)

 Eg: If a system has 2 CPUs, then the resource type CPU


has 2 instances.
 A process may utilize a resource in only the following
sequence:
1. Request: If a request cannot be granted
immediately(because the resource is being used by
another process), then the requesting process must
wait, until it can acquire the resource.
2. Use : The process can operate on the resource.
3. Release : The process releases the resource.
 The request and release of resources are system calls.
 A system call is how a program requests a
service from an operating system kernel.
 Eg: to access a file(resource) open , close, read,
write are system calls.
 The operating system maintains a table which records
whether each resource is free or allocated, and if a
resource is allocated, it is allocated to which process.
 If a process requests a resource a resource that is

currently allocated to another process, it is added to a


queue of processes waiting for this resource.
What is a deadlock?
Eg :1 Consider a system with 3 tape drives.
Suppose each of the 3 processes holds one of the tape
drives. If each process now requests another tape drive,
the 3 processes will be in a deadlock state.
Deadlocks may also involve different
resource
types.
Eg :Consider a system with 1 printer and 1
tape drive.
Suppose that Process Pi is holding the tape
dive and the process Pj is holding the
printer. If Pi requests the printer and Pj
requests the tape drive, a deadlock occurs.
Necessary conditions for a deadlock:
Deadlock can arise if four conditions hold simultaneously.
 Mutual exclusion: only one process at a time can use a
resource. If another process requests that resource, the
requesting process must be delayed until the resource has
been released.

 Hold and wait: a process holding at least one resource is


waiting to acquire additional resources held by other
processes.

 No preemption: a resource can be released only


voluntarily by the process holding it, after that process has
completed its task.
 Circular wait: there exists a set {P0, P1, …, P0} of
waiting processes such that P0 is waiting for a resource
that is held by P1, P1 is waiting for a resource that is
held by
P2, …, Pn–1 is waiting for a resource that is held by
Pn, and Pn is waiting for a resource that is held by P0.

ALL THE FOUR CONDITIONS MUST HOLD SIMULTANEOUSLY FOR A


DEADLOCK TO OCCUR
 Deadlocks can be described with the help of a
directed graph called a Resource Allocation
Graph(RAG)
A set of vertices V and a set of edges E.

 V is partitioned into two types:


◦ P = {P1, P2, …, Pn}, the set consisting of all
the processes in the system.

◦ R = {R1, R2, …, Rm}, the set consisting of all


resource types in the system.
 Request edge – directed edge Pi  Rj
 Assignment edge – directed edge Rj  Pi
 Process

 Resource Type with 4 instances

 Pi requests instance of Rj - request edge

Pi
Rj
 Pi is holding an instance of Rj – assignment edge

Pi
Rj
Eg of RAG
 P={P1,P2,P3}
 R={R1,R2,R3,R4}
 E={P1->R1, P2->R3, R1->P2,R2->P2, R2-
>P1, R3->P3 }

Resource instances
 1 instance of R1
 2 instances of R2
 1 instance of R3
 3 instances of R4
No cycle hence no
deadlock
 It can be shown that if the graph contains no
cycles, then no process in the system is
deadlocked.
 If the graph does contain a cycle, then a deadlock
may exist.
Cycles:
P1R1P2R3P3R2P1
P2R3P3R2P2

Processes P1, P2 and P3 are deadlocked:


P2 is waiting for R3, which is held by P3
P3 is waiting for either P1 or P2 to release R2
P1 is waiting for P2 to release R1
Graph With A Cycle But No Deadlock

Cycle:
P1R1P3R2P1

There is no deadlock:
P4 may release its instance on resource type
R2. That resource can then be allocated to P3,
breaking the cycle.
Necessary and sufficient
conditions for
deadlock
• A cycle is a necessary condition for a
deadlock to occur.
• But if a graph contains a cycle then a
deadlock may or may not exist.
• Hence presence of a cycle is a necessary
but not a sufficient condition for the
existence of a deadlock
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

Cycle but no deadlock since resource R1 has


2 units and there are no knots
Suppose P3 requests for R2 and a request edge
(R3,R2) is added to the graph then the modified
graph--- P1

R2
R1

P2

P3

This graph has 2 cycles (P1,R2,P2,R1,P1) and (P3,R2,P2,R1,P3) and a knot


(P1,P2,P3,R1,R2). Since the graph contains a knot, it represents a
deadlock
Single instance of each resource

P1

R2
R1

P3 P2

Cycle present hence P1,P2 and P3 are deadlocked


The sufficient conditions for deadlock is different
for
the following different cases :
1. A cycle in the graph is both a necessary and a sufficient
condition for deadlock if all of the resource types
requested by the processes forming the cycle have only a
single unit each.
2. A cycle in the graph is a necessary but not a sufficient
condition for a deadlock if one or more of the resource
types requested by the processes forming the cycle have
more than one unit. In this case a knot is a sufficient
condition for a deadlock
2. Wait for graph
When all resource types have a single unit
each, simplified form of resource allocation
graph called wait for graph could be used.
P1 P1
Simplified
R2
R1
to

P3 P2
P2
P3
There are 3 different methods for dealing with the
deadlock problem :
 Use a protocol to ensure that the system will never

enter a deadlock state.(Deadlock avoidance and


deadlock prevention)
 Allow the system to enter a deadlock state and then

recover.(Deadlock Detection)
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.
 For a deadlock to occur, each of the 4 necessary
conditions must hold.
 By ensuring that at least one of the 4 conditions
cannot hold, occurrence of deadlock can be
prevented.
 Examining each of the 4 necessary conditions
separately…….
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.
2. Hold and wait
 This method denies the hold and wait condition by
ensuring that whenever a process requests a resource, it
does not hold any other resources.
 One of the following policies are used:
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 it’s


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. Leads to low resource utilization because a process may
hold many resources but may not actually use several of
them for long periods.

2. It may cause starvation of a process that needs many


resources.
3. Circular wait
Denies circular wait
• Each resource type is assigned a unique global number to
impose a total ordering of all resource types.
• A process should not request a resource with a number
lower than the number of any resources that it is holding.
• That is if a process holds a resource type whose number is
i, it may request a resource type having the number j only if
j>i
• Hence with this rule there can never be cycles in the
resource allocation graph (denying circular wait condition)
and hence making deadlock impossible to occur.
• Note : Does not require that a process must acquire
all its resources in strictly increasing sequence
• For instance a process holding 2 resources numbered 3
and 7, may release the resource having number 7 before
requesting a resource having number 5.
• The ordering of resources is decided according to the
natural usage pattern of the resources.
• For eg : Since a tape drive is usually needed before the
printer, it would be reasonable to assign a lower number to
the tape drive than to the printer.
Drawbacks :
This natural ordering is not the same for all jobs.
4. No Preemption
 No preemption is a necessary condition for a
deadlock.
 To ensure that this condition does not hold we can use
the following protocol.

1. When a process requests for a resource that is not


currently available, all the resources held by the process
are taken away(preempted) from it and the process is
blocked. The process is unblocked when the resource
requested by it and the resources preempted from it
becomes available and can be allocated it.
2. When a process requests a resource that is not currently
available, the system checks if the requested resource is
currently held by a process that is blocked, waiting for
some other resource. If so the requested resource is taken
away from the waiting process and given to the requesting
process. Otherwise , the requesting process is blocked and
waits for the requested resource to become available.
Some of the resources that this process is already holding
may be taken away (preempted) from it while it is blocked,
waiting for the allocation of the requested resource.
The process is unblocked when the resource requested by it
and the resources preempted from it become available and
can be allocated it.
Dijkstra’s Banker algorithm for deadlock
 Dijkstra designed an algorithm known as bankers
algorithm.
 This algorithm takes analogy of a bank, where
customers can take loans from the bank.
◦ No customer is granted loan exceeding bank’s
total capital.
◦ All customers are given a maximum credit limit
◦ No customer is allowed to borrow over the limit.
◦ The sum of all loans won’t exceed bank’s total
capital.
 Predicts the future state of the system for avoiding
allocations that can eventually lead to a deadlock.
Steps for deadlock avoidance :
1. When a process requests for a resource, even if the
resource is available for allocation, it is not immediately
allocated to the process. Rather the system simply
assumes that the request is granted.
2. With this assumption made and the advance knowledge
of the resource usage of processes, the system performs
some analysis to decide whether granting the process’s
request is safe or unsafe.
3. If safe , the resource is allocated to the process,
otherwise the request is deferred.
 Simplest and most useful model requires that each
process declare the maximum number of resources of
each type that it may need.
The banker’s algorithm has 2 parts.
 The first part is a Safety Test algorithm that checks the
current state of the system for its safe state.
 The second part is resource request handling algorithm
that verifies whether the requested resources, when
allocated to the process, affects the safe state.
If it does, the request is denied.
IN THIS WAY BANKERS’S ALGORITHM AVOIDS
DEADLOCK.
 When a process requests an available resource, system must decide if
immediate allocation leaves the system in a safe state
 System is in a safe state if there exists a safe sequence of all processes
 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 Pi‘s 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
 If system is in safe state  no deadlock
 If system is in unsafe state  possibility of
deadlock
 Avoidance  ensure that system will never
enter an unsafe state
P1 P2 p3

Max 4 5 6
Holds 2 2 2
Free =2

P1 P2 p3
Eg
Max 4 5 6
Holds 4 2 2
Free =0

P1 P2 p3

Max - 5 6

Holds 0 2 2
Free =4

P1 P2 p3 P1 P2 p3

Max - 5 6 Max - 5 6
Holds 0 5 2 Holds 0 2 6
Free =1 Free =0

P1 P2 p3 P1 P2 p3

Max - - 6 Max - 5 -

Holds 0 0 2 Holds 0 2 0
Free =6 Free =6
Hence the analysis performed shows that this
state is safe because there exists a
sequence of allocations that allows all
processes to complete.
Hence the two safe sequences are (P1,P2,P3)
and (P1, P3, P2).
Hence the initial state is a safe state
because it avoids deadlocks
 Suppose system has 12 instances of a particular resource
Maximum Needs Currently Allocated
Need
P0 10 5 5
P1 4 2 2
P2 9 2
7
(i.e. currently 3 free)
 At time t0, system is in safe state because there exists a
process sequence that would allow each process to
immediately be allocated its maximum needed resources. .. For
example <P1, P0, P2>
 P1 could need 2 more, which results in 1 free, then P1 releases
resulting in 5 free, then P0 could get all its maximum, then P1
releases, for a total of 10 free, then P2 could get his maximum.

 No suppose that at time t1, P2 requests 1 more. The


question is, if we grant P1 1 more, will the resulting
state be safe ? (i.e. will there be a safe sequence ?)

Operating System Concepts


 Suppose system has 12 instances of a particular resource
Maximum Needs Currently Allocated Need
P0 10 5 5
P1 4 2 2
P2 9 3 6
(i.e. currently 2 free)
 What are some potential process sequences now ?
 <P0, P1, P2> … P0 can’t get next 5 b/c only 2 free
 <P1, P2, P0> … P1 can get next 2, run, (4 free), but then P2
can’t
 <P2, P0, P1> … P2 can’t get next 6, b/c only 2 free
 <P0, P2, P1> … P0 can’t get next 5 b/c only 2 free
 <P1, P0, P2> … P1 can get next 2, run, (4 free), but then P0
can’t
 <P2, P1, P0> … P2 can’t get next 6 b/c only 2 free
 Therefore, granting P2 the extra resource, leads to
unsafe state, which COULD lead to deadlock state.
Operating System Concepts
 Dijkstra’s Banker’s algorithm for deadlock avoidance in
multiple instances of resources.
1. DATA STRUCTURES :
Total resources in a system
Stores the total number of resources in a system.
Total_Res[i]=j
It means there are j instances of resource type Ri in the
system.
2. Maximum demand of a process
Whenever a process enters the system, it declares its
maximum demand of resources.
Max[i,j]=k
It means, a process Pi has a total demand of k instances of
resource type Rj.
3. Current Allocation of instances of each type of resource
It indicates the current allocation status of all resource types
to various processes in the system.
Alloc[i,j]=k
It means that process Pi is allocated k instances of resource
type Rj.
4. Number of available resources.
Stores the current available instances of each resource type.
Av[i]=j
It means that j instances of resource type Ri are available.
It is the difference between the total resources and the
allocated resources.
Av[i]= Total _Res[i]-∑all processes Alloc[i] where I is the
resource type Ri.
5. Current need of a process
Indicates the current remaining resource need of each
process.
Need[i,j]=k
It means process Pi may require k more instances of
resource
type Rj, so that it can complete execution.
Need[i,j]=Max[i,j]-Alloc[i,j]
6. Request for a process
Stores the resource request for process Pi.
Reqi[j]=k
It means that process Pi has requested k instances of
resource type Rj.
Safety Test 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++)
Marked [i]=false;
3. Find a process Pi such that
Needi <= 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]=true) for all processes, the system is in
the safe state.
Print SafeString.
Otherwise the system is unsafe state and may be
deadlocked.
Resource Request handling algorithm
Let Reqi be the vector to store the resource request for
process Pi.
1. If Reqi<=Needi, goto step2.
else raise an error condition, since the process has
exceeded its maximum claim.
2. If Reqi<=Av, goto step3.
else Pi must wait , since the resources are not
available.
3. Have the system pretend to have allocated the requested
resources to process Pi, by modifying the state as follows :
Av=Av-Reqi
Alloc i =Alloc i +Reqi
Needi = Needi – Reqi
Execute the safety test , assuming the state has been
changed
If(state is safe)
Change the state in actual and the resource will be
allocated to the process
otherwise
keep the state unchanged and do not
allocate the resource to the process.
 Consider a system with the following information.
Determine whether the system is in safe state.
 Total Res
R1 R2 R3
15 8 8

Proce Max Alloc


ss R1 R2 R3 R1 R2 R3

P1 5 6 3 2 1 0
P2 8 5 6 3 2 3
P3 4 8 2 3 0 2
P4 7 4 3 3 2 0
P5 4 3 3 1 0 1
Av

R1 R2 R3
3 3 2

Find Need matrix


Proces Max Alloc Need
s
R1 R2 R3 R1 R2 R3 R1 R2 R3

P1 5 6 3 2 1 0 3 5 3

P2 8 5 6 3 2 3 5 3 3

P3 4 8 2 3 0 2 1 8 0

P4 7 4 3 3 2 0 4 2 3

P5 4 3 3 1 0 1 3 3 2
Safety Test Algorithm to find whether the new state is safe
:
Current_Avail = Av = [ 3 3 2 ]
Marked
P1 false
P2 false
P3 false
P4 false
P5 false

Find a sequence of processes such that each process satisfies


the criteria Need<=Current_Avail.
A process after finishing its execution, must release all the
resources it is holding, so that the next process can avail
them as per its need.
Process Current_Avail SafeString Marked [ ]
found []
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.
At time t1, if P4 requests 2 more instances of R1 and 2
instances of R3.
 Request vector [2 0 2 ]
 Executing the request handling algorithm, to find out
whether the request can be granted.
 First check if Req4<=Need4. [2 0 2] <= [ 4 2 3 ]
 Check if Req4 <= Av. [ 2 0 2 ] <= [ 3 3 2]
 Assume to allocate these resources and update the
following
Av=Av-Req4 = [ 3 3 2] –[2 0 2]=[1 3 0 ]
Alloc 4 =Alloc 4 +Req4=[3 2 0 ] + [2 0 2]=[5 2 2]
Need4 = Need4 – Req4=[4 2 3] – [2 0 2]= [2 2 1]


Av
R1 R2 R3
1 3 0
Find Need matrix
Process Max Alloc Need

R1 R2 R3 R1 R2 R3 R1 R2 R3

P1 5 6 3 2 1 0 3 5 3

P2 8 5 6 3 2 3 5 3 3

P3 4 8 2 3 0 2 1 8 0

P4 7 4 3 5 2 2 2 2 1

P5 4 3 3 1 0 1 3 3 2

Check again whether the system is in a safe state

Current_Avail=Av=[1 3 0].

There is no process whose Need<=Current_Avail.


Hence no process can be started and thus the state would be unsafe if the request P4 is granted
Ex: 2 Consider the following snapshot of a system:
Allocation Max Available
ABCD ABCD ABCD
P0 0012 0012 1520
P1 1000 1750
P2 1354 2356
P3 0632 0652
P4 0014 0656

Answer the following questions using the banker’s


algorithm:
a. What is the content of the matrix Need?
b. Is the system in a safe state?
c. If a request from process P1 arrives for (0,4,2,0), can
the request be granted immediately?
Total resources 3 14 12 12
Allocation Max Need
ABCD ABCD ABCD
P0 0012 0012 0000
P1 1000 1750 0750
P2 1354 2356 1002
P3 0632 0652 0020
P4 0014 0656 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

Yes – System is in the safe state


Total resources 3 14 12 12
Allocation Max Need
ABCD ABCD ABCD
P0 0012 0012 0000
P1 1000 1750 0750
P2 1354 2356 1002
P3 0632 0652 0020
P4 0014 0656 0642
Available
ABCD
1520 Request from P1 [ 0 4 2 0]
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]
Perform the safety test
Allocation Max Need
ABCD ABCD ABCD
P0 0012 0012 0000
P1 1420 1750 0330
P2 1354 2356 1002
P3 0632 0652 0020
P4 0014 0656 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
Note : Solution is not unique
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 Max Available
ABC ABC ABC
P0 010 753 332
P1 200 322
P2 302 902
P3 211 222
P4 002 433

Is the state of the system safe ?


Need
ABC
P0 743
P1 122
P2 600
P3 011
P4 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

The system state: Safe


<p1 P3 P4 P0 P2> is a possible safe sequence
 P1 now requests (1,0,2).
◦ 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 P1’s request can be
granted.
When the system is in this state------
 Can request for (3,3,0) by P4 be granted?
 Can request for (0,2,0) by P0 be granted?
 Request for (3,3,0) by P4 cannot be
granted, sine the resources are not
available.
 A request for (0 2 0)by P0 cannot be

granted, even though the resources are


available , since the resulting state is unsafe
Example 4:
 4 types resources R1 R2 R3 R4 with 6 8 10 and 12

instances.
 5 processes(P1…P5)

Max Alloc

R R R R R R R R
1 2 3 4 1 2 3 4
P1 3 1 2 5 P1 1 0 2 1
P2 3 2 5 7 P2 1 1 2 0
P3 2 6 3 1 P3 1 2 3 1
P4 5 4 9 2 P4 1 1 1 1
How does the OS react if P5 requests 7 instances of R4.
P5 1 3 8 9 P5 1 0 2 2
Can this request be granted?
Need
R R R R
1 2 3 4
P1 2 1 0 4
P2 2 1 3 7
P3 1 4 0 0
P4 4 3 8 1
P5 0 3 6 7
 Av < 1 4 0 7 >
If P5 requests 7 instances of R4
Req <0 0 0 7>
Req<=Need5----yes
Req<=Av -----yes

Assume the request is granted


Av becomes ----<1 4 0 0>
Alloc Need
R R R R R R R R
1 2 3 4 1 2 3 4
P1 1 0 2 1 P1 2 1 0 4
P2 1 1 2 0 P2 2 1 3 7
P3 1 2 3 1 P3 1 4 0 0
P4 1 1 1 1 P4 4 3 8 1
Av P5
< 11 4 0
0 2
0> 9 P5 0 3 6 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.
 In deadlock detection we don’t take the
prior information from each process that is
the Max.
 But we allow processes to request and be

allocated the resources and periodically


invoke the deadlock detection algorithm
 i.e if resources are available, they are

allocated to a process.
 If a system is not able to implement neither deadlock
prevention nor a deadlock avoidance algorithm, it may
lead to a deadlock situation.
 For deadlock detection, the system must provide
◦ An algorithm that examines the state of the system to
detect whether a deadlock has occurred
◦ And an algorithm to recover from the deadlock

 Deadlock detection has 2 parts :


1. Detection when there is single instance of each resource.

2. Detection when there are multiple instance of each

resource.
Single Instance of Each Resource Type
 Requires the creation and maintenance of
a wait-for graph
◦ Consists of a variant of the resource-allocation graph
◦ The graph is obtained by removing the resource nodes from a
resource-allocation graph and collapsing the appropriate edges
◦ Consequently all nodes are processes
◦ Pi  Pj if Pi is waiting for Pj.

 Periodically invoke an algorithm that


searches for a cycle in the graph
◦ If there is a cycle, there exists a deadlock
Resource-Allocation Graph Corresponding wait-for graph
Several instances of resource type
 The WFG scheme is not applicable to a resource

allocation system with multiple instances of each


resource type.
Algorithm for deadlock detection
 Data structures similar to those in banker’s algorithm

Available(Av) A vector of length m indicates


the number of available resources of each type.
Allocation(Alloc): An pxr matrix defines the
number of resources of each type currently
allocated to each process.
Request(Req): An pxr matrix stores the
current request of each process.
Req[i,j]=k means process Pi is requesting k
instances of resource type Rj.
 Goal of the detection algorithm is to check if
there is any sequence in which all current
requests can be met.
 Note: If a process Pi’s request can be met,

then Pi can potentially run to completion,


and release all the resources it currently
holds.
 So for detection purpose, Pi’s current

allocation can be added to current


available.
 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.
Example of Detection Algorithm
• Five processes P0 through P4; three resource types
A (7 instances), B (2 instances), and C (6 instances)
• Snapshot at time T0:
Allocation RequestAvailable
ABC ABCABC
P0 010 000 000
P1 200 202
P2 303 000
P3 211 100
P4 002 002
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
Suppose that process P2 makes an additional request for
an instance of type C. The request matrix ---
Request
ABC
P0 0 0 0
P1 2 0 2
P2 0 0 1
P3 1 0 0
P4 0 0 2
System is deadlocked.
Although resources held by P0 are reclaimed , the number of available
resources is not sufficient to fulfill the requests of the other processes.
 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
1. Process Termination
 Two methods used.
 Both the methods reclaims all resources allocated to the
terminated processes.
a) Abort all deadlocked processes :
 This method clearly will break the deadlock cycle, but at
great
expense; the deadlocked processes may have computed for
along time, and the results of these partial computations
must
be discarded and probably will have to be recomputed later
b) Abort one process at a time until the deadlock cycle is
eliminated.
This method incurs considerable overhead, since, after each
process is aborted, a deadlock detection algorithm must be
invoked to determine whether any processes are still
deadlocked.
If the partial termination method is used, then we must
determine which deadlocked process (or processes) should
be
terminated.
Many factors may affect which process is chosen,
including:

 What the priority of the process is


 How long the process has computed and how much longer
the process will compute before completing its designated
task.
 How many and what type of resources the process has
used (for example, whether the resources are simple to
preempt)
 How many more resources the process needs in order to
complete
 Whether the process is interactive or batch
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.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy