Deadlock in OS - Deadlock Prevention, Deadlock Avoidance

By:   Last Updated: in: ,

Operating System: Q1.Deadlock in OS? Q2.Four conditions that causes deadlock in OS? Q3.Deadlock Prevention? - How we use this prevention in the operating system. Q4.Strategies to Avoid Deadlock in OS? Deadlock Avoidance? (Operating System all notes)


Q1. Deadlock Management? or Deadlock in OS?

Ans. Deadlock in OS: A specific sequence of tasks are performed when a process tries to access a resource. First, a process requests the operating system to allocate a resource. If the resource is available, the operating system allocates the resource to that process.

A process can acquire and lock a resource in an exclusive or shareable mode. If a process locks a resource in a shareable mode, the operating system allows other processes to access that resource. But if the process locks a resource in an exclusive mode, no other process is allowed to access that resource.

If all the instances of the requested resource are engaged with other processes, the process waits until an instance of the requested resource becomes free. A process can also request for multiple numbers of instances of a resource but it must be less than the total number of resources available.

Example of Deadlock in OS:

If a computer has two printers, no process can request three or more printers. After acquiring the resource, the process utilizes it to perform a specific task.

e.g., In case of scanner usage, a process has to hold a lock on the resource and then use it to scan a document. When the resource is not required, the process releases it, so that the operating system can allocate the resource to any other process.

The above-mentioned procedure occurs in the situation, when there is no deadlock. On the other hand, when a deadlock situation arises, the processes never complete their execution.

Q2. Give the four conditions that causes deadlock in OS? Deadlock Prevention?

Ans. Conditions for Deadlock: There are four conditions that cause the occurrence of deadlock in os which are as follows:

1. Mutual Exclusion:

One or more resources are acquired in an exclusive mode.
e.g., A process P1 locks the resource R in the exclusive mode. This means no other process can acquire the resource R. In other words, the concept of mutual exclusion depicts that a resource can be utilized by one process at a time.

2. Hold and Wait:

The processes involved in a deadlock acquire at least one resource in exclusive mode and wait for at least another resource that has been acquired by another resources in the exclusive mode.

3. No Pre-emption:

Processes do not release the resources allotted to them voluntarily without utilizing the resources.
e.g., The process P1 holds the resource R1 exclusively and waits for resource R2 which is locked by the process P2 in the exclusive mode. The concept of no pre-emption denotes the operating system cannot revoke the resource R1 from the process P1 forcefully.

4. Circular Wait: Deadlock Prevention

A closed chain or a circular relationship exists among the processes involved in a deadlock.
e.g., There are three processes P1 , P2, and P3 . The P1 process is waiting for a resource that has been exclusively acquired by the process P2 , the process P2 is waiting for a resource, which is held exclusively by the process P3 and the P3 process is waiting for a resource which has been acquired exclusively by the process P1 .

Q3. Deadlock Prevention? How we use this prevention in the operating system? Deadlock in OS

Ans. Deadlock Prevention: Deadlock prevention techniques allocate resources in such a manner that at least one of the four necessary conditions of deadlock cannot occur. It denies at least one of the conditions required for the occurrence of a deadlock. Deadlock prevention techniques do not utilize resource properly.

Deadlock Prevention - For Example:

An operating system adopting the deadlock prevention technique allows resources to be pre-empted from a blocked process. No pre-emption condition is violated and deadlock in os does not occur.
We have known that there are four conditions to be satisfied for the occurrence of a deadlock in os.

Below is a brief discussion about the deadlock prevention conditions:

1. First Condition:

The mutual exclusion condition indicates locking of resources in exclusion, also known as non-shareable mode, which leads to blocking of resources. An operating system can avoid blocking of resources by locking the resources in a shareable mode, if feasible. e.g., Consider two processes that need to read data from a file.

The operating system can allow the locking of that file in a shareable mode by the two processes simultaneously. Some resources can't be shared by multiple processes. e.g., Two processes can't simultaneously share a scanner. Again, when a process writes data to a file, it needs to lock the file in an exclusive mode.

2. Second condition:

The second condition for the occurrence of a deadlock is hold and wait. An operating system can avoid this by adopting a strategy that a process must request for all resources it requires at the same time. This strategy can be implemented in two ways:

Firstly, a process can request all the resources it needs when the execution begins. On the other hand, a process can start execution with a minimum set of resources required and can request other resources when required during the time of execution.

3. Third condition:

The third condition for deadlock is no pre-emption. An operating system can adopt a policy to avoid this condition. If a process requests for a resource that is unavailable, the process must release all the resources, it has presently acquired and waits for the required resources.

In other words, when a process P requests a busy resource this policy allows other processes to pre-empt the resources currently held by P. There are certain resources, which are good candidates to be pre-empted, CPU, and memory.

4. Fourth condition:

The fourth condition of deadlock is circular wait. An operating system can impose the ordering of resources for avoiding the circular wait. Resource in a grouped system into certain categories.

For Example: All magnetic disks from a group and all scanners from another group each category is assigned a number, when a process acquires a resource belonging to a specific category then no other process belonging to a category having a lower number can be claimed.

Formally, if there are n categories of resources in a system, ranging from 0 to n-1 and if a process acquires a resource belonging to category c, then the process can only request a resource belonging to category C +1.

Q4. Strategies to avoid deadlock in OS? or Deadlock Avoidance?

Ans. Deadlock Avoidance: In deadlock avoidance, an operating system requires additional information in advance about the process, which requires resources, to avoid a deadlock. The fundamental concept of deadlock avoidance is that the operating system only entertains those requests, for resources by processes, which will not lead to a deadlock.

Deadlock Avoidance: Banker's algorithm:

Banker's algorithm adopts the deadlock avoidance strategy. The name, Banker's algorithm, depicts the similarity of the concept to the field of banking. A Banker never allots the cash available in such a manner that the Banker cannot fulfill the needs of other clients.

The Banker's algorithm has two parts such as safety algorithm and resource request algorithm The safety algorithm is applied by an operating system to determine whether or not a system is in a safe state.

The resource request algorithm is applied by an operating system to determine whether or not a request is forwarded by a process. For acquiring a resource would lead the system to an unsafe state.

Following are the data structures required in the Banker's algorithm: Deadlock Avoidance


This is a vector of sizes 's' ranging from AVAILABLE [O] to [s -1]. This vector stores the number of resources available for a specific type.


This is a matrix having 't' number of rows and s number of columns. When a process begins its execution, it has to state the operating system that how many instances of a resource it may require during its execution.


This is a matrix having t number of rows and s number of columns. This matrix stores the number of instances of a resource allocated to a process. e.g., ALLOCATION [x,  y]=4 indicates that 4 instances of the resources Ry have been allocated to the process Px . The set of all resources allocated to the process Px  can be depicted as a vector, ALLOCATION x.

4. NEED:

This is a matrix having 't' number of rows and 's' number of columns. This indicates the number of instances of a resource presently required by a process to complete the process.

5. WORK:

This is a vector of size 't', for storing information about the resources temporarily.


This is a vector of size 't' for determining whether or not a process is complete.


This is a matrix of the dimension, 't' number of rows and ‘s' number of columns. CLAIMS [x,  y]=4 means that the process Px sends a request to the operating system to grant 4 instances of the resource Ry .

The second part of Banker's algorithm, known as the resource request algorithm determines whether granting a resource according to any claim by a process leads to an unsafe state or not. This algorithm is applied when a process sends a request to grant one or a set of resources.

Advantages: Deadlock Avoidance

The advantages of Banker's algorithm are that it allows mutual exclusion, hold and wait, and no pre-emption conditions. The system assures that a process will be allocated the resources without deadlock.

Disadvantages: Deadlock Avoidance

The disadvantages of Banker's algorithm is the overhead of simulation and calculation, each time a process requests for a resource. The deadlock avoidance techniques can't be applied until an operating system knows the resource requirement of a process in advance.

This approach may lead to a process of starvation while waiting for a long time to get the requested resources allocated by the operating system.

Join us on Facebook, Instagram, and Twitter to get the latest study material. You can also ask us any questions.
Facebook = @allbcaweb
(click on it or search "allbcaweb" on Facebook)
Instagram = @allbcaweb
(click on it or search "allbcaweb" on Instagram)
Twitter = @allbcaweb
(click on it or search "allbcaweb" on Twitter)
Send us your query anytime about Deadlock in OS!

External Links:-

1. Deadlock in OS - (click here)
2. Deadlock Prevention - (click here)
3. Deadlock Avoidance - (click here)

No comments:
Write comment