The objectives of Deadlocks in Operating Systems are:
- To develop a description of deadlocks, which prevent sets of concurrent processes from completing their tasks
- To present a number of different methods for preventing or avoiding deadlocks in a computer system
2. • System Model
• Deadlock Characterization
• Methods for Handling Deadlocks
• Deadlock Prevention
• Deadlock Avoidance
• Deadlock Detection
• Recovery from Deadlock
2
3. Dead Avoidance
• Possible side effects of preventing deadlocks with Deadlock prevention are -
• Low device utilization and reduced system throughput
• Deadlock avoidance requires additional information about how resources have to
be requested
• Example -
• In a system with one tape drive and one printer
• The system might need to know that process P will request first the tape drive
and then the printer before releasing both resources
• Process Q will request first the printer and then the tape drive
• With this knowledge of the complete sequence of requests and releases for each
process, the system can decide for each request whether or not the process should
wait in order to avoid a possible future deadlock
3
4. Dead Avoidance
• Each request requires that in making this decision, the system follows the followig
approach -
• the resources currently available,
• the resources currently allocated to each process
• the future requests and releases of each process
• Algorithms that use this approach differ in the amount and type of information
required
• The simplest and most useful model requires that each process declare the
maximum number of resources of each type that it may need
• A deadlock-avoidance algorithm dynamically examines the resource-allocation state
to ensure that a circular-wait condition can never exist
• Resource Allocation state is defined by the number of available and allocated
resources and the maximum demands of the processes
4
5. • A state is safe if the system can allocate resources to each process in some order and
still avoid a deadlock
• A system is in a safe state only if there exists a safe sequence
• Safe sequence: A sequence of processes <P1, P2, …, Pn> is a safe sequence for the
current allocation state 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
• In this situation,
• If the resources that Pi needs are not immediately available, then Pi can wait until
all Pj have finished
• When all Pj are finished, 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 no such sequence exists, then the system state is said to be unsafe
Safe State
5
6. • If a system is in unsafe state possibility of deadlock
• An unsafe state may lead to a deadlock
• In an unsafe state, the operating system cannot
prevent processes from requesting resources in such
a way that a deadlock occurs. The behavior of the
processes controls unsafe states
• Avoidance ensure that a system will never enter an
unsafe state
• If a system is in safe state no deadlocks
• As long as the state is safe, the operating system can
avoid unsafe (and deadlocked) states
Safe State
6
8. • Resource - Allocation - Graph Algorithm
• Banker’s Algorithm
• Idea behind deadlock avoidance algorithm -
• To ensure that the system will always remain in a safe state
• Whenever a process requests a resource that is currently available, the system
must decide whether the resource can be allocated immediately or whether the
process must wait
• The request is granted only if the allocation leaves the system in a safe state
• In this scheme, if a process requests a resource that is currently available, it may
still have to wait
• Thus, resource utilization may be lower than it would be
• Single instance of a resource type
• Use a resource-allocation graph
• Multiple instances of a resource type
• Use the banker’s algorithm
Two deadlock-avoidance algorithms
8
9. • Claim edge - Pi -->Rj indicates that process Pi may request resource Rj at some time
in the future and it is represented by a dashed line
• Claim edge converts to request edge when a process requests a resource
• Request edge converted to an assignment edge when the resource is allocated to
the process
• When a resource is released by a process, assignment edge reconverts to a claim
edge
• Resources must be claimed a priori in the system. That is, before process Pi starts
executing, all its claim edges must already appear in the resource-allocation graph
• Suppose that process Pi requests a resource Rj
• The request can be granted only if converting the request edge to an assignment
edge does not result in the formation of a cycle in the resource allocation graph
• Here, we need a cycle detection algorithm that requires an order of n*n operations,
where n is the number of processes in the system
Resource-Allocation-Graph Algorithm
9
10. • If no cycle exists, then the allocation of
the resource will leave the system in a
safe state. If a cycle is found, then the
allocation will put the system in an unsafe
state. In that case, process Pi will have to
wait for its requests to be satisfied
• Consider the resource-allocation graph,
suppose that P2 requests R2
• Although R2 is currently free, cannot
allocate it to P2, since this action will
create a cycle in the graph
• A cycle, as mentioned, indicates that the
system is in an unsafe state
• If P1 requests R2, and P2 requests R1,
then a deadlock will occur
Resource-Allocation-Graph Algorithm
10
11. • When each resource has multiple instance then banker algorithm is used
• When a new process enters the system, it must declare the maximum number of
instances of each resource type that it may need
• Number may not exceed the total number of resources in the system
• When a user requests a set of resources, the system must determine whether the
allocation of these resources will leave the system in a safe state
• If it will, the resources are allocated; otherwise, the process must wait until some
other process releases enough resources
• When a process gets all its resources it must return them in a finite amount of time
• Data structures must be maintained to implement the banker’s algorithm, to encode
the state of the resource-allocation system
Banker’s Algorithm
11
12. • The following data structures are used in this algorithm
n is the number of processes in the system
m is the number of resource types
• Available - Vector of length m. If available [j] = k, there are k instances of
resource type Rj available
• Max - n x m matrix. If Max [i,j] = k, then process Pi may request at most k
instances of resource type Rj
• Allocation - n x m matrix. If Allocation[i,j] = k then Pi is currently allocated k
instances of Rj
• Need - n x m matrix. If Need[i,j] = k, then Pi may need k more instances of Rj to
complete its task
Need [i,j] = Max[i,j] – Allocation [i,j]
• These data structures vary over time in both size and value
Banker’s Algorithm
12
13. • The vector Allocationi specifies the resources currently allocated to process Pi
• The vector Needi specifies the additional resources that process Pi may still request
to complete its task
• In Banker’s algorithm, we have two types –
• Safety algorithm
• Resource Allocation algorithm
• Safety Algorithm
• The algorithm for finding out whether or not a system is in a safe state. This
algorithm can be described as follows -
• Let Work and Finish be vectors of length m and n, respectively. Initialize -
• Work = Available
• Finish [i] = false for i = 0, 1, …, n- 1
Banker’s Algorithm
13
14. • Safety Algorithm
1. Let Work and Finish be vectors of length m and n, respectively. Initialize:
• Work = Available
• Finish [i] = false for i = 0, 1, …, n- 1
2. Find an index i such that both
• Finish[i] == false
• Needi ≤ Work
If no such i exists, go to step 4
3. Work = Work + Allocationi
• Finish[i] = true
Go to step 2
4. If Finish[i] == true for all i, then the system is in a safe state
• This algorithm may require an order of m × n2 operations to determine whether a
state is safe
Banker’s Algorithm
14
15. • Resource Request Algorithm
• Algorithm for determining whether requests can be safely granted
• Let Requesti be the request vector for process Pi
• If Requesti [j] == k, then process Pi wants k instances of resource type Rj
• When a request for resources is made by process Pi , the following actions are
taken
1. If Requesti (less than or equal to) Needi go to step 2. Otherwise, raise error
condition, since process has exceeded its maximum claim
2. If Requesti (less than or equal to) Available, go to step 3. Otherwise Pi
must wait, since resources are not available
3. Pretend to allocate requested resources to Pi by modifying the state as
follows: Available = Available – Requesti;
Allocationi = Allocationi + Requesti;
Needi = Needi – Requesti;
Banker’s Algorithm
15
16. Pro: Consider a system with five processes
P0 through P4 and three resource types A,
B, and C. Resource type A has ten
instances, resource type B has five
instances, and resource type C has seven
instances
Sol:
Given: A B C – 3 resource types
A=10 instances; B=5 instances;
C = 7 instances
- Total number of resources = 10+5+7 = 22
resources
- Number of resources available of
Resource type A = 10-7 = 3
B= 5-2 =3
C= 7-5 =2
Banker’s Algorithm
16
Proc
ess
Allocati
on
Max Availab
le
Need
A B C A B C A B C A B C
P0 0 1 0 7 5 3 3 3 2 7 4 3
P1 2 0 0 3 2 2 1 2 2
P2 3 0 2 9 0 2 6 0 0
P3 2 1 1 2 2 2 0 1 1
P4 0 0 2 4 3 3 4 3 1
7 2 5
Need [i,j] = Max[i,j] – Allocation [i,j]
17. • Safety Algorithm
Banker’s Algorithm
17
• Work= available then work= 3 3 2
• Finish[i]=false i=0,1,2,3,4 ; false indicate not executed
1. P0
Work= available ; work=3 3 2
finish[i]=false;
need<= work ; 7 4 3<= 3 3 2 --> false
So, P0 has to wait
2. P1
need <= work 1 2 2 <= 3 3 2 true
work= work+ allocation
3 3 2 +2 0 0 = 5 3 2 available; P1 starts its execution
Work = 5 3 2
18. • Safety Algorithm
Banker’s Algorithm
18
3. P2
need <= work 6 0 0 <= 5 3 2 false
p2 has to wait
4. P3
need <= work 0 1 1<= 5 3 2 true
work= work+ allocation
5 3 2 +2 1 1 = 7 4 3 available
5. P4
need <= work 4 3 1<= 7 4 3 true
work= work+ allocation
7 4 3 + 0 0 2 = 7 4 5 available