You are on page 1of 20

OPERATING SYSTEMS

Chapter 6
Deadlocks
Contents
• Resources
• Deadlocks
• Methods for Handling Deadlocks
• Deadlock Detection & Recovery
Resources
Chapter 6 - Deadlocks
Resources
• Multiple resources are used simultaneously by different processes
• However, a single resource can be used by only one process at a time
• Resources  Hardware Component, Memory Unit, CPU etc.
• Virtual resources  piece of information
• In resource allocation, major responsibility of OS is to release a resource after
its use and to deal with resource conflict when multiple processes wish to
access limited available resources
• Sequence to utilize a resource
• Request  may be a system call
• Use  is request is granted
• Release
• Possible to have multiple instances of a single resource type
Deadlocks
Chapter 6 - Deadlocks
Deadlocks
• A situation may arise when a process is waiting for some resources that are
held (or locked) by the other waiting processes
• A set of processes is said to be in a deadlock state if every process in the set is
waiting for an event that can be caused only by another process in the set
Deadlocks
• Conditions of Deadlock
• Mutual Exclusion
• Only one process at a time can use the resource
• If another process needs to use that resource, it must wait until resource has been released
• Hold & Wait
• A process must be holding at least one resource and waiting to acquire additional resources that
are currently being held by other processes in system
• No Preemption

• Circular Wait
• Circular Wait condition implies Hold and Wait condition
Resource Allocation Graph
• Deadlock can be described graphically using a directed graph
• Process Node, Resource Node
• Request Edge, Assignment Edge
• Dots in resource node represents instances of resources
• No cycles, No deadlock

Deadlock No deadlock
Methods for Handling Deadlocks
Chapter 6 - Deadlocks
Methods for Handling Deadlocks
• Protocol to prevent or avoid deadlocks
• Allow a system to enter a deadlock state, detect deadlock, and recover it
• Ignore deadlock problem and make believe that deadlocks never occur in
system
• This method is commonly used by most OS including Linux and Windows
• It can also be handled by developing a program
Deadlock Prevention
• Prevent deadlock by ensuring that at least one of four necessary conditions
for deadlock cannot occur
• Deadlock prevented but other problems may be created through this approach
• Mutual Exclusion
• Must hold for non-sharable resources  printer
• A printer cannot be simultaneously shared by two or more processes
• Shareable resources do not create Mutual Exclusion conditions and thus cannot be
involved in a deadlock  read-only file
• Multiple processes can be allowed to access the read-only file at the same time
• Deadlock can occur if multiple processes need write permission, which is not shareable
• We cannot prevent deadlocks by denying Mutual Exclusion condition, because some
resources like printer and scanner are non-shareable by nature
Deadlock Prevention
• Hold & Wait
• First Approach
• This condition can be prevented if a process requests all of its required resources
before starting execution
• If everything is available  allocated whatever a process needs and can run until
completion
• If complete set of resources needed by process is not currently available, then process
must wait until the complete set is available
- A process may not know in advance all of the resources, it will require. The resources needed may
depend on its computation
- If several resources are allocated at the beginning of process, process may hold these resources for
a long time; during that time they are denied to other processes.
Deadlock Prevention
• Hold & Wait
• Second Approach
• A process must be assigned selective resources in a sequence that are needed during
execution and must release them after use
• R1 & R3 at the beginning of execution
• R5, R2 & R4 at time t1 in its execution
• R3 & R2 at time t2 in its execution
- P1  disk
- T0  P1 releases disk and requests printer
- P2  disk
- Data may be lost before sending it to printer
Deadlock Prevention
• No Preemption
• This condition can be prevented in several ways
• Way 1
• If a process is holding some resources and requests another resource that cannot be immediately
allocated to it, then all the resources held by that process must be released
- Partly updated file cannot be preempted because data may be lost
• Way 2
• If a process requests some available resources, OS allocates them
• If they are not, OS checks whether they are allocated to some other process that is waiting for
additional resources  OS may preempt from waiting process
• This strategy can be applied to resources whose state can be easily saved and then later restored 
CPU registers and memory space
- Starvation
Deadlock Prevention
• Circular Wait
• Define a linear order of resource types
• Each resource is assigned a unique integer number
• Processes can get resources only in increasing order of these resources numbers
• Printer
• Plotter
• Tape Drive
• DVD Drive
• Disk Drive
Numerically Ordered of Resources
- Circular-wait prevention slows down processes and denies resource access
- Reduces resource utilization and performance of overall system
- Numerical ordering of resources eliminates problem of deadlock, but it may
be impossible to find an ordering that satisfies every process
Deadlock Detection & Recovery
Chapter 6 - Deadlocks
Deadlock Detection & Recovery
• Deadlock prevention & avoidance  impose restrictions on processes
• Deadlock Detection  allow deadlocks to occur
• An algorithm to detect a deadlock
• An algorithm to recover the system from the deadlock
• Does not limit resource access or restrict process execution
• Requested resources are granted to processes whenever possible
• OS periodically performs an algorithm to detect circular wait condition
• If a cycle is created at any time, then OS automatically recovers deadlock by
performing another recovery algorithm
Deadlock Detection
Single Instance of Each Resource Type
• We can define a deadlock detection algorithm by using a wait-for graph
• A wait-for graph is obtained from resource-allocation graph by removing
resource nodes and collapsing appropriate edges

Resource Allocation Graph Wait-for graph

• If a wait-for graph contains one or more cycles, then a deadlock exists


For more details, refers to

PM Series

Operating Systems

by
Dr. Muhammad Ismail Mohmand

You might also like