UNDER THE GUIDANCE OF : MRS. NIKITA GUPTA TOPIC : DEADLOCKS WHAT IS DEADLOCK ? Deadlock is a condition where a set of processes each holding a resource is waiting to acquire a resource held by another. Example : NECESSARY CONDITIONS FOR DEADLOCK There are four necessary conditions that must hold for a deadlock to be possible. Mutual Exclusion : Only one process at a time may use a resource. Hold & Wait : A process may hold allocated resources while waiting to acquire some more resources hold by other processes at the same time. No Preemption : A resource can be released only voluntarily by the process holding it, after that process has completed its task. Circular Wait : There exists a set { P0,P1,P2,.....,Pn } of waiting processes such that P0 is waiting for a resource that is held by P1, P1 is waiting for a resource that is held by P2, and so on. RESOURCE ALLOCATION GRAPH Resource Allocation Graph (RAG) is a graphical representation of an algorithm for detecting a deadlock. Each resource is shown as a circle and each process is shown as a square. An edge from process to resource represents a Request edge and an edge from resource to process represents an Assignment edge. RESOURCE ALLOCATION GRAPH DEADLOCK HANDLING Deadlock handling can be done by : Deadlock Prevention : Deadlock can be prevented by preventing any one of the necessary 4 conditions to occur. Deadlock Avoidance : The idea of avoiding a deadlock is simply not allow the system to enter an unsafe state that may cause a deadlock. ○ Safe State : If deadlock does not occur, then safe state. ○ Unsafe State : If deadlock occurs, then unsafe state. Deadlock Detection & Recovery : In this method it allows deadlock to occur, detect the deadlock and then recover the deadlock. DEADLOCK PREVENTION METHODS Mutual Exclusion : It is not possible to prevent deadlock by avoiding mutual exclusion as some resources are basically non - sharable.
Hold & Wait : Breaking of hold and wait condition
is possible if a process is allowed to request a resource when it has none resource and all the requested resources are allotted to the process in the beginning. It is practically not possible as it may lead to less utilization of computer resources. DEADLOCK PREVENTION METHODS No Preemption : Preempt resources forcefully from the process when resources are required by other high priority processes.
Circular Wait : In this method, the cycle in RAG
need to be broken by allotting ordering to resource types and ensure that each process request resource in an increasing order of enumeration. RECOVERY FROM DEADLOCK Recovery through Rollback Rollback each deadlocked process to some previously checkpoint and restart them. Abort all deadlocked processes (one of the most common solution adopted in OS). Recovery through Preemption Temporarily take a resource away from the process holding it. Give it to another process until deadlock no longer exists. DEADLOCK AVOIDANCE : BANKER’S ALGORITHM Banker’s algorithm is applicable to a resource allocation system with multiple instances of each type. Working : Each process must state its total need for resources. If there are sufficient resources to fulfil the request, then the system determines that whether the system would remain in safe state or not. If the system remains in safe state, then the request is granted to the process. If the system does not remains in safe state, then the request is kept pending and the process is blocked. DATA STRUCTURES FOR BANKER’S ALGORITHM Let ‘N’ be the number of processes and ‘M’ be the number of resources types. Available : Vector of size M indicating the current availability of each type of resources. Max : N×M matrix which indicates the maximum requirements of system resources of each process. Allocation : N×M matrix which indicates the resources allocated to each process. Need : N×M matrix which indicates the remaining need of resources of each process. Request : N×M matrix which indicates the pending request of resources in respect of all processes.