add Explanation of deadlock with example (#20271)

* add Explanation of deadlock with example

Explanation of Deadlock with example are done along with this Deadlock avoidance with Algorithm to Deadlock avoidance are also explained.

* added frontmatter, corrected formatting
This commit is contained in:
AMIT KUMAR RANJAN
2018-11-04 05:40:19 +05:30
committed by Christopher McCormack
parent f250a9bab7
commit 20c0fd9a04

View File

@ -2,13 +2,15 @@
title: Deadlock title: Deadlock
--- ---
## Deadlock ## Deadlocks
### The Deadlock Problem ### The Deadlock Problem
Deadlock is a situation which occurs when a process or thread enters a waiting state because a resource requested is being held by another waiting process, which in turn is waiting for another resource held by another waiting process.
In a deadlock state a process is unable to change its state(waiting) indefinitely because the resources requested by it are being used by another waiting process.
- A set of blocked processes each holding a resource and waiting to acquire a resource held by another process in the set. - A set of blocked processes each holding a resource and waiting to acquire a resource held by another process in the set.
- Example - Example:
- System has 2 disk drives. Let there be 2 processes P1 and P2 and 2 resources R1 and R2. Both P1 and P2 require Both R1 and R2 to complete their tasks. Suppose P1 acquires R1 first. In the mean time P2 acquires R2. Now when P1 requests for R2 it will have to wait (because R2 is being held by P2). Similarly, when P2 requests for R1 it will also have to wait. Neither P1 nor P2 will not be able to change their waiting state since both are holding the resource required by the other process and none will release its resource until their allocated task is over. Hence the system will be in deadlock.
- P 1 and P 2 each hold one disk drive and each needs another one.
#### Bridge Crossing Example #### Bridge Crossing Example
![bridge crossing example](http://www.eenadupratibha.net/pratibha/engineering/images/unit5/1.jpg) ![bridge crossing example](http://www.eenadupratibha.net/pratibha/engineering/images/unit5/1.jpg)
@ -46,6 +48,33 @@ Deadlock can arise if four conditions hold simultaneously.
- **Circular Wait** It imposes a total ordering of all resource types, and require that each process requests resources in an increasing order of enumeration. - **Circular Wait** It imposes a total ordering of all resource types, and require that each process requests resources in an increasing order of enumeration.
### Deadlock Avoidance
Provide information to Operating system that which all resources any process will require during its lifetime.
Then at every request the system can decide whether to grant that request or not.
This decision depends upon resources currently available, resources currently allocated to each process and future request and release of resources by each process.
Safe State: state is safe if the system can allocate resources to every process in some order and still avoid deadlock.
Safe sequence is an order {P1, P2, … Pn} in which the processes can be allocated resources safely.<br>
NOTE:
Safe state No deadlock
Unsafe State - Deadlock may or may not occur
Conclusion
Do not grant the request immediately
Check whether granting the request will leave the system in safe state or not?
If yes, grant the request otherwise do not grant the request
### Algorithms for deadlock avoidance
#### Resource allocation graph algorithm
<i>Resource Allocation graph</i> - this technique can be employed when there is single instance of every resource. In resource allocation graph for deadlock avoidance we introduce a third kind of edge called the claim edge which is a dotted line from a process towards a resource meaning that the resource can be requested by the process in future.
When ever a process requests for a resource the claim edge is changed to request edge and if the resource can be granted the request edge is changed to assignment edge. After this change look for a cycle in the graph. If no cycle exists, then the system is in safe state and the deadlock will not occur else the system is in unsafe state and deadlock may or may not occur.
#### Bankers algorithm
<i>Banker's Algorithm</i> - this technique is used when there are multiple instances of a resource. This algorithm requires four data structures to be implemented:
Available no. of available resources of each type
Max maximum need of any process for any resource
Allocation number of resources allocated to each process
Need (Max Allocation)
#### More information : #### More information :
- [Deadlock](https://en.wikipedia.org/wiki/Deadlock) - [Deadlock](https://en.wikipedia.org/wiki/Deadlock)