Microproject Osy
Microproject Osy
Micro-project Report of
"banker's algorithm”
Submitted by:
SR. No. Name of Student Roll Number
1. Adwait Nivandikar 46
2. Aditya Gaikwad 47
3. Hrutika Salunke 48
CERTIFICATE
This to certify that Project report entitled “banker's algorithm”Is
submitted in the partial fulfilment of requirement for the award of the
Diploma in Computer Engineering by Maharashtra State Board of
Technical as record of student’s own work carried out by them under the
guidance and supervision at DY Patil School of Engineering (Charholi),
during the academic year 2024-25.
Place: Pune
Date:
2
ACKNOWLEDGEMEMT
3
MICRO-PROJECT REPORT
Title of Micro-project -:
banker's algorithm
Aim of project: Showcasing the banker's algorithm
In this project we Discussed the
• What is banker's algorithm.
• Uses of banker's algorithm.
• How to perform banker's algorithm.
• Example of banker's algorithm.
• C++ code for banker's algorithm.
• Advantages and disadvantages of banker's algorithm.
• Conclusion.
4
What is banker's algorithm
5
Uses of banker's algorithm
The Banker's Algorithm is primarily used in operating systems and
resource management scenarios to ensure safe resource allocation
while avoiding deadlock. Here are the main uses:
1. Deadlock Avoidance:
4. Database Systems:
6
5. Cloud Computing and Virtualization:
6. Network Protocols:
7. Concurrency Control:
7
How to perform banker's algorithm
8
• Request[i] ≤ Need[i]: If a process requests more than its
maximum declared need, it's an error.
3. Pretend to Allocate:
If both conditions are satisfied, the system will pretend to allocate
the resources by temporarily modifying:
9
Example of banker's algorithm
1. Initial Data:
Available: [3, 3, 2]
3. Pretend to Allocate:
• Available: [2, 3, 0]
• Allocation P1: [3, 0, 2]
• Need P1: [0, 2, 0]
4. Safety Check:
• Initially, Work = [2, 3, 0].
• Can P1 finish? Need [0, 2, 0] ≤ Work [2, 3, 0] (Yes), so P1 can finish.
Update Work = Work + Allocation[P1] = [5, 3, 2].
• Can P0 finish? Need [7, 4, 3] ≤ Work [5, 3, 2] (No).
• Can P2 finish? Need [6, 0, 0] ≤ Work [5, 3, 2] (No).
5. Since not all processes can finish, the system would be in an unsafe
state, so the request is denied.
By following these steps, the Banker's Algorithm ensures the system only
allocates resources in a way that avoids deadlock.
10
c++ code for banker's
algorithm.
#include <iostream>
#include <vector>
using namespace std;
#define P 5
#define R 3
bool isSafe(vector<int> avail, vector<vector<int>> max, vector<vector<int>>
allot) {
vector<int> work = avail;
vector<bool> finish(P, false);
vector<vector<int>> need(P, vector<int>(R));
for (int i = 0; i < P; i++) {
for (int j = 0; j < R; j++) {
need[i][j] = max[i][j] - allot[i][j];
}
}
int count = 0;
while (count < P) {
bool found = false;
for (int i = 0; i < P; i++) {
if (!finish[i]) {
int j;
for (j = 0; j < R; j++) {
if (need[i][j] > work[j])
11
break;
}
if (j == R) {
for (int k = 0; k < R; k++)
work[k] += allot[i][k];
finish[i] = true;
found = true;
count++;
}
}
}
if (!found) {
return false;
}
}
return true;
}
void requestResources(int process, vector<int> request, vector<int>
&avail, vector<vector<int>> &max, vector<vector<int>> &allot) {
vector<vector<int>> need(P, vector<int>(R));
for (int i = 0; i < P; i++) {
for (int j = 0; j < R; j++) {
need[i][j] = max[i][j] - allot[i][j];
}
}
for (int i = 0; i < R; i++) {
12
if (request[i] > need[process][i]) {
cout << "Error: Process " << process << " has exceeded its maximum
claim.\n";
return;
}
}
for (int i = 0; i < R; i++) {
if (request[i] > avail[i]) {
cout << "Process " << process << " must wait: not enough resources
available.\n";
return;
}
}
for (int i = 0; i < R; i++) {
avail[i] -= request[i];
allot[process][i] += request[i];
}
if (isSafe(avail, max, allot)) {
cout << "Resources allocated to process " << process << ".\n";
} else {
for (int i = 0; i < R; i++) {
avail[i] += request[i];
allot[process][i] -= request[i];
}
cout << "Process " << process << " must wait: system would enter an
unsafe state.\n";
}
13
}
int main() {
vector<int> processes = {0, 1, 2, 3, 4};
vector<int> avail = {3, 3, 2};
vector<vector<int>> max = {
{7, 5, 3},
{3, 2, 2},
{9, 0, 2},
{2, 2, 2},
{4, 3, 3}
};
vector<vector<int>> allot = {
{0, 1, 0},
{2, 0, 0},
{3, 0, 2},
{2, 1, 1},
{0, 0, 2}
};
vector<int> request = {1, 0, 2};
int process = 1;
cout << "Process " << process << " requesting resources...\n";
requestResources(process, request, avail, max, allot);
return 0;
}
14
Explanation:
• Processes and Resources: There are P processes (5 in this case)
and R resource types (3 in this case).
• avail[]: This is the vector representing the available resources at the
start.
• max[][]: This matrix defines the maximum demand of each process
for each resource.
• allot[][]: This matrix represents the currently allocated resources
for each process.
• request[]: This is a vector representing a sample request for
additional resources from a process (e.g., Process 1 requests [1, 0,
2]).
How it Works:
This C++ version handles resource requests efficiently and ensures that
deadlock is avoided by only allowing resource allocation in a safe state.
15
Advantages and disadvantages of banker's
algorithm
The Banker’s Algorithm is a popular deadlock avoidance algorithm, but
like any algorithm, it has its pros and cons. Here are the main advantages
and disadvantages of using the Banker's Algorithm:
Advantages of the Banker's Algorithm:
1. Deadlock Avoidance:
• Primary Benefit: The main advantage of the Banker's
Algorithm is that it helps prevent deadlocks in systems by
ensuring that resources are allocated in a way that maintains
a safe state.
2. Optimal Resource Utilization:
• By carefully allocating resources only when it's safe, the
algorithm ensures efficient and optimal use of resources
without running into deadlocks.
3. Safe State:
• It guarantees that the system always stays in a safe state. The
system will only grant resource requests that keep the system
in a state where all processes can still finish execution at
some point.
4. Multiple Resources Handled:
• The algorithm can handle multiple types of resources (e.g.,
CPU, memory, I/O) simultaneously and ensures that all are
safely allocated.
5. Predictable:
• The algorithm is deterministic and ensures that decisions are
made based on a well-defined safety check, making it
predictable and reliable in resource-constrained
environments.
16
Disadvantages of the Banker's Algorithm:
1. High Overhead and Complexity:
• Complex Calculations: The algorithm involves complex
calculations, especially for larger systems with many
processes and resource types. The system must frequently
compute whether it is in a safe state, which can be
computationally expensive.
• Time-Consuming: Every time a process makes a request, the
system has to check if it can fulfill it safely, adding significant
overhead to resource allocation.
2. Limited Applicability:
• Fixed Maximum Demand: The Banker's Algorithm requires
each process to declare its maximum resource need at the
start. This is often impractical in dynamic environments where
processes' needs change over time, or they cannot predict
their maximum resource usage upfront.
3. Requires Accurate Information:
• For the algorithm to work, it requires accurate information
about the maximum resource needs and the current
allocation. If this information is inaccurate or changes
dynamically, the algorithm becomes ineffective or risky.
4. Resource Wastage:
• Sometimes, even if resources are available, the algorithm
may deny requests if it cannot guarantee that future requests
can be safely fulfilled. This can lead to resource under
utilization or wastage.
5. Not Suitable for Large Systems:
• For systems with a large number of processes and resources,
the performance cost of running the safety checks and
managing the resource matrices becomes a significant issue.
The algorithm doesn't scale well for very large, complex
systems.
17
Conclusion
18