Open In App

Conditions for Deadlock in Operating System

Last Updated : 06 Sep, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

A Deadlock is a situation that involves the interaction of more than one resource and process with each other. We can visualize the occurrence of deadlock as a situation where there are two people on a staircase.

Disadvantages in Deadlock

Deadlock is an infinite Process it means that once a process goes into deadlock it will never come out of the loop and the process will enter for an indefinite amount of time. There are only detection, resolution, and prevention techniques. But, there are no Deadlock-stopping techniques.

img2

Deadlock

Consider a simple scenario that includes two processes (Process A and Process B) and two resources (Resource 1 and Resource 2).

How a Deadlock Can Occur?

Let’s see that both processes begin execution at the same time.

  1. Process A obtains Resource 1.
  2. Process B obtains Resource 2.

We are currently in the following situation:

  1. Process A possesses Resource 1.
  2. Process B possesses Resource 2.

Let us now see what happens next:

Process A requires Resource 2 to continue execution but is unable to do so because Process B is currently holding Resource 2.

Similarly, Process B requires Resource 1 to continue execution but is unable to do so because Process A is currently holding Resource 1.

Both processes are now stuck in a loop:

  1. Process A is awaiting Resource 2 from Process B.
  2. Process B is awaiting Resource 1 from Process A.

We have a deadlock because neither process can release the resource it is holding until it completes its task, and neither can proceed without the resource the other process is holding. Both processes are effectively “deadlocked,” unable to move forward.

To break the deadlock and free up resources for other processes in this situation, an external intervention, such as the operating system killing one or both processes, would be required. Deadlocks are undesirable in operating systems because they waste resources and have a negative impact on overall system performance and responsiveness. To prevent deadlocks, various resource allocation and process scheduling algorithms, such as deadlock detection and avoidance, are employed.

Necessary Conditions for the Occurrence of a Deadlock

Let’s explain all four conditions related to deadlock in the context of the scenario with two processes and two resources:

Mutual Exclusion

This condition requires that at least one resource be held in a non-shareable mode, which means that only one process can use the resource at any given time. Both Resource 1 and Resource 2 are non-shareable in our scenario, and only one process can have exclusive access to each resource at any given time.

As an example:

  • Process A obtains Resource 1.
  • Process B acquires Resource 2.

Hold and Wait

The hold and wait condition specifies that a process must be holding at least one resource while waiting for other processes to release resources that are currently held by other processes. In our example,

  • Process A has Resource 1 and is awaiting Resource 2.
  • Process B currently has Resource 2 and is awaiting Resource 1.
  • Both processes hold one resource while waiting for the other, satisfying the hold and wait condition.

No Preemption

Preemption is the act of taking a resource from a process before it has finished its task. According to the no preemption condition, resources cannot be taken forcibly from a process a process can only release resources voluntarily after completing its task. In our scenario, neither Process A nor Process B can be forced to release the resources in their possession. The processes can only release resources voluntarily.

Circular Wait

This condition implies that circular processes must exist, with each process waiting for a resource held by the next process in the chain. In our scenario, Process A is waiting for Resource 2, which is being held by Process B.

Process B is awaiting Resource 1 from Process A.

This circular chain of dependencies causes a deadlock because neither process can proceed, resulting in a system shutdown.

To summarise, in the context of two processes and two resources, all four conditions (mutual exclusion, hold and wait, no preemption, and circular wait) must be met at the same time. Deadlocks are a major concern in operating systems, and various techniques are used to avoid them to prevent, detect, and recover from them.

PYQs on Deadlock

Q.1: Which of the following statements is/are TRUE with respect to deadlocks? [GATE CSE 2022]

Options:

  1. The circular wait is a necessary condition for the formation of deadlock.
  2. In a system where each resource has more than one instance, a cycle in its wait-for graph indicates the presence of a deadlock.
  3. If the current allocation of resources to processes leads the system to an unsafe state, then deadlock will necessarily occur.
  4. In the resource-allocation graph of a system, if every edge is an assignment edge, then the system is not in a deadlock state.

Answer: 1 and 4

Explanation:

  1. Circular Wait is one of the four necessary conditions for deadlock to happen Hence it is True.
  2. Not necessarily; if each resource only had one instance, a cycle would have been both necessary and sufficient for a deadlock to occur. A Cycle in a multi-instance resource is required but not sufficient, and in this case, each resource is made up of more than one instance, resulting in a simple contradiction.
  3. An unsafe state is one in which no method of resource allocation can prevent deadlock from occurring. In contrast, in a safe state, there is a method of allocation in which all processes are completed, so the answer is False.
  4. In the form of request edges, the Resource Allocation graph accommodates future resource requirements. If there are no request edges, then all process resource requirements are met, so it is True.

Q.2: Consider a system with 3 processes that share 4 instances of the same resource type. Each process can request a maximum of K instances. Resource instances can be requested and released only one at a time. The largest value of K that will always avoid deadlock is? [GATE CSE 2018]

Answer: 2

Frequently Asked Questions on Deadlock

Q.1: What are the necessary conditions for a deadlock to occur?

Answers:

Deadlock can occur if four conditions are present simultaneously:

Mutual Exclusion: At least one resource must be held in a non-sharable mode, meaning it cannot be simultaneously used by multiple processes.

Hold and Wait: A process must be holding at least one resource while waiting for another resource to be released by another process.

No Preemption: Resources cannot be forcibly taken away from a process. They must be released voluntarily.

Circular Wait: A circular chain of two or more processes exists, where each process is waiting for a resource held by another process in the chain.

Q.2: Can a deadlock occur if only one of the necessary conditions is present?

Answers:

No, a deadlock cannot occur if only one of the necessary conditions is present. All four conditions must be satisfied simultaneously for a deadlock to occur.

Q.3: Can a single-processor system experience a deadlock?

Answer:

Yes, both single-processor and multi-processor systems can experience deadlock. Deadlocks are not dependent on the number of processors, but rather on the previously mentioned conditions.

Q.4: Can a deadlock occur if only one of the required conditions exists?

Answer:

No, a deadlock cannot occur if only one of the required conditions is met. For a deadlock to occur, all four conditions must be met at the same time.

Q.5: Can a single-processor system experience a deadlock?

Answer:

Yes, both single-processor and multi-processor systems can experience deadlock. Deadlocks are not dependent on the number of processors, but rather on the previously mentioned conditions.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads