Deadlocks 13,14
Deadlocks 13,14
In a multi-programming environment,
several processes may occur or have
number of resources,
->A process request resources, if the
resources are not available at that time, the
process enters a wait state. waiting process
may never again change state, because the
resources they have requested one held by
other waiting processes.
This situation is called deadlock
Resources: memory space , CPU cycles, files
and i/o devices (such as printers and tape drivers)
Ex: if the system has two CPU's.
A process must request a resource before
using it, and must release the resource after
using it.
A process cannot request 3 printers if the
system has only two.
A process may utilize a resource in only
the following sequences.
Request: if the request cannot be granted
immediately then the requesting process must
wait untill it can acquire the resource .
Use: the process can operate on the resources.
If the resources is printer, the process can print.
P1 P2 P3
R2 R4
Process States
The process P1 is holding the instance of R2
and is waiting for an instance of resource type
R1.
Process P2 is holding an instance of R1and R2
and is waiting for an instance of resource type
R3.
Process P3 is holding an instance of R3.
Through this definition of a resource allocation
graph it can be shown that if the graph contains
no cycles, then no process in the system is dead
locked. If the graph does contain a cycle then a
deadlock may exist.
Resource-Allocation Graph with a Dead Lock
R1
R3
P1 P2 P3
R2 R4
P1->R1->P2->R3->P3->R2->P1
P2->R3->P3->R2->P2
Process P1,P2,P3 are deadlocks
Process P2 is waiting for the resources R3
which is held by process P3.
Resource-allocate graph with a cycle but no
deadlock
P2
R1
P3
P1
R2
P4
We also have a cycle
P1->R1->P3->R2->P1
There is no deadlock. Observe that process
P4 may release its instance of resource type
R2 that resource then can be allocated to
P3,breaking the cycle.
Methods for Handling Deadlocks
2)Deadlock prevention:
-> mutual exclusion
->Hold and wait
->No pre-emption
->circular wait
Deadlock avoidance
->Safe state
->Unsafe state
->Resource allocation graph
->Bankers algorithm
->Safety algorithm
->Resource Request algorithm.
->Examples
1)Safe State: a state is safe if the system can
allocate resources to each process (up to it’s
maximums) in some order and still avoid a deadlock.
->a system is in a safe state only if there exists a safe
sequence for the current allocation state if,
R1
p2
p1
R2
P2
P1
R2
Banker’s Algorithm
This algorithm is commonly known as the banker’s
algorithm. The name was chosen because this
algorithm could be used in a banking system to
ensure that the bank never allocates it’s available
cash such that it can no longer satisfy the needs of all
it’s customer’s.
->several data structures must be maintained to
implement the banker’s algorithm. These data
structures encode the state of the resource-allocation
system. Let n be the number of processes in the
system and m be the number of resources type. We
need the following data structures
Available: a vector of length m indicates the no. of
available resources of each type .
If available[j]=k, there are k instance type Rj available.
Max: an n*m matrix defines the maximum demand of
each process.
If max[I,j]=k, then process Pi may request at most K
instance of resources type Rj.
Allocation: an n*m matrix defines the number of
resources of each type currently allocated to each
process.
If allocation [I,j]=k, then process Pi is currently
allocated 1< instance 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
Instance of resource type Rj to complete it’s
task .note that need[i,j]=max[I,j]-allocation[I,j].
Safety algorithm:-
If a system is in a safe state can be described as
follows
1.Let work and finish be vector of length m and n
respectively.
Initialize work=available and finish [i]=f , for
i=1,2,3……n.
2) Find an I such that both
a) Finish [i]=f
b) Need <= work
If no such is exists go to step4
.
When a system is currently in a safe state. The
sequence <P1,P3,P4,P2,P0> satisfies the safety
criteria. Compare with available which is less than
the available and need 1 by 1 then put in a
sequence
DeadLock Detection
A deadlock situation may occur. In this situation the
system must provide .
-> the system must identify the state of the system
to determine whether a deadlock has occurred.
-> an algorithm to recover from the deadlock .
->the following discussion we have to check the
one instances of resources or two or multiple types
resources (single or multiple resources) what will
happen in single and multiple we will see.
Single instance of each resource
type:
R1
P2
R2
P1
2) Several instances of a resources type: for more
than one
-> Available:
-> Allocation
->Request: Request [I,j]=k
1) Let work and finish be vectors of length m and n
which initialize work=Available. For i=1,2,3….n if
Allocation I !=0, then finish[i]=F, otherwise ,
finish[i]=T.
2) 2) find an index i such that both
a. finish[i]=T.
b.Request I <=work
If no such i exists goto step4.
3) work= work+ allocation
Finish[i]=T
Goto step2
4) If finish[i]=F, for some I, 1<= i<=n, then the system
is in a deadlock state. Moreover if finish[i]=F, then
process Pi is deadlock.
To illustrate this algorithm, we consider a system with
five processes P0 through P4 and 3 Resources Types
A,B,C Resource Type A has 7 instances Resource
Type B has 2 instances and Resource Type C has 6
instances suppose that at a time we have the
following resources allocation state.
Allocation Request Available
ABC ABC ABC
P0 010 00 0 000
P1 20 0 202
P2 30 3 000
P3 21 1 100
P4 0 0 2 0 02
Detection – Alg usage:- detection answer
depends on 2 factors.
1) How often is a deadlock likely to occur if occur
the detection alg should be involved frequently.
2) How many processes will be affected by the
deadlock when it happens.