Experiment 3
Experiment 3
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.
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.
2. C Program Implementation
#include <stdio.h>
#define MAX_PROCESSES 10
#define MAX_RESOURCES 5
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];
// Banker's Algorithm
int work[MAX_RESOURCES];
int finish[MAX_PROCESSES] = {0};
int safe_index = 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
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.