OS 3
OS 3
Deadlocks may also involve different resource types. For example, consider a system with one
printer and one DVD drive. Suppose that process Pi is holding the DVD and process P j is
holding the printer. If Pi requests the printer and Pj requests the DVD drive, a deadlock occurs.
DEADLOCK CHARACTERIZATION
m
Necessary Conditions
o
A deadlock situation can arise if the following four conditions hold simultaneously in a system:
c
1. Mutual exclusion: At least one resource must be held in a non-sharable mode, that is,
.
only one process at a time can use the resource. If another process requests that resource,
the requesting process must be delayed until the resource has been released.
e
2. Hold and wait: A process must be holding at least one resource and waiting to acquire
additional resources that are currently being held by other processes.
d
3. No preemption: Resources cannot be preempted; that is, a resource can be released only
o
voluntarily by the process holding it, after that process has completed its task.
c
4. Circular wait: A set {P0, Pl, ... , Pn} of waiting processes must exist such that Po is
waiting for a resource held by P1, P1 is waiting for a resource held by P2, ... , Pn-1 is
u
waiting for a resource held by Pn and Pn is waiting for a resource held by Po.
t
Resource-Allocation Graph
v
Deadlocks can be described in terms of a directed graph called System Resource-Allocation
Graph
The graph consists of a set of vertices V and a set of edges E. The set of vertices V is
partitioned into two different types of nodes:
• P = {P1, P2, ...,Pn}, the set consisting of all the active processes in the system.
• R = {R1, R2, ..., Rm} the set consisting of all resource types in the system.
3
Operating Systems 21CS44
Pictorially each process Pi as a circle and each resource type Rj as a rectangle. Since resource
type Rj may have more than one instance, each instance is represented as a dot within the
rectangle.
A request edge points to only the rectangle Rj, whereas an assignment edge must also designate
one of the dots in the rectangle.
When process Pi requests an instance of resource type Rj, a request edge is inserted in the
m
resource-allocation graph. When this request can be fulfilled, the request edge is
instantaneously transformed to an assignment edge. When the process no longer needs access
o
to the resource, it releases the resource; as a result, the assignment edge is deleted.
c
The resource-allocation graph shown in Figure depicts the following situation.
e .
o d
u c
t
The sets P, K and E:
• P = {P1, P2, P3}
v
• R= {R1, R2, R3, R4}
• E = {Pl →Rl, P2 → R3, Rl → P2, R2 → P2, R2 → P1, R3 → P3 }
Resource instances:
• One instance of resource type R1
• Two instances of resource type R2
• One instance of resource type R3
• Three instances of resource type R4
Process states:
• Process P1 is holding an instance of resource type R2 and is waiting for an instance of
resource type R1.
• Process P2 is holding an instance of R1 and an instance of R2 and is waiting for an
instance of R3.
• Process P3 is holding an instance of R3.
4
Operating Systems 21CS44
• If each resource type has exactly one instance, then a cycle implies that a deadlock has
occurred. If the cycle involves only a set of resource types, each of which has only a
single instance, then a deadlock has occurred. Each process involved in the cycle is
deadlocked.
• If each resource type has several instances, then a cycle does not necessarily imply that
a deadlock has occurred. In this case, a cycle in the graph is a necessary but not a
m
sufficient condition for the existence of deadlock.
o
To illustrate this concept, the resource-allocation graph depicted in below figure:
c
Suppose that process P3 requests an instance of resource type R2. Since no resource instance is
.
currently available, a request edge P3 → R2 is added to the graph. At this point, two minimal
cycles exist in the system:
1. P1 →R1 → P2 → R3 → P3 → R2→P1
e
2. P2 →R3 → P3 → R2 → P2
o d
u c
vt Figure: Resource-allocation graph with a deadlock.
Processes P1, P2, and P3 are deadlocked. Process P2 is waiting for the resource R3, which is
held by process P3. Process P3 is waiting for either process P1 or process P2 to release resource
R2. In addition, process P1 is waiting for process P2 to release resource R1.
Consider the resource-allocation graph in below Figure. In this example also have a cycle:
P1→R1→P3→R2→P1
5
Operating Systems 21CS44
DEADLOACK PREVENTION
Deadlock can be prevented by ensuring that at least one of the four necessary conditions cannot
hold.
Mutual Exclusion
• The mutual-exclusion condition must hold for non-sharable resources. Sharable
m
resources, do not require mutually exclusive access and thus cannot be involved in a
deadlock.
• Ex: Read-only files are example of a sharable resource. If several processes attempt to
o
open a read-only file at the same time, they can be granted simultaneous access to the
file. A process never needs to wait for a sharable resource.
c
• Deadlocks cannot prevent by denying the mutual-exclusion condition, because some
.
resources are intrinsically non-sharable.
e
Hold and Wait
To ensure that the hold-and-wait condition never occurs in the system, then guarantee that,
d
whenever a process requests a resource, it does not hold any other resources.
• One protocol that can be used requires each process to request and be allocated all its
resources before it begins execution.
o
• Another protocol allows a process to request resources only when it has none. A process
may request some resources and use them. Before it can request any additional
c
resources, it must release all the resources that it is currently allocated.
u
Ex:
t
• Consider a process that copies data from a DVD drive to a file on disk, sorts the file, and
then prints the results to a printer. If all resources must be requested at the beginning of
v
the process, then the process must initially request the DVD drive, disk file, and printer.
It will hold the printer for its entire execution, even though it needs the printer only at
the end.
• The second method allows the process to request initially only the DVD drive and disk
file. It copies from the DVD drive to the disk and then releases both the DVD drive and
the disk file. The process must then again request the disk file and the printer. After
copying the disk file to the printer, it releases these two resources and terminates.
7
Operating Systems 21CS44
No Preemption
The third necessary condition for deadlocks is that there be no preemption of resources that
have already been allocated.
To ensure that this condition does not hold, the following protocols can be used:
• If a process is holding some resources and requests another resource that cannot be
immediately allocated to it, then all resources the process is currently holding are
m
preempted.
• The preempted resources are added to the list of resources for which the process is
o
waiting. The process will be restarted only when it can regain its old resources, as well
as the new ones that it is requesting.
. c
If a process requests some resources, first check whether they are available. If they are, allocate
them.
e
If they are not available, check whether they are allocated to some other process that is waiting
for additional resources. If so, preempt the desired resources from the waiting process and
d
allocate them to the requesting process.
If the resources are neither available nor held by a waiting process, the requesting process must
o
wait. While it is waiting, some of its resources may be preempted, but only if another process
requests them.
c
A process can be restarted only when it is allocated the new resources it is requesting and
recovers any resources that were preempted while it was waiting.
t u
Circular Wait
One way to ensure that this condition never holds is to impose a total ordering of all resource
v
types and to require that each process requests resources in an increasing order of enumeration.
To illustrate, let R = {R1, R2, ... , Rm} be the set of resource types. Assign a unique integer
number to each resource type, which allows to compare two resources and to determine
whether one precedes another in ordering. Formally, it defined as a one-to-one function
F: R ->N, where N is the set of natural numbers.
Example: if the set of resource types R includes tape drives, disk drives, and printers, then the
function F might be defined as follows:
F (tape drive) = 1
F (disk drive) = 5
F (printer) = 12
Now consider the following protocol to prevent deadlocks. Each process can request resources
only in an increasing order of enumeration. That is, a process can initially request any number
of instances of a resource type -Ri. After that, the process can request instances of resource type
Rj if and only if F(Rj) > F(Ri).
8
Operating Systems 21CS44
The system recovers from the deadlock automatically. There are two options for breaking a
deadlock one is simply to abort one or more processes to break the circular wait. The other is to
preempt some resources from one or more of the deadlocked processes.
Process Termination
To eliminate deadlocks by aborting a process, use one of two methods. In both methods, the
m
system reclaims all resources allocated to the terminated processes.
o
1. Abort all deadlocked processes: This method clearly will break the deadlock cycle, but
at great expense; the deadlocked processes may have computed for a long time, and the
c
results of these partial computations must be discarded and probably will have to be
.
recomputed later.
2. Abort one process at a time until the deadlock cycle is eliminated: This method
incurs considerable overhead, since after each process is aborted, a deadlock-detection
e
algorithm must be invoked to determine whether any processes are still deadlocked.
d
If the partial termination method is used, then we must determine which deadlocked process (or
o
processes) should be terminated. Many factors may affect which process is chosen, including:
c
1. What the priority of the process is
2. How long the process has computed and how much longer the process will compute
before completing its designated task
u
3. How many and what types of resources the process has used.
t
4. How many more resources the process needs in order to complete
5. How many processes will need to be terminated?
v
6. Whether the process is interactive or batch
Resource Preemption
18