Unit 3
Unit 3
Mutual Exclusion
The mutual exclusion condition states that some resources can only be
accessed by one process at a time. This means that once a process acquires a
resource, other processes are denied access until it is released. For example, in
a multi-threaded application, a critical section of code may be protected by a
lock that can only be held by one thread at a time.
No Preemption
The no preemption condition states that resources cannot be forcibly taken
away from a process. Once a process acquires a resource, it can only release it
voluntarily. This means that a process cannot be interrupted or preempted to
free up its resources and allow other processes to use them. Preemption could
potentially lead to data corruption and inconsistencies if a process is forcefully
interrupted during its execution.
Circular Wait
The circular wait condition occurs when a set of processes is circularly waiting
for resources. In other words, each process waits for a resource that another
process holds in the set, creating a circular dependency. This circular wait cycle
prevents any process from acquiring the necessary resources to continue its
execution.
By fulfilling these four conditions simultaneously, a deadlock in OS can occur. It
is important to note that all four conditions must be present for a deadlock to
happen. If any one of these conditions is not met, a deadlock cannot occur.
1.Deadlock Prevention
This is done by restraining the ways a request can be made. Since deadlock
occurs when all the above four conditions are met, we try to prevent any one
of them, thus preventing a deadlock.
2.Deadlock Avoidance
When a process requests a resource, the deadlock avoidance algorithm
examines the resource-allocation state. If allocating that resource sends the
system into an unsafe state, the request is got granted.
Therefore, it requires additional information such as how many resources of
each type is required by a process. If the system enters into an unsafe state, it
has to take a step back to avoid deadlock.
4.Deadlock Ignorance
In the method, the system assumes that deadlock never occurs. Since the
problem of deadlock situation is not frequent, some systems simply ignore it.
Operating systems such as UNIX and Windows follow this approach. However,
if a deadlock occurs we can reboot our system and the deadlock is resolved
automatically.
Note: The above approach is an example of Ostrich Algorithm. It is a strategy of
ignoring potential problems on the basis that they are extremely rare.
1.Deadlock Prevention
Preventing deadlocks is a proactive approach that focuses on eliminating one
or more of the Coffman conditions to ensure that deadlocks cannot occur. Let's
explore some strategies for preventing characteristics of deadlock in operating
system:
Mutual Exclusion Elimination
Mutual exclusion is a necessary condition for deadlock, as it restricts resources
to be accessed only one process at a time. The possibility of deadlocks can be
reduced by eliminating mutual exclusion, such as allowing multiple processes
to access a resource simultaneously. However, this strategy may not be feasible
in all scenarios, especially when resources are not shareable.
Hold and Wait Avoidance
The hold and wait condition can be avoided by adopting a strategy where a
process requests all the required resources before starting its execution. This
ensures that a process does not hold any resources while waiting for additional
resources, reducing the chances of a deadlock. However, this approach may
result in resource underutilization and decreased system efficiency.
No Preemption
To prevent deadlocks, resources can be preempted or forcefully taken away
from a process when required by another process. This approach allows the
system to reallocate resources dynamically and prioritize processes based on
their needs. However, preemption can introduce complexity and potential data
integrity issues, making it challenging to implement in certain contexts.
Circular Wait Resolution
By enforcing a total ordering of resource requests, circular waits can be
eliminated. This means that processes must request resources in a
predetermined order, preventing the formation of circular dependency chains.
However, this approach requires careful resource management and
coordination to ensure the correct order of resource requests.
Preventing deadlocks can be challenging, as it involves carefully analyzing the
system's resource allocation policies and making changes to the design and
implementation of the system. Prevention strategies should be evaluated
based on their feasibility, impact on system performance, and ability to
eliminate the conditions necessary for deadlock.
Detecting a deadlock is essential to handle and resolve the issue effectively.
Deadlock detection involves periodically monitoring the system's state to
identify if a deadlock has occurred. Several algorithms, such as the resource-
allocation graph and the banker's algorithm, can be used to detect deadlocks.
The resource-allocation graph is a graphical representation of the resources
and processes in the system. It consists of nodes representing processes and
resources and edges representing resource requests and allocations. By
analyzing the graph, it is possible to identify cycles that indicate the occurrence
of a deadlock in OS.
The banker's algorithm is a resource allocation and deadlock avoidance
algorithm. It ensures that a state is safe by simulating the execution of
processes and checking if they can complete their execution without entering a
deadlock state. If a safe state cannot be reached, a deadlock is detected.
Once a deadlock is detected, appropriate measures can be taken to resolve it
and allow the processes to proceed with their execution. Deadlock detection is
an important aspect of deadlock management, as it provides insights into the
system's current state and helps make informed decisions for deadlock
resolution.
PO 0, 1, 0 7, 5, 3 3, 3, 2
P1 2, 0, 0 3, 2, 2
P2 3, 0, 2 7, 0, 2
P3 2, 1, 0 2, 2, 2
First we will calculate the Need. The Need field can be calculated using the
formula:
Need[i] = Max[i] - Allocation[i]
After applying the formula, we found the following result.
A, B, C A, B, C A, B, C A, B, C
PO 0, 1, 0 7, 5, 3 3, 3, 2 7, 4, 3
P1 2, 0, 0 3, 2, 2 1, 2, 2
P2 3, 0, 2 7, 0, 2 4, 0, 0
P3 2, 1, 0 2, 2, 2 0, 1, 2
Now, let's check the safe sequence.
For Process P0, Need is (7,4,3), and Available is (3,3,2). It shows that the
resource needed is more than the available so the system will move to the
subsequent process request.
For Process P1, Need is (1,2,2), and the Available is (3,3,2). It shows that the
resource needed is less than the Available. So the system will move forward
with the request of P1.
For Process P2, Need is (4,0,0), and the Available is (5,3,2). It shows that the
resource needed is less than the Available. So the system will move forward
with the request of P2.
For Process P3, Need is (0,1,2), and the Available is (8,3,4). It shows that the
resource needed is less than the Available. So the system will move forward
with the request of P3.
For Process P0, Need is (7,4,3), and the Available is (10,4,4). It shows that the
resource needed is less than the Available. So the system will move forward
with the request of P0.
Available = Available + Allocation =(10,4,4) + (0,1,0) = (10,5,4)
Deadlock Recovery
Deadlock recovery becomes necessary when deadlock prevention or avoidance
strategies are not applicable or unsuccessful. Deadlock recovery involves taking
corrective actions to resolve the deadlock and allow the processes to continue
their execution. There are two primary approaches for deadlock recovery:
1. Process Termination
In this approach, the operating system terminates one or more processes
involved in the deadlock. The resources held by those processes are released
by terminating the processes, allowing other processes to proceed. However,
process termination can result in data loss or inconsistency, as the terminated
processes may have made partial progress before the deadlock occurred.
When selecting processes for termination, several factors can be considered,
such as the process's priority, the progress made by the process, and the
resources consumed by the process. By carefully selecting the processes to
terminate, the impact on the system can be minimized.
2. Resource Preemption
Resource preemption involves forcibly taking resources from one or more
processes involved in the deadlock. The operating system can break the circular
dependency by preempting resources and allowing the remaining processes to
proceed. However, resource preemption can be complex and may require the
system to roll back the affected processes to a safe state before resuming their
execution.
The selection of processes and resources for preemption requires careful
consideration to minimize the impact on system performance and ensure fair
resource allocation. Factors such as the number of resources held by a process
and the amount of time the process has consumed can be considered when
deciding which processes and resources to preempt.
Deadlock recovery strategies aim to resolve deadlocks and restore the system
to a consistent state. However, both process termination and resource
preemption strategies have their limitations and may result in performance
degradation or data loss. Therefore, careful planning and consideration are
necessary when implementing deadlock recovery mechanisms.