Wait For Graph Deadlock Detection in Distributed System
Last Updated :
28 Apr, 2022
Deadlocks are a fundamental problem in distributed systems. A process may request resources in any order and a process can request resources while holding others. A Deadlock is a situation where a set of processes are blocked as each process in a Distributed system is holding some resources and that acquired resources are needed by some other processes.
Example:
If there are three processes p1,p2 and p1 are acquiring r1 resource and that r1 is needed by p2 which is acquiring another resource r2 and that is needed by p1. Here cycle occurs. It is called a deadlock.
Here in the above graph we found a cycle from P1 to P2 and again to P1. So we can say that the system is in a deadlock state.
The Problem of deadlocks has been generally studied in distributed systems under the following models :
- The system has only reusable resources.
- Processes are allowed only exclusive access to resources.
- There is only one copy of each resource.
Deadlock Detection:
The deadlock Detection Algorithm is of two types:
- Wait-for-Graph Algorithm (Single Instance)
- Banker's Algorithm (Multiple Instance)
Wait-for-Graph Algorithm: It is a variant of the Resource Allocation graph. In this algorithm, we only have processes as vertices in the graph. If the Wait-for-Graph contains a cycle then we can say the system is in a Deadlock state. Now we will discuss how the Resource Allocation graph will be converted into Wait-for-Graph in an Algorithmic Approach. We need to remove resources while converting from Resource Allocation Graph to Wait-for-Graph.
- Resource Allocation Graph: Contains Processes and Resources.
- Wait-for-Graph: Contains only Processes after removing the Resources while conversion from Resource Allocation Graph.
Algorithm:
Step 1: Take the first process (Pi) from the resource allocation graph and check the path in which it is acquiring resource (Ri), and start a wait-for-graph with that particular process.
Step 2: Make a path for the Wait-for-Graph in which there will be no Resource included from the current process (Pi) to next process (Pj), from that next process (Pj) find a resource (Rj) that will be acquired by next Process (Pk) which is released from Process (Pj).
Step 3: Repeat Step 2 for all the processes.
Step 4: After completion of all processes, if we find a closed-loop cycle then the system is in a deadlock state, and deadlock is detected.
Now we will see the working of this Algorithm with an Example. Consider a Resource Allocation Graph with 4 Processes P1, P2, P3, P4, and 4 Resources R1, R2, R3, R4. Find if there is a deadlock in the Graph using the Wait for Graph-based deadlock detection algorithm.
Step 1: First take Process P1 which is waiting for Resource R1, resource R1 is acquired by Process P2, Start a Wait-for-Graph for the above Resource Allocation Graph.
Step 2: Now we can observe that there is a path from P1 to P2 as P1 is waiting for R1 which is been acquired by P2. Now the Graph would be after removing resource R1 looks like.
Step 3: From P2 we can observe a path from P2 to P3 as P2 is waiting for R4 which is acquired by P3. So make a path from P2 to P3 after removing resource R4 looks like.
Step 4: From P3 we find a path to P4 as it is waiting for P3 which is acquired by P4. After removing R3 the graph looks like this.
Step 5: Here we can find Process P4 is waiting for R2 which is acquired by P1. So finally the Wait-for-Graph is as follows:
Step 6: Finally In this Graph, we found a cycle as the Process P4 again came back to the Process P1 which is the starting point (i.e., it's a closed-loop). So, According to the Algorithm if we found a closed loop, then the system is in a deadlock state. So here we can say the system is in a deadlock state.
Now consider another Resource Allocation Graph with 4 Processes P1, P2, P3, P4, and 3 Resources R1, R2, R3. Find if there is a deadlock in the Graph using the Wait for Graph-based deadlock detection algorithm.
Step 1: First take Process P1 which is waiting for Resource R1, resource R1 is acquired by Process P2, Start a Wait-for-Graph for the above Resource Allocation Graph.
Step 2: Now we can observe that there is a path from P1 to P2 and also from P1 to P4 as P1 is waiting for R1 which is been acquired by P2 and P1 is also waiting for R2 which is acquired by P4. Now the Graph would be after removing resources R1 and R2 looks like.
Step 3: From P2 we can observe a path from P2 to P3 as P2 is waiting for R3 which is acquired by P3. So make a path from P2 to P3 after removing resource R3 looks like.
Step 4: Here we can find Process P4 is waiting for R3 which is acquired by P3. So finally the Wait-for-Graph looks like after removing Resource R3 looks like.
Step 5: In this Graph, we don't find a cycle as no process came back to the starting point (i.e., there is no closed loop). So, According to the Algorithm if we found a closed loop, then the system is in a deadlock state. But here we didn't find any closed loop so the system is not in a deadlock state. The system is in a safe state.
Note: In Example 2, even though it looks like a loop but there is no process that has reached the first process, or starting point again. So there is no closed loop.
Issues in Wait-for-Graph based Deadlock Detection Algorithm :
For every resource we call WFG Detection Algorithm, the computation time would be very high for the CPU if there are many resources. The solution for this is, rather than calling this algorithm for every resource request that cannot be granted immediately. Just invoke the algorithm after a definite interval.
Similar Reads
Operating System Tutorial An Operating System(OS) is a software that manages and handles hardware and software resources of a computing device. Responsible for managing and controlling all the activities and sharing of computer resources among different running applications.A low-level Software that includes all the basic fu
4 min read
OS Basics
Process & Threads
CPU Scheduling
Deadlock
Memory & Disk Management
Memory Management in Operating SystemThe term memory can be defined as a collection of data in a specific format. It is used to store instructions and process data. The memory comprises a large array or group of words or bytes, each with its own location. The primary purpose of a computer system is to execute programs. These programs,
10 min read
Fixed (or static) Partitioning in Operating SystemFixed partitioning, also known as static partitioning, is one of the earliest memory management techniques used in operating systems. In this method, the main memory is divided into a fixed number of partitions at system startup, and each partition is allocated to a process. These partitions remain
8 min read
Variable (or Dynamic) Partitioning in Operating SystemIn operating systems, Memory Management is the function responsible for allocating and managing a computerâs main memory. The memory Management function keeps track of the status of each memory location, either allocated or free to ensure effective and efficient use of Primary Memory. Below are Memo
4 min read
Paging in Operating SystemPaging is the process of moving parts of a program, called pages, from secondary storage (like a hard drive) into the main memory (RAM). The main idea behind paging is to break a program into smaller fixed-size blocks called pages.To keep track of where each page is stored in memory, the operating s
8 min read
Segmentation in Operating SystemA process is divided into Segments. The chunks that a program is divided into which are not necessarily all of the exact sizes are called segments. Segmentation gives the user's view of the process which paging does not provide. Here the user's view is mapped to physical memory. Types of Segmentatio
4 min read
Segmentation in Operating SystemA process is divided into Segments. The chunks that a program is divided into which are not necessarily all of the exact sizes are called segments. Segmentation gives the user's view of the process which paging does not provide. Here the user's view is mapped to physical memory. Types of Segmentatio
4 min read
Page Replacement Algorithms in Operating SystemsIn an operating system that uses paging for memory management, a page replacement algorithm is needed to decide which page needs to be replaced when a new page comes in. Page replacement becomes necessary when a page fault occurs and no free page frames are in memory. in this article, we will discus
7 min read
File Systems in Operating SystemA computer file is defined as a medium used for saving and managing data in the computer system. The data stored in the computer system is completely in digital format, although there can be various types of files that help us to store the data.File systems are a crucial part of any operating system
8 min read
File Systems in Operating SystemA computer file is defined as a medium used for saving and managing data in the computer system. The data stored in the computer system is completely in digital format, although there can be various types of files that help us to store the data.File systems are a crucial part of any operating system
8 min read
Advanced OS
Practice