Unit 3 Part 2
Unit 3 Part 2
Notes on Deadlock
1.4 Deadlock: A set of blocked processes each holding a resource and waiting to acquire a
resource held by another process in the set.
OR
A set of processes is deadlocked if each process in the set is waiting for an event that only
another process in the set can cause
Example
System has 2 process P1 & P2 and 2 tape ddrives A &B .P1 and P2 each hold one tape
drive and each needs another one.P1 requests tape drive 1 & gets it . P2 requests tape
drive 2 & gets it.P1
P1 requests tape drive 2 but is blocked
blocked.P2
P2 requests tape drive 1 but is
blocked.
1.4.5 Deadlock Prevention: Deadlock Prevention provides a set of methods for ensuring that at least
one of the necessary conditions cannot hold.
i. Mutual Exclusion – not required for sharable resources; must hold for non-sharable
resources.
ii. Hold and Wait – must guarantee that whenever a process requests a resource, it does not
hold any other resources.
Require process to request and be allocated all its resources before it begins
execution, or allow process to request resources only when the process has none.
Low resource utilization;
iii. No Preemption –
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.
Process will be restarted only when it can regain its old resources, as well as the
new ones that it is requesting.
iv. Circular Wait – imposes a total ordering of all resource types, and requires that each
process requests resources in an increasing order of enumeration.
1.4.6 Deadlock Avoidance: Requires that the system has some additional a priori 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.
A. Safe State:
A state is safe if the system can allocate resources to each process in some order and still
avoid a deadlock.
Formally , a System is in safe state if there exists a safe sequence <P1, P2, …, Pn> of all
the processes is the system such that 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.
That is:
i. If Pi resource needs are not immediately available, then Pi can wait until all Pj
have finished.
ii. When Pj is finished, Pi can obtain needed resources, execute, return allocated
resources, and terminate.
iii. When Pi terminates, Pi +1 can obtain its needed resources, and so on.
If a system is in safe state no deadlocks.
If a system is in unsafe state possibility of deadlock.
Avoidance ensure that a system will never enter an unsafe state.
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 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.
Safety Algorithm: We can now present the algorithm for finding out whether or not a system
is in a safe state.
1. Let Work and Finish be vectors of length m and n, respectively.
Let Request = request vector for process Pi. If Requesti [j] = k then process Pi wants k instances
of resource type Rj.
1. If Requesti Needi go to step 2. Otherwise, raise error condition, since process has
exceeded its maximum claim.
2. If Requesti 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;;
4. If safe the resources are allocated to Pi.
• If unsafe Pi must wait, and the old resource-allocation state is restored
Example :
Consider a system with five processes P0 through P4 and three resource types A, B, and C.
Resource type A has 10 instances, resource type B has 5 instances, and resource type C has 7
instances. Suppose that, at time To, the following snapshot of the system has been taken:
Safety algorithm :
Sequence<P1>
Avaliable =available +allocation(P1)
=3 3 2 + 2 0 0
=5 3 2
For process P2
Need(P2)<=available
6 0 0 <= 5 3 2 (Not Possible)
For process P3
Need(P3)<=available
0 1 1 <= 5 3 2(Possible)
1
Sequence <P1,P3>
Avaliable =available +allocation(P3)
=5 3 2 +2 1 1
=7 4 3
For process P4
Need(P4)<=available
4 3 1<= 7 4 3(Possible)
Sequence<P1,P3,P4>
Sequence<P1,P3,P4,P0>
Sequence<P1,P3,P4,P0,P2>
i. Available: A vector of length m indicates the number of available resources of each type.
ii. Allocation: An n x m matrix defines the number of resources of each type currently
allocated to each process.
iii. Request: An n x m matrix indicates the current request of each process. If Request [ij] =
k, then process Pi is requesting k more instances of resource type. Rj.
Detection Algorithm :
For step by step solution of this algorithm given above (refer class notes)
B. Resource Preemption: