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

Unit 3.2 Deadlock

Deadlock occurs in operating systems when processes are unable to proceed because each is waiting for resources held by another. It can be characterized by mutual exclusion, hold and wait, no preemption, and circular wait, and can be managed through prevention, avoidance, or detection and recovery strategies. Deadlock avoidance ensures that resource allocation does not lead to unsafe states by simulating resource requests and checking for potential deadlocks using algorithms like the Banker's Algorithm.
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)
6 views

Unit 3.2 Deadlock

Deadlock occurs in operating systems when processes are unable to proceed because each is waiting for resources held by another. It can be characterized by mutual exclusion, hold and wait, no preemption, and circular wait, and can be managed through prevention, avoidance, or detection and recovery strategies. Deadlock avoidance ensures that resource allocation does not lead to unsafe states by simulating resource requests and checking for potential deadlocks using algorithms like the Banker's Algorithm.
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/ 29

Unit 3: Deadlock

A deadlock occurs when every member of a set of processes is waiting for an event
that can only be caused by a member of the set. Deadlock is a situation that occurs in the
Operating System when any Process enters a waiting state because another waiting process is
holding the demanded resource. Deadlock is a common problem in multi-processing where
several processes share a specific type of mutually exclusive resource known as a soft lock or
software.

In the automotive world deadlocks are called gridlocks.

• The processes are the cars.


• The resources are the spaces occupied by the cars

example consider two processes A and B that each want to print a file currently on tape.

1. A has obtained ownership of the printer and will release it after printing one file.
2. B has obtained ownership of the tape drive and will release it after reading one file.
3. A tries to get ownership of the tape drive, but is told to wait for B to release it.
4. B tries to get ownership of the printer, but is told to wait for A to release the printer.

Deadlock Characteristics/Conditions

1. Mutual exclusion
2. Hold and wait
3. No preemption
4. Circular wait.

There are three ways to handle deadlock:

1. Deadlock prevention: The possibility of deadlock is excluded before making requests,


by eliminating one of the necessary conditions for deadlock.

Example: Only allowing traffic from one direction, will exclude the possibility
of blocking the road.

2. Deadlock avoidance: The Operating system runs an algorithm on requests to check for a
safe state. Any request that may result in a deadlock is not granted.
Example: Checking each car and not allowing any car that can block the road. If there is
already traffic on the road, then a car coming from the opposite direction can cause
blockage.

3. Deadlock detection & recovery: OS detects deadlock by regularly checking the system
state, and recovers to a safe state using recovery techniques. Example: Unblocking the
road by backing cars from one side. Deadlock prevention and deadlock avoidance are
carried out before deadlock occurs.

Resources
The resource is the object granted to a process.

Preemptable and Non-preemptable Resources

• Resources come in two types


1. Preemptable, meaning that the resource can be taken away from its
current owner (and given back later). An example is memory.
2. Non-preemptable, meaning that the resource cannot be taken away. An
example is a printer.
• The interesting issues arise with non-preemptable resources so those are the
ones we study.
• Life history of a resource is a sequence of
1. Request
2. Allocate
3. Use
4. Release
• Processes make requests, use the resource, and release the resource. The allocate
decisions are made by the system and we will study policies used to make these
decisions.

Resource Acquisition

Simple example of the trouble you can get into.

• Two resources and two processes.


• Each process wants both resources.
• Use a semaphore for each. Call them S and T.
• If both processes execute P(S); P(T); --- V(T); V(S)
all is well.
• But if one executes instead P(T); P(S); -- V(S); V(T)
disaster! This was the printer/tape example just above.
Recall from the semaphore/critical-section treatment last chapter, that it is easy to cause
trouble if a process dies or stays forever inside its critical section. Similarly, we assume
that no process maintains a resource forever. It may obtain the resource an unbounded
number of times (i.e. it can have a loop forever with a resource request inside), but each
time it gets the resource, it must release it eventually.

Deadlock Detection in OS Strategies:

• Resource Allocation Graph:


This method represents the resources and processes as nodes in a graph, with edges
representing resource requests and allocations. Deadlocks can be detected by identifying
cycles in the graph.
• Wait-Die and Wound-Wait Schemes:
These schemes are used in resource management systems where older processes can
preempt resources from younger processes (Wound-Wait) or vice versa (Wait-Die).
These strategies help to prevent potential deadlocks.
• Banker's Algorithm:
This algorithm is used to determine if a resource allocation will lead to a safe state, where
a safe state ensures that all processes can complete their execution without getting stuck
in a deadlock.
• Timeout Mechanisms:
Processes are given a certain time limit to complete their execution. If they fail to
complete within the time limit, their allocated resources are released, preventing potential
deadlocks.
• Periodic Checking:
In this approach, the system periodically checks for potential deadlocks by analyzing the
state of resources and processes. If a deadlock is detected, appropriate actions are taken.

Advantages of Deadlock Detection in OS:

• Prevents Hangs:
Detects and resolves deadlocks, preventing system freezes.
• Optimizes Allocation:
Identifies inefficient resource usage and reallocates for efficiency.
• Dynamic Allocation:
Responds to real-time demands, improving responsiveness.
• Minimizes Impact:
Reduces disruptions, enhancing user experience.
• Predictable Behavior:
Enables better management and predictability.
• Monitors Health:
Provides insights, aids performance optimization.
• Reclaims Resources:
Frees locked resources, increasing availability.
Limitations of Deadlock Detection:

• Detection Overhead:
Adds computational burden due to continuous monitoring.
• Delayed Resolution:
Only responds after deadlock occurs, not preventing it.
• Resource Usage:
Consumes additional system resources for monitoring.
• Complexity:
Requires careful design and tuning, adding complexity.
• Potential False Positives:
Can detect non-deadlock situations as deadlocks.

Deadlock Avoidance in OS
Deadlock Avoidance is used by the Operating System to Avoid Deadlock in the System. The
processes need to specify the maximum resources needed by the Operating System so that the
Operating System can simulate the allocation of available resources to the requesting processes
and check if it is possible to satisfy the need of all the processes' requirements.
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.

Process Maximum Required current Available Need


P1 9 5 4
P2 5 2 3
P3 3 1 2

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.
We use two words, safe and unsafe states. What are those states? Let's understand these
concepts.

Safe State and Unsafe State


Safe State - In the above example, we saw that the Operating System was able to satisfy the needs
of all three processes, P1, P2, and P3, with their resource requirements. So all the processes were
able to complete their execution in a certain order like P3->P2->P1.

So, If the Operating System is able to allocate or satisfy the maximum resource
requirements of all the processes in any order then the system is said to be in Safe State.

So safe state does not lead to Deadlock.

Unsafe State - If the Operating System is not able to prevent Processes from requesting
resources which can also lead to a Deadlock, then the System is said to be in an Unsafe State.

Unsafe State does not necessarily cause deadlock it may or may not cause deadlock.

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.

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.
Process R1 R2 R3 R4
P1 3 2 3 2
P2 2 3 1 4
P3 3 1 5 0

A number of currently allocated resources to the processes are:

Process R1 R2 R3 R4
P1 1 2 3 1
P2 2 1 0 2
P3 2 0 1 0

The total number of resources in the System are :

R1 R2 R3 R4
7 4 5 4

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 :

R1 R2 R3 R4
2 1 1 1

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:

Process R1 R2 R3 R4
P1 2 1 0 1
P2 0 2 1 2
P3 1 1 4 0

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:

1. 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.
2. 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>.
3. 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.

In the context of the Banker's Algorithm, what is an 'unsafe state'?


It is a state where resources are allocated such that no deadlocks can occur.
It is a state where resources are allocated such that a deadlock is guaranteed.
It is a state that may lead to a deadlock, but not necessarily.
It is a state where no resources are available.
Submit

Deadlock Avoidance Solution


Deadlock Avoidance can be solved by two different algorithms:

• Resource allocation Graph


• Banker's Algorithm

We will discuss both algorithms in detail in their separate article.

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
➢ Banker's algorithm does the same as we explained the Deadlock avoidance with the help
of an example. The algorithm predetermines whether the System will be in a safe state or
not by simulating the allocation of the resources to the processes according to the maximum
available resources. It makes an "s-state" check before actually allocating the
resources to the Processes. When there are more number of Processes and many
Resources, then Banker's Algorithm is useful.
➢ 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.
➢ Consider there are n account holders in a bank and the sum of the money in all of their
accounts is S. Every time a loan has to be granted by the bank, it subtracts the loan
amount from the total money the bank has. Then it checks if that difference is greater
than S. It is done because, only then, the bank would have enough money even if all
the n account holders draw all their money at once.
➢ Banker's algorithm works in a similar way in computers. Whenever a new process is
created, it must specify the maximum instances of each resource type that it needs, exactly.

Characteristics of Banker's Algorithm

The characteristics of Banker's algorithm are as follows:

• If any process requests for a resource, then it has to wait.


• This algorithm consists of advanced features for maximum resource allocation.
• There are limited resources in the system we have.
• In this algorithm, if any process gets all the needed resources, then it is that it should
return the resources in a restricted period.
• Various resources are maintained in this algorithm that can fulfill the needs of at least one
client.

Disadvantages of Banker's Algorithm

Some disadvantages of this algorithm are as follows:

1. During the time of Processing, this algorithm does not permit a process to change its maximum
need.
2. Another disadvantage of this algorithm is that all the processes must know in advance about the
maximum resource needs.
3. This algorithm permits the requests to be provided in constrained time, but for one year which is a
fixed period.

Example:

Let us consider the following snapshot for understanding the banker's algorithm:
Allocation Max Available
Processes
ABC ABC ABC

P0 112 433 210

P1 212 322

P2 401 902

P3 020 753

P4 112 112

1. calculate the content of the need matrix?


2. Check if the system is in a safe state?
3. Determine the total sum of each type of resource?

Solution:

1. The Content of the need matrix can be calculated by using the formula given below:

Need = Max – Allocation

2. Let us now check for the safe state.

Safe sequence:

1. For process P0, Need = (3, 2, 1) and

Available = (2, 1, 0)

Need <=Available = False

So, the system will move to the next process.

2. For Process P1, Need = (1, 1, 0)

Available = (2, 1, 0)
Need <= Available = True

Request of P1 is granted.

Available = Available +Allocation

= (2, 1, 0) + (2, 1, 2)

= (4, 2, 2) (New Available)

3. For Process P2, Need = (5, 0, 1)

Available = (4, 2, 2)

Need <=Available = False

So, the system will move to the next process.

4. For Process P3, Need = (7, 3, 3)

Available = (4, 2, 2)

Need <=Available = False

So, the system will move to the next process.

5. For Process P4, Need = (0, 0, 0)

Available = (4, 2, 2)

Need <= Available = True

Request of P4 is granted.

Available = Available + Allocation

= (4, 2, 2) + (1, 1, 2)

= (5, 3, 4) now, (New Available)

6. Now again check for Process P2, Need = (5, 0, 1)

Available = (5, 3, 4)

Need <= Available = True

Request of P2 is granted.
Available = Available + Allocation

= (5, 3, 4) + (4, 0, 1)

= (9, 3, 5) now, (New Available)

7. Now again check for Process P3, Need = (7, 3, 3)

Available = (9, 3, 5)

Need <=Available = True

The request for P3 is granted.

Available = Available +Allocation

= (9, 3, 5) + (0, 2, 0) = (9, 5, 5)

8. Now again check for Process P0, = Need (3, 2, 1)

= Available (9, 5, 5)

Need <= Available = True

So, the request will be granted to P0.

Safe sequence: < P1, P4, P2, P3, P0>

The system allocates all the needed resources to each process. So, we can say that the system is in a
safe state.

3. The total amount of resources will be calculated by the following formula:

The total amount of resources= sum of columns of allocation + Available

= [8 5 7] + [2 1 0] = [10 6 7]

Deadlock Ignorance
Ostrich Algorithm

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. Just as an
explanation we know that the number of processes is determined by the process table. Now as we know there are
only finite number of slots in the process table and hence when the table is full the fork fails. Now the reasonable
approach for the new fork has to wait and try again when the slot in the process table is empty.

Including UNIX and WINDOWS, all the operating system ignore the deadlock first assuming
that the user is restricted to one process. The deadlock ignorance comes into picture frequently
because by this method deadlock is eliminated for free rather than spending much on other
deadlock prevention methods also putting some inconvenient restrictions on process. Thus we
have to decide between the correctness and convenience between the different methods of
solving the deadlock.
Ignoring the possibility of deadlock in operating systems can have both advantages and
disadvantages.

Advantages:

1. Simplicity: Ignoring the possibility of deadlock can make the design and implementation of
the operating system simpler and less complex.
2. Performance: Avoiding deadlock detection and recovery mechanisms can improve the
performance of the system, as these mechanisms can consume significant system resources.

Disadvantages:

1. Unpredictability: Ignoring deadlock can lead to unpredictable behavior, making the system
less reliable and stable.
2. System crashes: If a deadlock does occur and the system is not prepared to handle it, it can
cause the entire system to crash, resulting in data loss and other problems.
3. Reduced availability: Deadlocks can cause processes to become blocked, which can reduce
the availability of the system and impact user experience.
In general, the disadvantages of ignoring deadlock outweigh the advantages. It is important
to implement effective deadlock prevention and recovery mechanisms in operating systems
to ensure the stability, reliability, and availability of the system.

Deadlock Prevention in Operating System


A process is a set of instructions. When a process runs, it needs resources like CPU cycles, Files,
or Peripheral device access. Some of the requests for resources can lead to deadlock.
Deadlock prevention is eliminating one of the necessary conditions of deadlock so that only safe
requests are made to OS and the possibility of deadlock is excluded before making requests.

Here OS does not need to do any additional tasks as it does in deadlock avoidance by running
an algorithm on requests checking for the possibility of deadlock.

Eliminate Mutual Exclusion: It is not possible to dis-satisfy the mutual


exclusion because some resources, such as the tape drive and printer, are inherently
non-shareable.

A mutual exclusion means that un-shareable resources cannot be accessed simultaneously by processes.
Shared resources do not cause deadlock but some resources can't be shared among processes,
leading to a deadlock.

For Example: read operation on a file can be done simultaneously by multiple processes, but
write operation cannot. Write operation requires sequential access, so, some processes have to
wait while another process is doing a write operation.

It is not possible to eliminate mutual exclusion, as some resources are inherently non-shareable,

For Example Tape drive, as only one process can access data from a Tape drive at a time.

For other resources like printers, we can use a technique called Spooling.

Spooling: It stands for Simultaneous Peripheral Operations online.

A Printer has associated memory which can be used as a spooler directory (memory that is used
to store files that are to be printed next).

In spooling, when multiple processes request the printer, their jobs ( instructions of the processes
that require printer access) are added to the queue in the spooler directory.

The printer is allocated to jobs on a first come first serve (FCFS) basis. In this way, the process
does not have to wait for the printer and it continues its work after adding its job to the queue.

We can understand the workings of the Spooler directory better with the diagram given below:
Eliminate Hold and Wait: Allocate all required resources to the process before the
start of its execution, this way hold and wait condition is eliminated but it will lead to
low device utilization. for example, if a process requires a printer at a later time and
we have allocated a printer before the start of its execution printer will remain blocked
till it has completed its execution. The process will make a new request for resources
after releasing the current set of resources. This solution may lead to starvation.

Eliminate No Preemption: Preempt resources from the process when resources are
required by other high-priority processes.
Eliminate Circular Wait: Each resource will be assigned a numerical number. A
process can request the resources to increase/decrease order of numbering. For
Example, if the P1 process is allocated R5 resources, now next time if P1 asks for R4,
R3 lesser than R5 such a request will not be granted, only a request for resources more
than R5 will be granted.
Deadlocks – Detection and Recovery
Deadlock Detection

• If there is no deadlock prevention or deadlock avoidance algorithm running in


the system, deadlock situation may occur. In this case, it is necessary to detect
deadlocks and recover from them. Two methods are explained in this section to
detect deadlocks.
• The first method detects deadlocks when there is only one instance of each
resource type. This method uses a variant of the resource-allocation graph.
• The second method detects deadlocks even when there are multiple instances of
each resource type. The second method uses a variant of the banker’s algorithm.

Deadlock Detection – Single Instance of Each Resource Type

The system maintains a resource allocation graph for detecting deadlocks. The wait-
for graph is a variant of the resource-allocation graph.To detect deadlocks from the
RAG or wait-for graph, it is required to periodically invoke an algorithm that searches
for a cycle in the wait-for graph.
✓ If there is a cycle in the wait-for graph, then it means that there is a deadlock in
the system.
✓ If there are no cycles, then it means that there is no deadlock in the system.

Fig: Resource-Allocation Graph and Corresponding wait-for graph

Note: The disadvantage of this method is that it is not suitable for a resource-allocation
system with multiple instances of each resource type.
Deadlock Detection – Several Instances of a Resource Type

In this subsection we learn a deadlock detection algorithm that will detect deadlocks
when there are multiple instances of each resource type.
Let n denote the number of processes and m denote the number of resource types
present in the system.

Data Structures Used in the Algorithm:

• Available: A vector of length m indicates the number of available resources of each


type

A B C

2 3 0

In the example shown above, there are three resource types A, B and C. The number
of available instances of resource types A, B and C are 2, 3 and 0 respectively.

• Allocation: An n x m matrix defines the number of resources of each type


currently allocated to each process. The n rows correspond to n processes and
the m columns correspond to the m resource types.

ABC

P0 010

P1 200

P2 302

P3 211
In the example shown above, there are three resource types A, B and C and four
processes P0, P1, P2 and P3. The number of instances of each resource type currently
allocated to each process is shown. Process P0 is currently allocated 0 instances of
resource type A, 1 instance of resource type B and 0 instances of resource type C.
Similarly, the current allocation of the other processes P1, P2 and P3 are also shown.

• 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

ABC

P0 753

P1 322

P2 902

P3 222

In the example shown above, there are three resource types A, B and C and four
processes P0, P1, P2 and P3. The number of instances of each resource type requested
by each process is shown. Process P0 is requesting 7 instances of resource type A, 5
instances of resource type B and 3 instances of resource type C. Similarly, the request
of the other processes P1, P2 and P3 are also shown.
Example of Detection Algorithm

Consider 5 processes P0 through P4; 3 resource types A, B and C. There are 7


instances of A, 2 instances of B and 6 instances of C.

The snapshot at time T0 is given below:

Allocation A B C Request A B C Available A B C

P0 010 000 000

P1 200 202

P2 303 000

P3 211 100

P4 002 002

We now simulate the algorithm for the above example.


Initially, Available = (0,0,0); Finish[i] = false for i = 0,1,2,3,4
i=0
We check if Request0 ≤ Available? Yes
Therefore, Work = Work + Allocation0 =(0,0,0) + (0,1,0) = (0,1,0)
Finish[0] = true , P0 added to safe sequence < P0>
Allocation Request Available

ABC ABC ABC

P0 010 000 000

P1 200 202

P2 303 000

P3 211 100

P4 002 002

Work = (0,1,0);
Is Request1 ≤ Available? No
Since Request1 is not less than Available, check the next process.
Allocation Request Available
ABC ABC ABC

P0 010 000 000

P1 200 202

P2 303 000

P3 211 100

P4 002 002
Work = (0,1,0);
Is Request2 ≤ Available? Yes
Work = Work + Allocation2 =(0,1,0) + (3,0,3) = (3,1,3) Finish[2] = true , P2 added
to safe sequence < P0, P2>
Allocation Request Available
ABC ABC ABC

P0 010 000 000

P1 200 202

P2 303 000

P3 211 100

P4 002 002

Work = (3,1,3);
Is Request3 ≤ Available? Yes
Work = Work + Allocation3 =(3,1,3) + (2,1,1) = (5,2,4) Finish[3] = true,
P3 added to safe sequence and the safe sequence is now < P0, P2, P3 >
Allocation Request Available
ABC ABC ABC

P0 010 000 000

P1 200 202

P2 303 000

P3 211 100
P4 002 002

Work = (5,2,4);
Is Request4 ≤ Available? Yes
Work = Work + Allocation4 =(5,2,4) + (0,0,2) = (5,2,6) Finish[4] = true,
P4 added to safe sequence and the safe sequence is < P0, P2, P3, P4 >

Now, we check again from the beginning all the other processes that were not added
to the safe sequence.
Allocation Request Available

ABC ABC ABC

P0 010 000 000

P1 200 202

P2 303 000

P3 211 100

P4 002 002

Work = (5,2,6);
Is Request1 ≤ Available? Yes
Work = Work + Allocation1 =(5,2,6) + (2,0,0) = (7,2,6) Finish[1] = true,
P1 added to safe sequence and the safe sequence now is < P0, P2, P3, P4, P1 >
Allocation Request Available

ABC ABC ABC

P0 010 000 000

P1 200 202

P2 303 000

P3 211 100

P4 002 002

Sequence <P0, P2, P3, P4, P1> now results in Finish[i] = true for all i.

There can be more than one safe sequence, that is there can be correct safe sequences
other than <P0, P2, P3, P1, P4>. We have found one safe sequence. Since there is at
least one safe sequence, the system is in a safe state. There is no deadlock in the system.

Let process P2 now make an additional request for an instance of resource type C.
The Request matrix is changed as shown below, after including the request of an
instance of resource type C by process P2.
Request A B C
P0 000

P1 202

P2 001

P3 100

P4 002

Now, let us check if the system will be in a safe state. The deadlock detection
algorithm is run again.
Allocation Request Available
ABC ABC ABC

P0 010 000 000

P1 200 202

P2 303 001

P3 211 100

P4 002 002
Initially, Work = Available = (0,0,0); Finish[i] = false for i = 0,1,2,3,4 When i = 0,
Check if Request0 ≤ Available? Yes
Work = Work + Allocation0 =(0,0,0) + (0,1,0) = (0,1,0) Finish[0] = true, P0 is added
to safe sequence < P0>
Allocation Request Available

ABC ABC ABC

P0 010 000 000

P1 200 202

P2 303 001

P3 211 100

P4 002 002

Work is now (0,1,0);


Is Request1 ≤ Available? No
Is Request2 ≤ Available? No
Is Request3 ≤ Available? No
Is Request4 ≤ Available? No

Since the request of all the processes cannot be allocated, the system is not in a safe
state. Though it possible to reclaim the resources held by process P0, there are
insufficient resources to fulfill other processes’ requests. Thus, a deadlock exists,
consisting of processes P1, P2, P3, and P4.
Recovery from Deadlocks

✓ Once deadlocks are detected, it is necessary to recover from deadlocks.


✓ There are different ways in which the system can recover from deadlocks.
▪ One method is to inform the operator that a deadlock has occurred.
The operator deals with the deadlock manually.
▪ The second method is to let the system recover from the deadlock
automatically.
▪ The third method is to break the deadlock. To break the deadlock,
there are two ways. One is to abort one or more processes to break
the circular wait (process termination). The second is to preempt
some resources from one or more of the deadlocked processes
(resource preemption).

Recovery Strategies:

• Process Termination:
One way to recover from a deadlock is to terminate one or more of the processes involved
in the deadlock. By releasing the resources held by these terminated processes, the
remaining processes may be able to continue executing. However, this approach should be
used cautiously, as terminating processes could lead to loss of data or incomplete
transactions.

• Resource Preemption:
Resources can be forcibly taken away from one or more processes and allocated to the
waiting processes. This approach can break the circular wait condition and allow the
system to proceed. However, resource preemption can be complex and needs careful
consideration to avoid disrupting the execution of processes.
• Process Rollback:
If a resource is preempted from a process, what to do with the process? The
process cannot continue, because it is missing some needed resource. Therefore,
the process must be rolled back to some safe state and the process must be
restarted from that state. But, the problem is that it is difficult to determine a safe
state. Therefore, a total rollback might have to be done or the process must be
aborted and restarted again.
• Wait-Die and Wound-Wait Schemes:
As mentioned in the Deadlock Detection in OS section, these schemes can also be used for
recovery. Older processes can preempt resources from younger processes (Wound-Wait),
or younger processes can be terminated if they try to access resources held by older
processes (Wait-Die).
• Kill the Deadlock:
In some cases, it might be possible to identify a specific process that is causing the deadlock
and terminate it. This is typically a last resort option, as it directly terminates a process
without any complex recovery mechanisms.
Recovery from Deadlock: Process Termination

✓ Termination of processes can be done in two ways.


a. One is to abort all the deadlocked processes. This method will break the
deadlock, but the results of all partial computations done by the aborted
processes must be discarded and recomputed later.
b. The second way is to abort one process at a time until the deadlock cycle
is eliminated. This method involves a lot of overhead, because, after each
process is aborted, deadlock-detection algorithm must be invoked to check
if the deadlock still exists.

When there are many processes involved in the deadlock, it is necessary to choose a
process to terminate first. In which order should we choose a process to abort? There
are many factors based on which the process to be aborted can be chosen:

– Priority of the process (process with the least priority is chosen)


– How long process has computed, and how much longer to completion (the process
that has done the least computations is chosen)
– Resources the process has used (the process that has used less number of resources
is chosen)
– Resources process needs to complete (the process that needs many resources
is chosen)
– How many processes will need to be terminated
– Is process interactive or batch? (batch processes are chosen to be terminated
earlier than interactive processes)

Recovery from Deadlock: Resource Preemption

To recover from deadlocks, resources can be preempted from the deadlocked processes.
When resources are preempted, it is necessary to consider certain issues. The issues are
discussed below:

1. Selecting a victim

It is necessary to select the victim process from which the resources are to be
preempted. The victim process has to be selected such that the cost is minimized.
Cost factors may include the number of resources a deadlock process is holding
Advantages of Deadlock Recovery:

• Resumes Processes:
Terminates deadlock-involved processes, allowing others to continue.
• Reclaims Resources:
Releases resources, improving allocation.
• Minimizes Downtime:
Swiftly resolves deadlocks, reducing system downtime.
• User Transparent:
Shields users from deadlock complexities.
• Ensures Stability:
Prevents prolonged hangs, enhances system stability.
• Optimizes Utilization:
Redistributes resources for efficient use.
• Automated Resolution:
Swift, automated recovery from deadlocks.
• Boosts Fault Tolerance:
Contributes to system reliability and fault tolerance.

Limitations of Deadlock Recovery:

• Process Termination:
May terminate processes, affecting user tasks.
• Resource Waste:
Terminated processes release resources, causing waste.
• User Impact:
Interruption to users due to process termination.
• Unfairness:
Selecting processes to terminate may seem arbitrary.
• Automated Risks:
Automated recovery decisions might not be optimal.

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