program 5

 #include <stdio.h>


#define MAX_PROCESS 10

#define MAX_RESOURCES 10


int available[MAX_RESOURCES];

int maximum[MAX_PROCESS][MAX_RESOURCES];

int allocation[MAX_PROCESS][MAX_RESOURCES];

int need[MAX_PROCESS][MAX_RESOURCES];


int n, m; // Number of processes and resources


void initialize() {

// Input the number of processes and resources

printf("Enter the number of processes: ");

scanf("%d", &n);


printf("Enter the number of resources: ");

scanf("%d", &m);


// Input the available resources

printf("Enter the available resources:\n");

for (int i = 0; i < m; i++) {

printf("Resource %d: ", i + 1);

scanf("%d", &available[i]);

}


// Input maximum resources needed by each process

printf("Enter the maximum resources needed by each process:\n");

for (int i = 0; i < n; i++) {

printf("Process %d:\n", i + 1);


for (int j = 0; j < m; j++) {

printf("Resource %d: ", j + 1);

scanf("%d", &maximum[i][j]);

}

}


// Input resources currently allocated to each process

printf("Enter the resources currently allocated to each process:\n");

for (int i = 0; i < n; i++) {

printf("Process %d:\n", i + 1);

for (int j = 0; j < m; j++) {

printf("Resource %d: ", j + 1);

scanf("%d", &allocation[i][j]);


// Calculate the need matrix

need[i][j] = maximum[i][j] - allocation[i][j];

}

}

}


int isSafe(int process, int request[]) {

// Check if the request can be granted

for (int i = 0; i < m; i++) {

if (request[i] > need[process][i] || request[i] > available[i]) {

return 0; // Unsafe

}

}


// Simulate resource allocation

for (int i = 0; i < m; i++) {

available[i] -= request[i];


allocation[process][i] += request[i];

need[process][i] -= request[i];

}


// Check for safety

int work[MAX_RESOURCES];

int finish[MAX_PROCESS] = {0};


for (int i = 0; i < m; i++) {

work[i] = available[i];

}


int count = 0;

while (count < n) {

int found = 0;

for (int i = 0; i < n; i++) {

if (!finish[i]) {

int j;

for (j = 0; j < m; j++) {

if (need[i][j] > work[j]) {

break;

}

}

if (j == m) {

for (int k = 0; k < m; k++) {

work[k] += allocation[i][k];

}

finish[i] = 1;

found = 1;

count++;

}


}

}

if (!found) {

// Roll back changes and return unsafe

for (int i = 0; i < m; i++) {

available[i] += request[i];

allocation[process][i] -= request[i];

need[process][i] += request[i];

}

return 0; // Unsafe

}

}


return 1; // Safe

}


void requestResources() {

int process;

printf("Enter the process number requesting resources: ");

scanf("%d", &process);


int request[MAX_RESOURCES];

printf("Enter the request for each resource:\n");

for (int i = 0; i < m; i++) {

printf("Resource %d: ", i + 1);

scanf("%d", &request[i]);

}


if (isSafe(process - 1, request)) {

printf("Request granted. System is safe.\n");

} else {


printf("Request denied. System is unsafe.\n");

}

}


void runSafetyAlgorithm() {

// Check if the system is in a safe state

int work[MAX_RESOURCES];

int finish[MAX_PROCESS] = {0};


for (int i = 0; i < m; i++) {

work[i] = available[i];

}


int count = 0;

while (count < n) {

int found = 0;

for (int i = 0; i < n; i++) {

if (!finish[i]) {

int j;

for (j = 0; j < m; j++) {

if (need[i][j] > work[j]) {

break;

}

}

if (j == m) {

for (int k = 0; k < m; k++) {

work[k] += allocation[i][k];

}

finish[i] = 1;

found = 1;

count++;


printf("Process %d finished\n", i + 1);

}

}

}

if (!found) {

printf("System is unsafe.\n");

return;

}

}


printf("System is safe.\n");

}


int main() {

initialize();


int choice;

do {

printf("\nBanker's Algorithm Menu:\n");

printf("1. Request Resources\n");

printf("2. Run Safety Algorithm\n");

printf("3. Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);


switch (choice) {

case 1:

requestResources();

break;

case 2:

runSafetyAlgorithm();


break;

case 3:

printf("Exiting the program.\n");

break;

default:

printf("Invalid choice. Please enter a valid option.\n");

}

} while (choice != 3);


return 0;

}

Comments

Popular posts from this blog

program 3

program 4