Notes Deadlock PDF
Notes Deadlock PDF
Ans. System State: The state of the system at any point of time is defined as
the allocation of resources to processes at that particular instance.
Ans. Safe State: the system state in that does not lead to deadlock is termed
as safe state/
Ans. Unsafe state: The system state that leads to deadlock is called unsafe
state.
The resources in any computing system are broadly classified into two
categories as, reusable and consumable resources. The consumable
resources are created and destroyed. Also there is no limit on consumable
resources of a particular type.
Ans. live lock : Live lock can be defined as a condition in which one or more
processes continuously change their state in response to changes in the other
process(es) without doing any useful work. This is similar to deadlock in that
no progress is made but differs in that neither process is blocked nor waiting
for anything.
The system can come out of livelock if their relative pace of execution
is changed while in execution.
Figure: Resource allocation graph for two process and two resources.
Matrix model:
This model has the advantage of generality.
It can model allocation state in a system that permits a process to
request and acquire any number of units of resource class and
instances.
R1 R2 … … Rm
A11 A12 … … A1m
P1
A21 A22 … … A2m
P2
… … … … … …
… … … … … …
An1 An2 … … Anm
Pn
Allocation Matrix of n resources for m types of resource classes
Process Resources
Why the system resources and their allotment to processes are mostly
represented with matrix rather than with resource allocation graphs?
Ans. The system has multiple resources. Also there is no control over number
of processes those can exist in system. These processes request multiple
copies of each resource which becomes difficult to model with resource
allocation graph. So, the matrix representation wins over the same.
R1 R2 … … Rm
C11 C12 … … C1m
P1
C21 C22 … … C2m
P2
… … … … … …
… … … … … …
Cn1 Cn2 … … Cnm
Pn
Figure: Resource Claimed Matrix for n Processes and m Types Of Resources
R1 R2 … … Rm
A11 A12 … … A1m
P1
A21 A22 … … A2m
P2
… … … … … …
… … … … … …
An1 An2 … … Anm
Pn
Figure: Resource Allocation Matrix for n Processes and m Types Of
Resources
3. Conditions to Deadlock, Approaches to Deadlock
A deadlock has four potential reasons to occur. Out of them, the first three do
not guarantee a deadlock, but when any one of them is combined with the
fourth one, the deadlock occurs for sure. These four conditions can be listed
as: Mutual Exclusion, Hold and wait, No preemption and Circular wait.
Mutual exclusion:
Hold and wait
No preemption
Circular wait
Hold and Wait prevention policy: The hold and wait condition can be
prevented by requiring that a process should request all its required
resources at once and blocking the process until all resources can be granted
simultaneously. This approach takes much time for a process to get started
and thus results in more response time, waiting time and obviously turnaround
time. Also, this approach keeps most of the resources with the process for its
lifetime and they may get underutilized.
Circular wait prevention policy: In this case, all the resources in system
are listed and are given some liner order numbers. If a process is allocated
resources of type R, then it may subsequently request only those resources of
types following R in the ordering.
i.e. Then resource Ri precedes Rj in the ordering if i < j. Now suppose that
two processes, A and B, are deadlocked because A has acquired Ri and
requested Rj, and B has acquired Rj and requested Ri. This condition would
be impossible here because it implies i < j and j < i.
This approach, also called as Banker’s algorithm, always try to keep the
system is safe state. The state concept is defined with reference to a system
of fixed number of resources and a fixed number of processes as follows,
State: the State of the system depicts the current allocation of resources to
the processes.
Safe state: Safe state is characterized as there exist at least one sequence of
resource allocation that does not result in deadlock.
Unsafe state: the state which is not safe, i.e. which does not assure even one
sequence that doesn’t end up in deadlock.
Here, there is no restriction on process initiation. Instead, processes
are allowed to get created. All the processes give their overall resource
requirements and their requirements in initial state. At every resource
allocation request, the OS evaluates a hypothetical situation ‘what if the
required resources are granted, the process in question completes and
returns back the possessions, will there be at least one sequence in which all
the processes in set can get executed?” i.e. if the resource granting will result
in a safe state? If yes, the allocation is completed otherwise it gets denied.
Vector R = all resources indicating various resource types and their instances.
R= (R1,R2, . . . , Rm)
Vector V = the types and instances of available resources at any instance of
time.
V= (V1,V2, . . . ,Vm)
Matrix C = resources claimed by processes.(Cij = requirement of process i for
resource j, with one row dedicated to each process)
R1 R2 … … Rm
C11 C12 … … C1m
P1
C21 C22 … … C2m
P2
… … … … … …
… … … … … …
Cn1 Cn2 … … Cnm
Pn
Claimed Matrix C
Matrix A= a matrix of resources allocated to processes. (Aij= current allocation
to process i of resource j)
R1 R2 … … Rm
A11 A12 … … A1m
P1
A21 A22 … … A2m
P2
… … … … … …
… … … … … …
An1 An2 … … Anm
Pn
Allocation Matrix A
All these above data structures are bound in following relationships,
1. All resources are either allocated or available.
Rj = Vj + ∑i=1 to n Aij for all j
2. No process can claim more than the total amount of resources in the
system.
Cij<= Rj for all i, j
3. No process is allocated more resources of any type than the process
originally claimed to need.
Aij<=Cij for all I,j
Assume process P1,P2,P3,P4 join the system with some claims for resources
in the same order as they are written.
4 3 3
C= 7 2 4
2 2 5
0 0 1
So V= 7 5 9
Every process received is checked against the available vector one by one in
their order of arrival.
.
Process Request Available Is Request < Process
resources Available? initiated?
Vk=Vk-Cik
P1 {4,3,3} {7,5,9} Yes Yes
P2 {7,2,4} {3,2,6} No No
P3 {2,2,5} {3,2,6} Yes Yes
P4 {0,0,1} {1,0,1} Yes Yes
Thus in the above example only P1,P3, and P4 are allowed to initiate while P2
is denied its initiation.
Given all the above values the stafe status can be calculated as,
1. For any process i=1 to n, compare its associated row of matrix M with
vector V.
2. If that Mij is less than or equal to Aj for all j, this process i can be
assumed to be completed and thereby its resources can be given back
to system.
Thus, after hypothetical successful completion of process i the
available vector A is updated as,
V= V+ Aij where Aij indicates the resources j allocated earlier to
process i.
Also update Aij=0 for all js as the process holds no more resources.
3. Repeat step 1 and 2 until all processes are considered.
If all the processes in the set have the allocation matrix entries as 0s, the
system is in safe sate, and sequence in which the process were selected can
be termed as one of the possible execution sequence without any deadlock.
This approach seems simple and good, but it has certain preconditions X as,
1. All the resource requirements must be stated in advance.
2. The order in which processes get executed should not be constrained
by synchronization or any that kind of mechanism.
3. The system must have fixed number of resources to allocate
4. The processes participating here are not allowed to exit holding the
resources
Banker’s Algorithm makes sure that only processes that will run to completion
are scheduled to run. However, if there are deadlocked processes, the will
remain deadlocked. Banker’s Algorithm does not eliminate a deadlock.
Note: In the following matrices rows indicate process and columns represent
the resources.
4 3 3
7 2 4
Claimed matrix C =
4 2 5
5 3 3
2 0 0
Allocation Matrix A = 7 2 3
3 22 22 2
1 01 03 1
More requirement M 1 0 3 =
4 2 0
Solution: From the given processes P1,P2,P3 and p4, only p2 is the process
whose corresponding row in matrix M satisfies the criteria of being less than
values in vector V.
So, first P2 may run to completion. Then the above tables will have values
as,
2 0 0
4 3 3
0 0 0
0 0 0
3 2 2
4 2 5
5 3 3 1 1 3
Claimed Matrix C Allocation Matrix
A
2 2 2
0 0 0 7 2 4
1 0 3
4 2 0
Available vector V
More Requirements M
Now, seeing the entries in M and available vector V, any process can
be awarded with the resources and they can run to completion.
Available vector V= 9 2 40 0 0
0 0 0
1 0 3
More Requirements M= 4 2 0
0 0 0
0 0 0
0 0 0 Available vector V 12 4 6
4 2 0
More Requirements M
0 0 0
0 0 0
Available vector V 13 5 9
0 0 0
0 0 0
More Requirements M
Q. Give one example of potential unsafe state that can be avoided with
deadlock avoidance (Banker’s algorithm).
Ans.
Note: In the following matrices rows indicate process and columns represent
the resources.
4 3 3
Claimed matrix C = 7 2 5
4 2 5
5 3 3
2 0 0
Allocation Matrix A = 7 2 3
3 2 2
1 1 3
2 2 2
0 0 2
More requirement M =
1 0 3
4 2 0
The first policy reduces OS overhead but may lead the system into a serious
deadlock state while the later one results in early detection.
One the deadlock is detected, every kind of resource that has caused the
deadlock, has a different deadlock recovery strategy.
For the options offered in 3 &4, the criteria to choose a process is one or more
of the following ones.
least amount of CPU time consumed so far
least total resources allocated so far
least amount of “work” produced so far...
The following table gives example of three types of the resources device, file
and memory being requested, acquired by a process and released by the
same.
Request a Release a
resource Acquire/use a resource resource
Read from/write to a
Request a device device Release a device
Open a file Read/write a file Close a file
allocate memory Use the memory Free memory
Actually, every deadlock handling strategy has some merits and demerits
associated with them. So instead of employing any one the strategies, it’s
better to follow different strategy in different situations and with different
kinds of resources.
The general integrated deadlock strategy is:
1. Group the resources into number of different resource categories.
2. Use the linear ordering strategy defined for deadlock prevention of
circular wait to prevent deadlocks between resource classes.
3. Within a resource class, use the algorithm that is most appropriate
for that class.
This solution to the problem is the one originally proposed by Dijkstra. And it
is not always practical, especially when the list of required resources is not
completely known in advance.
Solution 2: monitor
Philosophers can eat if neither of their neighbors is eating. This
is comparable to a system where philosophers that cannot get the
second fork must put down the first fork before they try again. (no hold
and wait condition)
This solution uses a single mutual exclusion lock. This lock is not
associated with the forks but with the decision procedures that can
change the states of the philosophers. This is ensured by the monitor.
The procedures test, pickup and putdown are local to the monitor
and share a mutual exclusion lock. The philosophers wanting to eat
do not hold a fork. When the monitor allows a philosopher who wants
to eat to continue, the philosopher will reacquire the first fork before
picking up the now available second fork. When done eating, the
philosopher will signal to the monitor that both forks are now available.
R1 R2
P1 1 2
P2 0 0
P3 0 0
Ans: P4 1 0 R1 R2
R1 R2 P1 0 0
Given:
2 3 P2 1 2
R P3 0 1
P4 0 0
C A
There are many sequences in which the processes could be realize, but
process P2 must complete before process P1 as it hold the resources
requested by former.
Q. Given 5 total units of the resource of the same type, tell whether the
following system is in a safe or unsafe state.
R=5, P1 P2 P3 P4 P1 P2 P3 P4
C= 2 3 4 5 A= 1 1 2 0
Ans. safe state
Ans: unsafe
Q.
Consider the snapshot of a system.
P0 {0,1,0} {7,5,3} {3,3,2}
P1 {2,0,0} {3,2,2}
P2 {3,0,2} {9,0,2}
P3 {2,1,1} {2,2,2}
P4 {0,0,2} {4,3,3}
A B C A B C A B C
R= 3 2 1 P1 2 1 1 P1 1 1 0
Resource vector P2 2 1 1 P2 2 0 1
P3 1 2 0 P3 0 1 0
Claim
Matrix Allocation Matrix
Run the deadlock detection algorithm on the above example and check is the
system in a deadlock? If yes, then what are the process(es) that need to be
pre-empted and in what order, to ensure that deadlock is overcome?