Chapter 3
Chapter 3
Contents:
Introduction
Resource
Deadlock characterization
Deadlock modeling
Deadlock prevention
Deadlock avoidance
Communication deadlock
Live lock
starvation
1
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.
• all the processes are waiting, none of them will ever cause any of the
events that could wake up any of the other members of the set, and all the
processes continue to wait forever.
• Each member of the set of deadlocked processes is waiting for a resource
that is owned by a deadlocked process.
• None of the processes can run, none of them can release any resources,
and none of them can be awakened.
• This kind of deadlock is called a resource deadlock. It is probably the
most common kind, but it is not the only kind.
2
Analogy
Deadlock
System Breakdown
3
Resource deadlock
Deadlock:
A set of blocked processes, each:
11 Holding a resource and
Deadlock
Resource
Process B Resource
Process A
Deadlock conditions
6
Mutual Exclusion
only one process can use a resource at a time.
No Preemption
A resource is released only by the process holding it after it
completed its task
Circular Wait
A set of a processes each waits for another one in a circular fashion.
Note: the four conditions must occur to have a deadlock. If one condition
is absent, a deadlock may not exist. 7
Circular
wait
There exists a set {P0, P1, P2, ….., Pn} of waiting processes such that:
Circular P2
Wait
P3
P4
Pn waiting for a resource held by P0.
8
Deadlock modeling
In order to solve the deadlock problem, we must find a method to
express it.
This can be achieved using resource allocation graph.
The graphs have two kinds of nodes and directed arc.
Processes, shown as circles
Resources, shown as squares.
A directed arc from a resource node to a process node means that
the resource has previously been requested by, granted to, and is
currently held by that process.
A directed arc from a process to a resource means that the process is
currently blocked waiting for that resource. 9
Deadlock modeling(con’t…)
Edge
Rj Rj
Pi Pi
( Request edge ) ( Assignment edge )
Process Pi requests an instance of resource Rj An instance of Resource Rj is assigned
to Process Pi
22 Processes States:
Waiting Holding Process
Instance of R1 Instance of R2 P1
Instance of R3 Instance of R1 and R2 P2
------- Instance of R3 P3 11
Deadlock modeling(con’t…)
If a Resource Allocation Graph contains a cycle, then a deadlock
may or may not exist.
Case 1: The system has one instance per resource type
• If a cycle exists, the system in a deadlock state.
• Each process involved in the cycle is deadlocked.
P1
Cycle:
R2 R1 P1 R1 P2 R2
Cycle
(Deadlock)
P2 R3 P3 R2
13
Ex 2: a cycle without deadlock:
Cycle:
P1 R1 P3 R2
14
Deadlock handling mechanism
Deadlock problems can be handled in one of the following 4 ways:
1. Structurally negating one of the four required conditions (deadlock
prevention).
2. Allow the system to enter a deadlock state and then recover
(deadlock detection and recovery)
3. Using a protocol avoids deadlock by ensuring that a system will
never enter a deadlock state (deadlock avoidance)
4. Ignore the problem and pretend that deadlocks never occur in the
system(ostrich algorithm); used by most operating systems,
including UNIX
15
Deadlock prevention
1. Mutual Exclusion – This is not required for sharable resources; however
to prevent a system from deadlock, the mutual exclusion condition must
hold for non-sharable resources
2. Hold and Wait – in order to prevent the occurrence of this condition in a
system, we must guarantee that whenever a process requests a resource, it
does not hold any other resources. Two protocols are used to
implement this:
1. Require a process to request and be allocated all its resources
before it begins execution or
2. Allow a process to request resources only when the process has
none 16
Deadlock prevention(con’t…)
Both protocols have two main disadvantages:
o Since resources may be allocated but not used for a long
period, resource utilization will be low
o A process that needs several popular resources has to wait
indefinitely because one of the resources it needs is allocated
to another process. Hence starvation is possible.
3. No Preemption
• If a process holding certain resources is denied further request,
that process must release its original resources allocated to it
17
Deadlock prevention(con’t…)
• If a process requests a resource allocated to another process
waiting for some additional resources, and the requested resource
is not being used, then the resource will be preempted from the
waiting process and allocated to the requesting process
» 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
» This approach is practical to resources whose state can easily
saved and retrieved easily
18
Deadlock prevention(con’t…)
4. Circular Wait
• A linear ordering of all resource types is defined and each
process requests resources in an increasing order of
enumeration.
• So, if a process initially is allocated instances of resource type
R, then it can subsequently request instances of resources
types following R in the ordering
19
Deadlock detection and recovery
o The system does not attempt to prevent deadlocks from occurring.
o It lets them occur, tries to detect when this happens, and then takes
some action to recover after the fact.
o In this mechanism, the system must provide :
20
Deadlock Detection with One Resource of Each Type
o only one resource of each type exists.
o If the resource allocation graph contains one or more cycles, a
deadlock exists.
o Any process that is part of a cycle is deadlocked. If no cycles
exist, the system is not deadlocked.
o Many algorithms for detecting cycles in directed graphs are
known.
o Below we will give a simple one that inspects a graph and
terminates either when it has found a cycle or when it has shown
that none exists.
21
Deadlock Detection with One Resource of Each Type
The algorithm operates by carrying out the following steps as
specified:
1. For each node, N in the graph, perform the following five steps
with N as the starting node.
2. Initialize L 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.
22
Deadlock Detection with One Resource of Each Type
23
Example
The order of processing the nodes is from left to right, top to bottom i.e (R, A, B, C, S, D,
T, E, F, and so forth.)
2. Then we add R to the list and since it appear only once in L. visit unmark outgoing arc
from R.
3. There is only one outgoing arc which is move to A and add it to L, giving L = [R, A].and
Rj. 26
Deadlock Detection Algorithm
1. Let Work and Finish be vectors of length m and n, respectively
Initialize:
(a) Work = Available
(b)For i = 1,2, …, n, if Allocationi 0, then
Finish[i] = false; otherwise, Finish[i] = true.
2. Find an index i such that both:
(a) Finish[i] == false
(b)Requesti 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] == false, for some i, 1 i n, then the system is in
deadlock state.
Moreover, if Finish[i] == false, then Pi is deadlocked.
27
Deadlock Detection Algorithm (contd.)
Example
• Five processes P0 through P4; three resource types :A (7 instances), B (2
instances), and C (6 instances)
Allocation Request Available Existing
ABC ABC ABC ABC
P1 200 202
P2 303 000
P3 211 100
P4 002 002
• Sequence <P0, P2, P3, P1, P4> will result in Finish[i] = true for all i. 28
Deadlock Detection Algorithm (cont.)
• P2 requests an additional instance of type C.
Request
ABC
P0 000
P1 201
P2 001
P3 100
P4 002
• State of system?
In which order should we choose a process to abort? Chose the process with
Least amount of processor time consumed so far
Least amount of output produced so far
Most estimated time remaining
Least total resources allocated so far
Lowest priority 30
Recovery from Deadlock
Resource Preemption
o In this recovery strategy, we successively preempt resources and allocate them to another
process until the deadlock is broken
o While implementing this strategy, there are three issues to be considered
Selecting a victim – which resources and process should be selected to minimize cost
just like in process termination. The cost factors may include parameters like the number
of resources a deadlocked process is holding, number of resources it used so far
Rollback – if a resource is preempted from a process, then it can not continue its normal
execution
• The process must be rolled back to some safe state and started
Starvation – same process may always be picked as victim several times. As a result,
starvation may occur. The best solution to this problem is to only allow a process to be
picked as a victims for a limited finite number of times. This can be done by including
the number of rollback in the cost factor
31
Deadlock avoidance
• Deadlock avoidance scheme requires each process to declare the
maximum number of resources of each type that it may need in
advance
• 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.
• Simplest and most useful model requires that each process declare
the maximum number of resources of each type that it may need.
32
Safe and Unsafe States
• A state is said to be safe if there is some scheduling order in which every process
can run to completion even if all of them suddenly request their maximum
Number of resources immediately.
• A state said to be unsafe if there is no granted, given to any of process to
complete.
are no deadlocks.
is a possibility of deadlock
state 33
Example(safe state)
• Show whether the following process are safe or not using a total of
10 instances of the resource exist
• the state is safe because the system, by careful scheduling, can
avoid deadlock.
34
Example(unsafe state)
• the state of is unsafe because the system, have no sequence that
guarantees completion.
35
Deadlock Avoidance Algorithms
36
Deadlock Avoidance Algorithms (contd.)
Resource-Allocation Graph Scheme
• A new type of edge (Claim edge), in addition to the request and assignment
edge is introduced.
Claim Edge
Suppose that
P2 requests R2
Cycle
39
Deadlock Avoidance Algorithms (contd.)
Banker’s Algorithm
• When a process gets all its resources it must return them in a finite
amount of time
40
Deadlock Avoidance Algorithms (contd.)
Banker’s Algorithm
• The following data structures are used in the algorithm:
Let n = number of processes, and
m = number of resources types.
• Available: Vector of length m.
• Max: n x m matrix.
type Rj.
• Allocation: n x m matrix.
• Need: n x m matrix.
Work = Available
Finish [i] = false for i = 0, 1, …, n- 1.
2. Find an i such that both:
(a) Finish [i] = false
Executing safety algorithm shows that sequence < P1, P3, P4, P0, P2> satisfies
safety requirement.
o In the first phase, the process tries to lock all the records it needs, one at a
time.
o If it succeeds, it begins the second phase, performing its updates and
46
releasing the locks.
Communication deadlock
o process wants something that another process has and must wait until the first one gives it up.
o Another kind of deadlock can occur in communication systems (e.g., networks), in which two
none) or avoided by careful scheduling (since there are no moments when a request could be
postponed).
o The technique that can usually be employed to break communication deadlocks: timeouts.
47
Livelock
o In some situations, polling (busy waiting) is used to enter a critical region or access
a resource.
o Let pair of processes (process A and process B)using two resources.
o process A is use resource 1 and request resource 2 and process B use resource 2
and request resource 1
o if the processes wait for required resource by spooling rather than blocking, this
situation is called livelock.
o Thus we do not have a deadlock (because no process is blocked) but we have
something functionally equivalent to deadlock; will make no further progress.
Starvation
o When processes wait for execution, but it cannot be possible to get CPU to be run,
because of scheduler or something else these processes are said to be under
48
starvation.