OS Unit3
OS Unit3
Process Deadlocks
Introduction
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.
Because all the processes are waiting, none of them will ever cause any event that could wake up
any of the other members of the set, and all the processes continue to wait forever.
Deadlocks are a set of blocked processes each holding a resource and waiting to acquire resource
held by other process.
(a) (b)
Figute: (a) process is requesting resource and a recource is in holding process (b) a deadlock scenario
Example:
1
Collected by Bipin Timalsina
Unit-3/OS
Resources
In short, a resource is anything that must be acquired, used, and released over the course
of time.
The resources include devices, data records, files, and so forth.
A resource can be a hardware device (e.g., a Blu-ray drive) or a piece of information (e.g.,
a record in a database).
A computer will normally have many different resources that a process can acquire.
A major class of deadlocks involves resources to which some process has been granted
exclusive access
The abstract sequence of events required to use a resource is given below.
1. Request the resource.
2. Use the resource.
3. Release the resource.
Example: Memory
Consider, for example, a system with 1 GB of user memory, one printer, and two
1-GB processes that each want to print something.
Process A requests and gets the printer, then starts to compute the values to print.
Before it has finished the computation, it exceeds its time quantum and is swapped
out to disk.
Process B now runs and tries, unsuccessfully as it turns out, to acquire the printer.
Potenially, we now hav e a deadlock situation, because A has the printer and B has
the memory, and neither one can proceed without the resource held by the other.
Fortunately, it is possible to preempt (take away) the memory from B by swapping
it out and swapping A in. Now A can run, do its printing, and then release the
printer. No deadlock occurs.
A nonpreemptable resource, is that resource which cannot be taken away from its current
owner process without potentially causing failure.
2
Collected by Bipin Timalsina
Unit-3/OS
Deadlock Characterization
Deadlock characterization describes the distinctive features that are the cause of deadlock
occurrence
The following four conditions must hold for there to be a (resource) deadlock
1. Mutual exclusion condition.
Each resource is either currently assigned to exactly one process or is available.
2. Hold-and-wait condition.
Processes currently holding resources that were granted earlier can request new
resources.
3. No-preemption condition.
Resources previously granted cannot be forcibly taken away from a process.
They must be explicitly released by the process holding them.
4. Circular wait condition.
There must be a circular list of two or more processes, each of which is waiting
for a resource held by the next member of the chain.
3
Collected by Bipin Timalsina
Unit-3/OS
Figure: Resource allocation graphs. (a) Holding a resource. (b) Requesting a resource (c) Deadlock.
4
Collected by Bipin Timalsina
Unit-3/OS
Ostrich Algorithm
When storm approaches, an ostrich puts it's head in the
sand (ground) and pretends (imagines) that there is no
problem at all.
Deadlock prevention
o If we can ensure that at least one of four conditions of deadlock is never satisfied, then
deadlocks will be structurally impossible
o To prevent the system from deadlocks, one of the four discussed conditions that may create a
deadlock should be invalidated.
o Deadlock can be prevented by attacking the one of the four conditions that leads to deadlock.
Attacking the Mutual Exclusion Condition
No deadlock if each resource can be assigned to more than one process.
5
Collected by Bipin Timalsina
Unit-3/OS
We cannot assign some resources to more than one process at a time such as CD
Recorder, Printer etc...
So this solution is not feasible.
Attacking the Hold and Wait Condition
If we can prevent processes that hold resources from waiting for more resources, we
can eliminate deadlocks
One way to achieve this goal is to require processes to request all their resources
before starting execution.
A process is allowed to run if all resources it needed is available. Otherwise nothing
will be allocated and it will just wait.
Problem with this strategy is that a process may not know required resources at start of
run.
Resource will not be used optimally.
Attacking the No Preemption Condition
When a process P0 request some resource R which is held by another process P1 then
resource R is forcibly taken away from the process P1 and allocated to P0
This strategy is tricky at best and impossible at worst.
Consider a process holds the printer, halfway through its job; taking the printer away
from this process without having any ill effect is not possible.
This is not a possible option.
Attacking the Circular Wait Condition
The circular wait can be eliminated in several ways
One way is simply to have a rule saying that a process is entitled only to a single
resource at any moment. If it needs a second one, it must release the first one.
For a process that needs to copy a huge file from a tape to a printer, this
restriction is unacceptable.
Another way to avoid the circular wait is to provide a global numbering of all the
resources, as shown in below
1) Printer 2) Scanner
3) Plotter 4) Tape Drive
5) CD Rom
6
Collected by Bipin Timalsina
Unit-3/OS
Now the rule is that: processes can request resources whenever they want to, but all
requests must be made in numerical order.
A process need not acquire them all at once.
Resource graph can never have cycle.
Assume A has i and B has j are where i and j are distinct resources and they will have
different numbers.
If i > j, then A is not allowed to request j because that is lower than what it already
has.
If i < j, then B is not allowed to request i because that is lower than what it already
has
A process may request 1st a printer, then tape drive. But it may not request 1st a plotter,
then the printer.
Deadlock Avoidance
Given some additional information on how each process will request resources, it is possible to
construct an algorithm that will avoid deadlock states. The algorithm will dynamically examine
the resource allocation operations to ensure that there won't be a circular wait on resources. One
of the deadlock avoidance algorithms is Banker's algorithm.
7
Collected by Bipin Timalsina
Unit-3/OS
8
Collected by Bipin Timalsina
Unit-3/OS
The state of figure (a) is safe because there exists a sequence of allocations that allows all
processes to complete.
The scheduler can simply run B exclusively, until it asks for and gets two more
instances of the resource, leading to the state of Fig. (b).
When B completes, we get the state of Fig.(c).
Then the scheduler can run C, leading eventually to Fig. (d).
When C completes, we get Fig (e).
Now A can get the six instances of the resource it needs and also complete.
Thus, the state of fig. (a) is safe because the system, by careful scheduling, can avoid
deadlock.
9
Collected by Bipin Timalsina
Unit-3/OS
Now suppose we have the initial state shown in Fig.(a), but this time A requests and gets
another resource, giving Fig.(b).
Can we find a sequence that is guaranteed to work?
Let us try.
The scheduler could run B until it asked for all its resources, as shown in Fig.(c).
Eventually, B completes and we get the state of Fig (d).
At this point we are stuck. We only have four instances of the resource free, and
each of the active processes needs five.
There is no sequence that guarantees completion.
Thus, the allocation decision that moved the system from above Fig. (a) to (b) went from
a safe to an unsafe state.
Basic Facts:
10
Collected by Bipin Timalsina
Unit-3/OS
Available units = 10
In following figure (a) we see four customers (processes), A, B, C, and D, each of whom
has been granted a certain number of credit units (e.g., 1 unit is 1K dollars).
The banker knows that not all customers will need their maximum credit immediately, so
he has reserved only 10 units (resources say tape drives) rather than 22 to service them.
(In this analogy, customers are processes, units are, say, tape drives, and the banker is the
operating system.)
The customers go about their respective businesses, making loan requests from time to
time (i.e., asking for resources). At a certain moment, the situation is as shown in following
figure (b).
11
Collected by Bipin Timalsina
Unit-3/OS
This state is safe because with two units left, the banker can delay any requests except C’s,
thus letting C finish and release all four of his resources. With four units in hand, the banker
can let either D or B have the necessary units, and so on.
Consider what would happen if a request from B for one more unit were granted in above
Fig(b). We would have situation as in following Fig(c), which is unsafe.
If all the customers suddenly asked for their maximum loans, the banker could not satisfy
any of them, and we would have a deadlock.
NOTE: An unsafe state does not imply the existence or even eventual existence of a
deadlock. What an unsafe state does imply is that some unfortunate sequence of events
might lead a deadlock.
12
Collected by Bipin Timalsina
Unit-3/OS
Problem 01: State whether the given processes are in deadlock or not. Given that resource
instance is 10.
A 3 9
B 2 4
C 2 7
Solution:
We get,
A 3 9 6
B 2 4 2
C 2 7 5
Here currently,
With current free resources process B can be executed, since need of B ≤ Free i.e 2 ≤ 3 . So ,
B executes. After execution of B it release the resources allocated by it. Total free resource
becomes,
free = current free + Allocation by B
= (1+2+2) = 5
13
Collected by Bipin Timalsina
Unit-3/OS
Now, with current free resources process C can be executed, since need of C ≤ Free i.e 5 ≤ 5 .
So, C executes. After execution of C it release the resources allocated by it. Total free resource
becomes,
= (0+5+2) = 7
With current free resources process A can be executed, since need of A ≤ Free i.e 6 ≤ 7. So ,
A executes. After execution of A it releases the resources allocated by it. Total free resource
becomes
free = current free + Allocation by A
= (1+6+3) = 10
Here all the process runs successfully hence they are is safe state and occurs no deadlock. Safe
sequence is: B→C→A
Algorithm
1. Look for a row, R, whose unmet resource needs are all smaller than or equal to A (Available
resources). If no such row exists, the system will eventually deadlock since no process can
run to completion (assuming processes keep all resources until they exit).
2. Assume the process of the chosen row requests all the resources it needs (which is
guaranteed to be possible) and finishes. Mark that process as terminated and add all of its
resources to the A vector
3. Repeat steps 1 and 2 until either all processes are marked terminated (in which case the
initial state was safe) or no process is left whose resource needs can be met (in which case
the system was not safe).
NOTE: If several processes are eligible to be chosen in step 1, it does not matter which one
is selected: the pool of available resources either gets larger, or at worst, stays the same.
14
Collected by Bipin Timalsina
Unit-3/OS
Example:
In following figure left matrix shows how many of each resource are currently assigned to
each of the five processes (A, B, C, D and E).
The matrix on the right shows how many resources each process still needs in order to
complete.
As in the single-resource case, processes must state their total resource needs before
executing, so that the system can compute the right-hand matrix at each instant.
The three vectors (E, P and A) at the right of the figure show the existing, possessed and
available resources.
E represents the number of existing resources
P represents the number of possessed resources
A, represents number of the available resources
E = (6 3 4 2 ) means the system has 6 tape drives, 3 plotters, 4 printers and 2 blue
–rays (existing)
P = (5 3 2 2) means 5 tape drives, 3 plotters, 2 printers and 2 blue-rays are currently
used by processes (possessed).
A = (1 0 2 0 ) means 1 tape drive and 2 printers are free now (available)
The available resource vector is just the difference between what the system has and what
is currently in use
15
Collected by Bipin Timalsina
Unit-3/OS
Limitations:
Problem 01: A system has four process P1, P2, P3 and P4 and three resources R1, R2
and R3 with existing resources E = (15, 9, 5). After allocating resources to all the
processes available resources becomes A = (3, 2, 0). State whether the process is safe
or not using banker's algorithm. If safe, write the safe sequence.
NOTE: If Need is not given, it can be calculated using Need = Maximum – Allocation
16
Collected by Bipin Timalsina
Unit-3/OS
Step 1: With current available resources A = (3, 2, 0) P4 can be executed. Since need of P4 ≤ A
i.e. (2, 1, 0) ≤ (3, 2, 0) so P4 executes after complete execution of P4 it releases the resources which
is allocated by it. Now total current available resources A becomes,
Step 2: With current available resources A = (5, 3, 3) P1 can be executed. Since need of P1 ≤ A
i.e. (0, 2, 1) ≤ (5, 3, 3) so P1 executes. After complete execution of P1 it releases the resources
which is allocated by it. Now total current available resources A becomes,
Step 3: With current available resources A = (8, 3, 4) P3 can be executed. Since need of P3 ≤ A
i.e. (1,0,4) ≤ (8,3,4) so P3 executes After complete execution of P3 it releases the resources which
is allocated by it. Now total current available resources A becomes,
Step 4: With current available resources A = ( 10, 5, 4) P2 can be executed. Since need of P2 ≤ A
i.e. (1,4,1) ≤ (10,5,4) so P2 executes After complete execution of P2 it releases the resources which
is allocated by it. Now total current available resources A becomes,
Here all the process runs hence they are in safe state and occurs no deadlock. Safe sequence is:
P4→P1→P3→P2
17
Collected by Bipin Timalsina
Unit-3/OS
Deadlock Detection
System allows deadlock to occur and tries to detect when it happens, and then takes some
action recover after the fact.
When this technique is used, the system does not attempt to prevent deadlocks from
occurring. Instead, it lets them occur, tries to detect when this happens, and then takes some
action to
The question is: ‘‘Is this system deadlocked, and if so, which processes are involved?’’
18
Collected by Bipin Timalsina
Unit-3/OS
Solution:
Many algorithms for detecting cycles in directed graphs are known. One of them is given below:
1. For each node, N, in the graph, perform the following five steps with N as the starting
node.
2. Initialize L (a dynamic data structure) to the empty list, and designate all the arcs as
unmarked.
3. Add the current node to the end of L and check to see if the node now appears in L two
times. If it does, the graph contains a cycle (listed in L) and the algorithm terminates.
4. From the given node, see if there are any unmarked outgoing arcs. If so, go to step 5; if
not, go to step 6.
5. Pick an unmarked outgoing arc at random and mark it. Then follow it to the new current
node and go to step 3.
6. If this node is the initial node, the graph does not contain any cycles and the algorithm
terminates. Otherwise, we have now reached a dead end. Remove it and go back to the
previous node, that is, the one that was current just before this one, make that one the
current node, and go to step 3.
19
Collected by Bipin Timalsina
Unit-3/OS
20
Collected by Bipin Timalsina
Unit-3/OS
Figure: The four data structures needed by the deadlock detection algorithm.
An important invariant holds for these four data structures. In particular, every resource is
either allocated or is available. This observation means that
In other words, if we add up all the instances of the resource j that have been allocated
and to this add all the instances that are available, the result is the number of instances of
that resource class that exist.
Main Idea:
(This algorithm assumes a worst-case scenario: all processes keep all acquired resources until
they exit.)
21
Collected by Bipin Timalsina
Unit-3/OS
1. Look for an unmarked process, Pi , for which the ith row of R is less than or equal to A.
2. If such a process is found, add the ith row of C to A, mark the process, and go back to
step 1.
4. If no such process exists, the algorithm terminates.
Example:
Let us consider three processes and four resource classes, which we have arbitrarily labeled tape
drives, plotters, scanners, and Blu-ray drives.
Process 1 has one scanner. Process 2 has two tape drives and a Blu-ray drive. Process 3
has a plotter and two scanners.
Each process needs additional resources, as shown by the R matrix.
To run the deadlock detection algorithm, we look for a process whose resource request can be
satisfied.
The first one cannot be satisfied because there is no Blu-ray drive available.
The second cannot be satisfied either, because there is no scanner free.
Fortunately, the third one can be satisfied, so process 3 runs and eventually returns
all its resources, giving
A = previous free + Allocation by P3
A = (2 1 0 0 ) + (0 1 2 0)
A = (2 2 2 0)
22
Collected by Bipin Timalsina
Unit-3/OS
At this point process 2 can run and return its resources, giving
A = previous free + Allocation by P2
A = (2 2 2 0) + (2 0 0 1)
A (4 2 2 1)
Now the remaining process can run. There is no deadlock in the system.
Suppose that process 3 needs a Blu-ray drive as well as the two tape drives and the plotter.
None of the requests can be satisfied, so the entire system will eventually be deadlocked.
Even if we give process 3 its two tape drives and one plotter, the system deadlocks when it
requests the Blu-ray drive.
Another example:
Five processes P0 through P4; three resource types A (7 instances), B (2 instances), and C (6
instances)
Sequence <P0, P2, P3, P1, P4> can run without deadlock
23
Collected by Bipin Timalsina
Unit-3/OS
24
Collected by Bipin Timalsina
Unit-3/OS
NOTE: Checkpointing a process means that its state is written to a file so that it can be restarted later. The
checkpoint contains not only the memory image, but also the resource state, in other words, which
resources are currently assigned to the process
Figure: State of process A is saved at file F1 at some point of time (first checkpoint) then A has
acquired resource R. At another time, state of A is saved on File F2 (second checkpoint). After
that Process B has requested the resource R which caused deadlock. Now the Process A is rolled
back to the first checked point.
25
Collected by Bipin Timalsina
Unit-3/OS
26
Collected by Bipin Timalsina
Unit-3/OS
Deadlock vs Starvation
27
Collected by Bipin Timalsina