0% found this document useful (0 votes)
17 views38 pages

Deadlocks 13,14

The document discusses deadlocks that can occur in a multi-processing environment where multiple processes require shared resources. It provides four key conditions for a deadlock to occur: mutual exclusion, hold and wait, no preemption, and circular wait. The document also describes resource allocation graphs that can be used to model deadlocks and discusses different methods for handling deadlocks, including deadlock prevention, avoidance, and recovery.

Uploaded by

kalle satish
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 views38 pages

Deadlocks 13,14

The document discusses deadlocks that can occur in a multi-processing environment where multiple processes require shared resources. It provides four key conditions for a deadlock to occur: mutual exclusion, hold and wait, no preemption, and circular wait. The document also describes resource allocation graphs that can be used to model deadlocks and discusses different methods for handling deadlocks, including deadlock prevention, avoidance, and recovery.

Uploaded by

kalle satish
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/ 38

Dead Locks:

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.

Release: the process release the resources.


Deadlock characterization

Features of the deadlock


1) Necessary conditions:
A deadlock condition can arise if the
following 4 conditions hold simultaneously.
1) mutual exclusion
2) hold & wait
3) non preemption
4) circular wait
1) Mutual Exclusion: At least one resource
must be held in a non sharable mode that is
only one process at a time can use the
resource. If another process request that
resource, the requesting process must be
delayed until the resource has been
released.
2) Hold & Wait: A process must be holding at
least one resource and waiting to acquire
additional resources that are currently
being held by the other processes.
3) No Preemption: Resources cannot be pre
empted

4) Circular Wait: A set {po,p1,------pn} of


waiting processes.
Po is waiting for a resource that is held by
p1,p1 is waiting for a resource that is held by
pn-1 is waiting for a resource that is held by pn,
pn is waiting for a resource that is held by p0
Resource Allocation Graph

Deadlocks can be described in terms of a


directed graph called a system resource
allocation graph. This graph consists of a set
of vertices V & a set of edges E. the set of
vertices V is partitioned into two different
types of nodes.
P={p1,p2,p3----pn}, the set consisting of all
the active process in the system and
R={R1,R2----Rn}
P= is the process type
R= is the resource type
A directed edge from the process Pi to resource type
Rj is denoted by Pi->Rj; it significant the process
Pi requested an instance of resource type Rj
and is currently waiting for that resources
Rj->Pi significant
1) A directed edge Pi->Rj is called a request edge.
2) A directed edge Rj->Pi is called an assignment
edge.
We represent each process Pi as circle and each
resource type Rj may have more than one
instance, we request each such instance as a
dot with in the square.
The resource allocation graph shares the
following situations.
1) The set P,R,& E
2) P={P1,P2,P3}
3) R={R1.R2.R3,R4}
4) E={P1->R1,P2->R3,R1->P2,R2->P2,R2-
>P1,R3->P3}
5) 2) Resource Instances
a) One instance of resource type R1.
b) Two instance of the resources type R2.
c) Three instance of the resource type R3.
d) Four instance of the resource type R4.
R1 R3

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

Three different methods


1) We can use protocol to ensure that the
system will never enter a deadlock state.
2) we can allow the system to enter a deadlock
state and then recover.
3)we can ignore the problem.
To ensure that deadlock never occur, the system
can use either a deadlock prevention or a
deadlock –avoidance scheme
Deadlock prevention -> is a set of methods for
ensuring that at least one of the necessary conditions
cannot hold.
Deadlock avoidance-> requires that the OS be
given in advance additional information concerning
which resource a process will request and use during
it’s lifetime.

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,

->if no such sequence exists then the system state is


said to be unsafe.
->a safe state is not a deadlock.
-> an unsafe state may lead to a deadlock.
-> as long as the state is safe the OS can avoid
unsafe and deadlock states.
Ex:- safe & unsafe state
A system with 12 magnetic tape drives and 3
processes p0,p1,p2. the process p0 requires
10 tape drives .the process p1 requires 4 tape
drives, the process requires 9 tape drives
suppose that at a time 10
maximum needs current needs
P0 10 5
P1 4 needs 2 used
P2 9 2
By 3 which one we can fill flint or satisfy first
P1 need 4, current 2, if we allocate 2 , 4 filled.
Unsafe to safe
<po,p1,p2> it is in a safe state, no deadlock

2) Resource- allocation graph algorithm.

R1

p2
p1

R2

To illustrate this algorithm we consider the resource-allocation graph of figure .


Suppose that p2 request R2. although R2 is currently free, we can allocate to P2
Since this action will create a cycle in the
graph.
-> a cycle indicates that the system is in an
unsafe state.
-> if P1 request R2 and P2 request R1 then a
deadlock will occur
An unsafe state in a resource-allocation graph
R1

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

3) Work =work + allocation.


Finish [i]=T
Goto step2.
4) If finish [i]=T
For all I, then the system is in a safe state.
Resource –Request algorithm
Let Request I be the request vector for process Pi. if
the requesti[j]=K then the processs Pi wants K
instances of resources type Rj. When a request
for resources is made by the process P1, the
following actions are taken.
1) If request I <= need I, goto step2. otherwise raise
an error condition.
2) If request 1<=available , goto step3. other wise
Pi must wait, since the resources are not
available.
3) Having allocated the request resources to
process Pi by modifying the state as follows:
Available:- available-Request I;
Allocation:- allocation i+ Request I;
Need:- need i –Request I;
If the resulting resource-allocation state if safe,
the transaction is completed and process pi is
allocated it’s resources.
Ex: consider a system with five processes P0
through P4 and the resources type A,B,C.
resources type A has 10 instances and the
resource B has five instances and the
resources type C has the 7 instances
Allocation Max Available
ABC ABC ABC
P0 010 753 3 32
P1 200 322
P2 302 902
P3 211 222 find and need
P4 002 433

.
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:

1) In a single instance of each resource type .


For more than one instance we will have
one more instance we have here a
deadlock.
P3

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.

Recovery from Deadlock:


There are two options for breaking a deadlock .
1) Solutions is simply to abort one or more
processes to break the circular wait.
2) Is to pre empt some resources from one or more
of the deadlocked processes.
1) Process Termination:- To eliminate
deadlocks by aborting a process, we use
one of the two methods. In both methods

-> Abort all deadlocked process: this method


clearly break the deadlock cycle , but a
great expense and time.(1 by 1)
-> Abort one process at a time until the
deadlock cycle is eliminates: after each
process is aborted, a deadlock- detection
alg must be invoked to determine whether
any processes are still in deadlocked.
2) Resource Pre-emption: to eliminate
deadlock using resource preemptions, we
successively preempt some resources frome
processes and give these resources to other
processes until the deadlock cycle is broken.
Using preemption 3 issues needs:
1) Selecting a victim: which resources and
which processes one to be preempted as in process
termination, we must determine the order of
preemption to minimize the cost. cost factor may
include such parameters as the no of resources a
deadlock process is holding and the amount of time
a deadlock process has thus for consumed during
his executions.
2) Roll back: if we preempt a resource from a
process, what should be done with that process it
cannot continue with it’s normal executions, it is
missing some needed resources. So, we must roll
back that process to some safe state and restart it
from that state.
3) starvation: how do we ensure that starvation
will not occur? Is how can we guarantee that
resources will not always be preempted from the
same process.
In a system where victim selection is based primarily
on cost factors, it may happen that the same process
is always peddled as a victim. As a result this
process never completes it’s designated task

You might also like