0% found this document useful (0 votes)
6 views13 pages

Operating System and System

Uploaded by

wovec92659
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views13 pages

Operating System and System

Uploaded by

wovec92659
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

OPERATING SYSTEM AND SYSTEM PROGRAMMING LAB

PROJECT REPORT(15B17CI472)

Topic: deadlock detection using resource allocation


graph

SUBMITTED TO: Ashish Kumar


Submitted by:
Shubhi verma (9921103250)
Saksham Bhardwaj (9921103244)
Kartikeya Chauhan (9921103259)
Labham jain (9921103252)
INTRODUCTION

Aim: to study and implement the deadlock detection using


Resource Allocation Diagram project , to devlop a project which will
saught out the problem of having deadlock state acquired when two
or more processes are working simultaneously in such a way that
each process is holding a resource and waiting for another one.
Consider an example when two trains are coming toward each
other on the same track and there is only one track, none of the
trains can move once they are in front of each other. A similar
situation occurs in operating systems when there are two or more
processes that hold some resources and wait for resources held by
other(s). For example, in the below diagram, Process 1 is holding
Resource 1 and waiting for resource 2 which is acquired by process
2, and process 2 is waiting for resource 1.

Deadlock detection algorithm


Step 1:
Obtain resource allocation graph from the user
Number of process nodes
Number of resource nodes
Resources allocated (matrix)
Resources requested (matrix)
Resource capacities (vector)

Step 2:
Present inputs obtained to the user to verify if required

Step 3:
Identify if there are any sinks in the given graph and reduce such nodes. Update the
process state vector appropriately.

Step 4:
Calculate current resource vector by releasing the resources previously held by the
sinks.

Step 5:
Check if there exists a connected node on the remaining graph.
If yes,
Loop through all processes
check if we can allocate the requested resources to the process.
If yes,
Allow the process to finish and release the previously held resource to the
current resource vector.
Else
Go back to step 5
End of loop – Graph is not completely reducible, so the system Is in a
deadlock state. Print the
irreducible processes from the process state vector.
Exit
Else
Graph is completely reduced and Is deadlock free.
Exit

EXAMPLE GRAPHS

Example#1
No of processes: 3
No of resources: 3
Resources allocated matrix: 1 0 0
0 1 0
0 0 1

Resource request matrix: 0 1 0


1 1 1
0 1 0

Resource capacity vector: 1 2 1


Result: No deadlock

Example#2
No of processes: 3
No of resources: 3
Resources allocated matrix: 0 2 0
1 0 1
0 1 0

Resource request matrix: 2 0 0


0 0 0
0 0 1

Resource capacity vector: 2 3 1


Result: No Deadlock
Example#3
No of processes: 4
No of resources: 2
Resources allocated matrix: 0 1
4 0
0 0
0 0

Resource request matrix: 4 0


0 2
0 1
0 1

Resource capacity vector: 6 2


Result: Deadlock detected

Project objectives:
This problem of having deadlock in real life can be more easily detected with
the help of resource allocation graph. A resource allocation graphs shows
which resource is held by which process and which process is
waiting for a resource of a specific kind. It is amazing and straight –
forward tool to outline how interacting processes can deadlock.
Therefore, resource allocation graph describe what the condition of
the system as far as process and resources are concern like what
number of resources are allocated and what is the request of each
process.
So, resource allocation graph is explained to us what is the state of
the system in terms of processes and resources.

Features of the project:


1. In this approach, The OS doesn't apply any mechanism to
avoid or prevent the deadlocks.
2. The main task of the OS is detecting the deadlocks. The
OS can detect the deadlocks with the help of Resource
allocation graph.
3. If a cycle is being formed in a Resource allocation graph
where all the resources have the single instance then the
system is deadlocked.
4. In Case of Resource allocation graph with multi-instanced
resource types, Cycle is a necessary condition of deadlock
but not the sufficient condition.
5. In contrast, to all above points we are able to say that
rag(resource allocation graphs)
Are the most simplest way to tackle the problem of
deadlock occurred in a system.

running code
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>

using namespace std;

class ResourceAllocationGraph {
private:
int numProcesses;
int numResources;
vector<vector<int>> graph;

public:
ResourceAllocationGraph(int processes, int resources)
: numProcesses(processes), numResources(resources) {
graph.resize(numProcesses,
vector<int>(numResources, 0));
}

void addEdge(int process, int resource, int instances)


{
graph[process][resource] += instances;
}

bool isDeadlocked() {
vector<int> inDegree(numProcesses, 0);
vector<int> work(numResources, 0);
vector<bool> visited(numProcesses, false);
// Calculate in-degree for each process
for (int i = 0; i < numProcesses; ++i) {
for (int j = 0; j < numResources; ++j) {
inDegree[i] += graph[i][j];
work[j] += graph[i][j];
}
}

// Use a queue to perform a topological order


traversal
queue<int> q;
for (int i = 0; i < numProcesses; ++i) {
if (inDegree[i] == 0 && !visited[i]) {
q.push(i);
visited[i] = true;
}
}

while (!q.empty()) {
int process = q.front();
q.pop();

for (int i = 0; i < numResources; ++i) {


work[i] += graph[process][i];
}
for (int i = 0; i < numProcesses; ++i) {
if (graph[process][i] > 0 && !visited[i]) {
inDegree[i]--;
if (inDegree[i] == 0) {
q.push(i);
visited[i] = true;
}
}
}
}

// If any process is not visited, there is a cycle


(deadlock)
return any_of(visited.begin(), visited.end(), [](bool
v) { return !v; });
}
};

int main() {
// Example usage
ResourceAllocationGraph rag(4, 3);

// Set up the resource allocation graph


rag.addEdge(0, 0, 1);
rag.addEdge(1, 1, 2);
rag.addEdge(2, 2, 1);
rag.addEdge(3, 0, 1);
rag.addEdge(3, 1, 1);
// Check for deadlock
if (rag.isDeadlocked()) {
cout << "Deadlock detected!" << endl;
} else {
cout << "No deadlock detected." << endl;
}

return 0;
}

SCREENSHOTS
Output

You might also like