0% found this document useful (0 votes)
5 views7 pages

DS(EX-3)

Uploaded by

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

DS(EX-3)

Uploaded by

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

JEYANTH.

S
22IZ016

EXERCISE-3

AIM:
To implement Centralized approach and Hierarchical Approach for detection of
deadlocks in a system.

1. Centralized approach:

In distributed systems, multiple processes run concurrently, sharing resources


that may lead to deadlocks—situations where a set of processes waits
indefinitely for resources held by others. A centralized approach employs a single
node or process to monitor system-wide resource allocation and detect cycles in
the wait-for graph, indicating potential deadlocks.

Algorithm

1.​ Resource Allocation Monitoring:


○​ Each process informs the central coordinator about resource
requests and releases.
○​ The coordinator maintains an updated WFG.
2.​ Cycle Detection:
○​ The coordinator applies a cycle detection algorithm (e.g., depth-first
search or graph reduction) to identify deadlocks.
3.​ Deadlock Resolution:
○​ If a cycle is found, the system selects a victim process to terminate
or preempts resources to break the cycle.

The centralized approach to deadlock detection provides an efficient mechanism


for identifying and resolving deadlocks in distributed systems. While it simplifies
monitoring and reduces communication overhead, its reliance on a single
coordinator poses scalability and fault tolerance challenges. Future
improvements may involve hybrid or distributed enhancements to mitigate these
limitations.
CODE:

#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:

The hierarchical approach provides a structured solution by dividing the system


into multiple levels, allowing localized deadlock detection while minimizing
communication overhead.

Algorithm:

●​ Each local monitor collects wait-for graphs (WFGs) within its domain.

●​ Local monitors analyze cycles in the WFGs and send reports to


higher-level controllers if cycles exist.

●​ The higher-level controllers merge reports and analyze inter-domain


cycles.

●​ If a deadlock is confirmed, a resolution strategy is applied, such as


selecting a victim process for termination or rollback.

The hierarchical approach for deadlock detection provides an efficient, scalable


method for managing resource allocation conflicts in distributed systems. While it
introduces complexity in coordination, its ability to localize detection and
minimize system-wide communication overhead makes it a viable solution for
large-scale networks.
CODE:

#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", &regionGraphs[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.

You might also like