0% found this document useful (0 votes)
5 views5 pages

Experiment 3

Uploaded by

Sahaj jain
Copyright
© © All Rights Reserved
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% found this document useful (0 votes)
5 views5 pages

Experiment 3

Uploaded by

Sahaj jain
Copyright
© © All Rights Reserved
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/ 5

Banker's Algorithm: Theory,

Implementation, and Example


1. Theory
The Banker's Algorithm is a resource allocation and deadlock avoidance algorithm used in operating systems. It was
developed by Edsger Dijkstra. The algorithm is used to determine whether allocating resources to a process will lead to a
safe state or an unsafe state.

Key Concepts:
1. Safe State: A state is considered safe if the system can allocate resources to each process in some order and
still avoid a deadlock.

2. Unsafe State: A state that is not safe. It may lead to a deadlock.

3. Available Resources: The number of available resources of each type.

4. Maximum Demand: The maximum number of resources of each type that a process may need.

5. Allocation: The number of resources of each type currently allocated to each process.

6. Need: The remaining resources that a process may request to complete its task.

How the Algorithm Works:


1. Initialize the system state with available resources, maximum demand, and current allocation.
2. Calculate the need matrix by subtracting allocation from maximum demand.
3. Find a process whose need can be satisfied with the available resources.
4. If found, temporarily allocate the resources and mark the process as finished.
5. Add the allocated resources back to the available resources.
6. Repeat steps 3-5 until all processes are marked as finished.
7. If all processes can finish, the state is safe. Otherwise, it's unsafe.

2. C Program Implementation
#include <stdio.h>

#define MAX_PROCESSES 10
#define MAX_RESOURCES 5

void print_matrix(int rows, int cols, int matrix[MAX_PROCESSES][MAX_RESOURCES]) {


for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
}

int main() {
int processes, resources;
int available[MAX_RESOURCES];
int max[MAX_PROCESSES][MAX_RESOURCES];
int allocation[MAX_PROCESSES][MAX_RESOURCES];
int need[MAX_PROCESSES][MAX_RESOURCES];
int safe_sequence[MAX_PROCESSES];

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


scanf("%d", &processes);

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


scanf("%d", &resources);

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


for (int i = 0; i < resources; i++) {
scanf("%d", &available[i]);
}

printf("Enter the maximum resource matrix:\n");


for (int i = 0; i < processes; i++) {
for (int j = 0; j < resources; j++) {
scanf("%d", &max[i][j]);
}
}

printf("Enter the allocation matrix:\n");


for (int i = 0; i < processes; i++) {
for (int j = 0; j < resources; j++) {
scanf("%d", &allocation[i][j]);
need[i][j] = max[i][j] - allocation[i][j];
}
}

// Banker's Algorithm
int work[MAX_RESOURCES];
int finish[MAX_PROCESSES] = {0};
int safe_index = 0;

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


work[i] = available[i];
}

while (safe_index < processes) {


int found = 0;
for (int i = 0; i < processes; i++) {
if (!finish[i]) {
int j;
for (j = 0; j < resources; j++) {
if (need[i][j] > work[j])
break;
}
if (j == resources) {
for (int k = 0; k < resources; k++) {
work[k] += allocation[i][k];
}
safe_sequence[safe_index++] = i;
finish[i] = 1;
found = 1;
}
}
}
if (!found) {
printf("System is not in safe state.\n");
return 0;
}
}

printf("System is in safe state.\nSafe sequence: ");


for (int i = 0; i < processes; i++) {
printf("%d ", safe_sequence[i]);
}
printf("\n");
return 0;
}

3. Example Output
Enter the number of processes: 5
Enter the number of resources: 3
Enter the available resources:
3 3 2
Enter the maximum resource matrix:
7 5 3
3 2 2
9 0 2
2 2 2
4 3 3
Enter the allocation matrix:
0 1 0
2 0 0
3 0 2
2 1 1
0 0 2
System is in safe state.
Safe sequence: 1 3 4 0 2

4. Explanation of the Example


In this example:

We have 5 processes (P0 to P4) and 3 types of resources (A, B, C).


Available resources: 3 instances of A, 3 of B, and 2 of C.
The maximum resource matrix shows the maximum demand of each process.
The allocation matrix shows the currently allocated resources to each process.

The algorithm determines that the system is in a safe state and provides a safe sequence for process execution: P1, P3,
P4, P0, P2.

This means that if we execute the processes in this order, we can guarantee that all processes will complete without
causing a deadlock, even in the worst-case scenario of each process requesting its maximum resources.

5. Conclusion
The Banker's Algorithm is a powerful tool for deadlock avoidance in resource allocation systems. While it has some
limitations (like requiring knowledge of maximum resource needs in advance), it provides a systematic approach to
maintaining system safety in multi-process, multi-resource environments.

You might also like