Oslecture8-9 (Copy)

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 94

Deadlocks

Outline
• System Model
• Deadlock Characterization
• Methods for handling deadlocks
• Deadlock Prevention
• Deadlock Avoidance
• Deadlock Detection
• Recovery from Deadlock
• Combined Approach to Deadlock Handling
Deadlock
• In a multiprogramming environment, several
processes may complete for a finite number of
resources.
• A process requests resources,if the resources
are not available at that time,the process enters
a wait state.
• Waiting processes never again change state
because the resources they have requested are
held by other waiting processes. This situation
is called a Deadlock.
Cont…
• For Example 1 : A System contains one tape
drives and printer device and two processes
Pi and Pj. Both Pi and Pj require the tape
and printer devices for their functioning.
• The processes make their resource requests
in the following order:
1. Process Pi requests the tape,
2. Process Pj requests the printer.
3. Process Pi requests the printer,
4.Process Pj requests the tape.
Cont…

• Example 2
– Semaphores A and B each initialized to 1
P0 P1
wait(A) ; wait(B)
wait(B) ; wait(A)
Signal (A); Signal (B)
Signal (B); Signal (A)
Example - Bridge Crossing

– Assume traffic in one direction.


• Each section of the bridge is viewed as a resource.
– If a deadlock occurs, it can be resolved only if
one car backs up (preempt resources and
rollback).
• Several cars may have to be backed up if a
deadlock occurs.
• Starvation is possible
System Model
• A set of processes is in a deadlock state
when every process in the set is waiting
for an event that can be caused by
another process in the set.
• Events: Resource acquisition and release.
• Resource types
– R1, R2,….Rn
– Physical resources: CPU cycles, memory space,
I/O devices.
Cont..
• Logical resources: files, semaphores and
monitors
• Each resource type Ri has Wi instances
• Each process utilizes a resource as
follows:
Request: if the request is not granted, then it must
wait.
Use: The process can operate on the resource.
Cont..
• Release: The process releases the resource.
request -> use -> release
• Serially Reusable Resources
- CPU cycles, memory space, I/O devices,
files
acquire -> use -> release
Cont…
• Consumable Resources
– Produced by a process, needed by a
process - e.g. Messages, buffers of
information, interrupts
create ->acquire ->use
– Resource ceases to exist after it has been
used.
- Multi-threaded programs are
good candidates for deadlock
because multiple threads
complete for shared resources.
Conditions for Deadlock
• The following 4 conditions are necessary and
sufficient for deadlock (must hold
simultaneously)
1.Mutual Exclusion:
– Only once process at a time can use the
resource. If another process requests that
resource, the requesting process must be
delayed until the resource has been released.
2.Hold and Wait:
– A Processes hold resources already allocated
to them while waiting for other resources.
Contd..
3.No preemption:
-Resources cannot be preempted that is a
resource can be released only voluntarily by the
process holding it , after that process has
completed its task.
4. Circular wait:
-A set {P0,P1,...,Pn} of waiting processes must exit 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.
Resource Allocation Graph
• Deadlock can be described in terms of a
directed graph called system resource-
allocation graph.
• Consider of a set of vertices V and a set of
edges E
• V is partitioned into 2 types of nodes
• P = {P1, P2,…,Pn} - the set of processes in the
system
• R = {R1, R2,…,Rm} - the set of resource types in
the system
Cont..
• Two kinds of edges
• Request edge - Directed edge Pi ---> Rj
- It signifies that process Pi requested
an instance of resource type Rj and
is currently waiting for that resource.
• Assignment edge –
Directed edge Rj ----> Pi
- It signifies that an instance of resource type
Rj has been allocated to process Pi .
Cont..
• Represent each Process as a circle.

• Resource type Rj as a square with 4


instances

• Pi requests instance of Rj

• Pi is holding an instance of Rj
Cont..
• The resource – allocation graph depicts
the following situation.
• The sets P, R, and E:
- P = {P1,P2,P3}
- R = {R1,R2,R3,R4}
- E = {P1->R1, P2->R3, R1->P2,R2->P2,
R2->P1,R3->P3}
Cont..
• Resource instances:
- One instance of resource type R1
- Two instance of resource type R2
- One Instance of resource type R3
- Three instance of resource type R4
• Process States:
- Process P1 is holding an instance of resource type R2,

and is waiting for an instance of resource type R1.


Resource-allocation Graph
R1 R3

P1 P2 P3

R2 R4
Cont…
- Process P2 is holding an instance of R1 and
R2, and is waiting for an instance of resource
type R3.
- Process P3 is holding an instance of R3
• Given the definition of resource-allocation, if the
graph contains no cycles, then no process in
the system is deadlocked.
• If the graph contain a cycle, then a deadlock
may exist.
Cont..
• If each resource type has exactly one
instance, then a cycle implies that a
deadlock has occurred.
• If the cycle involves only a set of resource
types, each of which has only a single
instance, then a deadlock has occurred.
Graph with cycles and
deadlock
R1 R3

P1 P2 P3

R2 R4
Cont..
• Each process involved in the cycle is
deadlocked.
• Let us illustrate the concept of deadlock.
• Suppose process P3 requests an instance of
resource type R2
• A request edge P3->R2 is added to the graph,
two minimal cycles exist in the system.
P1->R1->P2->R3->P3->R2->P1
P2->R3->P3->R2->P2
Cont..
• Process P1,P2 and P3 are deadlocked.
• Process P2 is waiting for the resource R3,
which is held by process P3.
• Process P3, on the other hand is waiting for
either process P1 or process P2 to release
resource R2.
• Process P1 is waiting for process P2 to release
R1
• Consider the resource-allocation graph have a
cycle.
Cont..
P1->R1->P3->R2->P1
• Process P4 may release its instance of resource
type R2, then it is allocated to P3, breaking the
cycle.
• If a resource-allocation graph does not have a
cycle, then the system is not in a deadlock state.
• On the other hand, if there is a cycle, then the
system may or may not be in a deadlock state.
Graph with cycles but No
Deadlock
R1 P2

P1 P3

R2 P4
Basic facts
• If graph contains no cycles
– NO DEADLOCK
• If graph contains a cycle
– if only one instance per resource type, then
deadlock
– if several instances per resource type,
possibility of deadlock.
Methods for handling deadlocks
• Three ways
1. Ensure that the system will never enter
a deadlock state.
2. Allow the system to enter a deadlock
state, detect it and then recover
3. Ignore the problem and pretend that
deadlocks never occur in the system;
• Used by most operating systems including UNIX
Deadlock Management
• To ensure that deadlocks never occur,
the system can use either deadlock
prevention and deadlock-avoidance
schemes.
- Deadlock Prevention:
– Design the system in such a way that
deadlocks can never occur.
– Set of methods for ensuring that at least
one of the necessary conditions can not
hold.
Cont..
– Deadlock Avoidance: Requires that
operating system be given in advance
additional information concerning which
resources a process will request and use
during its life time.
– Deadlock Detection: Examines the state of
the system to determine whether a deadlock
has occurred or not.
Cont..
– Recovery:
– After detection, clear the problem, allow
processes to complete and resources to
be reused. May involve destroying and
restarting processes.
Deadlock Prevention
– A set of methods for ensuring that at least one
of the necessary conditions can not hold.
– Restrain ways in which requests can be made
• Mutual Exclusion
– non-issue for sharable resources
– cannot deny this for non-sharable resources (important)
• Hold and Wait – must guarantee that when a
process requests a resource, it does not hold other
resources.
– Force each process to acquire all the required resources
at once. Process cannot proceed until all resources
have been acquired.
Cont..
• Protocol 1: Requires a process to request and be
allocated all its resources before it begins execution.
- System calls requesting resources for a process
precede all other system calls.
• Protocol 2: Allow process to request resources only
when the process has none.
- A process can request some resources and use them.
before it can request additional resources, it must
release all the resources that is currently allocated.
Cont..
• For eg. Consider a process that copies data
from tape drive to a disk file and then prints the
results to the printer.
Protocol1: If all the resources are requested at the
beginning of a process, then the process must request
the tape drive, disk file and printer.
- It will hold the printer during entire execution, even
though it needs at the end.
Protocol 2: It copies data from tape drive to disk file and
releases them. It again requests disk file and printer.
Cont..
Disadvantages:
1. Resource utilization is very low.
2. Starvation is possible.
• No Preemption
• Protocol 1: If a process that is holding some resources
requests another resource that cannot be immediately
allocated to it, then all resources currently being held are
released.
- Preempted resources are added to the list of resources
for which the process is waiting.
Deadlock Prevention (cont.)
- Process will be restarted only when it can
regain its old resources as well as the new
ones that it is requesting.
– It is possible for CPU registers and memory
space whose state can be restored later, but
not possible for printers and tape drives.
• Circular Wait
Protocol 1: Impose a total ordering of all resource
types , require that each process requests
resources in increasing order of enumeration; if a
resource of type N is held, process can only
request resources of types > N.
Cont..
• Let R = {R1,R2,…Rm} be the set of resource
types.
• Define a one-to-one function F:R->N,
where N is the set of natural numbers.
• For eg. If the set of resource types R includes
tape drives, disk drives and printers, then the
function F might be defined as follows:
F(tape drive) = 1,
F(disk drive) = 5,
Cont..
F(printer) = 12
• Consider the following protocol to prevent
deadlocks:
• Each process can request resources only an
increasing order of enumeration that is a
process can initially request any no. of instances
of a resource type Ri
• The process can request instances of resource
type Rj if and only F(Rj) > F(Ri),
Cont..
• Alternatively, whenever a process
requests an instance of resource type Rj,
it has released any other resources Ri
such that F(Ri) >= F(Rj)
• If these two protocols are used then the
circular wait condition can not hold.
Deadlock Avoidance

Deadlock Prevention algorithms
-Low device utilization,reduced system
throughput

Alternative method is get additional
information about the processes.
-Resources currently available, Resources currently
allocated to a process,future requests and releases of
each process.

Various algorithms differ about amount and
type of information required.
Deadlock Avoidance
• Requires that the system has some additional
information available.
• Simplest and most useful model requires that
each process declare the maximum number of
resources of each type that it may need.
• The deadlock-avoidance algorithm dynamically
examines the resource-allocation state to
ensure that there can never be a circular-wait
condition.
• Resource allocation state is defined by the
number of available and allocated resources,
and the maximum demands of the processes.
Safe state
• When a process requests an available resource,
system must decide if immediate allocation leaves the
system in a safe state.
• System is in safe state if there exists a safe sequence
of all processes.
• Sequence <P1, P2, …Pn> is safe, if for each Pi, the
resources that Pi can still request can be satisfied by
currently available resources + resources held by Pj
with j<i.
Cont..
• If Pi resource needs are not available,
Pi can wait until all Pj have finished.

• When Pj is finished, Pi can obtain


needed resources, execute, return
allocated resources, and terminate.

• When Pi terminates, Pi+1 can obtain


its needed resources...
Example
• Consider a system with 12 magnetic tape
drives and 3 processes P0,P1,P2.
• P0 requires 10 tape drives, P1 may need 4
and P2 may need upto 9 tape drives.
• Suppose at time t0,P0 is holding 5 tape
drives , P1 is holding 2 and P2 is holding 2
tape drives
• There are 3 free tape drives.
Cont.
Process Maximum needs Allocated Current needs

P0 10 5 5

P1 4 2 2

P2 9 2 7
Cont..
• A system may go from a safe state to an unsafe state.
• At t0,system is in safe condition.
• <P1,P0,P2> satisfies safety condition.
• Suppose, at t1 , process P2 request & is allocated 1
more tape drive.
• The system is no longer in a safe state.
• At this point, only process P1 can be allocated all its
tape drives.
• When it return them, the system will have only 4
available tape drives.
Cont.
• Since process P0 is allocated 5 tape
drives, but has maximum of 10,it may
then request 5 more tape drives. Since
they are unavailable, Process P0 must
wait. It is in a deadlock.
• This idea is simply to ensure that the
system will always remain in a safe state.
Safety Algorithm
1. Let Work and Finish be vectors of length m and
n respectively.
Initialize :
Work = Available
Finish[i] = false for i=1,2,…,n
2. Find and i such that both:
a) Finish[i] = false
b) Needi ≤ Work
If no such i exists , go to step4.
Cont..
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.
• Safety algorithm may require an order
of m*n2 operations
Example of Safety Algorithm
• 5 processes
– P0 - P4;
• 3 resource types
– A(10 instances), B (5 instances), C (7 instances)
• Snapshot at time T0
Allocation Max Available
A B C A B C A B C
P0 0 1 0 7 5 3 3 3 2
P1 2 0 0 3 2 2
P2 3 0 2 9 0 2
P3 2 1 1 2 2 2
P4 0 0 2 4 3 3
Example (cont.)
a) Find need matrix?
b) Is the system in safe state?
• The content of the matrix Need is defined to be
Max - Allocation.
• The system is in a safe state since the sequence <P1,P3,P4,P0,P2>
satisfies safety criteria.
Need
A B C
P0 7 4 3
P1 1 2 2
P2 6 0 0
P3 0 1 1
P4 4 3 1
Cont..
• Suppose the process P1 requests 1 additional
instance of resource type A and 2 instances of
resource type C,
Request_i = (1,0,2)
Request_i ≤ Need (i.e. (1,0,2) ≤ (1,2,2))
• To decide whether this request can be
immediately granted, First check that
Request_i ≤ Available ; i.e (1,0,2) ≤ (3,3,2)
which is true
Example: P1 requests (1,0,2)
– Therefore request has been fulfilled and
new state arrive.

Allocation Need Available


A B C A B C A B C
P0 0 1 0 7 4 3 2 3 0
P1 3 0 2 0 2 0
P2 3 0 2 6 0 0
P3 2 1 1 0 1 1
P4 0 0 2 4 3 1
Cont..
• Apply safety algorithm and find the sequence of
process that satisfy our safety requirement.
Work = Available
Work = 2 3 0
P0 i = 0 check Need0 ≤ work
(7 4 3 ≤ 2 3 0) ; which is false
i = 1 finish[1] = true
Work = [ 2 3 0 ] + [3 0 2] = [ 5 3 2]
Go to step 2
Cont..
i = 2 finish[2] = false
i = 3 finish[3] = true
work = [5 3 2] + [2 1 1] = [7 4 3]
Goto step 2
i = 4 finish[4] = true
work = [7 4 3] + [0 0 2] = [7 4 5]
goto step 2 (still there is I exist P0 and P2)
i = 0 finish[0] = true
work = [7 4 5] + [0 1 0] = [7 5 5]
Example (cont.)
i = 2 finish[2] = true
work = [7 5 5] + [3 0 2] = [10 5 7]
• Executing the safety algorithm shows that
sequence <P1, P3, P4, P0, P2> satisfies safety
requirement. Hence we can immediately grant
the request of process P1.
• Can request for (3,3,0) by P4 be granted?
• Can request for (0,2,0) by P0 be granted?
Basic Facts
• If a system is in a safe state  no
deadlocks.
• If a system is in unsafe state  possibility
of deadlock.
• Avoidance  ensure that a system will
never reach an unsafe state.
Resource Allocation Graph
Algorithm
• Used for deadlock avoidance when there
is only one instance of each resource type.

– Claim edge: Pi  Rj indicates that process Pi may


request resource Rj; represented by a dashed line.
– Claim edge converts to request edge when a process
requests a resource.
– When a resource is released by a process, assignment
edge reconverts to claim edge.
– Resources must be claimed a priori in the system.
• If request assignment does not result in the
formation of a cycle in the resource allocation
graph - safe state, else unsafe state.
Claim Graph

Process claims resource

Process requests resource

Process is assigned resource

Process releases resource


Claim Graph

1 2

Possible Deadlock!!
3
5
4
Banker’s Algorithm
• Bank never allocates its available cash such that
it no longer satisfy the needs of all customers.
• Used for multiple instances of each resource
type.
• Each process must a priori claim maximum use
of each resource type.
• 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 amount of time.
Data Structures for the Banker’s
Algorithm
• Let n = number of processes and m =
number of resource types.
- Available: Vector of length m.
If Available[j] = k, there are k instances of
resource type Rj available.
- Max: n  m matrix.
If Max[i,j] = k, then process Pi may request

at most k instances of resource type Rj.


Data Structures for the Banker’s
Algorithm
- Allocation: n  m matrix. If Allocation[i,j] = k, then
process Pi is currently allocated k instances 0f
resource type Rj.
- Need: n  m matrix. 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]


Resource-Request Algorithm for
Process Pi
Request_i = request vector for process Pi. If
Request_i[j] = k, then process Pi wants k
instances of resource type Rj.
1. If Request(i)  Need(i), go to step 2. Otherwise, raise
error condition, since process has exceeded its
maximum claim.
2: If Request(i)  Available, go to step 3.
Otherwise, Pi must wait since resources are not
available.
Contd...

3: Pretend to allocate requested resources to Pi by


modifying the state as follows:
Available := Available - Request (i);
Allocation (i) := Allocation (i) + Request (i);
Need (i) := Need (i) - Request (i);
- If safe  resources are allocated to Pi.
- If unsafe  Pi must wait and the old resource-
allocation state is restored.
Example of Banker’s Algorithm
Process Allocation Max Available
A B C D A B C D A B C D
P0 0 0 1 2 0 0 1 2 1 5 2 0
P1 1 0 0 0 1 7 5 0
P2 1 3 5 4 2 3 5 6
P3 0 6 3 2 0 6 5 2
P4 0 0 1 4 0 6 5 6
•Answer the following question using the Banker’s algorithm
1)What is the content of the matrix need?
2)Is the system in a safe state?
3)If a request from process P1 arrives for(0,4,2,0) can the request be
granted immediately?
Cont…
Solution:
1) Need Matrix
Need[i,j] = Max[i,j] - Allocation[i,j]
Process A B C D
P0 0 0 0 0
P1 0 7 5 0
P2 1 0 0 2
P3 0 0 2 0
P4 0 6 4 2
Cont…
2) Applying the Banker’s Algorithm
Let Avail be a vector of dimension 4
Let Avail = Available i.e Avail = (1,5,2,0)
Iteration 1: Check all processes starting from process P0
Need P0 < Avail ; Hence P0 is marked
Now Avail = Avail + Allocated P0
[1,5,2,0] + [0,0,1,2] = [1,5,3,2]
P1 Need >Avail ; Hence P1 not marked.
P2 Need< Avail ; P2 is marked
Avail = Avail + Allocated P2
Cont..
[1,5,3,2] + [1,3,5,4] = [2,8,8,6]
P3 Need< Avail ; P3 is marked
Avail = Avail + Allocated P3
[2,8,8,6] +[0,6,3,2] = [2,14,11,8]
P4 Need< Avail ; P4 is marked
Avail = Avail + Allocated P4
[2,14,11,8] + [0,0,1,4] = [2,14,12,12]
Iteration 2:
P1 Need< Avail ; P1 is marked
Avail = Avail + Allocated P1
Cont..
[2,14,12,12] + [1,0,0,0] = [3,14,12,12]
Safe Sequence <P0 P2 P3 P4 P1>
Hence system is in safe state.
3) Request P1(0,4,2,0)
Available – Requested
(1 5 2 0) – (0 4 2 0) = (1 1 0 0)
New state:
Cont..
Process Allocation Max Available
A B C D A B C D A B C D
P0 0 0 1 2 0 0 1 2 1 1 0 0
P1 1 4 2 0 1 7 5 0
P2 1 3 5 4 2 3 5 6
P3 0 6 3 2 0 6 5 2
P4 0 0 1 4 0 6 5 6

Process A B C D
P0 0 0 0 0
P1 0 3 3 0
P2 1 0 0 2
P3 0 0 2 0
P4 0 6 4 2
Cont..
• Apply Banker’s Algorithm to determine whether
the new state is safe or not. If the state is safe,
the request can be safely granted immediately,
else grant of the request is to be differ.
• Let Avail = Available = (1,1,0,0)
• Iteration 1:
P0 Need < Avail , P0 is marked.
Avail = Avail + Allocated P0
[1,1,0,0] + [0,0,1,2] = [1,1,1,2]
Cont…
P1 Need > Avail , P1 not marked.
P2 Need < Avail , P2 is marked.
Avail = Avail + Allocated P2
[1,1,1,2] + [1,3,5,4] = [2,4,6,6]
P3 Need < Avail , P3 is marked
Avail = Avail + Allocated P3
[2,4,6,6] + [0,6,3,2] = [2,10,9,8]
P4 Need < Avail , P4 is marked
Avail = Avail + Allocated P4
[2,10,9,8] + [0,0,1,4] = [2,10,10,12]
Cont..
Iteration2:
P1 Need < Avail , P1 is marked
Avail = Avail + Allocated P1
[2,10,10,12] + [1,4,2,0] = [3,14,12,12]

Safe Sequence <P0 P2 P3 P4 P1>


Hence system is in safe state.
Example2
Process Allocation Max Available
R1 R2 R3 R4 R1 R2 R3 R4 R1 R2 R3 R4

P1 0 0 1 2 0 0 1 2 2 1 0 0

P2 2 0 0 0 2 7 5 0
P3 0 0 3 4 6 6 5 6
P4 2 3 5 4 4 3 5 6
P5 0 3 3 2 0 6 5 2
•Answer the following question using the Banker’s algorithm
1)What is the content of the matrix need?
2)Is the system in a safe state? Justify
3)Can a request (0,1,0,0)from P3 be granted immediately? Justify the
answer. Show the system state after grant of request?
Deadlock Detection
• Deadlock detection is the process of actually
determining that a deadlock exists and
identifying the processes and resources involved
in the deadlock.
• If deadlock prevention and avoidance are not
done properly, as deadlock may occur and only
things left to do is to detect deadlock and
recover from the deadlock.
Cont..
• If graph contains no cycle – no deadlock
• If graph contains a cycle-
- If only single instance per resource
type, then deadlock
- If multiple instances per resource
type, possibility of deadlock
Single Instance of each
resource type
• Maintain wait-for graph
• Nodes are processes
• Pi  Pj if Pi is waiting for Pj.
• Periodically invoke an algorithm that
searches for a cycle in the graph.
• An algorithm to detect a cycle in a graph
requires an order of n^2 operations, where
n is the number of vertices in the graph.
Several instances of a resource
type
• Data Structures
– Available: Vector of length m. If Available[j] =
k, there are k instances of resource type Rj
available.
– Allocation: n  m matrix. If Allocation[i,j] = k,
then process Pi is currently allocated k
instances of resource type Rj.
– Request : An n  m matrix indicates the current
request of each process. If Request [i,j] = k,
then process Pi is requesting k more instances
of resource type Rj .
Deadlock Detection Algorithm
• Step 1: Let Work and Finish be vectors of length m
and n, respectively. Initialize
– Work := Available
– For i = 1,2,…,n, if Allocation(i)  0, then Finish[i] := false,
otherwise Finish[i] := true.
• Step 2: Find an index i such that both:
– Finish[i] = false
– Request (i)  Work
– If no such i exists, go to step 4.
Deadlock Detection Algorithm
• Step 3: Work := Work + Allocation(i)
– Finish[i] := true
– go to step 2
• Step 4: If Finish[i] = false for some i, 1  i  n, then
the system is in a deadlock state. Moreover, if
Finish[i] = false, then Pi is deadlocked.

Algorithm requires an order of m  (n^2)


operations to detect whether the system is in
a deadlocked state.
Example of Detection Algorithm
• 5 processes - P0 - P4; 3 resource types - A(7
instances), B(2 instances), C(6 instances)
• Snapshot at time T 0: <P0,P2,P3,P1,P4> will result
in Finish[i] = true for all i.
Process Allocation Request Available
A B C A B C A B C
P0 0 1 0 0 0 0 0 0 0

P1 2 0 0 2 0 2

P2 3 0 3 0 0 0

P3 2 1 1 1 0 0

P4 0 0 2 0 0 2
Cont..
Work = Available
Work = 0 0 0
Request_i ≤ work
P0 i = 0 check Request ≤ work
(0 0 0 ≤ 0 0 0)
Finish[0] = true
Work = [0 0 0] + [0 1 0] = [0 1 0]
P1 i = 1 (2 0 2 ≤ 0 1 0)
finish[1] = false
Cont..
P2 i = 2 check Request ≤ work
(0 0 0 ≤ 0 1 0)
Finish[2] = true
Work = [0 1 0] + [3 0 3] = [3 1 3]
P3 i = 3 check Request ≤ work
(1 0 0 ≤ 3 1 3)
Finish[3] = true
Work = [3 1 3] + [2 1 1] = [5 2 4]
Cont..
P4 i = 4 check Request ≤ work
( 0 0 2 ≤ 5 2 4)
Finish[4] = true
Work = [5 2 4] + [0 0 2] = [5 2 6]
P1 i = 1 check Request ≤ work
( 2 0 2 ≤ 5 2 6)
Finish[1] = true
Work = [5 2 6] + [2 0 0] = [7 2 6]
•Sequence<P0,P2,P3,P4,P1> will result in finish [i] = true
for all.
Cont…
• P2 requests an additional instance of type C then
Request matrix is as follows.

Request
A B C
P0 0 0 0
P1 2 0 2
P2 0 0 1
P3 1 0 0
P4 0 0 2
Cont..
Available = 0 0 0
Now check Request ≤ work
P0 i = 0 check Request ≤ work
(0 0 0 ≤ 0 0 0)
Finish[0] = true
Work = [0 0 0] + [0 1 0] = [0 1 0]
P1 i = 1 (2 0 2 ≤ 0 1 0)
finish[1] = false
P2 i = 2 check Request ≤ work
(0 0 1 ≤ 0 1 0)
Finish[2] = false
Cont..
P3 i = 3 check Request ≤ work
(1 0 0 ≤ 0 1 0)
Finish[3] = false
P4 i = 4 check Request ≤ work
(0 0 2 ≤ 0 1 0)
Finish[4] = false
•The resouece held by process P0.
•The no. of available resources is not sufficient to fulfill the
request of the other processes. Thus deadlock exist.
Detection-Algorithm Use
– When, and how often to invoke depends on:
• How often a deadlock is likely to occur?
• How many processes will need to be rolled back?
– One for each disjoint cycle
– How often --
• Every time a request for allocation cannot be
granted immediately
– Allows us to detect set of deadlocked processes
and process that “caused” deadlock.
– Extra overhead.
– Every hour or whenever CPU utilization drops.
Contd..

- With arbitrary invocation there may be many


cycles in the resource graph and we would not be
able to tell which of the many deadlocked
processes “caused” the deadlock.
Recovery from Deadlock
• Several alternatives exist.
- Inform the operator
- Automatic: System will recover from the
deadlock automatically
- Two options exist for automatically breaking a
deadlock
- Process termination
- Resource preemption
Recovery from Deadlock:
Process Termination
– Abort all deadlocked processes.
– Abort one process at a time until the deadlock
cycle is eliminated.
– In which order should we choose to abort?
– Priority of the process
– How long the process has computed, and how much
longer to completion.
– Resources the process has used.
– Resources process needs to complete.
– How many processes will need to be terminated.
– Is process interactive or batch?
Recovery from Deadlock:
Resource Preemption
• We preempt some resources from one process
and give it to other processes until the deadlock
cycle is broken.
• For preemption 3 issues are to be addressed
• Selecting a victim - minimize cost.
- Number of resources the deadlocked process
is holding, time consumed by it for execution.
Cont..
• Rollback
• return to some safe state, restart process
from that state. It is necessary to break the
deadlock.
• Starvation
• same process may always be picked as
victim; include number of rollback in cost
factor.
Combined approach to
deadlock handling
• Combine the three basic approaches
• Prevention
• Avoidance
• Detection
allowing the use of the optimal approach for each
class of resources in the system.
• Partition resources into hierarchically
ordered classes.
– Use most appropriate technique for handling
deadlocks within each class.

You might also like