COS Exp7 Vijit
COS Exp7 Vijit
Expt No: 7
Title: Simulate Bankers Algorithm for Deadlock Avoidance.
COs to be achieved:
CO 2: To illustrate and analyze the process, threads, process scheduling and thread
scheduling.
Theory:
The Banker's algorithm is a resource allocation and deadlock avoidance algorithm developed by
Edsger Dijkstra.
DATA STRUCTURES
(where n is the number of processes in the system and m is the number of resource types)
Implementation details:
#include <stdio.h>
#define P 3 // Number of processes
#define R 3 // Number of resource types
int main() {
int available[R] = {3, 3, 2}; // Initial available resources
int max[P][R] = {
{7, 5, 3},
{3, 2, 2},
{9, 0, 2}
};
int allocation[P][R] = {
{0, 1, 0},
{2, 0, 0},
{3, 0, 2}
};
resources to work
for (int j = 0; j < R; j++) {
work[j] += allocation[p][j];
}
// Add the process to the safe sequence and mark it as finished
safeSequence[count++] = p;
finish[p] = 1;
found = 1;
printf("After Process %d completes, available resources are: {", p);
for (int k = 0; k < R; k++) {
printf("%d ", work[k]);
}
printf("}\n");
}
}
}
Output:
4) Consider a system having ‘m’ resources of the same type. These resources are
shared by 3 processes A, B, C which have peak time demands of 3, 4, 6 respectively. The
minimum value of ‘m’ that ensures that deadlock will never occur is
a) 11
b) 12
c) 13
d) 14
Conclusion:
In this experiment, we explored key concepts related to deadlock in operating systems, including
detection, prevention, and avoidance techniques. We implemented the Banker's Algorithm, which
ensures safe resource allocation by checking system states to avoid deadlock.