The document presents an implementation of the Banker's Algorithm in C, which checks the safety of a system with respect to resource allocation. It defines a function to determine if the system is in a safe state based on current allocations, maximum needs, and available resources. The main function initializes sample data and invokes the safety check, printing whether the system is safe or potentially in a deadlock state.
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 ratings0% found this document useful (0 votes)
4 views6 pages
Ex 4
The document presents an implementation of the Banker's Algorithm in C, which checks the safety of a system with respect to resource allocation. It defines a function to determine if the system is in a safe state based on current allocations, maximum needs, and available resources. The main function initializes sample data and invokes the safety check, printing whether the system is safe or potentially in a deadlock state.
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/ 6
Banker's Algorithm
#include <stdio.h> #include <stdbool.h>
// Function to check system safety using Banker's Algorithm
bool is_safe_state( int allocation[5][3], int max_need[5][3], int available[3], int processes, int resources) { int work[3]; bool finish[5] = {false}; int i, j; for (i = 0; i < resources; i++) work[i] = available[i]; bool progress; do { progress = false; for (i = 0; i < processes; i++) { if (!finish[i]) { bool can_allocate = true; for (j = 0; j < resources; j++) { if ((max_need[i][j] - allocation[i][j]) > work[j]) { can_allocate = false; break; } } if (can_allocate) { for (j = 0; j < resources; j++) work[j] += allocation[i][j]; finish[i] = true; progress = true; } } } } while (progress); for (i = 0; i < processes; i++) { if (!finish[i]) return false; } return true; } int main() { int allocation[5][3] = { {0, 1, 0}, {2, 0, 0}, {3, 0, 2}, {2, 1, 1}, {0, 0, 2}}; int max_need[5][3] = { {7, 5, 3}, {3, 2, 2}, {9, 0, 2}, {2, 2, 2}, {4, 3, 3} }; int available[MAX_RESOURCES] = {3, 3, 2}; printf("Checking system safety using Banker's Algorithm:\n"); if (is_safe_state(allocation, max_need, available, 5, 3)) printf("The system is in a safe state.\n"); else printf("The system is in an unsafe state (possible deadlock).\n"); return 0; }