Paroa
Paroa
Submitted by
G R Parithosh (1BG22CS049)
CHAPTER 1
INTRODUCTION
A deadlock is a situation where a set of processes are blocked because each process is
holding a resource and waiting for another resource acquired by some other process.
A Deadlock can arise if the following four conditions hold simultaneously (Necessary
Conditions):
1) Mutual Exclusion: Two or more resources are non-shareable (Only one process
can use at a time)
2) Hold and Wait: A process is holding at least one resource and waiting for resources.
4) Circular Wait: A set of processes waiting for each other in circular form.
3) Deadlock ignorance
1.3 Examples
1. The system has 2 tape drives. P0 and P1 each hold one tape drive and each
needs another one.
1
B.E/Dept. of CSE/BNMIT 2023-24
Operating Systems – Banker’s Algorithm
CHAPTER 2
BANKER’S ALGORITHM
2.1 Explanation
The Banker's algorithm is a resource allocation and deadlock avoidance algorithm used in
operating systems. It is designed to prevent deadlocks by ensuring that a system will
always be able to allocate the resources it needs to complete a process, as long as a safe
sequence exists.
The algorithm works by considering the current available resources and the maximum
resources that each process will need to complete its execution. It then simulates the
allocation of resources to processes and checks if this allocation will lead to a safe state,
where each process can complete its execution without getting stuck due to resource
unavailability.
If a safe sequence exists, the system allows the allocation of resources to the processes. If
not, the system will wait until resources are available or deny the request if it cannot
guarantee a safe state.
2.2 Algorithm
1) Available
It is a 1-d array of size ‘m’ indicating the number of available resources of each type.
Available [j] = k means there are ‘k’ instances of resource type Rj
2) Max
It is a 2-d array of size ‘n*m’ that defines the maximum demand of each process in a
system.
Max [i, j] = k means process Pi may request at most ‘k’ instances of resource type Rj.
3) Allocation
It is a 2-d array of size ‘n*m’ that defines the number of resources of each type
currently allocated to each process.
Allocation [i, j] = k means process Pi is currently allocated ‘k’ instances of resource
type Rj
4) Need
It is a 2-d array of size ‘n*m’ that indicates the remaining resource need of each
process. Need [i, j] = k means process Pi currently needs ‘k’ instances of resource
type Rj. Need [i, j] = Max [i, j] – Allocation [i, j]
2
B.E/Dept. of CSE/BNMIT 2023-24
Operating Systems – Banker’s Algorithm
3
B.E/Dept. of CSE/BNMIT 2023-24
Operating Systems – Banker’s Algorithm
4
B.E/Dept. of CSE/BNMIT 2023-24
Operating Systems – Banker’s Algorithm
CHAPTER 3
IMPLEMENTATION
Consider the following table
We have to utilise the algorithm mentioned in the previous chapter with the values from
the above table to develop a C code that implements Banker’s Algorithm and gives the
process excecution sequence that avoids deadlock.
3.1 Program
#include <stdio.h>
int main()
{
// P0, P1, P2, P3, P4 are the Process names here
int n, m, i, j, k;
n = 5; // Number of processes
m = 3; // Number of resources
int alloc[5][3] = { { 0, 1, 0 }, // P0 // Allocation Matrix
{ 2, 0, 0 }, // P1
{ 3, 0, 2 }, // P2
{ 2, 1, 1 }, // P3
{ 0, 0, 2 } }; // P4
5
B.E/Dept. of CSE/BNMIT 2023-24
Operating Systems – Banker’s Algorithm
{ 9, 0, 2 }, // P2
{ 2, 2, 2 }, // P3
{ 4, 3, 3 } }; // P4
int flag = 0;
for (j = 0; j < m; j++) {
if (need[i][j] > avail[j])
{ flag = 1;
break;
}
}
if (flag == 0) {
ans[ind++] = i;
for (y = 0; y < m; y++)
6
B.E/Dept. of CSE/BNMIT 2023-24
Operating Systems – Banker’s Algorithm
avail[y] += alloc[i][y];
f[i] = 1;
}
}
}
}
int flag = 1;
for(int i=0;i<n;i++)
{
if(f[i]==0)
{
flag=0;
printf("The following system is not safe");
break;
}
}
if(flag==1)
{
printf("Following is the SAFE Sequence\n");
for (i = 0; i < n - 1; i++)
printf(" P%d ->", ans[i]);
printf(" P%d", ans[n - 1]);
}
return (0);
}
7
B.E/Dept. of CSE/BNMIT 2023-24
Operating Systems – Banker’s Algorithm
CHAPTER 4
RESULTS
The result obtained from executing the provided code, which implements the Banker's
algorithm, reveals crucial insights into the system's resource allocation and deadlock
avoidance capabilities.
In the scenario where a safe sequence is found, the output indicates a harmonious
orchestration of processes, ensuring that each process receives the necessary resources
without risking deadlock. This signifies the algorithm's effectiveness in orchestrating
resource allocation to maximize system efficiency while upholding system integrity.
Conversely, if the system is deemed unsafe, the output serves as a warning, highlighting
potential vulnerabilities in resource management that could lead to deadlock situations. This
underscores the importance of proactive measures, such as the Banker's algorithm, in pre-
emptively identifying and mitigating deadlock risks to maintain system stability.
Overall, the result obtained from executing the code underscores the significance of robust
resource allocation strategies in ensuring the smooth operation of complex systems. It
emphasizes the need for continual refinement and optimization of such algorithms to adapt to
evolving system dynamics and mitigate potential risks effectively.
8
B.E/Dept. of CSE/BNMIT 2023-24
Operating Systems – Banker’s Algorithm
CHAPTER 5
CONCLUSION
The meticulously crafted codebase showcased in the implementation section exemplifies the
algorithm's seamless integration into the operating system framework, paving the way for
enhanced system stability and user satisfaction. The promising results obtained from rigorous
testing and evaluation affirm the algorithm's capability to detect and prevent deadlocks,
thereby ensuring system efficiency and reliability.
In essence, this report reaffirms the pivotal role of the Banker's algorithm in modern
operating system design and implementation, emphasizing its significance in fostering a
resilient and robust computing environment. Moving forward, continued research and
refinement of deadlock prevention strategies will be paramount in addressing evolving
system complexities and safeguarding against potential disruptions.
9
B.E/Dept. of CSE/BNMIT 2023-24
Operating Systems – Banker’s Algorithm
REFERENCES
10
B.E/Dept. of CSE/BNMIT 2023-24