SlideShare a Scribd company logo
1 of 128
Download to read offline
Deadlocks
Amir H. Payberah
amir@sics.se
Amirkabir University of Technology
(Tehran Polytechnic)
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 1 / 1
Motivation
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 2 / 1
Motivation
Multiprogramming environment: several processes compete for a
finite number of resources.
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 3 / 1
Motivation
Multiprogramming environment: several processes compete for a
finite number of resources.
A process requests resources: if the resources are not available at
that time, the process enters a waiting state.
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 3 / 1
Motivation
Multiprogramming environment: several processes compete for a
finite number of resources.
A process requests resources: if the resources are not available at
that time, the process enters a waiting state.
What if the requests resources are held by other waiting processes?
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 3 / 1
Motivation
Multiprogramming environment: several processes compete for a
finite number of resources.
A process requests resources: if the resources are not available at
that time, the process enters a waiting state.
What if the requests resources are held by other waiting processes?
This situation is called a deadlock.
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 3 / 1
Deadlocks
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 4 / 1
System Model
System consists of resources: R1, R2, · · · , Rm
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 5 / 1
System Model
System consists of resources: R1, R2, · · · , Rm
Resource types: CPU cycles, memory space, I/O devices
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 5 / 1
System Model
System consists of resources: R1, R2, · · · , Rm
Resource types: CPU cycles, memory space, I/O devices
Each resource type Ri has Wi instances.
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 5 / 1
System Model
System consists of resources: R1, R2, · · · , Rm
Resource types: CPU cycles, memory space, I/O devices
Each resource type Ri has Wi instances.
Each process utilizes a resource as follows:
• Request
• Use
• Release
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 5 / 1
Deadlock Characterization (1/3)
Deadlock can arise if four conditions hold simultaneously:
• Mutual exclusion
• Hold and wait
• No preemption
• Circular wait
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 6 / 1
Deadlock Characterization (2/3)
Mutual exclusion
• Only one process at a time can use a resource.
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 7 / 1
Deadlock Characterization (2/3)
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.
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 7 / 1
Deadlock Characterization (3/3)
No preemption
• A resource can be released only voluntarily by the process holding
it, after that process has completed its task.
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 8 / 1
Deadlock Characterization (3/3)
No preemption
• A resource can be released only voluntarily by the process holding
it, after that process has completed its task.
Circular wait
• A set processes: {P0, P1, · · · , Pn}
• P0 is waiting for a resource that is held by P1
• P1 is waiting for a resource that is held by P2
• ...
• Pn is waiting for a resource that is held by P0
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 8 / 1
Deadlock Example (1/2)
/* Create and initialize the mutex locks */
pthread_mutex_t first_mutex;
pthread_mutex_t second_mutex;
pthread_mutex_init(&first_mutex, NULL);
pthread_mutex_init(&second_mutex, NULL);
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 9 / 1
Deadlock Example (2/2)
/* thread one runs in this function */
void *do_work_one(void *param) {
pthread_mutex_lock(&first mutex);
pthread_mutex_lock(&second mutex);
// do some work
pthread_mutex_unlock(&second mutex);
pthread_mutex_unlock(&first mutex);
pthread_exit(0);
}
/* thread two runs in this function */
void *do_work_two(void *param) {
pthread_mutex_lock(&second mutex);
pthread_mutex_lock(&first mutex);
// do some work
pthread_mutex_unlock(&first mutex);
pthread_mutex_unlock(&second mutex);
pthread_exit(0);
}
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 10 / 1
Resource-Allocation Graph
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 11 / 1
Resource-Allocation Graph (1/2)
A set of vertices V and a set of edges E.
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 12 / 1
Resource-Allocation Graph (1/2)
A set of vertices V and a set of edges E.
Vertices
• All the processes in the system: P = P1, P2, · · · , Pn
• All resource types in the system: R = R1, R2, · · · , Rm
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 12 / 1
Resource-Allocation Graph (1/2)
A set of vertices V and a set of edges E.
Vertices
• All the processes in the system: P = P1, P2, · · · , Pn
• All resource types in the system: R = R1, R2, · · · , Rm
Edges
• Request edge: directed edge Pi → Rj
• Assignment edge: directed edge Rj → Pi
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 12 / 1
Resource-Allocation Graph (2/2)
Process (vertices)
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 13 / 1
Resource-Allocation Graph (2/2)
Process (vertices)
Resource type with 4 instances (vertices)
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 13 / 1
Resource-Allocation Graph (2/2)
Process (vertices)
Resource type with 4 instances (vertices)
Pi requests instance of Rj (edge)
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 13 / 1
Resource-Allocation Graph (2/2)
Process (vertices)
Resource type with 4 instances (vertices)
Pi requests instance of Rj (edge)
Pi is holding an instance of Rj (edge)
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 13 / 1
Resource-Allocation Graph Example (1/3)
Example of a resource allocation graph.
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 14 / 1
Resource-Allocation Graph Example (2/3)
Resource allocation graph with a deadlock.
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 15 / 1
Resource-Allocation Graph Example (3/3)
Resource allocation graph with a cycle but no deadlock.
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 16 / 1
Basic Facts
If graph contains no cycles
• No deadlock
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 17 / 1
Basic Facts
If graph contains no cycles
• No deadlock
If graph contains a cycle
• If only one instance per resource type, then deadlock.
• If several instances per resource type, possibility of deadlock.
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 17 / 1
Methods for Handling Deadlocks
Ensure that the system will never enter a deadlock state:
• Deadlock prevention
• Deadlock avoidance
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 18 / 1
Methods for Handling Deadlocks
Ensure that the system will never enter a deadlock state:
• Deadlock prevention
• Deadlock avoidance
Allow the system to enter a deadlock state and then recover.
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 18 / 1
Methods for Handling Deadlocks
Ensure that the system will never enter a deadlock state:
• Deadlock prevention
• Deadlock avoidance
Allow the system to enter a deadlock state and then recover.
Ignore the problem and pretend that deadlocks never occur in the
system; used by most operating systems.
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 18 / 1
Deadlock Prevention
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 19 / 1
Deadlock Prevention (1/3)
Deadlock can arise if four conditions hold simultaneously:
• Mutual exclusion
• Hold and wait
• No preemption
• Circular wait
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 20 / 1
Deadlock Prevention (1/3)
Deadlock can arise if four conditions hold simultaneously:
• Mutual exclusion
• Hold and wait
• No preemption
• Circular wait
Restrain the ways request can be made.
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 20 / 1
Deadlock Prevention (2/3)
Mutual exclusion
• Not required for sharable resources, e.g., read-only files.
• Must hold for non-sharable resources.
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 21 / 1
Deadlock Prevention (2/3)
Mutual exclusion
• Not required for sharable resources, e.g., read-only files.
• Must hold for non-sharable resources.
Hold and wait
• Must guarantee that whenever a process requests a resource, it does
not hold any other processes.
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 21 / 1
Deadlock Prevention (2/3)
Mutual exclusion
• Not required for sharable resources, e.g., read-only files.
• Must hold for non-sharable resources.
Hold and wait
• Must guarantee that whenever a process requests a resource, it does
not hold any other processes.
• Solution 1: require a process to request and be allocated all its
resources before it begins execution.
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 21 / 1
Deadlock Prevention (2/3)
Mutual exclusion
• Not required for sharable resources, e.g., read-only files.
• Must hold for non-sharable resources.
Hold and wait
• Must guarantee that whenever a process requests a resource, it does
not hold any other processes.
• Solution 1: require a process to request and be allocated all its
resources before it begins execution.
• Solution 2: allows a process to request resources only when it has
none.
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 21 / 1
Deadlock Prevention (2/3)
Mutual exclusion
• Not required for sharable resources, e.g., read-only files.
• Must hold for non-sharable resources.
Hold and wait
• Must guarantee that whenever a process requests a resource, it does
not hold any other processes.
• Solution 1: require a process to request and be allocated all its
resources before it begins execution.
• Solution 2: allows a process to request resources only when it has
none.
• Low resource utilization
• Starvation possible
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 21 / 1
Deadlock Prevention (3/3)
No preemption
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 22 / 1
Deadlock Prevention (3/3)
No preemption
• If a process that is holding some resources, requests another
resource that cannot be immediately allocated to it, then all
resources currently being held are released.
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 22 / 1
Deadlock Prevention (3/3)
No preemption
• If a process that is holding some resources, requests another
resource that cannot be immediately allocated to it, then all
resources currently being held are released.
• Preempted resources are added to the list of resources for which the
process is waiting.
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 22 / 1
Deadlock Prevention (3/3)
No preemption
• If a process that is holding some resources, requests another
resource that cannot be immediately allocated to it, then all
resources currently being held are released.
• Preempted resources are added to the list of resources for which the
process is waiting.
• Process will be restarted only when it can regain its old resources,
as well as the new ones that it is requesting.
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 22 / 1
Deadlock Prevention (3/3)
No preemption
• If a process that is holding some resources, requests another
resource that cannot be immediately allocated to it, then all
resources currently being held are released.
• Preempted resources are added to the list of resources for which the
process is waiting.
• Process will be restarted only when it can regain its old resources,
as well as the new ones that it is requesting.
Circular wait
• Impose a total ordering of all resource types, and require that each
process requests resources in an increasing order of enumeration.
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 22 / 1
Deadlock Example with Lock Ordering
Lock ordering does not guarantee deadlock prevention if locks can
be acquired dynamically.
void transaction(Account from, Account to, double amount) {
mutex lock1, lock2;
lock1 = get_lock(from);
lock2 = get_lock(to);
acquire(lock1);
acquire(lock2);
withdraw(from, amount);
deposit(to, amount);
release(lock2);
release(lock1);
}
transaction(checking_account, savings_account, 25);
transaction(savings_account, checking_account, 50);
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 23 / 1
Deadlock Avoidance
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 24 / 1
Deadlock Avoidance
Requires that the system has some additional a priori information
available.
• The maximum number of resources of each type that it may need.
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 25 / 1
Deadlock Avoidance
Requires that the system has some additional a priori information
available.
• 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.
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 25 / 1
Deadlock Avoidance
Requires that the system has some additional a priori information
available.
• 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.
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 25 / 1
Safe State (1/2)
When a process requests an available resource, system must decide
if immediate allocation leaves the system in a safe state.
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 26 / 1
Safe State (1/2)
When a process requests an available resource, system must decide
if immediate allocation leaves the system in a safe state.
Safe state: there exists a sequence P1, P2, · · · , Pn of all the
processes in the systems such that for each Pi , the resources that
Pi can still request be satisfied by
currently available resources + resources held by all the Pj , with
j < i.
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 26 / 1
Safe State (2/2)
If Pi resource needs are not immediately available, then Pi can
wait until all Pj have finished.
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 27 / 1
Safe State (2/2)
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.
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 27 / 1
Safe State (2/2)
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.
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 27 / 1
Basic Facts
If a system is in the safe state
• No deadlock
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 28 / 1
Basic Facts
If a system is in the safe state
• No deadlock
If a system is in the unsafe state
• Possibility of deadlock
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 28 / 1
Basic Facts
If a system is in the safe state
• No deadlock
If a system is in the unsafe state
• Possibility of deadlock
Avoidance
• Ensure that a system will never enter an unsafe state.
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 28 / 1
Safe Mode Example (1/2)
3 processes: P0 through P2
1 resource type:
• A (12 instances)
Snapshot at time T0
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 29 / 1
Safe Mode Example (1/2)
3 processes: P0 through P2
1 resource type:
• A (12 instances)
Snapshot at time T0
Safe mode sequence?
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 29 / 1
Safe Mode Example (1/2)
3 processes: P0 through P2
1 resource type:
• A (12 instances)
Snapshot at time T0
Safe mode sequence? P1, P0, P2
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 29 / 1
Safe Mode Example (2/2)
3 processes: P0 through P2
1 resource type:
• A (12 instances)
Snapshot at time T0
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 30 / 1
Safe Mode Example (2/2)
3 processes: P0 through P2
1 resource type:
• A (12 instances)
Snapshot at time T0
Suppose that, at time T1, process P2 requests and is allocated one
more resource.
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 30 / 1
Safe Mode Example (2/2)
3 processes: P0 through P2
1 resource type:
• A (12 instances)
Snapshot at time T0
Suppose that, at time T1, process P2 requests and is allocated one
more resource.
Safe mode sequence?
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 30 / 1
Safe Mode Example (2/2)
3 processes: P0 through P2
1 resource type:
• A (12 instances)
Snapshot at time T0
Suppose that, at time T1, process P2 requests and is allocated one
more resource.
Safe mode sequence? Not safe
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 30 / 1
Avoidance Algorithms
Single instance of a resource type
• Use a resource-allocation graph
Multiple instances of a resource type
• Use the banker’s algorithm
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 31 / 1
Resource-Allocation Graph
Algorithm
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 32 / 1
Resource-Allocation Graph Scheme
Claim edge Pi → Rj : indicates that process Pj may request
resource Rj ; represented by a dashed line
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 33 / 1
Resource-Allocation Graph Scheme
Claim edge Pi → Rj : indicates that process Pj may request
resource Rj ; represented by a dashed line
Claim edge converts to request edge when a process requests a
resource.
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 33 / 1
Resource-Allocation Graph Scheme
Claim edge Pi → Rj : indicates 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.
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 33 / 1
Resource-Allocation Graph Scheme
Claim edge Pi → Rj : indicates 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.
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 33 / 1
Resource-Allocation Graph Scheme
Claim edge Pi → Rj : indicates 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.
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 33 / 1
Resource-Allocation Graph
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 34 / 1
Unsafe State In Resource-Allocation Graph
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 35 / 1
Resource-Allocation Graph Algorithm
Suppose that process Pi requests a resource Rj .
The request can be granted only if converting the request edge to
an assignment edge does not result in the formation of a cycle in
the resource allocation graph.
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 36 / 1
Banker’s Algorithm
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 37 / 1
Banker’s Algorithm
Multiple instances
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 38 / 1
Banker’s Algorithm
Multiple instances
Each process must a priori claim of the maximum use.
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 38 / 1
Banker’s Algorithm
Multiple instances
Each process must a priori claim of the maximum use.
When a process requests a resource it may have to wait.
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 38 / 1
Banker’s Algorithm
Multiple instances
Each process must a priori claim of the maximum use.
When a process requests a resource it may have to wait.
When a process gets all its resources, it must return them in a
finite amount of time.
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 38 / 1
Data Structures for Banker’s Algorithm
n = number of processes, and m = number of resources types
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 39 / 1
Data Structures for Banker’s Algorithm
n = number of processes, and m = number of resources types
Available: vector of length m.
• If Available[j] = k, there are k instances of resource type Rj
available.
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 39 / 1
Data Structures for Banker’s Algorithm
n = number of processes, and m = number of resources types
Available: vector of length m.
• If Available[j] = k, there are k instances of resource type Rj
available.
Max: n × m matrix.
• If Max[i, j] = k, then process Pi may request at most k instances of
resource type Rj .
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 39 / 1
Data Structures for Banker’s Algorithm
n = number of processes, and m = number of resources types
Available: vector of length m.
• If Available[j] = k, there are k instances of resource type Rj
available.
Max: n × m matrix.
• If Max[i, j] = k, then process Pi may request at most k instances of
resource type Rj .
Allocation: n × m matrix.
• If Allocation[i, j] = k then Pi is currently allocated k instances of
Rj .
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 39 / 1
Data Structures for Banker’s Algorithm
n = number of processes, and m = number of resources types
Available: vector of length m.
• If Available[j] = k, there are k instances of resource type Rj
available.
Max: n × m matrix.
• If Max[i, j] = k, then process Pi may request at most k instances of
resource type Rj .
Allocation: n × m matrix.
• If Allocation[i, j] = k then Pi is currently allocated k instances of
Rj .
Need: n × m matrix.
• If Need[i, j] = k, then Pi may need k more instances of Rj to
complete its task Need[i, j] = Max[i, j] − Allocation[i, j]
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 39 / 1
Safety Algorithm
1 Let Work and Finish be vectors of length m and n, respectively.
Initialize:
Work = Available
Finish[i] = false for i = 0, 1, · · · n − 1
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 40 / 1
Safety Algorithm
1 Let Work and Finish be vectors of length m and n, respectively.
Initialize:
Work = Available
Finish[i] = false for i = 0, 1, · · · n − 1
2 Find an i such that both:
1. Finish[i] = false
2. Needi ≤ Work
If no such i exists, go to step 4.
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 40 / 1
Safety Algorithm
1 Let Work and Finish be vectors of length m and n, respectively.
Initialize:
Work = Available
Finish[i] = false for i = 0, 1, · · · n − 1
2 Find an i such that both:
1. Finish[i] = false
2. Needi ≤ Work
If no such i exists, go to step 4.
3 Work = Work + Allocationi
Finish[i] = true
Go to step 2
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 40 / 1
Safety Algorithm
1 Let Work and Finish be vectors of length m and n, respectively.
Initialize:
Work = Available
Finish[i] = false for i = 0, 1, · · · n − 1
2 Find an i such that both:
1. Finish[i] = false
2. Needi ≤ Work
If no such i exists, go to step 4.
3 Work = Work + Allocationi
Finish[i] = true
Go to step 2
4 If Finish[i] == true for all i, then the system is in a safe state.
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 40 / 1
Resource-Request Algorithm for Process Pi (1/2)
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.
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 41 / 1
Resource-Request Algorithm for Process Pi (2/2)
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
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 42 / 1
Banker’s Algorithm Example (1/2)
5 processes: P0 through P4
3 resource types:
• A (10 instances), B (5 instances), and C (7 instances)
Snapshot at time T0
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 43 / 1
Banker’s Algorithm Example (2/2)
The content of the matrix Need is defined to be Max − Allocation
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 44 / 1
Banker’s Algorithm Example (2/2)
The content of the matrix Need is defined to be Max − Allocation
Is the system safe?
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 44 / 1
Banker’s Algorithm Example (2/2)
The content of the matrix Need is defined to be Max − Allocation
Is the system safe? P1, P3, P4, P2, P0 satisfies safety criteria.
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 44 / 1
Safety Algorithm Example
P1 Request (1, 0, 2)
Check that Request ≤ Available: (1, 0, 2) ≤ (3, 3, 2) ⇒ true
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?
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 45 / 1
Deadlock Detection
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 46 / 1
Deadlock Detection
Allow system to enter deadlock state
Detection algorithm
Recovery scheme
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 47 / 1
Single Instance of Each Resource Type
Maintain wait-for graph.
• Nodes are processes.
• Pi → Pj if Pi is waiting for Pj .
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 48 / 1
Single Instance of Each Resource Type
Maintain wait-for graph.
• Nodes are processes.
• Pi → Pj if Pi is waiting for Pj .
Periodically invoke an algorithm that searches for a cycle in the
graph.
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 48 / 1
Single Instance of Each Resource Type
Maintain wait-for graph.
• Nodes are processes.
• Pi → Pj if Pi is waiting for Pj .
Periodically invoke an algorithm that searches for a cycle in the
graph.
If there is a cycle, there exists a deadlock.
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 48 / 1
Single Instance of Each Resource Type
Maintain wait-for graph.
• Nodes are processes.
• Pi → Pj if Pi is waiting for Pj .
Periodically invoke an algorithm that searches for a cycle in the
graph.
If there is a cycle, there exists a deadlock.
An algorithm to detect a cycle in a graph requires an O(n2)
operations, where n is the number of vertices in the graph.
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 48 / 1
Resource-Allocation Graph and Wait-for Graph
Resource-allocation graph Corresponding Wait-for graph
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 49 / 1
Data Structures for Deadlock Detection
Available: vector of length m, indicates the number of available
resources of each type.
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 50 / 1
Data Structures for Deadlock Detection
Available: vector of length m, indicates the number of available
resources of each type.
Allocation: n × m matrix, defines the number of resources of each
type currently allocated to each process.
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 50 / 1
Data Structures for Deadlock Detection
Available: vector of length m, indicates the number of available
resources of each type.
Allocation: n × m matrix, defines the number of resources of each
type currently allocated to each process.
Request: n × m matrix, indicates the current request of each
process.
• If Request[i, j] = k, then Pi requesting k more instances of resource
type Rj .
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 50 / 1
Detection Algorithm (1/2)
1. Let Work and Finish be vectors of length m and n, respectively.
Initialize:
a. Work = Available
b. For i = 1, 2, · · · , n, if Allocationi = 0, then Finish[i] = false;
otherwise, Finish[i] = true
2. Find an index i such that both:
a. Finish[i] == false
b. Requesti ≤ Work
If no such i exists, go to step 4
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 51 / 1
Detection Algorithm (2/2)
3. Work = Work + Allocationi
Finish[i] = true
go to step 2
4. If Finish[i] == false, for some i, 1 ≤ i ≤ n, then the system is
in deadlock state. Moreover, if Finish[i] == false, then Pi is
deadlocked.
Algorithm requires an order of O(m × n2) operations to detect
whether the system is in deadlocked state.
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 52 / 1
Detection Algorithm Example (1/2)
5 processes: P0 through P4
3 resource types:
• A (7 instances), B (2 instances), and C (6 instances)
Snapshot at time T0
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 53 / 1
Detection Algorithm Example (1/2)
5 processes: P0 through P4
3 resource types:
• A (7 instances), B (2 instances), and C (6 instances)
Snapshot at time T0
Deadlock?
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 53 / 1
Detection Algorithm Example (1/2)
5 processes: P0 through P4
3 resource types:
• A (7 instances), B (2 instances), and C (6 instances)
Snapshot at time T0
Deadlock? Sequence P0, P2, P3, P1, P4 will result in
Finish[i] = true for all i
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 53 / 1
Detection Algorithm Example (2/2)
P2 requests an additional instance of type C
Can reclaim resources held by process P0, but insufficient resources
to fulfill other processes; requests
Deadlock exists, consisting of processes P1, P2, P3, and P4
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 54 / 1
Recovery From Deadlock
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 55 / 1
Recovery from Deadlock
Process termination
Resource preemption
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 56 / 1
Process Termination
Abort all deadlocked processes.
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 57 / 1
Process Termination
Abort all deadlocked processes.
Abort one process at a time until the deadlock cycle is eliminated
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 57 / 1
Process Termination
Abort all deadlocked processes.
Abort one process at a time until the deadlock cycle is eliminated
In which order should we choose to abort?
1 Priority of the process.
2 How long process has computed, and how much longer to
completion.
3 Resources the process has used.
4 Resources process needs to complete.
5 How many processes will need to be terminated.
6 Is process interactive or batch.
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 57 / 1
Resource Preemption
Selecting a victim: minimize cost
Rollback: return to some safe state, restart process for that state.
Starvation: same process may always be picked as victim, include
number of rollback in cost factor.
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 58 / 1
Summary
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 59 / 1
Summary
Deadlock
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 60 / 1
Summary
Deadlock
Four simultaneous conditions: mutual exclusion, hold and wait, no
preemption, circular wait
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 60 / 1
Summary
Deadlock
Four simultaneous conditions: mutual exclusion, hold and wait, no
preemption, circular wait
Deadlock prevention:
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 60 / 1
Summary
Deadlock
Four simultaneous conditions: mutual exclusion, hold and wait, no
preemption, circular wait
Deadlock prevention:
Deadlock avoidance: resource-allocation algorithm, banker’s algo-
rithm
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 60 / 1
Summary
Deadlock
Four simultaneous conditions: mutual exclusion, hold and wait, no
preemption, circular wait
Deadlock prevention:
Deadlock avoidance: resource-allocation algorithm, banker’s algo-
rithm
Deadlock detection: Wait-for graph
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 60 / 1
Summary
Deadlock
Four simultaneous conditions: mutual exclusion, hold and wait, no
preemption, circular wait
Deadlock prevention:
Deadlock avoidance: resource-allocation algorithm, banker’s algo-
rithm
Deadlock detection: Wait-for graph
Deadlock recovery: process termination, resource preemption
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 60 / 1
Questions?
Acknowledgements
Some slides were derived from Avi Silberschatz slides.
Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 61 / 1

More Related Content

What's hot

Process Management - Part1
Process Management - Part1Process Management - Part1
Process Management - Part1Amir Payberah
 
Introduction to Operating Systems - Part2
Introduction to Operating Systems - Part2Introduction to Operating Systems - Part2
Introduction to Operating Systems - Part2Amir Payberah
 
Process Management - Part3
Process Management - Part3Process Management - Part3
Process Management - Part3Amir Payberah
 
Process Synchronization - Part2
Process Synchronization -  Part2Process Synchronization -  Part2
Process Synchronization - Part2Amir Payberah
 
Process Management - Part2
Process Management - Part2Process Management - Part2
Process Management - Part2Amir Payberah
 
CPU Scheduling - Part1
CPU Scheduling - Part1CPU Scheduling - Part1
CPU Scheduling - Part1Amir Payberah
 
Introduction to Operating Systems - Part1
Introduction to Operating Systems - Part1Introduction to Operating Systems - Part1
Introduction to Operating Systems - Part1Amir Payberah
 
Operating Systems 1 (11/12) - Input / Output
Operating Systems 1 (11/12) - Input / OutputOperating Systems 1 (11/12) - Input / Output
Operating Systems 1 (11/12) - Input / OutputPeter Tröger
 
Pipelinig hazardous
Pipelinig hazardousPipelinig hazardous
Pipelinig hazardousjasscheema
 
Chapter 13 - I/O Systems
Chapter 13 - I/O SystemsChapter 13 - I/O Systems
Chapter 13 - I/O SystemsWayne Jones Jnr
 
Input output systems ppt - cs2411
Input output systems ppt - cs2411Input output systems ppt - cs2411
Input output systems ppt - cs2411Geerthik Varun
 
Ch05 cpu-scheduling
Ch05 cpu-schedulingCh05 cpu-scheduling
Ch05 cpu-schedulingNazir Ahmed
 
DEVICE FILE AND INPUT OUTPUT SUBSYSTEMS MANAGEMENT
DEVICE FILE AND INPUT OUTPUT SUBSYSTEMS MANAGEMENTDEVICE FILE AND INPUT OUTPUT SUBSYSTEMS MANAGEMENT
DEVICE FILE AND INPUT OUTPUT SUBSYSTEMS MANAGEMENTJOLLUSUDARSHANREDDY
 

What's hot (20)

Process Management - Part1
Process Management - Part1Process Management - Part1
Process Management - Part1
 
Introduction to Operating Systems - Part2
Introduction to Operating Systems - Part2Introduction to Operating Systems - Part2
Introduction to Operating Systems - Part2
 
Threads
ThreadsThreads
Threads
 
Process Management - Part3
Process Management - Part3Process Management - Part3
Process Management - Part3
 
Process Synchronization - Part2
Process Synchronization -  Part2Process Synchronization -  Part2
Process Synchronization - Part2
 
Process Management - Part2
Process Management - Part2Process Management - Part2
Process Management - Part2
 
CPU Scheduling - Part1
CPU Scheduling - Part1CPU Scheduling - Part1
CPU Scheduling - Part1
 
Cloud Computing
Cloud ComputingCloud Computing
Cloud Computing
 
Introduction to Operating Systems - Part1
Introduction to Operating Systems - Part1Introduction to Operating Systems - Part1
Introduction to Operating Systems - Part1
 
main memory
main memorymain memory
main memory
 
OS tutoring #1
OS tutoring #1OS tutoring #1
OS tutoring #1
 
IO Management
IO ManagementIO Management
IO Management
 
Operating Systems 1 (11/12) - Input / Output
Operating Systems 1 (11/12) - Input / OutputOperating Systems 1 (11/12) - Input / Output
Operating Systems 1 (11/12) - Input / Output
 
Pipelinig hazardous
Pipelinig hazardousPipelinig hazardous
Pipelinig hazardous
 
Chapter 13 - I/O Systems
Chapter 13 - I/O SystemsChapter 13 - I/O Systems
Chapter 13 - I/O Systems
 
Input output systems ppt - cs2411
Input output systems ppt - cs2411Input output systems ppt - cs2411
Input output systems ppt - cs2411
 
Ch05 cpu-scheduling
Ch05 cpu-schedulingCh05 cpu-scheduling
Ch05 cpu-scheduling
 
CPU Scheduling
CPU SchedulingCPU Scheduling
CPU Scheduling
 
Lec 2
Lec 2Lec 2
Lec 2
 
DEVICE FILE AND INPUT OUTPUT SUBSYSTEMS MANAGEMENT
DEVICE FILE AND INPUT OUTPUT SUBSYSTEMS MANAGEMENTDEVICE FILE AND INPUT OUTPUT SUBSYSTEMS MANAGEMENT
DEVICE FILE AND INPUT OUTPUT SUBSYSTEMS MANAGEMENT
 

Viewers also liked

P2P Content Distribution Network
P2P Content Distribution NetworkP2P Content Distribution Network
P2P Content Distribution NetworkAmir Payberah
 
Process Synchronization - Part1
Process Synchronization -  Part1Process Synchronization -  Part1
Process Synchronization - Part1Amir Payberah
 
The Stratosphere Big Data Analytics Platform
The Stratosphere Big Data Analytics PlatformThe Stratosphere Big Data Analytics Platform
The Stratosphere Big Data Analytics PlatformAmir Payberah
 
Data Intensive Computing Frameworks
Data Intensive Computing FrameworksData Intensive Computing Frameworks
Data Intensive Computing FrameworksAmir Payberah
 
File System Interface
File System InterfaceFile System Interface
File System InterfaceAmir Payberah
 
Virtual Memory - Part1
Virtual Memory - Part1Virtual Memory - Part1
Virtual Memory - Part1Amir Payberah
 
File System Implementation - Part1
File System Implementation - Part1File System Implementation - Part1
File System Implementation - Part1Amir Payberah
 
File System Implementation - Part2
File System Implementation - Part2File System Implementation - Part2
File System Implementation - Part2Amir Payberah
 
Spark Stream and SEEP
Spark Stream and SEEPSpark Stream and SEEP
Spark Stream and SEEPAmir Payberah
 
Linux Module Programming
Linux Module ProgrammingLinux Module Programming
Linux Module ProgrammingAmir Payberah
 
MegaStore and Spanner
MegaStore and SpannerMegaStore and Spanner
MegaStore and SpannerAmir Payberah
 

Viewers also liked (13)

P2P Content Distribution Network
P2P Content Distribution NetworkP2P Content Distribution Network
P2P Content Distribution Network
 
Process Synchronization - Part1
Process Synchronization -  Part1Process Synchronization -  Part1
Process Synchronization - Part1
 
Storage
StorageStorage
Storage
 
The Stratosphere Big Data Analytics Platform
The Stratosphere Big Data Analytics PlatformThe Stratosphere Big Data Analytics Platform
The Stratosphere Big Data Analytics Platform
 
Data Intensive Computing Frameworks
Data Intensive Computing FrameworksData Intensive Computing Frameworks
Data Intensive Computing Frameworks
 
File System Interface
File System InterfaceFile System Interface
File System Interface
 
Virtual Memory - Part1
Virtual Memory - Part1Virtual Memory - Part1
Virtual Memory - Part1
 
File System Implementation - Part1
File System Implementation - Part1File System Implementation - Part1
File System Implementation - Part1
 
File System Implementation - Part2
File System Implementation - Part2File System Implementation - Part2
File System Implementation - Part2
 
Spark Stream and SEEP
Spark Stream and SEEPSpark Stream and SEEP
Spark Stream and SEEP
 
MapReduce
MapReduceMapReduce
MapReduce
 
Linux Module Programming
Linux Module ProgrammingLinux Module Programming
Linux Module Programming
 
MegaStore and Spanner
MegaStore and SpannerMegaStore and Spanner
MegaStore and Spanner
 

Similar to Deadlocks

Introduction to stream processing
Introduction to stream processingIntroduction to stream processing
Introduction to stream processingAmir Payberah
 
Gossip-based algorithms
Gossip-based algorithmsGossip-based algorithms
Gossip-based algorithmsAmir Payberah
 
Time, clocks and the ordering of events
Time, clocks and the ordering of eventsTime, clocks and the ordering of events
Time, clocks and the ordering of eventsAmir Payberah
 
Deadlock- System model, resource types, deadlock problem, deadlock characteri...
Deadlock- System model, resource types, deadlock problem, deadlock characteri...Deadlock- System model, resource types, deadlock problem, deadlock characteri...
Deadlock- System model, resource types, deadlock problem, deadlock characteri...Wakil Kumar
 
Deadlock in Operating System
Deadlock in Operating SystemDeadlock in Operating System
Deadlock in Operating SystemAUST
 
Mch7 deadlock
Mch7 deadlockMch7 deadlock
Mch7 deadlockwahab13
 
Module 3 Deadlocks.pptx
Module 3 Deadlocks.pptxModule 3 Deadlocks.pptx
Module 3 Deadlocks.pptxshreesha16
 
6. Deadlock_1640227623705.pptx
6. Deadlock_1640227623705.pptx6. Deadlock_1640227623705.pptx
6. Deadlock_1640227623705.pptxshrijanasharma3
 
7308346-Deadlock.pptx
7308346-Deadlock.pptx7308346-Deadlock.pptx
7308346-Deadlock.pptxsheraz7288
 
Graph processing - Pregel
Graph processing - PregelGraph processing - Pregel
Graph processing - PregelAmir Payberah
 
Process Synchronization And Deadlocks
Process Synchronization And DeadlocksProcess Synchronization And Deadlocks
Process Synchronization And Deadlockstech2click
 

Similar to Deadlocks (20)

Chubby
ChubbyChubby
Chubby
 
Introduction to stream processing
Introduction to stream processingIntroduction to stream processing
Introduction to stream processing
 
Paxos
PaxosPaxos
Paxos
 
Gossip-based algorithms
Gossip-based algorithmsGossip-based algorithms
Gossip-based algorithms
 
Time, clocks and the ordering of events
Time, clocks and the ordering of eventsTime, clocks and the ordering of events
Time, clocks and the ordering of events
 
Chapter 4
Chapter 4Chapter 4
Chapter 4
 
Os module 2 d
Os module 2 dOs module 2 d
Os module 2 d
 
Deadlock- System model, resource types, deadlock problem, deadlock characteri...
Deadlock- System model, resource types, deadlock problem, deadlock characteri...Deadlock- System model, resource types, deadlock problem, deadlock characteri...
Deadlock- System model, resource types, deadlock problem, deadlock characteri...
 
Deadlock in Operating System
Deadlock in Operating SystemDeadlock in Operating System
Deadlock in Operating System
 
Mch7 deadlock
Mch7 deadlockMch7 deadlock
Mch7 deadlock
 
Mesos and YARN
Mesos and YARNMesos and YARN
Mesos and YARN
 
Module 3 Deadlocks.pptx
Module 3 Deadlocks.pptxModule 3 Deadlocks.pptx
Module 3 Deadlocks.pptx
 
6. Deadlock_1640227623705.pptx
6. Deadlock_1640227623705.pptx6. Deadlock_1640227623705.pptx
6. Deadlock_1640227623705.pptx
 
Deadlocks
DeadlocksDeadlocks
Deadlocks
 
deadlock in OS.pptx
deadlock in OS.pptxdeadlock in OS.pptx
deadlock in OS.pptx
 
7308346-Deadlock.pptx
7308346-Deadlock.pptx7308346-Deadlock.pptx
7308346-Deadlock.pptx
 
Graph processing - Pregel
Graph processing - PregelGraph processing - Pregel
Graph processing - Pregel
 
Process Synchronization And Deadlocks
Process Synchronization And DeadlocksProcess Synchronization And Deadlocks
Process Synchronization And Deadlocks
 
Deadlock
DeadlockDeadlock
Deadlock
 
Deadlock
DeadlockDeadlock
Deadlock
 

Recently uploaded

A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxComplianceQuest1
 
Introduction to Decentralized Applications (dApps)
Introduction to Decentralized Applications (dApps)Introduction to Decentralized Applications (dApps)
Introduction to Decentralized Applications (dApps)Intelisync
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdfWave PLM
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number SystemsJheuzeDellosa
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideChristina Lin
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...harshavardhanraghave
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...Christina Lin
 
Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...aditisharan08
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsAlberto González Trastoy
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptkotipi9215
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityNeo4j
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)OPEN KNOWLEDGE GmbH
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Modelsaagamshah0812
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationkaushalgiri8080
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxbodapatigopi8531
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfjoe51371421
 

Recently uploaded (20)

A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
Introduction to Decentralized Applications (dApps)
Introduction to Decentralized Applications (dApps)Introduction to Decentralized Applications (dApps)
Introduction to Decentralized Applications (dApps)
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number Systems
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
 
Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.ppt
 
Exploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the ProcessExploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the Process
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered Sustainability
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanation
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdf
 

Deadlocks

  • 1. Deadlocks Amir H. Payberah amir@sics.se Amirkabir University of Technology (Tehran Polytechnic) Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 1 / 1
  • 2. Motivation Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 2 / 1
  • 3. Motivation Multiprogramming environment: several processes compete for a finite number of resources. Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 3 / 1
  • 4. Motivation Multiprogramming environment: several processes compete for a finite number of resources. A process requests resources: if the resources are not available at that time, the process enters a waiting state. Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 3 / 1
  • 5. Motivation Multiprogramming environment: several processes compete for a finite number of resources. A process requests resources: if the resources are not available at that time, the process enters a waiting state. What if the requests resources are held by other waiting processes? Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 3 / 1
  • 6. Motivation Multiprogramming environment: several processes compete for a finite number of resources. A process requests resources: if the resources are not available at that time, the process enters a waiting state. What if the requests resources are held by other waiting processes? This situation is called a deadlock. Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 3 / 1
  • 7. Deadlocks Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 4 / 1
  • 8. System Model System consists of resources: R1, R2, · · · , Rm Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 5 / 1
  • 9. System Model System consists of resources: R1, R2, · · · , Rm Resource types: CPU cycles, memory space, I/O devices Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 5 / 1
  • 10. System Model System consists of resources: R1, R2, · · · , Rm Resource types: CPU cycles, memory space, I/O devices Each resource type Ri has Wi instances. Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 5 / 1
  • 11. System Model System consists of resources: R1, R2, · · · , Rm Resource types: CPU cycles, memory space, I/O devices Each resource type Ri has Wi instances. Each process utilizes a resource as follows: • Request • Use • Release Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 5 / 1
  • 12. Deadlock Characterization (1/3) Deadlock can arise if four conditions hold simultaneously: • Mutual exclusion • Hold and wait • No preemption • Circular wait Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 6 / 1
  • 13. Deadlock Characterization (2/3) Mutual exclusion • Only one process at a time can use a resource. Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 7 / 1
  • 14. Deadlock Characterization (2/3) 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. Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 7 / 1
  • 15. Deadlock Characterization (3/3) No preemption • A resource can be released only voluntarily by the process holding it, after that process has completed its task. Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 8 / 1
  • 16. Deadlock Characterization (3/3) No preemption • A resource can be released only voluntarily by the process holding it, after that process has completed its task. Circular wait • A set processes: {P0, P1, · · · , Pn} • P0 is waiting for a resource that is held by P1 • P1 is waiting for a resource that is held by P2 • ... • Pn is waiting for a resource that is held by P0 Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 8 / 1
  • 17. Deadlock Example (1/2) /* Create and initialize the mutex locks */ pthread_mutex_t first_mutex; pthread_mutex_t second_mutex; pthread_mutex_init(&first_mutex, NULL); pthread_mutex_init(&second_mutex, NULL); Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 9 / 1
  • 18. Deadlock Example (2/2) /* thread one runs in this function */ void *do_work_one(void *param) { pthread_mutex_lock(&first mutex); pthread_mutex_lock(&second mutex); // do some work pthread_mutex_unlock(&second mutex); pthread_mutex_unlock(&first mutex); pthread_exit(0); } /* thread two runs in this function */ void *do_work_two(void *param) { pthread_mutex_lock(&second mutex); pthread_mutex_lock(&first mutex); // do some work pthread_mutex_unlock(&first mutex); pthread_mutex_unlock(&second mutex); pthread_exit(0); } Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 10 / 1
  • 19. Resource-Allocation Graph Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 11 / 1
  • 20. Resource-Allocation Graph (1/2) A set of vertices V and a set of edges E. Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 12 / 1
  • 21. Resource-Allocation Graph (1/2) A set of vertices V and a set of edges E. Vertices • All the processes in the system: P = P1, P2, · · · , Pn • All resource types in the system: R = R1, R2, · · · , Rm Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 12 / 1
  • 22. Resource-Allocation Graph (1/2) A set of vertices V and a set of edges E. Vertices • All the processes in the system: P = P1, P2, · · · , Pn • All resource types in the system: R = R1, R2, · · · , Rm Edges • Request edge: directed edge Pi → Rj • Assignment edge: directed edge Rj → Pi Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 12 / 1
  • 23. Resource-Allocation Graph (2/2) Process (vertices) Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 13 / 1
  • 24. Resource-Allocation Graph (2/2) Process (vertices) Resource type with 4 instances (vertices) Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 13 / 1
  • 25. Resource-Allocation Graph (2/2) Process (vertices) Resource type with 4 instances (vertices) Pi requests instance of Rj (edge) Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 13 / 1
  • 26. Resource-Allocation Graph (2/2) Process (vertices) Resource type with 4 instances (vertices) Pi requests instance of Rj (edge) Pi is holding an instance of Rj (edge) Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 13 / 1
  • 27. Resource-Allocation Graph Example (1/3) Example of a resource allocation graph. Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 14 / 1
  • 28. Resource-Allocation Graph Example (2/3) Resource allocation graph with a deadlock. Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 15 / 1
  • 29. Resource-Allocation Graph Example (3/3) Resource allocation graph with a cycle but no deadlock. Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 16 / 1
  • 30. Basic Facts If graph contains no cycles • No deadlock Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 17 / 1
  • 31. Basic Facts If graph contains no cycles • No deadlock If graph contains a cycle • If only one instance per resource type, then deadlock. • If several instances per resource type, possibility of deadlock. Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 17 / 1
  • 32. Methods for Handling Deadlocks Ensure that the system will never enter a deadlock state: • Deadlock prevention • Deadlock avoidance Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 18 / 1
  • 33. Methods for Handling Deadlocks Ensure that the system will never enter a deadlock state: • Deadlock prevention • Deadlock avoidance Allow the system to enter a deadlock state and then recover. Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 18 / 1
  • 34. Methods for Handling Deadlocks Ensure that the system will never enter a deadlock state: • Deadlock prevention • Deadlock avoidance Allow the system to enter a deadlock state and then recover. Ignore the problem and pretend that deadlocks never occur in the system; used by most operating systems. Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 18 / 1
  • 35. Deadlock Prevention Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 19 / 1
  • 36. Deadlock Prevention (1/3) Deadlock can arise if four conditions hold simultaneously: • Mutual exclusion • Hold and wait • No preemption • Circular wait Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 20 / 1
  • 37. Deadlock Prevention (1/3) Deadlock can arise if four conditions hold simultaneously: • Mutual exclusion • Hold and wait • No preemption • Circular wait Restrain the ways request can be made. Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 20 / 1
  • 38. Deadlock Prevention (2/3) Mutual exclusion • Not required for sharable resources, e.g., read-only files. • Must hold for non-sharable resources. Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 21 / 1
  • 39. Deadlock Prevention (2/3) Mutual exclusion • Not required for sharable resources, e.g., read-only files. • Must hold for non-sharable resources. Hold and wait • Must guarantee that whenever a process requests a resource, it does not hold any other processes. Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 21 / 1
  • 40. Deadlock Prevention (2/3) Mutual exclusion • Not required for sharable resources, e.g., read-only files. • Must hold for non-sharable resources. Hold and wait • Must guarantee that whenever a process requests a resource, it does not hold any other processes. • Solution 1: require a process to request and be allocated all its resources before it begins execution. Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 21 / 1
  • 41. Deadlock Prevention (2/3) Mutual exclusion • Not required for sharable resources, e.g., read-only files. • Must hold for non-sharable resources. Hold and wait • Must guarantee that whenever a process requests a resource, it does not hold any other processes. • Solution 1: require a process to request and be allocated all its resources before it begins execution. • Solution 2: allows a process to request resources only when it has none. Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 21 / 1
  • 42. Deadlock Prevention (2/3) Mutual exclusion • Not required for sharable resources, e.g., read-only files. • Must hold for non-sharable resources. Hold and wait • Must guarantee that whenever a process requests a resource, it does not hold any other processes. • Solution 1: require a process to request and be allocated all its resources before it begins execution. • Solution 2: allows a process to request resources only when it has none. • Low resource utilization • Starvation possible Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 21 / 1
  • 43. Deadlock Prevention (3/3) No preemption Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 22 / 1
  • 44. Deadlock Prevention (3/3) No preemption • If a process that is holding some resources, requests another resource that cannot be immediately allocated to it, then all resources currently being held are released. Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 22 / 1
  • 45. Deadlock Prevention (3/3) No preemption • If a process that is holding some resources, requests another resource that cannot be immediately allocated to it, then all resources currently being held are released. • Preempted resources are added to the list of resources for which the process is waiting. Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 22 / 1
  • 46. Deadlock Prevention (3/3) No preemption • If a process that is holding some resources, requests another resource that cannot be immediately allocated to it, then all resources currently being held are released. • Preempted resources are added to the list of resources for which the process is waiting. • Process will be restarted only when it can regain its old resources, as well as the new ones that it is requesting. Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 22 / 1
  • 47. Deadlock Prevention (3/3) No preemption • If a process that is holding some resources, requests another resource that cannot be immediately allocated to it, then all resources currently being held are released. • Preempted resources are added to the list of resources for which the process is waiting. • Process will be restarted only when it can regain its old resources, as well as the new ones that it is requesting. Circular wait • Impose a total ordering of all resource types, and require that each process requests resources in an increasing order of enumeration. Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 22 / 1
  • 48. Deadlock Example with Lock Ordering Lock ordering does not guarantee deadlock prevention if locks can be acquired dynamically. void transaction(Account from, Account to, double amount) { mutex lock1, lock2; lock1 = get_lock(from); lock2 = get_lock(to); acquire(lock1); acquire(lock2); withdraw(from, amount); deposit(to, amount); release(lock2); release(lock1); } transaction(checking_account, savings_account, 25); transaction(savings_account, checking_account, 50); Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 23 / 1
  • 49. Deadlock Avoidance Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 24 / 1
  • 50. Deadlock Avoidance Requires that the system has some additional a priori information available. • The maximum number of resources of each type that it may need. Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 25 / 1
  • 51. Deadlock Avoidance Requires that the system has some additional a priori information available. • 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. Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 25 / 1
  • 52. Deadlock Avoidance Requires that the system has some additional a priori information available. • 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. Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 25 / 1
  • 53. Safe State (1/2) When a process requests an available resource, system must decide if immediate allocation leaves the system in a safe state. Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 26 / 1
  • 54. Safe State (1/2) When a process requests an available resource, system must decide if immediate allocation leaves the system in a safe state. Safe state: there exists a sequence P1, P2, · · · , Pn of all the processes in the systems such that for each Pi , the resources that Pi can still request be satisfied by currently available resources + resources held by all the Pj , with j < i. Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 26 / 1
  • 55. Safe State (2/2) If Pi resource needs are not immediately available, then Pi can wait until all Pj have finished. Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 27 / 1
  • 56. Safe State (2/2) 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. Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 27 / 1
  • 57. Safe State (2/2) 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. Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 27 / 1
  • 58. Basic Facts If a system is in the safe state • No deadlock Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 28 / 1
  • 59. Basic Facts If a system is in the safe state • No deadlock If a system is in the unsafe state • Possibility of deadlock Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 28 / 1
  • 60. Basic Facts If a system is in the safe state • No deadlock If a system is in the unsafe state • Possibility of deadlock Avoidance • Ensure that a system will never enter an unsafe state. Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 28 / 1
  • 61. Safe Mode Example (1/2) 3 processes: P0 through P2 1 resource type: • A (12 instances) Snapshot at time T0 Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 29 / 1
  • 62. Safe Mode Example (1/2) 3 processes: P0 through P2 1 resource type: • A (12 instances) Snapshot at time T0 Safe mode sequence? Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 29 / 1
  • 63. Safe Mode Example (1/2) 3 processes: P0 through P2 1 resource type: • A (12 instances) Snapshot at time T0 Safe mode sequence? P1, P0, P2 Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 29 / 1
  • 64. Safe Mode Example (2/2) 3 processes: P0 through P2 1 resource type: • A (12 instances) Snapshot at time T0 Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 30 / 1
  • 65. Safe Mode Example (2/2) 3 processes: P0 through P2 1 resource type: • A (12 instances) Snapshot at time T0 Suppose that, at time T1, process P2 requests and is allocated one more resource. Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 30 / 1
  • 66. Safe Mode Example (2/2) 3 processes: P0 through P2 1 resource type: • A (12 instances) Snapshot at time T0 Suppose that, at time T1, process P2 requests and is allocated one more resource. Safe mode sequence? Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 30 / 1
  • 67. Safe Mode Example (2/2) 3 processes: P0 through P2 1 resource type: • A (12 instances) Snapshot at time T0 Suppose that, at time T1, process P2 requests and is allocated one more resource. Safe mode sequence? Not safe Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 30 / 1
  • 68. Avoidance Algorithms Single instance of a resource type • Use a resource-allocation graph Multiple instances of a resource type • Use the banker’s algorithm Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 31 / 1
  • 69. Resource-Allocation Graph Algorithm Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 32 / 1
  • 70. Resource-Allocation Graph Scheme Claim edge Pi → Rj : indicates that process Pj may request resource Rj ; represented by a dashed line Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 33 / 1
  • 71. Resource-Allocation Graph Scheme Claim edge Pi → Rj : indicates that process Pj may request resource Rj ; represented by a dashed line Claim edge converts to request edge when a process requests a resource. Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 33 / 1
  • 72. Resource-Allocation Graph Scheme Claim edge Pi → Rj : indicates 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. Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 33 / 1
  • 73. Resource-Allocation Graph Scheme Claim edge Pi → Rj : indicates 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. Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 33 / 1
  • 74. Resource-Allocation Graph Scheme Claim edge Pi → Rj : indicates 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. Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 33 / 1
  • 75. Resource-Allocation Graph Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 34 / 1
  • 76. Unsafe State In Resource-Allocation Graph Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 35 / 1
  • 77. Resource-Allocation Graph Algorithm Suppose that process Pi requests a resource Rj . The request can be granted only if converting the request edge to an assignment edge does not result in the formation of a cycle in the resource allocation graph. Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 36 / 1
  • 78. Banker’s Algorithm Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 37 / 1
  • 79. Banker’s Algorithm Multiple instances Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 38 / 1
  • 80. Banker’s Algorithm Multiple instances Each process must a priori claim of the maximum use. Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 38 / 1
  • 81. Banker’s Algorithm Multiple instances Each process must a priori claim of the maximum use. When a process requests a resource it may have to wait. Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 38 / 1
  • 82. Banker’s Algorithm Multiple instances Each process must a priori claim of the maximum use. When a process requests a resource it may have to wait. When a process gets all its resources, it must return them in a finite amount of time. Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 38 / 1
  • 83. Data Structures for Banker’s Algorithm n = number of processes, and m = number of resources types Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 39 / 1
  • 84. Data Structures for Banker’s Algorithm n = number of processes, and m = number of resources types Available: vector of length m. • If Available[j] = k, there are k instances of resource type Rj available. Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 39 / 1
  • 85. Data Structures for Banker’s Algorithm n = number of processes, and m = number of resources types Available: vector of length m. • If Available[j] = k, there are k instances of resource type Rj available. Max: n × m matrix. • If Max[i, j] = k, then process Pi may request at most k instances of resource type Rj . Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 39 / 1
  • 86. Data Structures for Banker’s Algorithm n = number of processes, and m = number of resources types Available: vector of length m. • If Available[j] = k, there are k instances of resource type Rj available. Max: n × m matrix. • If Max[i, j] = k, then process Pi may request at most k instances of resource type Rj . Allocation: n × m matrix. • If Allocation[i, j] = k then Pi is currently allocated k instances of Rj . Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 39 / 1
  • 87. Data Structures for Banker’s Algorithm n = number of processes, and m = number of resources types Available: vector of length m. • If Available[j] = k, there are k instances of resource type Rj available. Max: n × m matrix. • If Max[i, j] = k, then process Pi may request at most k instances of resource type Rj . Allocation: n × m matrix. • If Allocation[i, j] = k then Pi is currently allocated k instances of Rj . Need: n × m matrix. • If Need[i, j] = k, then Pi may need k more instances of Rj to complete its task Need[i, j] = Max[i, j] − Allocation[i, j] Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 39 / 1
  • 88. Safety Algorithm 1 Let Work and Finish be vectors of length m and n, respectively. Initialize: Work = Available Finish[i] = false for i = 0, 1, · · · n − 1 Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 40 / 1
  • 89. Safety Algorithm 1 Let Work and Finish be vectors of length m and n, respectively. Initialize: Work = Available Finish[i] = false for i = 0, 1, · · · n − 1 2 Find an i such that both: 1. Finish[i] = false 2. Needi ≤ Work If no such i exists, go to step 4. Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 40 / 1
  • 90. Safety Algorithm 1 Let Work and Finish be vectors of length m and n, respectively. Initialize: Work = Available Finish[i] = false for i = 0, 1, · · · n − 1 2 Find an i such that both: 1. Finish[i] = false 2. Needi ≤ Work If no such i exists, go to step 4. 3 Work = Work + Allocationi Finish[i] = true Go to step 2 Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 40 / 1
  • 91. Safety Algorithm 1 Let Work and Finish be vectors of length m and n, respectively. Initialize: Work = Available Finish[i] = false for i = 0, 1, · · · n − 1 2 Find an i such that both: 1. Finish[i] = false 2. Needi ≤ Work If no such i exists, go to step 4. 3 Work = Work + Allocationi Finish[i] = true Go to step 2 4 If Finish[i] == true for all i, then the system is in a safe state. Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 40 / 1
  • 92. Resource-Request Algorithm for Process Pi (1/2) 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. Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 41 / 1
  • 93. Resource-Request Algorithm for Process Pi (2/2) 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 Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 42 / 1
  • 94. Banker’s Algorithm Example (1/2) 5 processes: P0 through P4 3 resource types: • A (10 instances), B (5 instances), and C (7 instances) Snapshot at time T0 Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 43 / 1
  • 95. Banker’s Algorithm Example (2/2) The content of the matrix Need is defined to be Max − Allocation Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 44 / 1
  • 96. Banker’s Algorithm Example (2/2) The content of the matrix Need is defined to be Max − Allocation Is the system safe? Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 44 / 1
  • 97. Banker’s Algorithm Example (2/2) The content of the matrix Need is defined to be Max − Allocation Is the system safe? P1, P3, P4, P2, P0 satisfies safety criteria. Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 44 / 1
  • 98. Safety Algorithm Example P1 Request (1, 0, 2) Check that Request ≤ Available: (1, 0, 2) ≤ (3, 3, 2) ⇒ true 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? Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 45 / 1
  • 99. Deadlock Detection Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 46 / 1
  • 100. Deadlock Detection Allow system to enter deadlock state Detection algorithm Recovery scheme Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 47 / 1
  • 101. Single Instance of Each Resource Type Maintain wait-for graph. • Nodes are processes. • Pi → Pj if Pi is waiting for Pj . Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 48 / 1
  • 102. Single Instance of Each Resource Type Maintain wait-for graph. • Nodes are processes. • Pi → Pj if Pi is waiting for Pj . Periodically invoke an algorithm that searches for a cycle in the graph. Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 48 / 1
  • 103. Single Instance of Each Resource Type Maintain wait-for graph. • Nodes are processes. • Pi → Pj if Pi is waiting for Pj . Periodically invoke an algorithm that searches for a cycle in the graph. If there is a cycle, there exists a deadlock. Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 48 / 1
  • 104. Single Instance of Each Resource Type Maintain wait-for graph. • Nodes are processes. • Pi → Pj if Pi is waiting for Pj . Periodically invoke an algorithm that searches for a cycle in the graph. If there is a cycle, there exists a deadlock. An algorithm to detect a cycle in a graph requires an O(n2) operations, where n is the number of vertices in the graph. Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 48 / 1
  • 105. Resource-Allocation Graph and Wait-for Graph Resource-allocation graph Corresponding Wait-for graph Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 49 / 1
  • 106. Data Structures for Deadlock Detection Available: vector of length m, indicates the number of available resources of each type. Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 50 / 1
  • 107. Data Structures for Deadlock Detection Available: vector of length m, indicates the number of available resources of each type. Allocation: n × m matrix, defines the number of resources of each type currently allocated to each process. Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 50 / 1
  • 108. Data Structures for Deadlock Detection Available: vector of length m, indicates the number of available resources of each type. Allocation: n × m matrix, defines the number of resources of each type currently allocated to each process. Request: n × m matrix, indicates the current request of each process. • If Request[i, j] = k, then Pi requesting k more instances of resource type Rj . Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 50 / 1
  • 109. Detection Algorithm (1/2) 1. Let Work and Finish be vectors of length m and n, respectively. Initialize: a. Work = Available b. For i = 1, 2, · · · , n, if Allocationi = 0, then Finish[i] = false; otherwise, Finish[i] = true 2. Find an index i such that both: a. Finish[i] == false b. Requesti ≤ Work If no such i exists, go to step 4 Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 51 / 1
  • 110. Detection Algorithm (2/2) 3. Work = Work + Allocationi Finish[i] = true go to step 2 4. If Finish[i] == false, for some i, 1 ≤ i ≤ n, then the system is in deadlock state. Moreover, if Finish[i] == false, then Pi is deadlocked. Algorithm requires an order of O(m × n2) operations to detect whether the system is in deadlocked state. Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 52 / 1
  • 111. Detection Algorithm Example (1/2) 5 processes: P0 through P4 3 resource types: • A (7 instances), B (2 instances), and C (6 instances) Snapshot at time T0 Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 53 / 1
  • 112. Detection Algorithm Example (1/2) 5 processes: P0 through P4 3 resource types: • A (7 instances), B (2 instances), and C (6 instances) Snapshot at time T0 Deadlock? Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 53 / 1
  • 113. Detection Algorithm Example (1/2) 5 processes: P0 through P4 3 resource types: • A (7 instances), B (2 instances), and C (6 instances) Snapshot at time T0 Deadlock? Sequence P0, P2, P3, P1, P4 will result in Finish[i] = true for all i Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 53 / 1
  • 114. Detection Algorithm Example (2/2) P2 requests an additional instance of type C Can reclaim resources held by process P0, but insufficient resources to fulfill other processes; requests Deadlock exists, consisting of processes P1, P2, P3, and P4 Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 54 / 1
  • 115. Recovery From Deadlock Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 55 / 1
  • 116. Recovery from Deadlock Process termination Resource preemption Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 56 / 1
  • 117. Process Termination Abort all deadlocked processes. Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 57 / 1
  • 118. Process Termination Abort all deadlocked processes. Abort one process at a time until the deadlock cycle is eliminated Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 57 / 1
  • 119. Process Termination Abort all deadlocked processes. Abort one process at a time until the deadlock cycle is eliminated In which order should we choose to abort? 1 Priority of the process. 2 How long process has computed, and how much longer to completion. 3 Resources the process has used. 4 Resources process needs to complete. 5 How many processes will need to be terminated. 6 Is process interactive or batch. Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 57 / 1
  • 120. Resource Preemption Selecting a victim: minimize cost Rollback: return to some safe state, restart process for that state. Starvation: same process may always be picked as victim, include number of rollback in cost factor. Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 58 / 1
  • 121. Summary Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 59 / 1
  • 122. Summary Deadlock Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 60 / 1
  • 123. Summary Deadlock Four simultaneous conditions: mutual exclusion, hold and wait, no preemption, circular wait Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 60 / 1
  • 124. Summary Deadlock Four simultaneous conditions: mutual exclusion, hold and wait, no preemption, circular wait Deadlock prevention: Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 60 / 1
  • 125. Summary Deadlock Four simultaneous conditions: mutual exclusion, hold and wait, no preemption, circular wait Deadlock prevention: Deadlock avoidance: resource-allocation algorithm, banker’s algo- rithm Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 60 / 1
  • 126. Summary Deadlock Four simultaneous conditions: mutual exclusion, hold and wait, no preemption, circular wait Deadlock prevention: Deadlock avoidance: resource-allocation algorithm, banker’s algo- rithm Deadlock detection: Wait-for graph Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 60 / 1
  • 127. Summary Deadlock Four simultaneous conditions: mutual exclusion, hold and wait, no preemption, circular wait Deadlock prevention: Deadlock avoidance: resource-allocation algorithm, banker’s algo- rithm Deadlock detection: Wait-for graph Deadlock recovery: process termination, resource preemption Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 60 / 1
  • 128. Questions? Acknowledgements Some slides were derived from Avi Silberschatz slides. Amir H. Payberah (Tehran Polytechnic) Deadlocks 1393/8/3 61 / 1