Os Unit III Notes
Os Unit III Notes
UNIT III
Deadlocks: System model, Deadlock characterization, Methods for
handling deadlocks, Deadlock
prevention, Detection and avoidance, Recovery from deadlock.
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 −
o 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.
o 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.
A deadlock occurs if the four Coffman conditions hold true. But these conditions are not
mutually exclusive. They are given as follows −
Mutual Exclusion
There should be a resource that can only be held by one process at a time. In the diagram
below, there is a single instance of Resource 1 and it is held by Process 1 only.
No Preemption
A resource cannot be preempted from a process by force. A process can only release a
resource voluntarily. In the diagram below, Process 2 cannot preempt Resource 1 from
Process 1. It will only be released when Process 1 relinquishes it voluntarily after its
execution is complete.
Circular Wait
A process is waiting for the resource held by the second process, which is waiting for the
resource held by the third process and so on, till the last process is waiting for a resource
held by the first process. This forms a circular chain. For example: Process 1 is allocated
Resource2 and it is requesting Resource 1. Similarly, Process 2 is allocated Resource 1
and it is requesting Resource 2. This forms a circular wait loop.
Methods for Handling Deadlocks
Deadlock detection, deadlock prevention and deadlock avoidance are the main methods for
handling deadlocks. Details about these are given as follows −
Deadlock Detection
Deadlock can be detected by the resource scheduler as it keeps track of all the resources
that are allocated to different processes. After a deadlock is detected, it can be handed
using the given methods −
All the processes that are involved in the deadlock are terminated. This
approach is not that useful as all the progress made by the processes is
destroyed.
Resources can be preempted from some processes and given to others until
the deadlock situation is resolved.
Deadlock Prevention
It is important to prevent a deadlock before it can occur. So, the system checks each
transaction before it is executed to make sure it does not lead to deadlock. If there is even a
slight possibility that a transaction may lead to deadlock, it is never allowed to execute.
Some deadlock prevention schemes that use timestamps in order to make sure that a
deadlock does not occur are given as follows −
The wait for graph shows the relationship between the resources and transactions. If a
transaction requests a resource or if it already holds a resource, it is visible as an edge on
the wait for graph. If the wait for graph contains a cycle, then there may be a deadlock in the
system, otherwise not.
Ostrich Algorithm
The ostrich algorithm means that the deadlock is simply ignored and it is assumed that it will
never occur. This is done because in some systems the cost of handling the deadlock is
much higher than simply ignoring it as it occurs very rarely. So, it is simply assumed that the
deadlock will never occur and the system is rebooted if it occurs by any chance.
Deadlock Prevention
ntroduction
Deadlock prevention is a technique used in computer science to avoid situations where
multiple processes or threads are blocked and unable to proceed because they are waiting
for each other to release resources that they need to complete their tasks. Deadlocks can
be detrimental to the performance of a system and can cause significant delays in
completing tasks. To prevent deadlocks, various techniques such as resource allocation
graph, preemptive scheduling, and detection and recovery are used. Each of these
techniques helps in ensuring that resources are used efficiently and prevent situations
where a deadlock may occur, thereby ensuring optimal performance of the system.
In summary, the resource allocation graph is a useful technique for preventing deadlocks in
computer systems. It allows the system to monitor the use of resources and detect potential
deadlocks by analyzing the graph for cycles. By taking appropriate steps to break the cycle,
the system can avoid the occurrence of deadlocks and ensure the smooth operation of the
system.
Preemptive Scheduling
Preemptive scheduling is a technique used for preventing deadlocks in computer systems.
In this technique, the system is designed to preempt a process that is holding a resource for
too long. When a process is preempted, the system forces the process to release the
resource, which can then be allocated to another process that needs it.
Explanation of preemptive scheduling
To identify processes that are holding resources for too long, the system can use various
techniques such as monitoring the waiting time of processes, tracking the use of resources,
and analyzing the behavior of processes. Once the system identifies a process that is
holding a resource for too long, it can preempt that process and force it to release the
resource.
Preemptive scheduling can be an effective technique for preventing deadlocks, but it may
also introduce some overhead and may not be suitable for all systems. Additionally,
preemptive scheduling may impact the performance of the system if too many processes
are preempted, and resources are frequently released and allocated. Therefore, it is
important to use preemptive scheduling judiciously and evaluate its impact on the system's
performance.
Conclusion
In conclusion, preventing deadlocks is crucial for ensuring the smooth operation and optimal
performance of computer systems. The techniques used for preventing deadlocks, such as
resource allocation graph, preemptive scheduling, and detection and recovery, help in
identifying and avoiding potential deadlocks. By adopting appropriate deadlock prevention
techniques, computer systems can improve their efficiency, reduce delays, and avoid the
risk of system failure caused by deadlocks. The prevention of deadlocks is an ongoing
challenge for computer scientists, and as technology evolves, new techniques and
approaches may need to be developed to keep pace with changing demands and
requirements of modern computing systems.