[go: up one dir, main page]

0% found this document useful (0 votes)
22 views6 pages

lab Exp OS 6

The document outlines an experiment to develop a C program that simulates the Bankers Algorithm for deadlock avoidance in operating systems. It explains the concept of deadlock, the necessary data structures, and algorithms for determining safe states and resource allocation. The document also includes a complete C program that implements these algorithms to manage resource allocation among processes.

Uploaded by

freakstudy38
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)
22 views6 pages

lab Exp OS 6

The document outlines an experiment to develop a C program that simulates the Bankers Algorithm for deadlock avoidance in operating systems. It explains the concept of deadlock, the necessary data structures, and algorithms for determining safe states and resource allocation. The document also includes a complete C program that implements these algorithms to manage resource allocation among processes.

Uploaded by

freakstudy38
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/ 6

Experiment No.

Performed Date

Title: Develop a C program to simulate Bankers Algorithm for Deadlock Avoidance

Aim: To study the use Bankers Algorithm for Deadlock Avoidance

Deadlock is a situation where in two or more competing actions are waiting f or the other to finish,
and thus neither ever does. When a new process enters a system, it must declare the Maximum
number of instances of each resource type it needed. This number may exceed the total number of
resources in the system. When the user request a set of resources, the system must determine
whether the allocation of each resources will leave the system in safe state. If it will the resources
are allocation; otherwise the process must wait until some other process release the resources.

Data structures:

n-Number of process,
m-number of resource types.
Available: Available[j]=k, k – instance of resource type Rj is available.
Max: If max[i, j]=k, Pi may request at most k instances resource Rj.
Allocation: If Allocation [i, j]=k, Pi allocated to k instances of resource Rj Need: If Need[I, j]=k,
Pi may need k more instances of resource type Rj, Need[I, j]=Max[I, j]- Allocation[I, j];

Safety Algorithm

1. Work and Finish be the vector of length m and n respectively,


Work=Available and
Finish[i] =False.
2. Find an i such that both
Finish[i] =False
Need<=Work
If no such Iexists go to step 4.
3. work= work + Allocation, Finish[i] =True;
4. if Finish[1]=True for all I, then the system is in safe state. Resource request algorithm
Let Request i be request vector for the process Pi, If request i=[j]=k, then process Pi wants k
instances of resource type Rj.
1. if Request<=Need I go to step 2. Otherwise raise an error condition.
2. if Request<=Available go to step 3. Otherwise Pi must since the resources are
available.
3. Have the system pretend to have allocated the requested resources to process Pi by
modifying the state as follows;
Available=Available-Request I;
Allocation I=Allocation +Request I;
Need i=Need i- Request I;
If the resulting resource allocation state is safe, the transaction is completed and
process Pi is allocated its resources. However if the state is unsafe, the Pi must wait for
Request i and the old resource-allocation state is restored.

ALGORITHM:
1. Start the program.
2. Get the values of resources and processes.
3. Get the avail value.
4. After allocation find the need value.
5. Check whether its possible to allocate.
6. If it is possible then the system is in safe state.
7. Else system is not in safety state.
8. If the new request comes then check that the system is in safety.
9. or not if we allow the request.
10. stop the program.
11. end

Conclusion:
Program:

#include <stdio.h>
#include <string.h>

void main() {
int alloc[10][10], max[10][10], need[10][10];
int avail[10], work[10], total[10];
int i, j, k, n, m, count = 0, c = 0;
char finish[10]; // process executed or not

printf("Enter the number of processes and resources: ");


scanf("%d%d", &n, &m);
for(i = 0; i < n; i++) {
finish[i] = 'n';
}
printf("Enter the claim matrix (maximum resources required):\n");
for(i = 0; i < n; i++) {
for(j = 0; j < m; j++) {
scanf("%d", &max[i][j]);
}
}
printf("Enter the allocation matrix:\n");
for(i = 0; i < n; i++) {
for(j = 0; j < m; j++) {
scanf("%d", &alloc[i][j]);
}
}
printf("Enter the total resource vector: ");
for(i = 0; i < m; i++) {
scanf("%d", &total[i]);
}
for(i = 0; i < m; i++) {
avail[i] = total[i];
for(j = 0; j < n; j++) {
avail[i] -= alloc[j][i];
}
}
for(i = 0; i < n; i++) {
for(j = 0; j < m; j++) {
need[i][j] = max[i][j] - alloc[i][j];
}
}
while(count < n) {
c = 0; // Reset the count of processes that can be executed
for(i = 0; i < n; i++) {
if(finish[i] == 'n') {
int canExecute = 1;
for(j = 0; j < m; j++) {
if(need[i][j] > avail[j]) {
canExecute = 0;
break;
}
}

if(canExecute) {
printf("All resources can be allocated to process %d\n", i+1);
printf("Available resources before allocation: ");
for(k = 0; k < m; k++) {
printf("%d ", avail[k]);
}
printf("\n");
for(k = 0; k < m; k++) {
avail[k] += alloc[i][k];
}

printf("Available resources after allocation: ");


for(k = 0; k < m; k++) {
printf("%d ", avail[k]);
}
printf("\n");
finish[i] = 'y';
count++;
printf("Process %d executed: %c\n", i+1, finish[i]);
break;
}
}
}
if(count == 0) {
printf("\nSystem is not in a safe state\n");
return;
}
}
printf("\nSystem is in safe mode.\n");
printf("The given state is a safe state.\n");
}
Output:

You might also like