0% found this document useful (0 votes)
5 views

OS_Chapter_6_Deadlock[1]

Chapter 6 discusses the deadlock problem in computing, defining it as a situation where processes are blocked, each waiting for resources held by others. It outlines the necessary conditions for deadlock, methods for handling it, including prevention, avoidance, and detection, and introduces the Resource-Allocation Graph and Banker's Algorithm as tools for managing resources to avoid deadlocks. The chapter emphasizes the importance of understanding resource allocation and the implications of deadlock in system performance.

Uploaded by

mulukengashaw21
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

OS_Chapter_6_Deadlock[1]

Chapter 6 discusses the deadlock problem in computing, defining it as a situation where processes are blocked, each waiting for resources held by others. It outlines the necessary conditions for deadlock, methods for handling it, including prevention, avoidance, and detection, and introduces the Resource-Allocation Graph and Banker's Algorithm as tools for managing resources to avoid deadlocks. The chapter emphasizes the importance of understanding resource allocation and the implications of deadlock in system performance.

Uploaded by

mulukengashaw21
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 76

Chapter -6

Deadlock
The Deadlock Problem
✓ A deadlock is a situation where a process or a set of processes is blocked, waiting for some

resource that is held by some other waiting process.

✓ A set of blocked processes each holding a resource and waiting to acquire a resource held by

another process in the set.

Example:- System has 2 tape drives.

✓ P1 and P2 each hold one tape drive.

✓ If each process now requests another drive, the two processes will be in a
deadlocked state.
Bridge Crossing Example

✓ Traffic only in one direction.

✓ Each section of a bridge can be viewed as a resource.

✓ If a deadlock occurs, it can be resolved if one car backs up (preempt resources and

rollback).

✓ Several cars may have to be backed up if a deadlock occurs.


• Starvation is possible. why??
System Model
➢ A set of blocked processes each holding a resource and waiting to acquire a resource held by another process in

the set.

✓ A system consists of a finite number of resources to be distributed among a number of competing processes.

➢ Resource types R1, R2, . . ., Rm (like CPU cycles, memory space, printers or any other i/o devices, etc…)

➢ The resources may be either physical (printers, tape drives, memory space, and CPU cycles) or logical (files and

semaphores).Semaphore: is a variable or abstract data type that is used for controlling access by
multiple processes to a common resource in a concurrent system such as a multiprogramming
operating system.
Con’t…
➢ A process must request a resource before using it, and must release the resource after

using it.

➢ A process may utilize a resource in only the following sequence:

1. Request: If the request cannot be granted immediately, then the requesting process must
wait until it can acquire the resource.

2. Use: The process can operate on the resource (example, print on printer).

3. Release: The process releases the resource.


Con’t…

➢ The request and release of resources are system calls

(Examples: request and release device, open and close file, and
allocate and free memory system calls).

➢ Request and release of other resources can be accomplished

through the wait and signal operations on semaphores.


Deadlock characterization
✓ A deadlock situation can arise if the following four conditions hold
simultaneously in a system:
Mutual execution
Hold and wait
No preemption
Circular wait
Cont…
Mutual exclusion- only one process at a time can use a resource. If another process
requests that resource, the requesting process must be delayed until the resource has been
released.

 Hold and wait- A process must be simultaneously holding at least one resource and
waiting for at least one resource that is currently being held by some other process.

No preemption- Resources cannot be preempted; that is a resource can be released only
voluntarily by the process holding it, after that process has completed its task.

Circular Wait- There must be a circular chain of two or more processes, each of which
is waiting for a resource held by the next member of the chain.
Resource-Allocation Graph
✓ A deadlock is described in terms of a direct graph called a system RAG
✓ 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
Con’t…
• Process

• Resource type with 4 instances

• Pi requests instance of Rj Pi

• Pi is holding an instance of Rj
Pi
Example of a Resource Allocation Graph

Process State:
✓ P1 is holding an instance of R2 and waiting for an

instance of R1.
✓ P2 is holding an instance of R1 and R2, and is

waiting for an instance of R3.


✓ P3 is holding an instance of R3.

• The graph does not contain any cycles.


Resource Allocation Graph With A Deadlock

• Two minimal cycles exist in the system:


✓ P1→R1→P2→R3→ P3→ R2→P1

✓ P2→R3→P3→R2→P2

• Processes P1, P2, P3 are deadlocked.


✓ P1 is waiting for P2 to release R1.

✓ P2 is waiting for R3 which is held by P3.

✓ P3 is waiting for P1 or P2 to release R2.


Resource Allocation Graph With A Cycle

✓ We have a cycle:

P1→R1→P3→R2→P1
✓P4 may release its instance of R2 and

that resource can then be allocated to P3


breaking the cycle.
Basic Facts
➢If a resource-allocation graph does contain cycles AND each
resource category contains only a single instance, then a deadlock
exists.

➢If a resource category contains more than one instance, then the
presence of a cycle in the resource-allocation graph indicates
the possibility of a deadlock, but does not guarantee one.
Methods for Handling Deadlocks
 Generally speaking we can deal with the deadlock problem in one of
four ways:
• Just ignore the problem. Maybe if you ignore it, it will ignore you.

• Prevention, by structurally negating*one of the four required conditions.

• Dynamic avoidance by careful resource allocation.

• Detection and recovery. Let deadlocks occur, detect them, and take action.
4.4.1. Deadlock Prevention

➢ Prevent any one of the four (4) conditions from happening.

➢ By ensuring that at least one of these conditions cannot hold, we can


prevent the occurrence of a deadlock.

✓ Do not allow one of the four conditions to occur.


1. Mutual Exclusion
• Not required for sharable resources must hold for non-sharable
resources.
• Mutual section from the resource point of view is the fact that a
resource can never be used by more than one process simultaneously
which is fair enough but that is the main reason behind the deadlock.
• If a resource could have been used by more than one process at the
same time then the process would have never been waiting for any
resource.
• However, if we can be able to violate resources behaving in the
mutually exclusive manner then the deadlock can be prevented.
Cont…
Spooling
• For a device like printer, spooling can work.
• There is a memory associated with the printer which stores
jobs from each of the process into it.
• Later, Printer collects all the jobs and print each one of them
according to FCFS.
• By using this mechanism, the process doesn't have to wait
for the printer and it can continue whatever it was doing.
• Later, it collects the output when it is produced.
Cont…
Cont…
• Spooling suffers from two kinds of problems.
1.This cannot be applied to every resource.
2.After some point of time, there may arise a race condition
between the processes to get space in that spool.
2. Hold and Wait: Collect all resources before execution.
• Hold and wait condition lies when a process holds a resource and waiting for
some other resource to complete its task.
• Deadlock occurs because there can be more than one process which are
holding one resource and waiting for other in the cyclic order.
• To prevent deadlock, a process must be assigned all the necessary resources
before the execution starts. A process must not wait for any resource once the
execution has been started.
• !(Hold and wait) = !hold or !wait (negation of hold and
wait is, either you don't hold or you don't wait)
Cont…
• This can be implemented practically if a process declares all the
resources initially.
• This can't be done in the computer system because a process can't
determine necessary resources initially.

• Protocol 1: request all tape, disk, printer and hold for entire
execution; note printer will be idle for a long time.
Cont…

• The problem with the approach is:


1.Practically not possible.
2.Low resource utilization
3.Possibility of getting starved will be increases due to the
fact that some process may hold a resource for a very long
time.
3. No preemption
• Release any resource already being held if the process can‘t get an
additional resource.

• Deadlock arises due to the fact that a process can't be stopped once it
starts.

• However, if we take the resource away from the process which is


causing deadlock then we can prevent deadlock.
Cont…
• 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.
Cont…
• This is not a good approach at all since if we take a resource away
which is being used by the process then all the work which it has done
till now can become inconsistent.
• Consider a printer is being used by any process. If we take the printer
away from that process and assign it to some other process then all the
data which has been printed can become inconsistent and ineffective and
also the fact that the process can't start printing again from where it has
left which causes performance inefficiency.
Con’t…
4. Circular Wait: Number of resources and only request in ascending order.
✓To violate circular wait, we can assign a priority number to each of the resource. A
process can't request for a lesser priority resource.

✓This ensures that not a single process can request a resource which is being utilized
by some other process and no cycle will be formed.
✓To ensure that the circular-wait condition never holds is to determine a total
ordering of all resource types, and to require that each process requests resources
in an increasing order of enumeration.
✓ Impose a total ordering of all resource types, and require that each process
requests resources in an increasing order of enumeration.
Con’t…
Example: Let R={R1, R2, …, Rm} be the set of resource types.

✓Assign to each resource type a unique integer number to compare two resources
and to determine whether one proceeds another in ordering.

✓ For example: If the set of resource types R includes tape drives, disk drives, and
printers, then a function F might be defined as follows:

F(tape drive)= 1; F(disk drive)= 5; F(Printer)= 12.

✓A protocol to prevent deadlocks: Each process can request resources only in an


increasing order of enumeration.
Is practically possible?

• Among all the methods, violating Circular wait is the only approach that can be
implemented practically.
4.4.2. Deadlock Avoidance
✓ It is to prevent deadlocks from ever happening, by preventing at least one of the aforementioned conditions.

✓ Requires that the system has some additional a priori information about how resources are to be requested.

✓ Simplest and most useful model requires that each process declare the maximum number of resources of each type that it

may need.

✓ The deadlock-avoidance algorithm dynamically examines the resource-allocation state to ensure that there can never be a

circular-wait condition.

✓ Resource-allocation state is defined by the number of available and allocated resources, and the maximum demands of the

processes.
Cont…
• The main difference between deadlock prevention and deadlock
avoidance is that the deadlock prevention ensures that at least one of
the necessary conditions to cause a deadlock will never occur, while
deadlock avoidance ensures that the system will not enter an unsafe
state.
Safe State

✓ When a process requests an available resource, system must decide if

immediate allocation leaves the system in a safe state.

✓A state is safe if the system can allocate all resources requested by all

processes ( up to their stated maximums ) without entering a deadlock


state.
Cont…
✓ More formally, a state is safe if there exists a safe sequence of processes { P0, P1, P2,

..., PN }

✓Sequence of all processes <P1, P2, …, Pn> is safe if for each Pi, the resources

that Pi can still request can be satisfied by currently available resources +


resources held by all the Pj, with j<I.
• 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.
Cont…
• If a safe sequence does not exist, then the system is in an
unsafe state, which MAY lead to deadlock. ( All safe states
are deadlock free, but not all unsafe states lead to deadlocks. )
Example one
• Consider a system with 12 tape drives and 3 processes, allocated as
follows in a table at state T0.

Q: Is this a safe state? What is the safe sequence?


• Available resource=total resource- allocated resource
• Available resource= 12-9= 3
Cont…
• First we have to find out the safe sequence
• So that, we can fulfill the request of P1, P0 and P2 respectively.

• <P1, P0, P2> satisfies the safety condition.


Or
• Yes, it is a safe state and the save sequence is <P1,P0,P2>
Example two
Q1: Is the following system safe?
• A system with 12 tape drives and 3 processes: P0, P1, P2 at
state T0.
Cont…

• Available resource=total resource- allocated resource

• Available resource= 12-10= 2

• This sequence <P1, P0, P2> is unsafe; only P1 can be satisfied.


P0 and P2 must waits yielding to a deadlock.
Basic Facts
✓ If a system is in safe state ⇒ no deadlocks.

✓ If a system is in unsafe state ⇒ possibility of deadlock.

✓ Avoidance ⇒ ensure that a system will never enter in an

unsafe state.

✓ Safe, unsafe , deadlock state spaces.


Avoidance algorithms
• Single instance of a resource type
✓ Use a resource-allocation graph

• Multiple instances of a resource type


✓ Use the banker’s algorithm
1. Resource-Allocation Graph Algorithm
✓ In addition to the request and assignment edges already described, we introduce a new type of
edge, called a claim edge.

✓ Claim edge Pi ----->Rj indicated that process Pi may request resource Rj for future (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.


Resource-Allocation Graph For Deadlock
Avoidance

✓ Figure 2 shows an unsafe state in a resource-allocation graph, because there is a cycle in the graph.

✓ The resource-allocation graph algorithm is not applicable to a resource allocation system with
multiple instances of each resource type.
2. Banker’s Algorithm
✓ Banker’s algorithm is applicable to a resource-allocation system with multiple instances

of each type.

✓ From it’s name, it could be used in a banking system to ensure that the bank never allocates

its available cash such that it can no longer satisfy the needs of all customers.(banks never
run out of the resources or money and always been in a safe state).

✓ 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.
Data Structures for the Banker’s Algorithm

Let “n” be the number of processes and “m” be the number of resources types. So we needs the
following DS to implement Bankers algorithm.

Available: Vector of length “m” indicates the number of available resources of each type.

If available [j] = k, there are k instances of resource type Rj available.

Max: An (n*m) matrix defines the maximum demands of each process.

If Max [i,j] = k, then process Pi may request at most k instances of resource type Rj.
Con’t….
Allocation: An (n*m) matrix defines the number of resources of each type currently allocated to each process.

If Allocation [i,j] = k then Pi is currently allocated k instances of Rj.

Need: An (n*m) matrix indicates the remaining resource need of each process.

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].
Safety Algorithm
Step-1: Let Work and Finish be vectors of length m and n, respectively.
Initialize: Work = Available and

Finish [i] = false, for i = 0,1, …,n-1.

Step-2: Find an i such that both:


(a) Finish [i] = false
(b) Need i ≤ Work
If no such i (no process) exists, go to step 4.

Step-3: Work = Work + Allocation i


Finish[i] =true
go to step 2.
Step-4: If Finish [i] = true for all i, then the system is in a safe state
Resource-Request Algorithm for Process Pi
Request = request vector for process Pi.If Requesti [j]= k then process Pi wants k instances of resource type Rj.

Step-1: If Requesti  Needi, go to step 2. Otherwise, raise error condition, since process has exceeded its maximum claim.

Step-2: If Requesti  Available, go to step 3. Otherwise Pi must wait, since resources are not available.
Step-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
Example of Banker’s Algorithm
✓ 5 processes P0 through P4; 3 resource types A (10 instances), B (5 instances, and C (7 instances).

✓ Snapshot at time T0: we have the following resource allocation state.


Allocation Max Available
ABC ABC ABC
P0 010 753 3 32
P1 200 322
P2 302 902
P3 211 222
P4 002 433
Cont…
❑what is the need matrix?
❑Is the system in safe state?
❑If yes, find the safe sequence.
Answer
• Total resource= Total allocation + Available resource
• Need is defined to be Max – Allocation
• (Need [n][m]= Max [n][m] – Allocation[n][m])
Allocation Max. R.N Available Need
ABC ABC ABC ABC
P0 0 1 0 753 332 743
P1 2 0 0 322 532 122
P2 3 0 2 902 743 600
P3 2 1 1 222 745 011
P4 0 0 2 433 755 431
Safe sequence: <P1, P3 , P4 , P2, P0>
Yes, the system is in a safe state
In bankers algorithm inputs are:
• Processes
• Any 2 out of 3(Max, Need, Allocation)
• Available or Total no. of resources
Exercise 1:
Q: The following table is given:
Allocation Max Available
ABCD ABCD ABCD
P0 0 0 1 2 0012 15 2 0
P1 1 0 0 0 1750
P2 1 3 5 4 2356
P3 0 6 3 2 0652
P4 0 0 1 4 0656
Q1: Find the total number of resources?
Q2: what is the need matrix?
Q3: Is the system in safe state?
Q4: If yes, find the safe sequence?
Answer
Allocation Max Available Need
ABCD ABCD ABCD ABCD
P0 0 0 1 2 0012 15 2 0 0 0 0 0
P1 1 0 0 0 1750 15 3 2 0 7 5 0
P2 1 3 5 4 2356 2 8 8 6 1 0 0 2
P3 0 6 3 2 0652 2 14 11 8 0 0 2 0
P4 0 0 1 4 0656 2 14 12 12 0 6 4 2
• Total no of resource: 3 14 12 12
• System is in a Safe state
• Safe sequence is: <P0, P2, P3, P4,P1>
Example P1 Request (1,0,2) Con’t.
✓ Check that Request  Available (that is, (1,0,2)  (3,3,2)  true.
Allocation Max Available Need Available after relase
ABC ABC ABC ABC ABC
P0 0 1 0 753 2 3 0 7 43 755
P1 3 0 2 322 020 532
P2 3 0 2 902 600 10 5 7
P3 2 1 1 222 011 743
P4 0 0 2 433 431 745

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 P be granted?


Deadlock Detection
✓ If a system does not employ a deadlock prevention algorithm or a deadlock avoidance algorithm, then a
deadlock situation may occur.

• Therefore, the system must provide:


– Detection: an algorithm that examines the state of the system to determine whether a deadlock has occurred.

– Recovery: an algorithm to recover from the deadlock.

A detection and recovery scheme requires overhead that includes:


– Run-time costs of maintaining the necessary information.
– Executing the detection algorithm.
– The potential losses inherent in recovering from a deadlock.
Single Instance of Each Resource Type
✓ A deadlock detection algorithm uses a variant of the resource allocation graph called wait-for
graph.

✓ An edge from Pi to Pj in a wait-for graph implies that process Pi is waiting for process Pj to release a
resource that Pi needs.

✓ An edge Pi Pj exists in a wait-for graph if and only if the corresponding resource-allocation graph
contains two edges Pi Rq and Rq Pj for some resource Rq.

✓ A deadlock exists in the system if and only if the wait-for graph contains a cycle.
Con’t…
✓ To detect deadlocks, the system needs to:-
– Maintain the wait-for graph.
– Periodically invoke an algorithm that searches for a cycle in the
graph.

✓ An algorithm to detect a cycle in a graph requires an order of n2


operations, where n is the number of vertices in the graph.
Resource-Allocation Graph and Wait-for Graph
RAG Wait for graph

• There is no cycle in this wait for graph so that there is no deadlock


RAG wait for graph

• There is a deadlock, so now deadlock detection mechanism called.


• How frequently this deadlock detection algorithm would be called?
• Whenever a deadlock happens, Deadlock detection algorithm will be invoked.
Cont…
• The wait-for graph scheme is not applicable to a resource
allocation system with multiple instances of each resource
type.
Several Instances of a Resource Type
✓ The algorithm employs several time-varying data structures that are similar to those used in the
banker’s algorithm:

Note: “n” is number of processes in the system and “m” is the number of resource types.

Available: A vector of length “m” indicates the number of available resources of each type.

Allocation: An “n*m” matrix defines the number of resources of each type currently allocated to
each process.

Request: An “n*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.
Detection Algorithm
Step-1: Let Work and Finish be vectors of length m and n, respectively Initialize:
Work = Available, For i = 1,2, …, n, if Allocation i  0, then ,Finish[i] = false;
otherwise, Finish[i] = true.

Step-2: Find an index i such that both:

(a) Finish[i] = false

(b) Requesti  Work


If no such i exists, go to step 4.
Con't…
Step-3: Work = Work + Allocationi
Finish[i] = true
go to step 2.

Step-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 m x n2 operations to detect whether the system is in deadlocked


state.
Example of Detection Algorithm
✓ Five processes P0 through P4; three resource types A (7 instances), B (2 instances), and C (6 instances).

✓ Snapshot at time T0: we have the following resource allocation state:


Allocation Request Available
ABC ABC ABC
P0 010 000 000
P1 2 0 0 202
P2 3 0 3 000
P3 2 1 1 100
P4 0 0 2 002
✓ Sequence <P0, P2, P3, P1, P4> will result in Finish [i] = true for all i.
Con't…
✓ P2 requests an additional instance of type C.
Request
ABC
P0 000
P1 202
P2 001
P3 100
P4 002
✓ State of system?
• Can reclaim resources held by process P0, but insufficient resources to fulfill other processes; requests.
• Deadlock exists, consisting of processes P1, P2, P3, and P4.
Detection-Algorithm Usage
• When should we invoke the detection algorithm? The answer depends on two factors:
1. How often is a deadlock likely to occur?
2. How many processes will be affected by deadlock when it happens?

✓ If deadlocks occur frequently, then the detection algorithm should be invoked frequently.

✓ Resources allocated to deadlocked processes will be idle until the deadlock can be broken.

✓ In addition, the number of processes involved in the deadlock cycle may grow.

✓ Deadlocks occur only when some process makes a request that cannot be granted immediately.
4.4.4. Recovery from Deadlock
✓ When a detection algorithm determines that a deadlock exists, several alternatives are available.

✓ One possibility is to inform the operator that a deadlock has occurred and to let the operator deal
with the deadlock manually.

✓ Another possibility is to let the system recover from the deadlock automatically.

• There are two options for breaking a deadlock:


– To abort one or more processes to break the circular wait (Process Termination).

– To preempt some resources from one or more of deadlock processes (Resource Preemption).
Recovery from Deadlock: Process Termination
• Two methods to eliminate deadlocks by aborting a process. In both methods, the system reclaims
all resources allocated to the terminated processes:

A. Abort all deadlocked processes: Clearly will break the deadlock cycle, but at great expense.

✓ The deadlocked processes may have computed for a long time, and the results of these partial
computations must be discarded and probably will have to be recomputed later.
B. Abort one process at a time until the deadlock cycle is eliminated:

✓ This method incurs considerable overhead, since after each process is


aborted, a deadlock-detection algorithm must be invoked to determine
whether any processes are still deadlocked.

• Aborting a process may not be easy. If the process was in the midst of
updating a file, terminating it will leave that file in an incorrect state.
Recovery from Deadlock: Resource Preemption
✓ If preemption is required to deal with deadlocks, then three issues need to be addressed:

Selecting a victim: which resources and which processes are to be preempted? (minimize cost)

Rollback: If we preempt a resource from a process, what should be done with that process? 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, include number of rollback in cost factor.
Combined Approach to Deadlock Handling
✓ Combine the three basic approaches
– prevention
– avoidance
– detection

allowing the use of the optimal approach for each of resources in the system.

✓ Partition resources into hierarchically ordered classes.

✓ Use most appropriate technique for handling deadlocks within each class.
Chapter 7

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy