Notes
Notes
---
Experiment Title:
Objective:
To understand and implement the Banker's Algorithm in C++ for resource allocation and
deadlock avoidance in a multi-process environment.
Theory:
The Banker's Algorithm, developed by Edsger Dijkstra, is used in operating systems for
deadlock avoidance by managing resource allocation. This algorithm works by ensuring
that a system remains in a safe state when it allocates resources to processes. A "safe
state" means there exists a sequence in which all processes can complete their tasks
without leading to deadlock.
# Key Terminologies:
1. Safe State : A state in which all processes can be executed to completion without
causing a deadlock.
The algorithm checks if granting a request leads to a safe state by simulating the
allocation. If it’s safe, the resources are allocated; otherwise, the request is denied.
---
Requirements:
1. Data Structures :
- `Need[i][j]`: Remaining resource need for process `i` for resource `j`.
2. Functionality :
- Safety Check : Determine if the system remains in a safe state after allocation.
---
Implementation Steps:
4. Implement the safety algorithm to ensure the system remains in a safe state.
---
Sample C++ Code:
#include <iostream>
int need[P][R];
int safeSeq[P];
int work[R];
work[i] = available[i];
int count = 0;
int j;
break;
if (j == R) {
work[k] += allocation[p][k];
safeSeq[count++] = p;
finish[p] = 1;
found = true;
if (!found) {
return false;
return true;
int main() {
int max[P][R] = {{7, 5, 3}, {3, 2, 2}, {9, 0, 2}, {2, 2, 2}, {4, 3, 3}};
int allocation[P][R] = {{0, 1, 0}, {2, 0, 0}, {3, 0, 2}, {2, 1, 1}, {0, 0, 2}};
return 0;
---
Sample Output:
---
Explanation of Code:
---
Conclusion:
Viva Questions:
```cpp
#include <iostream>
```
- **`P`** and **`R`** are constants representing the number of processes and
resource types. In this example, we have `5` processes and `3` types of resources.
```cpp
```
- This function calculates the **Need** matrix, which shows the remaining resources
each process requires to complete its task.
- **Formula**: `Need[i][j] = Max[i][j] - Allocation[i][j]`.
- The function takes three matrices as input: `need`, `max`, and `allocation`.
```cpp
int need[P][R];
int safeSeq[P];
int work[R];
work[i] = available[i];
int count = 0;
if (!finish[p]) {
int j;
break;
if (j == R) {
work[k] += allocation[p][k];
safeSeq[count++] = p;
finish[p] = 1;
found = true;
if (!found) {
return false;
return true;
```
The **`isSafe`** function performs the main steps of the Banker's Algorithm.
2. **Finish Array**: `finish` keeps track of completed processes. Initially, all are set to
`false`.
The algorithm checks each process to see if it can finish with the resources in the
`work` array.
- For each resource type `j`, verify if the `Need` of process `p` for resource `j` is
less than or equal to `work[j]`. If this condition fails, the process cannot finish with the
current resources, and the algorithm breaks out of the loop.
- If `j` equals `R` (all resource checks passed), the process can safely finish:
- Set `found` to `true`, indicating at least one process was able to proceed in this
iteration.
2. If no process could proceed (`found` is `false`), the system is unsafe, and the
function exits, returning `false`.
3. If all processes finish successfully, the function outputs the safe sequence and
returns `true`, indicating a safe state.
```cpp
int main() {
int processes[] = {0, 1, 2, 3, 4};
int max[P][R] = {{7, 5, 3}, {3, 2, 2}, {9, 0, 2}, {2, 2, 2}, {4, 3, 3}};
int allocation[P][R] = {{0, 1, 0}, {2, 0, 0}, {3, 0, 2}, {2, 1, 1}, {0, 0, 2}};
return 0;
```
- **Max Matrix**: Specifies each process's maximum requirement for each resource
type.
The `main` function initializes these matrices and then calls `isSafe` to check if the
system is in a safe state. If it is, the safe sequence is displayed.
---
- **Safety Check**: The algorithm ensures the system remains in a safe state by only
granting requests that lead to a safe sequence.
- **Importance of the Safe Sequence**: A valid safe sequence ensures that all
processes can finish without causing deadlock.