7-DeadLocks 2
7-DeadLocks 2
• Process
• Pi requests instance of Rj Pi
Rj
• Pi is holding an instance of Rj Pi
Rj
Example of a Resource Allocation Graph
Resource Allocation Graph With A Deadlock
Graph With A Cycle But No Deadlock
Basic Facts
Requesti = 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;
l If safe the resources are allocated to Pi
l If unsafe Pi must wait, and the old resource-allocation state is
restored
Example of Banker’s Algorithm
The system is in a safe state since the sequence < P1, P3, P4, P2,
P0> satisfies safety criteria
Example: P1 Request (1,0,2)
• Check that Request Available (that is, (1,0,2) (3,3,2) true
Old New
• Executing safety algorithm shows that sequence < P1, P3, P4, P0,
P2> satisfies safety requirement
• Can request for (3,3,0) by P4 be granted?
• Can request for (0,2,0) by P0 be granted?
Practice Questions
Problem-1
Problem-2
A single processor system has three resource types X, Y and Z, which
are shared by three processes. There are 5 units of each resource type.
Consider the following scenario, where the column alloc denotes the
number of units of each resource type allocated to each process, and
the column request denotes the number of units of each resource type
requested by a process in order to complete execution. Which of these
processes will finish LAST?
P0
P1
P2
None of the above since the system is in a deadlock
• An operating system uses the banker’s algorithm for deadlock avoidance
when managing the allocation of three resource types X, Y and Z to three
processes P0, P1 and P2. The table given below presents the current system
state. Here, the Allocation matrix shows the current number of resources of
each type allocated to each process and the Max matrix shows the maximum
number of resources of each type required by each process during its
execution.
• There are 3 units of type X, 2 units of type Y and 2 units of type Z still
available. The system is currently in safe state. Consider the following
independent requests for additional resources in the current state-
• REQ1: P0 requests 0 units of X, 0 units of Y and 2 units of Z
• REQ2: P1 requests 2 units of X, 0 units of Y and 0 units of Z
• Detection algorithm
• Recovery scheme
Single Instance of Each Resource Type
• Sequence <P0, P2, P3, P1, P4> will result in Finish[i] = true for
all i
• P2 requests an additional instance of type C
Request
ABC
P0 000
P1 202
P2 001
P3 1 0 0
P4 002
• State of system?
– Can reclaim resources held by process P0, but insufficient resources to
fulfill other processes; requests
– Deadlock exists, consisting of processes P1, P2, P3, and P4
Detection-Algorithm Usage
• 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