#include <stdio.h>
#include <stdbool.h>
// Maximum number of processes and resources
#define MAX_PROCESSES 10
#define MAX_RESOURCES 10
// Global variables
int n, m; // Number of processes (n) and resource types (m)
int Available[MAX_RESOURCES];
int Max[MAX_PROCESSES][MAX_RESOURCES];
int Allocation[MAX_PROCESSES][MAX_RESOURCES];
int Need[MAX_PROCESSES][MAX_RESOURCES];
// Function to calculate the Need matrix
void calculateNeed() {
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
Need[i][j] = Max[i][j] - Allocation[i][j];
}
// Safety Algorithm to check if the system is in a safe state
bool isSafe() {
int Work[MAX_RESOURCES];
bool Finish[MAX_PROCESSES];
int safeSequence[MAX_PROCESSES];
int count = 0;
// Initialize Work = Available and Finish = false
for (int j = 0; j < m; j++)
Work[j] = Available[j];
for (int i = 0; i < n; i++)
Finish[i] = false;
// Safety algorithm
while (count < n) {
bool found = false;
for (int i = 0; i < n; i++) {
if (!Finish[i]) {
bool canAllocate = true;
for (int j = 0; j < m; j++) {
if (Need[i][j] > Work[j]) {
canAllocate = false;
break;
}
}
if (canAllocate) {
for (int j = 0; j < m; j++)
Work[j] += Allocation[i][j];
safeSequence[count] = i;
Finish[i] = true;
count++;
found = true;
}
}
}
if (!found) {
printf("System is not in a safe state.\n"); return false;
}
}
// Print safe sequence
printf("System is in a safe state.\nSafe sequence: "); for (int i = 0; i < n; i++)
printf("P%d ", safeSequence
[i
]); return true;
}
// Resource Request Algorithm
void requestResources(int process, int Request[]) {
// Step 1: Check if Request <= Need
for (int j = 0; j < m; j++) {
if (Request[j] > Need[process][j]) {
printf("Error: Request exceeds maximum need for process P%d.\n", process
); return;
}
}
// Step 2: Check if Request <= Available
for (int j = 0; j < m; j++) {
if (Request[j] > Available[j]) {
printf("Resources not available. Process P%d must wait.\n", process
); return;
}
}
// Step 3: Pretend to allocate resources
for (int j = 0; j < m; j++) {
Available[j] -= Request[j];
Allocation[process][j] += Request[j];
Need[process][j] -= Request[j];
}
// Check if the resulting state is safe
if (isSafe()) {
printf("Request granted for process P%d.\n", process
); } else {
// Restore the previous state
for (int j = 0; j < m; j++) {
Available[j] += Request[j];
Allocation[process][j] -= Request[j];
Need[process][j] += Request[j];
}
printf("Request denied for process P%d. System would be unsafe.\n", process
); }
}
int main() {
// Input number of processes and resources
printf("Enter number of processes: "); printf("Enter number of resource types: ");
// Input Available resources
printf("Enter Available resources (for %d resource types):\n", m
); for (int j = 0; j < m; j++) {
scanf("%d", &Available
[j
]); }
// Input Max matrix
printf("Enter Max matrix (%d processes x %d resources):\n", n
, m
); for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) {
printf("Max[P%d][R%d]: ", i
, j
); }
// Input Allocation matrix
printf("Enter Allocation matrix (%d processes x %d resources):\n", n
, m
); for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) {
printf("Allocation[P%d][R%d]: ", i
, j
); scanf("%d", &Allocation
[i
][j
]); }
// Calculate Need matrix
calculateNeed();
// Display matrices
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++)
}
printf("\nAllocation Matrix:\n"); for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++)
printf("%d ", Allocation
[i
][j
]); }
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++)
}
printf("\nAvailable Resources: "); for (int j = 0; j < m; j++)
// Check if the initial state is safe
isSafe();
// Input resource request
int process;
printf("\nEnter process number for resource request (0 to %d): ", n
- 1); int Request[MAX_RESOURCES];
printf("Enter Request vector for P%d (for %d resources):\n", process
, m
); for (int j = 0; j < m; j++) {
scanf("%d", &Request
[j
]); }
// Process the resource request
requestResources(process, Request);
return 0;
}