Deadlock
Deadlock
Principles of Operating
System
Course Code CIT11
Deadlocks
Objectives
• After completing this lecture, students will
be able to:
• Develop a description of deadlocks, which prevents
sets of concurrent processes from completing their
tasks
• Present a number of different methods for preventing
or avoiding deadlocks in a computer system
• Demonstrate how to detect and recover from
deadlock.
Wedad Al-Sorori Introduction 3/45
14 September 2017
CIT11: Principles of Operating System
System Model
• System consists of resources
Deadlock Characterization
• Mutual exclusion: only one process at a time can use a resource
• Hold and wait: a process holding at least one resource is waiting
to acquire additional resources held by other processes
• No preemption: a resource can be released only voluntarily by
the process holding it, after that process has completed its task
• Circular wait: there exists a set {P0, P1, …, Pn} of waiting processes
such that P0 is waiting for a resource that is held by P1, P1 is
waiting for a resource that is held by P2, …, Pn–1 is waiting for a
resource that is held by Pn, and Pn is waiting for a resource that is
held by P0.
• R = {R1, R2, …, Rm}, the set consisting of all resource types in the system
• Process
Pi
• Pi requests instance of Rj
Rj
Pi
• Pi is holding an instance of Rj Rj
Wedad Al-Sorori 14 September 2017 Introduction 7/45
CIT11: Principles of Operating System
• Deadlock avoidence
Deadlock Avoidance
• Requires that the system has some additional a priori information
available
• Simplest and most useful model requires that each process
declare the maximum number of resources of each type that
it may need
• 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
Safe State
• When a process requests an available resource, system must
decide if immediate allocation leaves the system in a safe state
• System is in safe state if there exists a sequence <P1, P2, …,
Pn> of ALL the processes in the systems such that for each P i,
the resources that Pi can still request can be satisfied by
currently available resources + resources held by all the Pj, with j
<I
• That is:
• If Pi resource needs are not immediately available, then Pi can wait
until all Pj have finished
• When Pj is finished, Pi can obtain needed resources, execute, return
allocated resources, and terminate
• When Pi terminates, Pi +1 can obtain its needed resources, and so on
Wedad Al-Sorori 14 September 2017 Introduction 16/45
CIT11: Principles of Operating System
Basic Facts
• If a system is in safe state no deadlocks
Avoidance Algorithms
• Single instance of a resource type
• Use a resource-allocation graph
Resource-Allocation Graph
Banker’s Algorithm
• Multiple instances
4. If Finish [i] == true for all i, then the system is in a safe state
Wedad Al-Sorori 14 September 2017 Introduction 26/45
CIT11: Principles of Operating System
Example (2/3):
• The content of the matrix Need is defined to be Max – Allocation
Need
ABC
P0 743
P1 122
P2 600
P3 011
P4 431
• The system is in a safe state since the sequence < P1, P3, P4, P2, P0>
satisfies safety criteria
Wedad Al-Sorori 14 September 2017 Introduction 29/45
CIT11: Principles of Operating System
• Executing safety algorithm shows that sequence < P1, P3, P4, P0, P2> satisfies safety
requirement
Deadlock Detection
• Allow system to enter deadlock state
• Detection algorithm
• Recovery scheme
• Wedad
Sequence
Al-Sorori <P0, P2, P3, P1, P4> will14result in Finish[i] = true for all i
September 2017 Introduction 37/45
CIT11: Principles of Operating System
Example (2/2)
• P2 requests an additional instance of type C
Request
ABC
P0 000
P1 202
P2 001
P3 100
P4 002
• State of system?
• Can reclaim resources held by process P0, but insufficient resources to fulfill other
processes; requests
Wedad Al-Sorori
• Deadlock Introduction 38/45
exists, consisting of processes P1, P2, P3, and P4
14 September 2017
CIT11: Principles of Operating System
Detection-Algorithm Usage
• When, and how often, to invoke depends on:
• How often a deadlock is likely to occur?
• How many processes will need to be rolled back?
• one for each disjoint cycle
A word on Starvation
• Starvation and deadlock are two different things
• With deadlock – no work is being accomplished for the
processes that are deadlocked, because processes are waiting
for each other. Once present, it will not go away.
Thanks