OSEXP7
OSEXP7
7
Process Management: Deadlock
A. Write a program to demonstrate the concept of deadlock
avoidance through Banker’s Algorithm
Experiment No. 7
Aim: Process Management: Deadlock
Objective:
1
Write a program to demonstrate the concept of deadlock avoidance through
Banker’s Algorithm
Theory:
It is a banker algorithm used to avoid deadlock and allocate resources safely to each
process in the computer system. The 'S-State' examines all possible tests or activities
before deciding whether the allocation should be allowed to each process. It also
helps the operating system to successfully share the resources between all the
processes. The banker's algorithm is named because it checks whether a person
should be sanctioned a loan amount or not to help the bank system safely
simulate allocation resources. In this section, we will learn the Banker's
Algorithm in detail. Also, we will solve problems based on the Banker's Algorithm.
To understand the Banker's Algorithm first we will see a real word example of it.
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.
Similarly, it works in an operating system. When a new process is created in a
computer system, the process must provide all types of information to the operating
system like upcoming processes, requests for their resources, counting them, and
delays. Based on these criteria, the operating system decides which process sequence
should be executed or waited so that no deadlock occurs in a system. Therefore, it is
also known as deadlock avoidance algorithm or deadlock detection in the operating
system.
Data Structures for the Banker’s Algorithm.
If Max [i,j] = k, then process Pi may request at most k instances of resource type
Rj Allocation: n x m matrix.
If Allocation[i,j] = k then Pi is currently allocated k
instances of Rj Need: n x m matrix.
If Need[i,j] = k, then Pi may need k more instances of Rj to complete
its task Need [i,j] = Max[i,j] – Allocation [i,j]
2
Safety Algorithm
Program:
#include<s
tdio.h> int
3
main() {
/* array will store at most 5 process with 3 resoures if your process or
[j]);
}
[j]);
}
4
printf("\n need resources
i++) {
for (j = 0; j < c; j++) {
printf("%d\t", need[i][j]);
printf("\n");
/* once process execute variable done will stop them for again
for (i = 0; i
< p; i++)
{ if (done[i]
== 0) {
for (j = 0; j < c; j++) {
if (need[i][j] >
available[j]) break;
}
//when need matrix is not greater then available matrix then if j==c
will true if (j == c) {
safe[cou
nt] = i;
5
done[i] =
1;
/* now process get execute release the resources and add them in available
count++;
terminat
e = 0;
} else
{ ter
min
ate+
+;
}
if (terminate == (p - 1)) {
exist"); break;
}
6
erminate != (p - 1)) {
i++) { printf("p%d\t",
safe[i]);
}
resources4 5
enter allocation of resource of all process 4x5 matrix56
7
6
4x5 matrix3 3
6
8
enter the available
resource3 5
6
-53 -3 2 -2 5
-2 5 5 -5 -2
-5 -2 -1 0 0
0 0 4 5 -1
75 27 24 20 20
p20 p20 p2 p3
Conclusion:
The negative numbers in the need matrix indicate that the allocation matrix entries
are greater than the maximum matrix entries, which is an error in the input. The
program should validate the input to prevent such inconsistencies. Because of the
negative numbers, the program results can't be trusted. The code also has a typo.
The variable 'terminate' is compared to '(p-1)' within the loop, and then is tested
again outside of the loop. If the first test caused the loop to break, the second test
will cause the output to be inaccurate.