0% found this document useful (0 votes)
3 views4 pages

Os Lab Assignment 8.

The document presents a C program for implementing a Resource Allocation Graph (RAG) to detect deadlocks in a system. It defines a function that checks for deadlocks based on the allocation and request matrices along with available resources. The program prompts the user to input the number of processes, resources, allocation matrix, request matrix, and available resources, then outputs whether a deadlock is detected or not.

Uploaded by

Sakshi Gupta
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)
3 views4 pages

Os Lab Assignment 8.

The document presents a C program for implementing a Resource Allocation Graph (RAG) to detect deadlocks in a system. It defines a function that checks for deadlocks based on the allocation and request matrices along with available resources. The program prompts the user to input the number of processes, resources, allocation matrix, request matrix, and available resources, then outputs whether a deadlock is detected or not.

Uploaded by

Sakshi Gupta
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/ 4

OS LAB ASSIGNMENT :8

Implementation of Resource Allocation Graph (RAG).


PROGRAM:
#include <stdio.h>
#include <stdbool.h>
#define MAX_PROCESS 10
#define MAX_RESOURCE 10
bool isDeadlock(int n, int m, int allocation[][MAX_RESOURCE], int
request[][MAX_RESOURCE], int available[])
{
bool visited[MAX_PROCESS] = {false};
int work[MAX_RESOURCE];
bool canFinish = false;
for (int i = 0; i < m; ++i)
work[i] = available[i];
while (!canFinish)
{
canFinish = true;
for (int i = 0; i < n; ++i)
{
if (!visited[i])
{
bool canAllocate = true;
for (int j = 0; j < m; ++j)
{
if (request[i][j] > work[j])
{
canAllocate = false;
break;
}
}
if (canAllocate)
{
visited[i] = true;
for (int j = 0; j < m; ++j)
work[j] += allocation[i][j];
canFinish = false;
}}}}
for (int i = 0; i < n; ++i)
{
if (!visited[i])
return true;
}
return false;
}
int main()
{
int n, m;
int allocation [MAX_PROCESS][MAX_RESOURCE];
int request [MAX_PROCESS][MAX_RESOURCE];
int available[MAX_RESOURCE];
printf("Enter number of processes: ");
scanf("%d", &n);
printf("Enter number of resources: ");
scanf("%d", &m);
printf("Enter allocation matrix (%dx%d):\n", n, m);
for (int i = 0; i < n; ++i)
for (int j = 0; j < m; ++j)
scanf("%d", &allocation[i][j]);
printf("Enter request matrix (%dx%d):\n", n, m);
for (int i = 0; i < n; ++i)
for (int j = 0; j < m; ++j)
scanf("%d", &request[i][j]);
printf("Enter available resources (%d):\n", m);
for (int j = 0; j < m; ++j)
scanf("%d", &available[j]);
if (isDeadlock(n, m, allocation, request, available))
printf("Deadlock detected.\n");
else
printf("No deadlock detected.\n");
return 0;
}

OUTPUT

You might also like