0% found this document useful (0 votes)
49 views18 pages

Microproject Osy

Uploaded by

anivandikar95
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)
49 views18 pages

Microproject Osy

Uploaded by

anivandikar95
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/ 18

D Y PATIL SCHOOL OF ENGINEERING

Via Lohegaon, Pune-412 105

Department of Computer Engineering


2024-25[5th Sem]

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

Under the Guidance of


-Mrs. Mayuri S. Narudkar
D Y PATIL SCHOOL OF ENGINEERING
Department of Computer Engineering
SEMESTER-5th 2024-25

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.

Sr. No. Name of Student Roll Number


1. Adwait Nivandikar 46
2. Aditya Gaikwad 47
3. Hrutika Salunke 48

Place: Pune
Date:

(Mrs. Mayuri S. Narudkar)


Guide Head of Computer Department

2
ACKNOWLEDGEMEMT

It is with profoundly sense of gratitude that we acknowledge from


our guide Mrs. Mayuri S. Narudkar. She has been guide in the true sense of
word, a guide who satisfaction from our word & progress.
We are highly obliged to Mrs. neeta pawar. Head of Computer
Department for aberrance & good co-operation given to us for bringing
this project to almost standard.
We are grateful to our principal Dr. f b sayyad. For proceeding
acknowledgement to us in the connection of this project concluding. We
appreciate the assistance of all staff that helps us in for their sincere &
obliging help to make our project successfully.

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

The Banker’s Algorithm is a smart way for computer systems to manage


how programs use resources, like memory or CPU time. It helps prevent
situations where programs get stuck and can not finish their tasks. This
condition is known as deadlock. By keeping track of what resources each
program needs and what’s available, banker algorithm makes sure that
programs only get what they need in a safe order. This helps computers
run smoothly and efficiently, especially when lots of programs are
running at the same time.
Suppose the number of account holders in a particular bank is 'n', and the
total money in a bank is 'T'. If an account holder applies for a loan; first, the
bank subtracts the loan amount from full cash and then estimates the
cash difference is greater than T to approve the loan amount. These steps
are taken because if another person applies for a loan or withdraws some
amount from the bank, it helps the bank manage and operate all things
without any restriction in the functionality of the banking system.

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:

• Primary Use: The algorithm helps prevent deadlocks by


ensuring that resource allocation happens only if it doesn't
lead to a situation where processes can no longer proceed.
Before allocating resources to a process, the system checks if
it can still remain in a safe state after the allocation.

2. Resource Allocation in Operating Systems:

• Multi-Process Environments: It manages resource allocation


among multiple processes in systems that support
multitasking, ensuring that processes only get resources
when it's safe to do so.
• Avoidance of Starvation: It helps prevent one process from
monopolizing resources, ensuring that all processes
eventually get what they need to complete their tasks.

3. Embedded Systems and Real-Time Systems:

• In some cases, embedded systems or real-time systems that


manage limited resources use variations of the Banker's
Algorithm to ensure resources are allocated efficiently
without causing a system freeze.

4. Database Systems:

• It is sometimes used in database systems to prevent


deadlocks when multiple transactions need access to shared
data or locks on database records.

6
5. Cloud Computing and Virtualization:

• In cloud computing environments, where virtual machines


and applications often contend for shared resources (CPU,
memory, etc.), the Banker's Algorithm can help allocate
resources dynamically without leading to contention or
deadlock.

6. Network Protocols:

• In some network resource allocation protocols, the algorithm


can ensure bandwidth, processing power, or other resources
are allocated fairly among devices or processes requesting
access to the network.

7. Concurrency Control:

• In environments where processes execute concurrently, the


algorithm helps to control access to shared resources
without deadlock, ensuring smooth and efficient execution.

The Banker's Algorithm is effective in systems where processes declare


their maximum resource needs beforehand and resource contention is
common, allowing the system to manage these resources effectively
while avoiding deadlocks.

7
How to perform banker's algorithm

The Banker’s Algorithm follows a series of steps to decide whether it is


safe to grant a requested resource to a process. It checks whether
allocating resources will keep the system in a safe state, ensuring
deadlocks are avoided.
Key Components:

1. Available: Vector of available resources.


2. Max: Matrix that shows the maximum number of each resource type
that each process may need.
3. Allocation: Matrix that shows the number of resources of each type
currently allocated to each process.
4. Need: Matrix that shows the remaining resource needs for each
process, calculated as Need[i][j] = Max[i][j] - Allocation[i][j].
5. Request: Vector representing the request of a process for additional
resources.

Steps to Perform the Banker's Algorithm:


1. Data Structures Initialization:
• Available: [available_1, available_2, ..., available_m] for m
resource types.
• Max: An n x m matrix, where Max[i][j] represents the maximum
demand of process i for resource type j.
• Allocation: An n x m matrix, where Allocation[i][j] represents how
many instances of resource j are currently allocated to process i.
• Need: This matrix is computed as Max[i][j] - Allocation[i][j].
2. Check Resource Request:
When a process P[i] makes a request Request[i] for resources, the
algorithm checks:

8
• Request[i] ≤ Need[i]: If a process requests more than its
maximum declared need, it's an error.

• Request[i] ≤ Available: If a process requests more resources


than are available, it must wait.

3. Pretend to Allocate:
If both conditions are satisfied, the system will pretend to allocate
the resources by temporarily modifying:

• Available = Available - Request[i]


• Allocation[i] = Allocation[i] + Request[i]
• Need[i] = Need[i] - Request[i]
4. Check for a Safe State:
The system must check whether this temporary allocation leaves
the system in a safe state by performing the Safety Algorithm:

• Initialize a Work array to Available and a Finish array (for all


processes) to false.
• Find a process P[i] such that Finish[i] == false and Need[i] <= Work.
If such a process is found, assume the process finishes, set
Finish[i] = true, and update Work = Work + Allocation[i].
• Repeat the above step until either all processes are finished
(Finish[i] == true for all i) or no such process can be found.

If all processes can finish, the system is in a safe state.

5. Revert or Approve Allocation:


• If the system remains in a safe state, the resource allocation is
approved.
• If the system is in an unsafe state, the temporary allocation is
reverted, and the process must wait.

9
Example of banker's algorithm

Let’s consider a system with 3 processes and 3 resource types:

1. Initial Data:
Available: [3, 3, 2]

Max Matrix: Allocation Matrix: Need Matrix(Max-Allo): P0


[7, 5, 3] P0 [0, 1, 0] P0 [7, 4, 3]
P1 [3, 2, 2] P1 [2, 0, 0] P1 [1, 2, 2]
P2 [9, 0, 2] P2 [3, 0, 2] P2 [6, 0, 0]

2. Process P1 requests [1, 0, 2]:


• Check: Request ≤ Need → [1, 0, 2] ≤ [1, 2, 2] (Yes).
• Check: Request ≤ Available → [1, 0, 2] ≤ [3, 3, 2] (Yes).

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:

1. Request Validation: The function first checks whether the requested


resources are within the process's declared maximum need and if
the system has enough available resources.
2. Pretend Allocation: If valid, the algorithm pretends to allocate the
resources.
3. Safety Check: After the pretend allocation, it checks if the system
remains in a safe state using the isSafe() function.
4. Decision: If the system remains safe, the allocation is confirmed.
Otherwise, the allocation is rolled back, and the process must wait.

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

In conclusion, the Banker's Algorithm is a powerful tool for avoiding


deadlocks in resource allocation by ensuring that a system remains in a
safe state. Its primary advantage is its ability to manage multiple
resources and avoid deadlocks while optimizing resource utilization.
However, the algorithm comes with significant drawbacks, such as high
computational overhead, the need for accurate and static information
about maximum resource needs, and its limited scalability in dynamic or
large-scale systems. Despite these limitations, the Banker's Algorithm
remains useful in environments where stability and predictability are
prioritized, especially when resource needs are well-defined in advance.

Its applicability is best suited for smaller, controlled systems where


resource management is crucial, but it may not be the best choice for
more dynamic or complex environments.

18

You might also like