PROPRIETARY MATERIAL. © 2007 The McGrawHill Companies, Inc. All rights reserved. No part of this PowerPoint slide may be displayed, reproduced or distributed
in any form or by any means, without the prior written permission of the publisher, or used beyond the limited distribution to teachers and educators permitted by McGrawHill
for their individual course preparation. If you are a student using this PowerPoint slide, you are using it without permission.
Chapter 11: Deadlocks
Dhamdhere: Operating Systems—
A ConceptBased Approach, 2 ed
Slide No: 1
Copyright © 2008
Deadlocks
•
A deadlock is a situation in which a set of processes face
indefinite waits
– A deadlock results in reduction of concurrency and parallelism
– Both response times to applications and system performance
suffer
•
Definition
– A deadlock involving a set of processes D is a situation in which
* Every process Pi in D is blocked on some event ei
* Event ei can be caused only by actions of some process(es) in D
Chapter 11: Deadlocks
Dhamdhere: Operating Systems—
A ConceptBased Approach, 2 ed
Slide No: 2
Copyright © 2008
Deadlocks
•
Various kinds of deadlocks can arise in an OS, e.g.,
– Synchronization deadlocks
* Processes wait for one another to perform expected actions
– Message communication deadlocks
* Processes wait for messages from one another
– Resource deadlocks
* Processes wait for requested resources to be allocated to them
•
An OS handles only resource deadlocks, if at all
Chapter 11: Deadlocks
Dhamdhere: Operating Systems—
A ConceptBased Approach, 2 ed
Slide No: 3
Copyright © 2008
Resource deadlocks
•
Events related to resource allocation
– Resource request
* A process requests a resource through a system call
* The process is blocked if the resource cannot be allocated to it
– Resource allocation to a process
* If the process was blocked, its state is changed to ready
* The process becomes a holder of the resource
* The resource state is changed to ‘allocated’
– Resource release
* A process releases a resource through a system call
* The OS allocates the resource to a process blocked for it, if any
* Otherwise, it changes the state of the resource to ‘free’
Chapter 11: Deadlocks
Dhamdhere: Operating Systems—
A ConceptBased Approach, 2 ed
Slide No: 4
Copyright © 2008
Conditions for resource deadlock
•
A resource deadlock can arise only if the following
conditions hold simultaneously
– Non-shareable resources
* Processes need exclusive access to resources
– Hold-and-wait
* A process continues to hold resources allocated to it while it waits
for other resources
– No preemption
* OS cannot preempt a resource from one process and allocate it to
another process
– Circular waits
* A circular chain of hold-and-wait conditions exists
Chapter 11: Deadlocks
Dhamdhere: Operating Systems—
A ConceptBased Approach, 2 ed
Slide No: 5
Copyright © 2008
The resource allocation state of a system
•
We use the term resource allocation state of the system,
or simply resource state, to indicate
– State of each resource in the system
– Resource allocations and pending resource requests
•
A resource class contains resources of the same kind
– Each resource in a class is called a resource unit or resource
instance
– Each resource instance has a state
Chapter 11: Deadlocks
Dhamdhere: Operating Systems—
A ConceptBased Approach, 2 ed
Slide No: 6
Copyright © 2008
Modeling the resource state
•
We use two kinds of models of resource state
– Graph models
* Can be used only if a process requests only one resource of a
resource class
Processes and resources are denoted as nodes in the graph
Allocations and pending requests are denoted as edges
– The matrix model
* Does not impose any constraints on resource requests
Resources, allocations and pending requests are represented
in separate matrices
Chapter 11: Deadlocks
Dhamdhere: Operating Systems—
A ConceptBased Approach, 2 ed
Slide No: 7
Copyright © 2008
Wait-for graph (WFG)
•
A WFG can be used to depict the resource state of a
system in which every resource class contains only one
resource unit
– A Node in the graph is a process
– An edge is a wait-for relationship between processes
* A wait-for edge (Pi, Pj) indicates that
Process Pj holds the resource unit of a resource class
Process Pi has requested the resource and it has become
blocked on it
In essence Pi waits for Pj to release the resource
Chapter 11: Deadlocks
Dhamdhere: Operating Systems—
A ConceptBased Approach, 2 ed
Slide No: 8
Copyright © 2008
Resource request and allocation graph (RRAG)
•
Nodes and edges in an RRAG
– Two kinds of nodes exist in an RRAG
* A circle is a process
* A rectangle is a resource class
Each bullet in a rectangle is one resource unit
– Edges can also be of two kinds
* An edge from a resource class to a process is a resource allocation
* An edge from a process to a resource class is a pending resource
request
Chapter 11: Deadlocks
Dhamdhere: Operating Systems—
A ConceptBased Approach, 2 ed
Slide No: 9
Copyright © 2008
Examples of RRAG and WFG
Part (a)
• R1 contains one resource unit,
which is allocated to P
1
• R2 contains two units, allocated
to P2 and P3
• P2, P3 wait for R1, P4 for R2
Chapter 11: Deadlocks
Part (b)
RRAG (left half of Part (b))
• R1 contains one resource unit,
which is allocated to P1
• P2 and P3 have requested R1
• WFG is shown in the right half
Dhamdhere: Operating Systems—
A ConceptBased Approach, 2 ed
Slide No: 10
Copyright © 2008
Paths in WFG and RRAG
•
A path in a graph is a sequence of edges such that the
destination node of an edge is the source node of the
subsequent edge
– Consider a path P1 – Ri – P2 – Rj – P3 – Rk – P4
This path indicates that
* Process P4 has been allocated a resource unit of Rk
* Process P3 has been allocated a resource unit of Rj and awaits a
resource unit of Rk, etc.
– No deadlock exists because a resource unit of Rk is allocated to
P4, and P4 is not blocked
* Hence it may finish using the resource and release Rk
* Then P3 can use Rk and so on
Chapter 11: Deadlocks
Dhamdhere: Operating Systems—
A ConceptBased Approach, 2 ed
Slide No: 11
Copyright © 2008
An example of a deadlock
• A system contains a printer and a tape and two
processes which require both the resources
Process Pi Process Pj
Request a tape Request a printer
Request a printer Request a tape
Chapter 11: Deadlocks
RRAG
Dhamdhere: Operating Systems—
A ConceptBased Approach, 2 ed
Slide No: 12
Copyright © 2008
Another example
•
What if the system contained two tapes and a
printer and three processes that used these
resources as follows:
Process Pi
Process Pj
Request a tape
Request a printer
…
…
Request a printer
Process Pk
Request a tape
…
Request a tape
Q: Do we have a deadlock?
Chapter 11: Deadlocks
Dhamdhere: Operating Systems—
A ConceptBased Approach, 2 ed
Slide No: 13
Copyright © 2008
RRAG after all requests of the example are made
• The printer is allocated to Pj
• The units of tape are allocated to Pi and Pk
• Pi has requested a printer and Pj has requested a tape
● From this and the previous example, we can conclude that
– A cycle in an RRAG is a necessary condition for a deadlock
– A cycle in an RRAG is not a sufficient condition for a deadlock
Chapter 11: Deadlocks
Dhamdhere: Operating Systems—
A ConceptBased Approach, 2 ed
Slide No: 14
Copyright © 2008
Deadlock handling approaches
•
Three approaches are used to handle deadlocks
– Deadlock detection and resolution
* Kernel periodically analyzes resource state
* If a deadlock exists, it aborts some processes and reallocates their
resources to other processes so that there is no deadlock
– Deadlock prevention
* The kernel’s resource allocation policy ensures that the four
conditions for deadlock cannot be satisfied simultaneously
– Deadlock avoidance
* The kernel grants only those resource requests whose granting
cannot lead to deadlocks in future
* Other requests are kept pending until they can be granted
Chapter 11: Deadlocks
Dhamdhere: Operating Systems—
A ConceptBased Approach, 2 ed
Slide No: 15
Copyright © 2008
Deadlock detection
•
We use a matrix model to represent resource state
•
Given a system with r resource classes and n processes:
– Allocated resources: n x r matrix
– Requested resources: n x r matrix
– Free resources: 1-D array with r elements
Chapter 11: Deadlocks
Dhamdhere: Operating Systems—
A ConceptBased Approach, 2 ed
Slide No: 16
Copyright © 2008
Deadlock detection
•
How can we detect deadlocks?
– A deadlock implies that
* there is no sequence of process completion, resource release, and
resource allocation events through which all processes can
complete
– So the kernel simulates process completion, resource release
and resource allocation actions to check whether all processes
can complete
* It is assumed that a process whose current allocation request has
been granted may complete without requesting more resources
Chapter 11: Deadlocks
Dhamdhere: Operating Systems—
A ConceptBased Approach, 2 ed
Slide No: 17
Copyright © 2008
Algorithm for deadlock detection
•
Steps of the algorithm
– Input two sets of processes—Blocked and Running
* Set Blocked contains processes blocked on resource requests
* Set Running contains ready processes
– Finished is a set of finished processes; it is initially empty
– Perform following steps until set Running is empty
* Transfer a process from Running to Finished and release its
resources
This process was not blocked for a resource, so it may finish
* If all resources requested by a process in Blocked set can be
granted, grant the resources and transfer the process to Running
– A deadlock exists if set Blocked is empty
Chapter 11: Deadlocks
Dhamdhere: Operating Systems—
A ConceptBased Approach, 2 ed
Slide No: 18
Copyright © 2008
Deadlock resolution
•
Abort some processes and distribute their resources
among other processes such that a deadlock does not
exist
– The aborted processes are called victim processes
– The cost of resolution is the cost of re-executing victim
processes
Q: How to determine victim processes?
A:
They should be chosen such that the overall cost of resolution is
the minimum possible
Chapter 11: Deadlocks
Dhamdhere: Operating Systems—
A ConceptBased Approach, 2 ed
Slide No: 19
Copyright © 2008
Deadlock resolution
(a) A deadlock involving processes P1 – P4
(b) Process P2 is aborted and R3, the resource held by it, is
allocated to P1
Chapter 11: Deadlocks
Dhamdhere: Operating Systems—
A ConceptBased Approach, 2 ed
Slide No: 20
Copyright © 2008
Deadlock prevention
•
Use resource allocation policies that make deadlocks
impossible
– How to design a deadlock prevention approach?
* Consider the conditions for deadlock
* Ensure that they cannot hold simultaneously; i.e., make sure that
one of them cannot arise
– A simple policy
* Allocate all resources required by a process together. Hence the
hold-and-wait condition is never satisfied
This policy is expensive in practice as resources may be
requested much before they are actually needed by a process
Chapter 11: Deadlocks
Dhamdhere: Operating Systems—
A ConceptBased Approach, 2 ed
Slide No: 21
Copyright © 2008
Approaches to
deadlock prevention
• Each of the approaches
prevents one of the
conditions for deadlock
from arising
Chapter 11: Deadlocks
Dhamdhere: Operating Systems—
A ConceptBased Approach, 2 ed
Slide No: 22
Copyright © 2008
Resource ranking
•
The resource ranking policy operates as follows
– Numerical ranks are associated with resources
– When process Pi requests a resource Rk with rank rk
* Abort process Pi if rk is ≤ the rank of some resource already
allocated to Pi
* Allocate resource Rk if it is available; otherwise, let process Pi wait
for resource Rk
– Absence of deadlocks when this policy is used can be proved
Chapter 11: Deadlocks
Dhamdhere: Operating Systems—
A ConceptBased Approach, 2 ed
Slide No: 23
Copyright © 2008
Deadlock prevention using resource ranking
Resources R1–R5 have resource ranks 1–5, respectively
(a) Pi holds resources R1 and R3. Pj holds resource R5
(b) Pi requests resource R5. It is allowed to wait for it.
However, when Pj requests one of the resources R1–R4, it
is aborted. Thus, circular waits cannot arise
Chapter 11: Deadlocks
Dhamdhere: Operating Systems—
A ConceptBased Approach, 2 ed
Slide No: 24
Copyright © 2008
Deadlock avoidance
•
Under this policy, the kernel ensures that the system
goes from one safe resource allocation state to another
– Safe resource allocation state:
* If the system is in such a state, all processes will be able to
complete their operation without deadlocks …
… no matter how and when processes request and release
their resources in future
– Unsafe resource allocation state:
* There is a possibility of a deadlock in future
Chapter 11: Deadlocks
Dhamdhere: Operating Systems—
A ConceptBased Approach, 2 ed
Slide No: 25
Copyright © 2008