Professional Documents
Culture Documents
Chapter 8 - Deadlocks
Chapter 8 - Deadlocks
Deadlocks
Edited by
Ghada Mohammed S AlOsaimi
CS Lecturer, CCIS, IMSIU
Outline
System Model
Deadlock in Multithreaded Applications
Deadlock Characterization
Methods for Handling Deadlocks
Deadlock Prevention
Deadlock Avoidance Process
Management
Deadlock Detection
Recovery from Deadlock
Protection and Memory
Security Introduction Management
and Overview
Storage
Management
2
Objectives
Illustrate how deadlock can occur when mutex locks are used
3
Deadlock
In multicore system or multiprogramming environment, several threads or processes request several resources
If resources are not available at that time, thread or process enter waiting state
If waiting process or thread is not changing its state because the resources are held or occupied by other waiting
threads or processes
4
System Model
System consists of a limited number of resources that are distributed among different thread and processes
These resourses are classified in several types
Resource types R1, R2, . . ., Rm
CPU cycles, memory space, IO devices, ..etc.
Each resource type (Ri) is consisting of some number of identical instances (Wi)
For example, if a system has four CPUs, then the resource type CPU has four instances
Each resource type Ri has Wi instances
Each process or thread utilizes a resource in 3 steps as follows:
Request – process or thread requests the resource
If the request cannot be granted immediately (for example, if a mutex lock is currently held by another thread),
then the requesting thread must wait until it can acquire the resource
Use – process or thread can operate on the resource (for example, if the resource is a mutex lock, the thread can
access its critical section)
5
Release – process or thread releases the resource
Deadlock Characterization
Deadlocks can be described more precisely in terms of a directed graph called a system resource-allocation graph
A set of vertices V and a set of edges E
V is partitioned into two types:
P = {P1, P2, …, Pn}, the set consisting of all the processes in the system
R = {R1, R2, …, Rm}, the set consisting of all resource types in the system
Request edge – directed edge Pi → Rj
Assignment edge – directed edge Rj → Pi
Example:
Graph has four resource types R1, R2, R3 and R4
Number of instances: One instance of R1, Two instances of R2, One instance of R3 and Three instance of R4
Process T1: holds one instance of R2 and is waiting for an instance of R1
Process T2: holds one instance of R1, one instance of R2, and is waiting for an instance of R3
7
Process T3: holds one instance of R3
Resource-Allocation Graph (cont.)
Resource allocation graph with a deadlock Graph with a cycle but no deadlock
8
Methods for Handling Deadlocks
9
Prevention vs. Avoidance
Deadlock prevention
Provides a set of methods to ensure that at least one of the necessary four conditions cannot hold. These methods
prevent deadlocks by constraining how requests for resources can be made.
Deadlock avoidance
Requires that OS be given additional information in advance concerning which resources a thread will request and
use during its lifetime. With this additional knowledge, OS can decide for each request whether or not the thread
should wait.
10
Deadlock Prevention
first_mutex = 1
second_mutex = 5
12
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
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
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 processes in the systems such that for each Pi,
resources that Pi can still request can be satisfied by currently available resources + resources held by all Pj, with j < i
That is:
If Pi resource needs are not immediately available, then Pi can wait until all Pj have finished
13 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
Safe, Unsafe, Deadlock States
Avoidance algorithms ⇒ ensure that a system will never enter an unsafe state
May lower resource utilization because of if thread or process requests
a resource that is currently available but not sufficient, it may have to wait
14
Avoidance Algorithms
15
Resource-Allocation Graph Scheme
Claim edge Pi → Rj – indicated that process Pj may request resource Rj; represented by a dashed line
Claim edge converts to request edge when a process requests a resource
Request edge converted to an assignment edge when the resource is allocated to the process
When a resource is released by a process, assignment edge reconverts to a claim edge
Resources must be claimed a priori in the system
Process Pi starts execution when all its claim edges must appear in the resource allocation graph
16
Resource-Allocation Graph Algorithm
17
Banker’s Algorithm
18
Safety Algorithm
19
Safety Algorithm - Example
20
The system is in a safe state since the sequence < P1, P3, P4, P2, P0> satisfies safety criteria
Safety Algorithm - Example
21
The system is in a safe state since the sequence < P1, P3, P4, P2, P0> satisfies safety criteria
Safety Algorithm – Example (cont.)
Requesti – request vector for process Pi. If Requesti [j] = k then process Pi wants k instances of resource type Rj
1. If Requesti ≤ Needi go to step 2. Otherwise, raise error condition, since process has exceeded its maximum claim
2. If Requesti ≤ Available, go to step 3. Otherwise Pi must wait, since resources are not available
3. Pretend to allocate requested resources to Pi by modifying the state as follows:
Available = Available – Requesti;
Allocationi = Allocationi + Requesti;
Needi = Needi – Requesti;
If safe ⇒ the resources are allocated to Pi
If unsafe ⇒ Pi must wait, and the old resource-allocation state is restored
23
Resource-Request Algorithm - Example
P1 Request (1,0,2)
Check that Request ≤ Available (that is, (1,0,2) ≤ (3,3,2) ⇒ true
Allocation Need Available
ABC ABC ABC
P0 010 743 230
P1 302 020
P2 302 600
P3 211 011
P4 002 431
Executing safety algorithm shows that sequence < P1, P3, P4, P0, P2> satisfies safety requirement
Can request for (3,3,0) by P4 be granted? Can request for (0,2,0) by P0 be granted?
24
Deadlock Detection
Detection algorithm
Single-instance resources → wait-for graph
Multiple-instances resources → Detection algorithm
Recovery scheme
25
Single Instance of Each Resource Type
26
Resource-Allocation Graph Corresponding wait-for graph
Several Instances of a Resource Type
Wait-for graph cannot detect deadlock in several instances for resource types
Used data structures :
Available
A vector of length m indicates the number of available resources of each type
Allocation
An n x m matrix defines the number of resources of each type currently allocated to each process
Request
An n x m matrix indicates the current request of each process. If Request [i][j] = k, then process Pi is requesting
k more instances of resource type Rj
27
Detection Algorithm
30
Detection-Algorithm Usage
When should we invoke the detection algorithm? The answer depends on two factors:
How often a deadlock is likely to occur?
How many processes will need to be rolled back?
one for each disjoint cycle
If detection algorithm is invoked arbitrarily, there may be many cycles in the resource graph and so we would not be able to
tell which of the many deadlocked processes “caused” the deadlock
31
Recovery from Deadlock: Process Termination
32
Recovery from Deadlock: Resource Preemption
Selecting a victim
Which resources and which processes are to be preempted?
As in process termination, we must determine the order of preemption to minimize cost
Rollback
If we preempt a resource from a process, what should be done with that process?
Clearly, it cannot continue with its normal execution; it is missing some needed resource
We must roll back the process to some safe state and restart it from that state
Starvation
Same process may always be picked as victim
we must ensure that a process can be picked as a victim only a (small) finite number of times
The most common solution is to include the number of rollbacks in the cost factor
33