This question consists of designing and executing a program using any IDE (C, C+
ID: 3675138 • Letter: T
Question
This question consists of designing and executing a program using any IDE (C, C++, or Java) to illustrate how the banker’s algorithm may be invoked by the operating system to check if the current resource-allocation state is a safe state. The resource-allocation state is defined by the total number of resources in the system, the number of allocated resources, and the maximum resource claim of the processes. The state is stored in the following data structures:
A vector, C, with the total number of instances of every resource type in the system. The element, cj , represents the number of resource instances of resource type j in the system.
A matrix, A, with the number of resources instances of every resource type allocated to every process. The element, aij , is the number of resources instances type j that have been allocated to process i.
A matrix, M, with the maximum claim of every resource type declared by every process. The element, mij , represents the maximum number of resource instances of resource type j that will ever be requested by process i.
Explanation / Answer
#include <stdio.h>
#include <stdbool.h>
int main() {
int curr[5][5];
int max_claim[5][5];
int avl[5];
int alloc[5] = {0, 0, 0, 0, 0};
int max_res[5];
int running[5];
int i, j, exec, r, p;
int count = 0;
bool safe = false;
printf(" Enter the number of resources: ");
scanf("%d", &r);
printf(" Enter the number of processes: ");
scanf("%d", &p);
for (i = 0; i < p; i++) {
running[i] = 1;
count++;
}
printf(" Enter Claim Vector: ");
for (i = 0; i < r; i++)
scanf("%d", &max_res[i]);
printf(" Enter Allocated Resource Table: ");
for (i = 0; i < p; i++) {
for (j = 0; j < r; j++)
scanf("%d", &curr[i][j]);
}
printf(" Enter Maximum Claim table: ");
for (i = 0; i < p; i++) {
for (j = 0; j < r; j++)
scanf("%d", &max_claim[i][j]);
}
printf(" The Claim Vector is: ");
for (i = 0; i < r; i++)
printf("%d ", max_res[i]);
printf(" The Allocated Resource Table: ");
for (i = 0; i < p; i++) {
for (j = 0; j < r; j++)
printf(" %d", curr[i][j]);
printf(" ");
}
printf(" The Maximum Claim Table: ");
for (i = 0; i < p; i++) {
for (j = 0; j < r; j++)
printf(" %d", max_claim[i][j]);
printf(" ");
}
for (i = 0; i < p; i++)
for (j = 0; j < r; j++)
alloc[j] += curr[i][j];
printf(" Allocated resources: ");
for (i = 0; i < r; i++)
printf("%d ", alloc[i]);
for (i = 0; i < r; i++)
avl[i] = max_res[i] - alloc[i];
printf(" Available resources: ");
for (i = 0; i < r; i++)
printf("%d ", avl[i]);
printf(" ");
while (count != 0) {
safe = false;
for (i = 0; i < p; i++) {
if (running[i]) {
exec = 1;
for (j = 0; j < r; j++) {
if (max_claim[i][j] - curr[i][j] > avl[j]) {
exec = 0;
break;
}
}
if (exec) {
printf(" Process%d is executing. ", i + 1);
running[i] = 0;
count--;
safe = true;
for (j = 0; j < r; j++)
avl[j] += curr[i][j];
break;
}
}
}
if (!safe) {
printf(" The processes are in unsafe state.");
break;
}
if (safe)
printf(" The process is in safe state.");
printf(" Available vector: ");
for (i = 0; i < r; i++)
printf("%d ", avl[i]);
}
return 0;
}
/*
Sample Output
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.