OPERATING SYSTEMS SYLLABUS
unit-3
operating systems
DEADLOCKS & SYNVHRONIZATION
DEADLOCKS
Deadlock System Model
The Deadlock System model is a way to describe and analyze systems that may be
prone to deadlocks, which occur when two or more processes are unable to
proceed because they are each waiting for the other to release a
resource. Below are the components of this model −
Resources − The system has a set of resources that are shared among processes. These
resources can be hardware or software components, such as memory, files, printers, or network
connections. Each resource is identified by a unique name or identifier.
Processes − The system has a set of processes that request and release
resources. Processes are units of execution that can be started, suspended,
resumed, and terminated. Each process is identified by a unique process ID.
Resource Allocation − Each resource can be in one of two states , allocated
or available. A resource that is allocated to a process cannot be used by any other
process until it is released.
Request and Release − A process can request a resource by sending a request
to the system. If the resource is available, it will be allocated to the process. When
a process is finished using a resource, it must release it so that it can be used by
other processes.
Resource Dependency − Some processes may require multiple resources to
complete their tasks. A resource dependency graph can be used to represent the
relationships between processes and resources and to detect potential deadlocks.
Deadlock Detection − A deadlock can occur when two or more processes are
waiting for resources that are being held by other processes, creating a circular
dependency. Deadlock detection algorithms can be used to detect when a
deadlock has occurred, so that corrective action can be taken.
Deadlock Resolution − Once a deadlock has been detected, it can be resolved
by breaking the circular dependency between the processes. This can be done by
releasing one or more resources that are being held by a process, or by
preempting one or more processes that are holding resources. The Working of
some of the techniques are given below −
Resource preemption is a technique used to break the circular wait condition
of a deadlock. The operating system can preempt resources from one or more
processes involved in the deadlock and allocate them to the processes that need
them. Preemption can be done either selectively or globally. In selective
preemption, only the resources that are required to resolve the deadlock are
preempted, while in global preemption, all the resources held by the deadlocked
processes are preempted.
When a process is terminated, all the resources held by the process are
released, and other processes can proceed. However, this approach can lead to
data loss and inconsistency if the terminated process was in the middle of a
critical task.
Deadlock Avoidance − Deadlock avoidance is a technique used to prevent the
occurrence of deadlocks in a computer system. The goal of deadlock avoidance is
to ensure that all resources required by a process are available before the process
starts execution, thereby avoiding the possibility of deadlock.
There are several algorithms that can be used for deadlock avoidance, including
the banker's algorithm and the resource allocation graph. These algorithms use a
mathematical model to analyze resource allocation and to determine whether a
process should be allowed to start or wait for resources.
The banker's algorithm is a widely used method for deadlock avoidance. It is a
resource allocation algorithm that checks whether a requested resource can be
granted to a process without causing a deadlock. The algorithm works by
simulating the allocation of resources and checking whether a safe state can be
reached. A safe state is a state where all processes can complete their execution
without causing a deadlock.
The resource allocation graph is another method for deadlock avoidance. It
represents the allocation of resources as a directed graph. Each process is
represented by a node, and each resource is represented by an edge. The
algorithm checks for cycles in the graph to determine whether a deadlock has
occurred. If a cycle is detected, the process requesting the resource is blocked
until the required resource becomes available.
DEADLOCK CHARACTERIZATION
OR
NECESSARY CONDITIONS
Resource Allocation Graph?
The resource allocation graph is a visual depiction of a system’s current status.
The resource allocation graph, as its name implies, contains all the information
about all of the activities that are holding or waiting for resources.
It also provides information on all instances of all resources, whether available or
in use by processes.
The process is represented by a circle in the Resource Allocation
Graph, whereas the resource is represented using a rectangle.
Let’s take a closer look at the various types of vertices and edges.
There are two sorts of vertices: resource and process. A distinct form will be used to symbolise each of
them. The circle denotes the process, whereas the rectangle denotes the
resource.
There can be several instances of a resource.
In RAG, there are two sorts of edges: One represents assignment, and the other
represents a process waiting for a resource
In case the tail of the arrow is connected to a resource instance and the head is connected to a process,
the resource is shown to be assigned to that process. If the tail of the arrow is connected to the process
and the head is pointed towards the resource, the process is represented as waiting for a resource.
Example
Consider three processes, P1, P2, and P3, as well as two resource types, R1 and
R2. Each resource has a single instance. According to the graph, P1 is using R1,
P2 is holding R2 while waiting for R1, and P3 is waiting for both R1 and R2.
Because no cycle is generated in the graph, there is no deadlock.
Components of Resource Allocation Graph (RAG)
Vertices
We employ two types of vertices in the resource allocation graph:
Process Vertices: Process vertices are used to depict a process. We use a circle to represent the
process vertices and write the process name inside the circle.
Resource Vertices: Resource vertices are used to represent a resource. We use a rectangle to
represent the resource vertices and use dots inside the circle to indicate the number of instances in that
resource.
Edges
In the resource allocation graph, we use two types of edges:
Request Edges: The request edge is used to represent the
process’s waiting state. An arrow can be used to draw an arrow
edge, just as it is in the assigned edge. The arrowhead points to
a resource instance, while the process tail points to the process.
If a process requires ‘n’ instances of a resource type, for
example, we will draw ‘n’ assign edges.
Assign Edges: An assign edge is used to indicate the process’s
resource allocation. We use an arrow to draw assigned edges,
with the arrowhead pointing to the process and the process tail
pointing to the resource instance.
Methods for Handling Deadlock :
METHODS OF HANDLING DEADLOCK
In general, there are four strategies of dealing with
deadlock problem:
1. Deadlock Prevention: Prevent deadlock by resource
scheduling so as to negate at least one of the four
conditions.
2. Deadlock Avoidance: Avoid deadlock by careful
resource scheduling.
3. Deadlock Detection and Recovery: Detect deadlock
and when it occurs, take steps to recover.
4. The Ostrich Approach: Just ignore the deadlock
problem altogether.
DEADLOCK PREVENTION
Mutual Exclusion: Two or more events that cannot happen simultaneously.
For example, provide a global numbering of all the
resources, as shown
1 ≡ Card reader
2 ≡ Printer
3 ≡ Optical driver
4 ≡ HDD
5 ≡ Card punch
Now the rule is this: processes can request resources
whenever they want to, but all requests must be
made in numerical order. A process may request first
printer and then a HDD(order: 2, 4), but it may not
request first a optical driver and then a printer (order: 3,
2). The problem with this strategy is that it may be
impossible to find an ordering that satisfies everyone.
Deadlock prevention algorithms ensure that at least one
of the four necessary conditions for deadlocks namely
mutual exclusion,hold and wait,no preemption and
circular wait do not hold.
Disadvantage with prevention algorithm is poor resource
utilization and thus reduced system throughput.
An alternative method is to avoid deadlocks.
Deadlock Avoidance
This approach to the deadlock problem anticipates
deadlock before it actually occurs. This approach
employs an algorithm to access the possibility that
deadlock could occur and acting accordingly. If the
necessary conditions for a deadlock are in place, it is still
possible to avoid deadlock by being careful when
resources are allocated. It employs the most famous
deadlock avoidance algorithm that is the Banker’s
algorithm.
A deadlock-avoidance algorithm dynamically examines
the resource-allocation state to ensure that a circular
wait condition can never exist. The resource-allocation
state is defined by the number of available and allocated
resources, and the maximum demands of the processes.
Safe and Unsafe States
A system is said to be in a Safe State, if there is a safe
execution sequence. An execution sequence is
an ordering for process execution such that each process
runs until it terminates or blocked and all request for
resources are immediately granted if the resource is
available.
A system is said to be in an Unsafe State, if there is no
safe execution sequence. An unsafe state may
not be deadlocked, but there is at least one sequence of
requests from processes that would make the system
deadlocked.
So, in the above diagram shows the three states of the
System. An unsafe state does not always cause a
Deadlock. Some unsafe states can lead to a Deadlock, as
shown in the diagram.
But how can an Operating System avoid Deadlock?
The operating system avoids Deadlock by knowing the
maximum resource requirements of the processes
initially, and also, the Operating System knows the free
resources available at that time. The operating system
tries to allocate the resources according to the process
requirements and checks if the allocation can lead to a
safe state or an unsafe state. If the resource allocation
leads to an unsafe state, then the Operating System does
not proceed further with the allocation sequence.
How does Deadlock Avoidance Work?
Let's understand the working of Deadlock Avoidance with the help of an intuitive example.
Let's consider three processes P1, P2, P3. Some more information on which the processes tell the
Operating System are :
P1 process needs a maximum of 9 resources (Resources can be any software or hardware Resources like
tape drive or printer etc..) to complete its execution. P1 is currently allocated with 5 Resources and
needs 4 more to complete its execution.
P2 process needs a maximum of 5 resources and is currently allocated with 2 resources. So it needs 3
more resources to complete its execution.
P3 process needs a maximum of 3 resources and is currently allocated with 1 resource. So it needs 2
more resources to complete its execution.
The Operating System knows that only 2 resources out of the total available resources are currently
free.
But only 2 resources are free now. Can P1, P2, and P3 satisfy their requirements? Let's try to
find out.
As only 2 resources are free for now, only P3 can satisfy its need for 2 resources. If P3 takes 2 resources
and completes its execution, then P3 can release its 3 (1+2) resources. Now the three free resources
that P3 released can satisfy the need of P2. Now, P2 after taking the three free resources, can complete
its execution and then release 5 (2+3) resources. Now five resources are free. P1 can now take 4 out of
the 5 free resources and complete its execution. So, with 2 free resources available initially, all the
processes were able to complete their execution leading to a Safe State. The order of execution of the
processes was <P3, P2, P1>.
What if initially there was only 1 free resource available? None of the processes would be able to
complete its execution. Thus leading to an unsafe state.
Deadlock Avoidance Example
Let's take an example that has multiple resource
requirements for every Process. Let there be three
Processes P1, P2, P3, and 4 resources R1, R2, R3, R4.
The maximum resource requirements of the Processes are
shown in the below table.
A number of currently allocated resources to the
processes are:
The total number of resources in the System are :
We can find out the no of available resources for each of
P1, P2, P3, P4 by subtracting the currently allocated
resources from total resources.
Available Resources are :
Now, The need for the resources for the processes can be
calculated by :
Need = Maximum Resources Requirement - Currently
Allocated Resources.
The need for the Resources is shown below:
The available free resources are <2,1,1,1> of resources of
R1, R2, R3, and R4 respectively, which can be used to
satisfy only the requirements of process P1 only initially
as process P2 requires 2 R2 resources which are not
available. The same is the case with Process P3, which
requires 4 R3 resources which is not available initially.
The Steps for resources allotment is explained below:
Firstly, Process P1 will take the available resources and
satisfy its resource need, complete its execution and then
release all its allocated resources. Process P1 is initially
allocated <1,2,3,1> resources of R1, R2, R3, and R4
respectively. Process P1 needs <2,1,0,1> resources of R1,
R2, R3 and R4 respectively to complete its execution. So,
process P1 takes the available free resources <2,1,1,1>
resources of R1, R2, R3, R4 respectively and can
complete its execution and then release its current
allocated resources and also the free resources it used to
complete its execution. Thus P1 releases
<1+2,2+1,3+1,1+1> = <3,3,4,2> resources of R1, R2, R3,
and R4 respectively.
After step 1 now, available resources are now <3,3,4,2>,
which can satisfy the need of Process P2 as well as
process P3. After process P2 uses the available Resources
and completes its execution, the available resources are
now <5,4,4,4>.
Now, the available resources are <5,4,4,4>, and the only
Process left for execution is Process P3, which requires
<1,1,4,0> resources each of R1, R2, R3, and R4. So it can
easily use the available resources and complete its
execution. After P3 is executed, the resources available
are <7,4,5,4>, which is equal to the maximum resources
or total resources available in the System.
So, the process execution sequence in the above example
was <P1, P2, P3>. But it could also have been <P1, P3,
P2> if process P3 would have been executed before
process P2, which was possible as there were sufficient
resources available to satisfy the need of both Process P2
and P3 after step 1 above.
Deadlock Avoidance Solution
Deadlock Avoidance can be solved by two different
algorithms:
• Resource allocation Graph
• Banker's Algorithm
Resource Allocation Graph
Resource Allocation Graph (RAG) is used to represent the
state of the System in the form of a Graph. The Graph
contains all processes and resources which are allocated
to them and also the requesting resources of every
Process. Sometimes if the number of processes is less, We
can easily identify a deadlock in the System just by
observing the Graph, which can not be done easily by
using tables that we use in Banker's algorithm.
Resource Allocation Graph has a process vertex
represented by a circle and a resource vertex represented
by a box. The instance of the resources is represented by a
dot inside the box. The instance can be single or multiple
instances of the resource. An example of RAG is shown
below.
Banker's Algorithm
The Banker's algorithm is a resource allocation &
deadlock avoidance algorithm developed by Edsger
Dijkstra that test for safety by simulating the allocation of
pre-determined maximum possible amounts of all
resources. Then it makes a "safe-state" check to test for
possible deadlock conditions for all other pending
activities, before deciding whether allocation should be
allowed to continue.
The Banker's algorithm is run by the operating system
whenever a process requests resources. Thealgorithm
prevents deadlock by denying or postponing the request if
it determines that accepting the request could put the
system in an unsafe state (one where deadlock could
occur).
For the Banker's algorithm to work, it needs to know
three things:
• How much of each resource could possibly request by
each process.
• How much of each resource is currently holding by each
process.
• How much of each resource the system currently has
available.
Resources may be allocated to a process only if it
satisfies the following conditions:
1. request ≤ max, else set error as process has crossed
maximum claim made by it.
2. request ≤ available, else process waits until resources
are available.
Several data structures must be maintained to implement
the banker's algorithm. These data structures encode the
state of the resource-allocation system. Let n be the
number of processes in the system and m be the number
of resource types. We need the following data structures:
Available: A 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 x m matrix defines the maximum demand of
each process. If Max[i,j] = k, then process Pi may request
at most k instances of resource type Rj.
Allocation: An n x m matrix defines 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.
Need: An n x m matrix indicates the remaining resource
need of each process. If Need[i,j] = k, then process Pi may
need k more instances of resource type Ri to complete its
task. Note that Need[i,j] = Max[i,j] - Allocafion[i,j].
These data structures vary over time in both size and
value. The vector Allocationi specifies the
resources currently allocated to process Pi; the vector
Needi specifies the additional resources that process Pi
may still request to complete its task.
Safety Algorithm
The algorithm for finding out whether or not a system is
in a safe state can be described as follows:
1) Let Work and Finish be vectors of length ‘m’ and ‘n’
respectively.
Initialize: Work = Available
Finish[i] = false; for i=1, 2, 3, 4….n
2) Find an i such that both
a) Finish[i] = false
b) Needi <= Work
if no such i exists goto step (4)
3) Work = Work + Allocation[i]
Finish[i] = true
goto step (2)
4) if Finish [i] = true for all i
then the system is in a safe state
Resource-Request Algorithm
Let Requesti be the request array for process Pi. Requesti
[j] = k means process Pi wants k instances of resource
type Rj. When a request for resources is made by process
Pi, the following actions are taken:
1) If Requesti <= Needi
Goto step (2) ; otherwise, raise an error condition, since
the process has exceeded its maximum claim.
2) If Requesti <= Available
Goto step (3); otherwise, Pi must wait, since the resources
are not available.
3) Have the system pretend to have allocated the
requested resources to process Pi by modifying the state
as
follows:
Available = Available – Requesti
Allocationi = Allocationi + Requesti
Needi = Needi– Requesti
Example:
Considering a system with five processes P0 through P4
and three resources of type A, B, C. Resource type A has
10 instances, B has 5 instances and type C has 7
instances. Suppose at time t0 following snapshot of the
system has been taken:
Q.1: What will be the content of the Need matrix?
Q.2: Is the system in a safe state? If Yes, then what is
the safe sequence?
Q.3: What will happen if process P1 requests one
additional instance of resource type A and two
instances of resource type C?
Q.1: What will be the content of the Need matrix?
Need [i, j] = Max [i, j] – Allocation [i, j]
So, the content of Need Matrix is:
Q.2: Is the system in a safe state? If Yes, then what is
the safe sequence?
Applying the Safety algorithm on the given system,
Q.3: What will happen if process P1 requests one
additional instance of resource type A and two
instances of resource type C?
We must determine whether this new system state is safe.
To do so, we again execute Safety algorithm on the above
data structures.
Hence the new system state is safe, so we can
immediately grant the request for process P1 .
DEADLOCK DETECTION
If a system does not employ either a deadlock-prevention
or a deadlock avoidance algorithm, then a deadlock
situation may occur. In this environment, the system must
provide:
• An algorithm that examines the state of the system to
determine whether a deadlock has occurred.
• An algorithm to recover from the deadlock.
According to number of instances in each resource type,
the Deadlock Detection algorithm can be classified into
two categories as follows:
1. Single Instance of Each Resource Type: If all
resources have only a single instance, then it can define a
deadlock detection algorithm that uses a variant of the
resource-allocation graph (is called a wait-for graph). A
wait–for graph can be draw by removing the nodes of
type resource and collapsing the appropriate edges from
the resource-allocation 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. For
Example:
A deadlock exists in the system if and only if the wait-for
graph contains a cycle. To detect deadlocks,
the system needs to maintain the wait-for graph and
periodically to 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.
2. Several Instances of a Resource Type: The following
deadlock-detection algorithm is applicable to several
instance of a resource type. The algorithm employs
several time-varying data structures:
Available: A vector of length m indicates the number of
available resources of each type.
Allocation: An n x m matrix defines the number of
resources of each type currently allocated to each process.
Request: An n x m matrix indicates the current request of
each process. If Request[i,j] = k, then process Pi is
requesting k more instances of resource type Rj.
The detection algorithm is described as follows:
1. Let Work and Finish be vectors of length m and n,
respectively.
Initialize, Work := Available. 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.
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 a deadlock state.
if Finish[i] =false, then process Pi is deadlocked.
This algorithm requires an order of m x n2 operations to
detect whether the system is in a deadlocked
state.
RECOVERY FROM DEADLOCK
When a detection algorithm determines that a deadlock
exists, then the system or operator is responsible for
handling deadlock problem.
There are two options for breaking a deadlock.
1. Process Termination
2. Resource preemption
[Link] Termination
There are two method to eliminate deadlocks by
terminating a process as follows:
a. Abort all deadlocked processes: This method will
break the deadlock cycle clearly by terminating all
process. This method is cost effective. And it removes the
partial computations completed by the processes.
b. Abort one process at a time until the deadlock cycle
is eliminated:
This method terminates one process at a time, and invokes
a deadlock-detection algorithm to determine whether any
processes are still deadlocked.
2. Resource Preemption
In resource preemption, the operator or system preempts
some resources from processes and give
these resources to other processes until the deadlock cycle
is broken.
If preemption is required to deal with deadlocks, then
three issues need to be addressed:
a. Selecting a victim: The system or operator selects
which resources and which processes are to be preempted
based on cost factor.
b. Rollback: The system or operator must roll back the
process to some safe state and restart it from that
state.
c. Starvation: The system or operator should ensure that
resources will not always be preempted from the same
process?
DEADLOCK IGNORANCE
Stick your head in the sand and pretend there is no
problem at all, this method of solving any problem is
called Ostrich Algorithm. This Ostrich algorithm is the
most widely used technique in order to ignore the
deadlock and also it used for all the single end-users uses.
If there is deadlock in the system, then the OS will reboot
the system in order to function well. The method of
solving any problem varies according to the people.
Scientists all over the world believe that the most efficient
method to deal with deadlock is deadlock prevention. But
the Engineers that deal with the system believe that
deadlock prevention should be paid less attention as there
are very less chances for deadlock occurrence.
System failure, compiler error, programming bugs,
hardware crashes that occur once a week should be paid
more attention rather than deadlock problem that occur
once in years. Therefore most of the engineers don’t pay
much amount in eliminating the deadlock.
Many operating systems suffers from deadlock that are
not even detected and then automatically broke.