Exp 10
Exp 10
1
AIM:
Theory:
The Banker's algorithm operates by considering the current allocation, maximum demand, and available
resources. It maintains several data structures, including the finish array, the work array, and the need
matrix.
The algorithm proceeds by simulating the resource allocation and checking if the system remains in a safe
state after each allocation. If at any point the system is not in a safe state, the algorithm stops and reports that
the system is not in a safe state.
The implementation takes user input for the number of processes, resources, maximum resources required
by each process, resources currently allocated to each process, and available resources. It then calls the
isSafe function to check if the system is in a safe state or not. If it is, the system is considered to be free from
deadlock.
Code:-
#include <stdio.h>
#define MAX_PROCESSES 10
#define MAX_RESOURCES 10
// Initialize finish and work arrays for (int i = 0; i < processes; i++) {
finish[i] = 0;
for (int i = 0; i < processes; i++) { for (int j = 0; j < resources; j++) {
need[i][j] = max[i][j] - alloc[i][j];
}
}
int count = 0;
if (j == resources) {
finish[i] = 1;
found = 1;
count++;
if (found == 0) {
int main() {
printf("Enter the maximum resources required by each process:\n"); for (int i = 0; i < processes; i++) {
for (int j = 0; j < resources; j++) { scanf("%d", &max[i][j]);
}
}
printf("Enter the resources currently allocated to each process:\n"); for (int i = 0; i < processes; i+
+) {
for (int j = 0; j < resources; j++) { scanf("%d", &alloc[i][j]);
}
Result :-
Theory:-
Deadlock detection involves periodically checking the system to determine whether a deadlock has occurred.
The Banker's algorithm can be adapted for deadlock detection by checking if there is any process that can
finish execution. If no such process is found, a deadlock is detected.
Code:-
#include <stdio.h>
#define MAX_PROCESSES 10
#define MAX_RESOURCES 10
int count = 0;
if (j == resources) {
finish[i] = 1;
found = 1; count++;
}
int main() {
int processes, resources;
printf("Enter the resources currently allocated to each process:\n"); for (int i = 0; i < processes; i+
+) {
for (int j = 0; j < resources; j++) { scanf("%d", &allocation[i][j]);
}
printf("Enter the resources requested by each process:\n"); for (int i = 0; i < processes; i++) {
for (int j = 0; j < resources; j++) { scanf("%d", &request[i][j]);
}
Output :-
Result :-