Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

Department of CSE/IT

Jaypee Institute of Information Technology, Noida

Academic Year : 2020-2021

Data Structures : Project Report


Topic : Deadlock Detection in a Process Topology

Submitted by: Submitted to:


• Nandini Varshney – 19103051 Ms. Parul Aggarwal
• Harshita Goyal – 19103060
• Deepanshu Choudhary - 19103057
Problem Statement
In this project, we intend to detect the presence of any
deadlock occurring in a process topology/dependency by
finding the presence of a cycle using Topological Sort
Algorithm.

Introduction
In the work progress of any task in a typical Operating System
(OS), many processes run concurrently in accordance to the
system for completion of the task. These processes are
dependent on each other in a complex way, thus making a
dependency on each other, which can be represented in a
Directed-Unweighted Graph.

Concerning to such dependency amongst the processes, a


deadlock is defined to occur when a process enters a into a
waiting state because of some other dependent process,
which in turn waits for another and so on. As a result, all such
processes are bound in a deadlock and can’t complete their
execution.
Data Structures Used
We will use the following STL containers to implement the
Directed-Unweighted Graph and Topological Sort Algorithm
using BFS:

• Vectors
• Queues
• Maps
• Set

Our project will be a menu-driven program that would allow


the user to:

• Add a new process to the existing topology.


• Display processes in the existing topology.
• Detect the presence of a deadlock in the topology.
Detailed Design
Starting with the main function, we’ve created a menu for
easy readability of the code; which has the options to
create and display a topology as well as to detect a
deadlock in the system.
A class named graph is defined which uses STL maps
and vectors to store the processes in a graph. It uses a
class template for flexibility of the data types. The class
contains the below listed member functions:
➢ Add_edge() – This function adds edges
(processes) to the graph.
➢ Display() – This function displays all the nodes of
the adjacency list in the same manner as entered by
the user.
➢ Deadlock() – This is the core function of our
program which detects whether a deadlock is present
in the topology or not. We’ve applied topological sort
using BFS(Breadth First Search).
Implementation
Details
Class graph has private member Adjacency list which is
implemented through unordered map. Unordered map’s key
represents the nodes and value stores a vector that represents
the neighbours.
Class graph has following public member functions:-

AddEdge()- Inserts processes relation where 1st value ie T a


(where T is the datatype as described in the template)
becomes the node(key) and neighbour(value) T b is pushed in
the vector of the key using push_back().

Display()- Iterates through the adjacency list using 2 nested for


loops. Outer iteration is used to display the node (keys of
adjacency list) and inner iteration displays its neighbours
stored in vector.

Deadlock()- Initially, it checks if adjacency list is empty or not.


If not empty, then presence of any deadlock is checked.
Firstly, indegree of all the nodes is calculated. For a vertex, the
number of head ends adjacent to a vertex is called the
indegree of the vertex.
A map indegree is defined (key represents node and value
represents indegree) to store the indegrees() of all the nodes
and indegrees of all the nodes is initialized with 0.
A set is created to insert each node once so as to get the
number of nodes (size of the set) in the graph.
Then iteration through the adjacency list takes place and each
time a node is visited, indegree of all its neighbours is
incremented by 1. Nodes are simultaneously pushed into the
set.
A variable ( say cnt) to keep the count of no of nodes popped
from the queue is maintained.
Now, all nodes having 0 indegree are pushed into the queue.
Front element of the queue is stored in a variable node then it
is popped from the queue. Also, cnt is increases by 1. All the
neighbours of the node are visited and neighbour’s indegree is
decremented by 1. If at any point, indegree of certain
neighbour becomes 0, then it is pushed into the queue.
This process is repeated until the queue becomes empty.
In the end, if cnt is not equal to the no of nodes, then deadlock
is said to be present.
Results
We’ve check the working of our program for the following
topologies:

Topology-1
Topology-2
When no processes are present in the topology:-

Conclusion
From the above examples, we can infer that our program is
capable of detecting deadlock in any given topology.
If no deadlock is present in the topology, then we can find out
the order in which all processes will execute.

You might also like