DS(EX-3)
DS(EX-3)
S
22IZ016
EXERCISE-3
AIM:
To implement Centralized approach and Hierarchical Approach for detection of
deadlocks in a system.
1. Centralized approach:
Algorithm
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define MAX_PROCESSES 10
// Graph representation using adjacency matrix
int waitForGraph[MAX_PROCESSES][MAX_PROCESSES];
int visited[MAX_PROCESSES];
// Function to perform DFS for cycle detection
bool dfs(int node, int numProcesses) {
if (visited[node] == 1) // Node is already in the current path
return true;
if (visited[node] == 2) // Node already processed
return false;
visited[node] = 1; // Mark as visited in the current path
for (int i = 0; i < numProcesses; i++) {
if (waitForGraph[node][i] == 1) {
if (dfs(i, numProcesses))
return true;
}
}
visited[node] = 2; // Mark as fully processed
return false;
}
// Function to detect deadlock using cycle detection
bool detectDeadlock(int numProcesses) {
for (int i = 0; i < numProcesses; i++) {
visited[i] = 0; // Reset visited array
}
for (int i = 0; i < numProcesses; i++) {
if (visited[i] == 0) {
if (dfs(i, numProcesses))
return true;
}
}
return false;
}
int main() {
int numProcesses;
printf("Enter the number of processes: ");
scanf("%d", &numProcesses);
printf("Enter the Wait-For Graph as an adjacency matrix:\n");
for (int i = 0; i < numProcesses; i++) {
for (int j = 0; j < numProcesses; j++) {
scanf("%d", &waitForGraph[i][j]);
}
}
if (detectDeadlock(numProcesses)) {
printf("Deadlock detected!\n");
} else {
printf("No deadlock detected.\n");
}
return 0;
}
OUTPUT:
Test case 1
Test case 2
2 . Hierarchical Approach:
Algorithm:
● Each local monitor collects wait-for graphs (WFGs) within its domain.
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define MAX_PROCESSES 10
// Graph representation using adjacency matrix
int regionGraphs[2][MAX_PROCESSES][MAX_PROCESSES]; // Two regions
int globalGraph[MAX_PROCESSES][MAX_PROCESSES]; // Combined graph
int visited[MAX_PROCESSES];
// Function to perform DFS for cycle detection
bool dfs(int node, int numProcesses) {
if (visited[node] == 1)
return true;
if (visited[node] == 2)
return false;
visited[node] = 1;
for (int i = 0; i < numProcesses; i++) {
if (globalGraph[node][i] == 1) {
if (dfs(i, numProcesses))
return true;
}
}
visited[node] = 2;
return false;
}
// Function to detect deadlock in a region
bool detectDeadlockInRegion(int region, int numProcesses) {
for (int i = 0; i < numProcesses; i++) {
for (int j = 0; j < numProcesses; j++) {
globalGraph[i][j] = regionGraphs[region][i][j];
return dfs(0, numProcesses);
}
// Function to detect global deadlock
bool detectGlobalDeadlock(int numProcesses) {
for (int i = 0; i < numProcesses; i++) {
for (int j = 0; j < numProcesses; j++) {
globalGraph[i][j] = regionGraphs[0][i][j] || regionGraphs[1][i][j];
return dfs(0, numProcesses);
}
int main() {
int numProcesses;
printf("Enter the number of processes: ");
scanf("%d", &numProcesses);
for (int region = 0; region < 2; region++) {
printf("Enter the Wait-For Graph for Region %d:\n", region + 1);
for (int i = 0; i < numProcesses; i++) {
for (int j = 0; j < numProcesses; j++) {
scanf("%d", ®ionGraphs[region][i][j]);
}
}
}
bool region1Deadlock = detectDeadlockInRegion(0, numProcesses);
bool region2Deadlock = detectDeadlockInRegion(1, numProcesses);
bool globalDeadlock = detectGlobalDeadlock(numProcesses);
if (region1Deadlock || region2Deadlock || globalDeadlock) {
printf("Deadlock detected!\n");
} else {
printf("No deadlock detected.\n");
}
return 0;
}
OUTPUT:
RESULT:
The programs for Centralized approach and Hierarchical Approach for
detection of deadlocks are executed successfully.