OS Unit 4
OS Unit 4
Examples Of Deadlock
1. The system has 2 tape drives. P0 and P1 each hold one tape
drive and each needs another one.
2. Semaphores A and B, initialized to 1, P0, and P1 are in
deadlock as follows:
• P0 executes wait(A) and preempts.
• P1 executes wait(B).
• Now P0 and P1 enter in deadlock.
P0 P1
wait(A); wait(B)
wait(B); wait(A)
P0 P1
2. **User-Level Deadlock**:
- This occurs within a specific application or program, involving
resources managed by the application.
- User-level deadlocks are handled by the application or
programming language rather than the operating system.
- Examples of user-level deadlocks include:
- Deadlock in multithreaded programs where threads are waiting
for resources held by other threads in the same program.
- Deadlock in database systems where transactions are waiting
for data held by other transactions.
- Deadlock in distributed systems where processes or nodes are
waiting for resources held by other processes or nodes.
Deadlock Detection:
1. If resources have a single instance –
In this case for Deadlock detection, we can run an algorithm to
check for the cycle in the Resource Allocation Graph. The presence
of a cycle in the graph is a sufficient condition for deadlock. In
single instanced resource types, if a cycle is being formed in the
system then there will definitely be a deadlock.
2. Abort one process at a time until the deadlock is eliminated: Abort one
deadlocked process at a time, until the deadlock cycle is eliminated from the
system. Due to this method, there may be considerable overhead, because,
after aborting each process, we have to run a deadlock detection algorithm to
check whether any processes are still deadlocked.
2.Resource Preemption
To eliminate deadlocks using resource preemption, we preempt some resources from
processes and give those resources to other processes. This method will raise three
issues –
1. Selecting a victim: We must determine which resources and which processes
are to be preempted and also in order to minimize the cost.
2. Rollback: We must determine what should be done with the process from
which resources are preempted. One simple idea is total rollback. That means
aborting the process and restarting it.
3. Starvation: In a system, it may happen that the same process is always picked
as a victim. As a result, that process will never complete its designated task.
This situation is called Starvation and must be avoided. One solution is that a
process must be picked as a victim only a finite number of times.
3.Priority Inversion
A technique for breaking deadlocks in real-time systems is called priority inversion.
This approach alters the order of the processes to prevent stalemates. A higher
priority is given to the process that already has the needed resources, and a lower
priority is given to the process that is still awaiting them. The inversion of priorities
that can result from this approach can impair system performance and cause
performance issues. Additionally, because higher-priority processes may continue to
take precedence over lower-priority processes, this approach may starve lower-
priority processes of resources.
4.RollBack
In database systems, rolling back is a common technique for breaking deadlocks.
When using this technique, the system reverses the transactions of the involved
processes to a time before the deadlock. The system must keep a log of all
transactions and the system’s condition at various points in time in order to use this
method. The transactions can then be rolled back to the initial state and executed
again by the system. This approach may result in significant delays in the
transactions’ execution and data loss.
**Conservative Algorithm:**
**Optimistic Algorithm:**
**Difference:**
2. **Safety Guarantee:**
- Conservative algorithms: They provide a safety guarantee by
ensuring that the system state is always deadlock-free before
making resource allocations.
- Optimistic algorithms: They provide safety by detecting and
resolving deadlocks dynamically when they occur.
3. **Resource Utilization:**
- Conservative algorithms: They may lead to lower resource
utilization as resources are allocated cautiously.
- Optimistic algorithms: They can lead to higher resource
utilization but may have to deal with occasional deadlocks.