ch7 Deadlocks - Read-Only - Compatibility Mode
ch7 Deadlocks - Read-Only - Compatibility Mode
Deadlock
Deadlock
DEADLOCK
CHARACTERIZATION
Deadlock can arise if four conditions hold simultaneously.
Deadlock
• 1) Mutual exclusion: In this condition, only one
method must be non-shareable. It means only one
process can use it at a time.
• This condition ensures that a resource cannot be
simultaneously accessed or modified
• 2) Hold and wait: The process must hold at least one
resource while waiting to acquire additional
resources. This condition can lead to a situation
where processes hold some resources and wait
indefinitely for others. ocesses.
Deadlock
• 3) No pre-emption: Resources cannot be pre-empted
or forcibly taken away from a process. This means that
a resource can only be released voluntarily by the
process holding it. If a process is holding a resource
and cannot complete its task due to waiting for
another resource, it will not release its current
resource, contributing to a potential Deadlock.
• 4) Circular wait: There must be a circular chain of
one or more processes, each of which is waiting for a
resource held by another process in the chain. This
circular waiting pattern means that no process in the
cycle can make progress because another process
blocks it
Deadlock
Deadlock
OF HANDLING
DEADLOCK
SEQUENCE
• Execution Sequence
Deadlock
• 1) Deadlock prevention: Deadlock prevention
strategies aim to eliminate one or more of the
necessary conditions for Deadlock. This approach
ensures that Deadlock cannot occur in the system.
One common method involves resource allocation
graphs or resource allocation policies to track and
manage resource allocation.
Deadlock
2) DEADLOCK
DETECTION AND
RECOVERY:
• Deadlock detection is a process of periodically
checking the system for the existing Deadlocks.
Once the Deadlock is detected, the system can take
action to resolve it. Detection and recovery are
reactive approaches to managing Deadlocks after
they occur.
Deadlock
3) DEADLOCK
AVOIDANCE
• Deadlock avoidance uses resource allocation
algorithms and request protocols to dynamically
allocate resources in a way that prevents the system
from entering a Deadlock state. The key is to make
sure that resource requests do not lead to situations
where all necessary conditions for Deadlock are met.
Deadlock
4) WOUND-WAIT
SCHEMES
• Wound-wait scheme handles Deadlock situations in
the Data Management system, allowing either older
transactions to wait or younger transactions to wait
and deciding which one should be aborted based on
their age.
Deadlock
Deadlock
Deadlock
DEAD LOCK
PREVENTION
• Mutual Exclusion
In terms of resources, mutual exclusion means that
multiple processes can’t use the same resource at the
same time. However, while this is reasonable, it results in
a deadlock. There will be no process waiting for the
resource if we may use the same resource for several
processes at the same time. However, if we can prevent
the resources from acting in a mutually exclusive
manner, we can keep the system from becoming stuck.
Deadlock
HOLD AND WAIT
Deadlock
• In practice, we can only do so if we first identify all of
the resources that the procedure will require.
Although this sounds highly practical, we cannot do
so in a computer system because no process can
identify the required resources at the outset.
Deadlock
• A process is a series of instructions that the CPU
executes. Each instruction makes many requests for
distinct resources. The operating system, however,
cannot control the demand for resources. The
following concerns arise from the approach:
• Practically, it is not possible.
• Because the process can retain a resource for a
lengthy time in some instances, there is a risk of
starving.
Deadlock
NO PREEMPTION
Deadlock
• Example – Let’s say we have a printer that is now
being used by a process, and we want to assign the
printer to another process. As a result, the data
produced by the printer becomes both inaccurate
and ineffective. Furthermore, the operation would not
resume printing from where it stopped off, resulting
in inefficient performance.
Deadlock
CIRCULAR WAIT
Deadlock
Deadlock
DEADLOCK
AVOIDANCE
• A deadlock avoidance policy grants a resource
request only if it can establish that granting the
request cannot lead to a deadlock either immediately
or in the future. The kernal lacks detailed knowledge
about future behavior of processes, so it cannot
accurately predict deadlocks. To facilitate deadlock
avoidance under these conditions, it uses the
following conservative approach: Each process
declares the maximum number of resource units of
each class that it may require. The kernal permits a
process to request these resource units in stages
Deadlock
• Resource Allocation Graph
• Banker’s Algorithm
Deadlock
LIMIT RESOURCE
UTILISATION
• This method involves limiting the huge number of
resources that can be allocated to a process. By
restricting resource usage, the system reduces the
likelihood of Deadlock occurrence. However, this
approach may impact system efficiency and resource
utilisation, so it should be used judiciously.
Deadlock
Consider there are n
processes in the system P1,
P2, P3, …… , Pn where-
Process P1 requires x1 units of
resource R
Process P2 requires x2 units of
resource R
Process P3 requires x3 units of
resource R and so on.
Deadlock
Deadlock
MAXIMUM NUMBER OF
UNITS THAT ENSURES
DEADLOCK-
• (x1-1) + (x2-1) + (x3-1) + …. + (xn-1)
• = ( x1 + x2 + x3 + …. + xn ) – n
• = ∑xi – n
• = Sum of max needs of all n processes – n
Deadlock
MINIMUM NUMBER OF
UNITS THAT ENSURES
NO DEADLOCK
• Minimum number of units of resource R that ensures no
deadlock
• = One more than maximum number of units of resource R
that ensures deadlock
• = (∑xi – n) + 1
Deadlock
QUESTION
Deadlock
DEADLOCK AVOIDANCE
• The deadlock Avoidance method is used by the
operating system in order to check whether the
system is in a safe state or in an unsafe state and in
order to avoid the deadlocks, the process must need
to tell the operating system about the maximum
number of resources a process can request in order
to complete its execution.
Deadlock
• In this method, the request for any resource will be
granted only if the resulting state of the system
doesn't cause any deadlock in the system. This
method checks every step performed by the
operating system. Any process continues its
execution until the system is in a safe state. Once
the system enters into an unsafe state, the operating
system has to take a step back.
Deadlock
SAFE STATE
Deadlock
• In an Unsafe state, the operating system cannot
prevent processes from requesting resources in such
a way that any deadlock occurs. It is not necessary
that all unsafe states are deadlocks; an unsafe state
may lead to a deadlock.
Deadlock
Deadlock
A SYSTEM MODEL
• A system model or structure consists of a fixed number of
resources to be circulated among some opposing processes. The
resources are then partitioned into numerous types, each consisting
of some specific quantity of identical instances. Memory space,
CPU cycles, directories and files, I/O devices like keyboards,
printers and CD-DVD drives are prime examples of resource types.
When a system has 2 CPUs, then the resource type CPU got two
instances.
Deadlock
SYSTEM MODEL
Deadlock
DEADLOCK SYSTEM
MODEL
• Resource types R1, R2, . . ., Rm
CPU cycles, memory space, I/O devices
• Each resource type Ri has Wi instances.
• Each process utilizes a resource as follows:
• request
• use
• release
Deadlock
Deadlock
RESOURCE ALLOCATION
GRAPH
• The resource allocation graph is the pictorial representation of the
state of a system. As its name suggests, the resource allocation
graph is the complete information about all the processes which are
holding some resources or waiting for some resources.
• It also contains the information about all the instances of all the
resources whether they are available or being used by the
processes.
• In Resource allocation graph, the process is represented by a Circle
while the Resource is represented by a rectangle. Let's see the
types of vertices and edges in detail.
•
Deadlock
RESOURCE-ALLOCATION
GRAPH
A set of vertices V and a set of edges E.
Deadlock
RESOURCE-ALLOCATION
GRAPH (CONT.)
• Process
• Pi requests instance of Rj
Pi
Rj
• Pi is holding an instance of Rj
Pi
Rj
Deadlock
•The resource can be of two types:
[Link] Instance - It contains the single instance of the
resource, and is represented by a single dot inside the rectangle
which is the resource vertex.
[Link] Instance - It contains the multiple instances of the
resource, and is represented by multiple (more than one) dots
inside the rectangle.
Deadlock
RULE-01:
•In a Resource Allocation Graph where all the resources are single
instance,
•If a cycle is being formed, then system is in a deadlock state.
•If no cycle is being formed, then system is not in a deadlock
state.
Deadlock
RULE-02:
• In a Resource Allocation Graph where all the resources
are NOT single instance,
• If a cycle is being formed, then system may be in a deadlock state.
• Banker’s Algorithm is applied to confirm whether system is in a
deadlock state or not.
• If no cycle is being formed, then system is not in a deadlock state.
• Presence of a cycle is a necessary but not a sufficient condition for
the occurrence of deadlock.
Deadlock
Deadlock
BANKERS ALGORITHM
Deadlock
Deadlock
MULTI-INSTANCE
Deadlock
MULTIPLE INSTANCE
Deadlock
EXPLANATION
• Current availability: (0,0)
• With the current availability we can fulfil the request of P3, because P3 is demanding nothing. So, P3
will get terminated after executing completely. Now the availability has changed as resource allocated to
P3 has been released by P3.
• Current availability: (0,1)
• Now, with the current availability we can fulfil the request of P1, because it requires 1 resource of R2
and we have it. So, P1 will get terminated after getting executed completely and the resource allocated to
P1 will be released.
• Current availability: (1,1)
• With this availability we can fulfil the request of P2.
• So, no deadlock is present in the system.
• This example contains circular wait but no deadlock is present, because this is multi-instance that only
happen in the case of single-instance.
Deadlock
EXAMPLE OF A RESOURCE
ALLOCATION GRAPH
Deadlock
RESOURCE ALLOCATION GRAPH
WITH A DEADLOCK
Deadlock
RESOURCE ALLOCATION GRAPH WITH A CYCLE BUT NO
DEADLOCK
Deadlock
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.
Deadlock
METHODS FOR HANDLING
DEADLOCKS
• Ensure that the system will never enter a deadlock state.
• 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, including UNIX.
Deadlock
DEADLOCK PREVENTION
Restrain the ways request can be made.
• Mutual Exclusion – not required for sharable resources;
must hold for non sharable resources.
• Hold and Wait – must guarantee that whenever a process
requests a resource, it does not hold any other resources.
• To avoid this, the process can acquire all the resources that it needs, before
starting its execution and after that, it starts its execution. In this way, the process
need not wait for some resources during its execution. But this method is not
practical because we can't know the resources required by a process in advance,
before its execution. So, another way of avoiding hold and wait can be the "Do
not hold" technique. For example, if the process needs 10 resources R1, R2,
R3,...., R10. At a particular time, we can provide R1, R2, R3, and R4. After
performing the jobs on these resources, the process needs to release these
resources and then the other resources will be provided to the process. In this
way, we can avoid the hold and wait condition.
Deadlock
DEADLOCK PREVENTION
(CONT.)
• 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.
Deadlock
DEADLOCK AVOIDANCE
•
• In the deadlock avoidance technique, we try to avoid
deadlock to happen in our system. Here, the system
wants to be in a safe state always. So, the system
maintains a set of data and using that data it decides
whether a new request should be entertained or not.
If the system is going into the bad state by taking
that new request, then the system will avoid those
kinds of request and will ignore the request. So, if a
request is made for a resource, from a system, then
that request should only be approved if the resulting
state of the system is safe i.e. not going into
deadlock.
Deadlock
STATE
• What is the state?
• The state of the system informs that if resources are allocated to
different processes then the system undergoes deadlock or not.
• What is a safe state? Describe how a safe state helps to avoid
deadlock
• If the system can allocate resources to the process in such a way
that it can avoid deadlock. Then the system is in a safe state.
• What is an unsafe state?
• If the system can’t allocate resources to the process safely, then
the system is in an unsafe state.
• Note: Unsafe state not always cause deadlock.
Deadlock
PREVENTION VS
• The main
AVOIDANCE
difference between deadlock prevention and deadlock
avoidance is that 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.
• Information
• In deadlock prevention, the system does not require information of
the existing resources, resource availability and resource requests
whereas, in deadlock avoidance, the system requires information on
the existing resources, resource availability and resource requests to
find whether the system is in a safe or unsafe state. Hence, this is
another difference between deadlock prevention and deadlock
avoidance.
Deadlock
DEADLOCK AVOIDANCE
Requires that the system has some additional a priori information
available.
• Simplest and most useful model requires that each process
declare the maximum number of resources of each type
that it may need.
• The deadlock-avoidance algorithm dynamically examines
the resource-allocation state to ensure that there can never
be a circular-wait condition.
• Resource-allocation state is defined by the number of
available and allocated resources, and the maximum
demands of the processes.
Deadlock
SAFE STATE
• When a process requests an available resource, system must decide
if immediate allocation leaves the system in a safe state.
• System is in safe state if there exists a safe sequence of all processes.
• Sequence <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.
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 an unsafe state.
Deadlock
SAFE, UNSAFE , DEADLOCK
STATE SPACES
Deadlock
RESOURCE-ALLOCATION
GRAPH ALGORITHM
• if we have one instace of each resource we use a variant of RAG
for deadlock avoidance
• Claim edge Pi Rj indicated that process Pj may request resource
Rj; represented by a dashed line.
• when a process makes a request Rj then
• Claim edge converts to request edge when a process requests a
resource.
• When a resource is released by a process, assignment edge
reconverts to a claim edge.
• Resources must be claimed a priori in the system.
Deadlock
RESOURCE-ALLOCATION GRAPH FOR DEADLOCK
AVOIDANCE
Deadlock
BANKER’S ALGORITHM
• RAG not applicable with multiple instances
Deadlock
DATA STRUCTURES FOR THE BANKER’S ALGORITHM
Deadlock
SAFETY ALGORITHM
Finding out whether the system is safe or not
Let Work and Finish be vectors of length m and n, respectively. Initialize:
Work := Available (keeps track of available resources)
Finish [i] = false for i - 1,3, …, n.
This means, initially, no process has finished and the number of available resources is represented by
the Available array.
Deadlock
RESOURCE-REQUEST ALGORITHM
FOR PROCESS PI
Requesti = request vector for process Pi. If Requesti [j] = k then
process Pi wants k instances of resource type Rj.
1. If Requesti Needi go to step 2. Otherwise, raise error condition,
since process has exceeded its maximum claim.
2. If Requesti Available, go to step 3. Otherwise Pi must wait, since
resources are not available.
3. Pretend to allocate requested resources to Pi by modifying the state
as follows:
Available := Available = Requesti;
Allocationi := Allocationi + Requesti;
Needi := Needi – Requesti;;
• If safe the resources are allocated to Pi.
• If unsafe Pi must wait, and the old resource-allocation state is
restored
Deadlock
Deadlock
DEADLOCK DETECTION
• If a system does not employ either a deadlock-prevention or
deadlock-avoidance algorithm, then there are chances of
occurrence of a deadlock.
• In this case, the system may provide two things:
• An algorithm is used to examines the state of the system in order
to determine whether a deadlock has occurred.
• An algorithm that is used to recover from the deadlock
Deadlock
DEADLOCK DETECTION
Deadlock
DEADLOCK DETECTION
• Deadlock detection algorithms work by regularly
checking the current state of resource usage in the
system. They often use a visual tool called a resource
allocation graph. This graph shows which processes
are using which resources and which resources each
process is waiting for
Deadlock
SINGLE INSTANCE OF
EACH RESOURCE TYPE
• If each resource category has a single instance, then we can use a
variation of the resource-allocation graph known as a wait-for
graph.
• A wait-for graph can be constructed from a resource-allocation
graph by eliminating the resources and collapsing the associated
edges, as shown in the figure below.
• An arc from Pi to Pj in a wait-for graph indicates that process Pi is
waiting for a resource that process Pj is currently holding.
Deadlock
(A) RESOURCE
ALLOCATION GRAPH. (B)
CORRESPONDING WAIT-
FOR GRAPH
Deadlock
SAFETY ALGORITHM
• The above scheme that is a wait-for graph is not applicable to the
resource-allocation system having multiple instances of each
resource type. Now we will move towards a deadlock detection
algorithm that is is applicable for such systems.
• This algorithm mainly uses several time-varying data structures
that are similar to those used in Banker's Algorithm and these are
as follows:
Deadlock
DATA STRUCTURES
USED
• 1. Available
• 2. Allocation
• It is an n x m matrix which represents the number of resources of each type currently allocated to each process.
• 3. Request
• It is an n*m matrix that is used to indicate the request of each process; if Request[i][j] equals to k, then process Pi is
requesting k more instances of resource type Ri.
• Allocation and Request are taken as vectors and referred to as Allocation and Request. The Given detection algorithm is
simply used to investigate every possible allocation sequence for the processes that remain to be completed.
Deadlock
DETECTION-ALGORITHM
USAGE
• When, and how often, to invoke depends on:
• How often a deadlock is likely to occur?
• How many processes will need to be rolled back
• If detection algorithm is invoked arbitrarily, there may be many
cycles in the resource graph and so we would not be able to tell
which of the many deadlocked processes “caused” the deadlock.
Deadlock
ADVANTAGES OF DEADLOCK
DETECTION ALGORITHMS
• Improved System Stability: Deadlocks are a major
concern in operating systems , and detecting and
resolving deadlocks can help to improve the stability
of the system.
• Better Resource Utilization: By detecting deadlocks
and freeing resources, the operating system can
ensure that resources are efficiently utilized and that
the system remains responsive to user requests.
Deadlock
• Easy Implementation: Some deadlock detection
algorithms, such as the Wait-For Graph , are
relatively simple to implement and can be used in a
wide range of operating systems and systems with
different resource allocation and synchronization
requirements.
Deadlock
DISADVANTAGES OF DEADLOCK
DETECTION ALGORITHMS
• Performance Overhead: Deadlock detection
algorithms can introduce a significant overhead in
terms of performance, as the system must regularly
check for deadlocks and take appropriate action.
• Complexity: Some deadlock detection algorithms,
such as the Resource Allocation Graph or
Timestamping, are more complex to implement and
require a deeper understanding of the system and its
behavior
Deadlock
Deadlock
RECOVERY FROM
DEADLOCK
• Deadlock recovery performs when a deadlock is detected.
• When deadlock detected, then our system stops working, and after
the recovery of the deadlock, our system start working again.
• Therefore, after the detection of deadlock, a method/way must
require to recover that deadlock to run the system again. The
method/way is called as deadlock recovery.
• Here are various ways of deadlock recovery that we will discuss
briefly in this tutorial.
• Deadlock recovery through preemption
• Deadlock recovery through rollback
• Deadlock recovery through killing processes
Deadlock
CONTINUED
• There one possibility and that is to inform the operator about the
deadlock and let him deal with this problem manually.
• Another possibility is to let the system recover from the deadlock
automatically. These are two options that are mainly used to break
the deadlock.
Deadlock
RECOVERY FROM
DEADLOCK
Deadlock
RECOVERY FROM DEADLOCK: 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?
• Priority of the process.
• How long process has computed, and how much longer to
completion.
• Resources the process has used.
• Resources process needs to complete.
• How many processes will need to be terminated.
• Is process interactive or batch?
Deadlock
ABORTING ALL
DEADLOCKED PROCESSES
Clearly, this method is helpful in breaking the cycle of deadlock, but
this is an expensive approach. This approach is not suggestable but
can be used if the problem becomes very serious. If all the processes
are killed then there may occur insufficiency in the system and all
processes will execute again from starting.
Deadlock
ABORT ONE PROCESS AT A TIME UNTIL THE
ELIMINATION OF THE DEADLOCK CYCLE
This method can be used but we have to decide which process to kill
and this method incurs considerable overhead. The process that has
done the least amount of work is killed by the Operating system
firstly.
Deadlock
RECOVERY FROM DEADLOCK: RESOURCE PREEMPTION
Deadlock
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.
Deadlock
QUESTION
Deadlock
QUESTIONS
Deadlock
QUESTION
Deadlock
QUESTIONS
Deadlock
BANKERS ALGORITHM
• Banker's algorithm is a deadlock avoidance algorithm.
It is named so because this algorithm is used in
banking systems to determine whether a loan can be
granted or not.
Deadlock
DATA STRUCTURES OF
BANKER'S ALGORITHM
1. Available
•It is an array of length m. It represents the number of
available resources of each type. If Available[j] = k,
then there are k instances available, of resource type
Rj.
Deadlock
2. MAX
• It is an n x m matrix which represents the maximum
number of instances of each resource that a process can
request. If Max[i][j] = k, then the process Pi can request
atmost k instances of resource type Rj.
Deadlock
3. ALLOCATION
• It is an n x m matrix which represents the number of
resources of each type currently allocated to each
process. If Allocation[i][j] = k, then process Pi is currently
allocated k instances of resource type Rj.
Deadlock
4. NEED
• It is a two-dimensional array. It is an n x m matrix
which indicates the remaining resource needs of each
process. If Need[i][j] = k, then process Pi may need k
more instances of resource type Rj to complete its
task.
Deadlock
• Need[i][j] = Max[i][j] - Allocation [i][j]
Deadlock
BANKERS ALGORITHM
NUMERICAL
• Consider a system that contains five processes P1,
P2, P3, P4, P5 and the three resource types A, B and
C. Following are the resources types: A has 10, B has
5 and the resource type C has 7 instances.
Deadlock
Deadlock
• Context of the need matrix is as follows:
Deadlock
Deadlock
• Hence, we created the context of need matrix.
Deadlock
• So, we examine another process, P2.
Deadlock
• Similarly, we examine another process P3.
Deadlock
• Step 4: For Process P4:
• 5, 3, 2 + 2, 1, 1 => 7, 4, 3
Deadlock
• Similarly, we examine another process P5.
• 7, 4, 3 + 0, 0, 2 => 7, 4, 5
• Now, we again examine each type of resource request for processes P1 and
P3.
Deadlock
• Step 6: For Process P1:
• 7, 4, 5 + 0, 1, 0 => 7, 5, 5
Deadlock
• So, we examine another process P2.
• 7, 5, 5 + 3, 0, 2 => 10, 5, 7
Deadlock
• Hence, we execute the banker's algorithm to find
the safe state and the safe sequence like P2, P4, P5,
P1 and P3.
Deadlock