0% found this document useful (0 votes)
96 views5 pages

Implementation of Deadlock Avoidance Banker's Algorithm

1. The document describes an implementation of the Banker's Algorithm for deadlock avoidance. It includes code for inputting resource allocation and maximum matrices, calculating need matrices, and determining if a safe state exists. 2. The algorithm requires O(m*n^2) operations to determine if a system state is safe, where m is the number of available resources and n is the number of processes. It works by finding unfinished processes whose needs are satisfied by available resources, allocating resources, and marking processes as finished in each step. 3. If all processes are marked finished, the system is in a safe state, otherwise an unsafe state exists.

Uploaded by

Smit Bhenjaliya
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)
96 views5 pages

Implementation of Deadlock Avoidance Banker's Algorithm

1. The document describes an implementation of the Banker's Algorithm for deadlock avoidance. It includes code for inputting resource allocation and maximum matrices, calculating need matrices, and determining if a safe state exists. 2. The algorithm requires O(m*n^2) operations to determine if a system state is safe, where m is the number of available resources and n is the number of processes. It works by finding unfinished processes whose needs are satisfied by available resources, allocating resources, and marking processes as finished in each step. 3. If all processes are marked finished, the system is in a safe state, otherwise an unsafe state exists.

Uploaded by

Smit Bhenjaliya
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/ 5

K. J.

Somaiya College of Engineering, Mumbai-77


(An Autonomous College Affiliated to University of Mumbai)

Batch: B4 Roll No.: 1913121


Experiment / assignment / tutorial No. 9.
Grade: AA / AB / BB / BC / CC / CD /DD

Signature of the Staff In-charge with date

TITLE: Deadlock
AIM: Implementation of Deadlock Avoidance Banker’s Algorithm

OUTCOME: Student can Compare different algorithms used for management and
scheduling of processes.

Implementation of Deadlock Avoidance Banker’s Algorithm.


Write full code for all algorithm along with output and screen
shot.
Code:
#include<stdio.h>
#include<stdlib.h>
int max[100][100];
int alloc[100][100];
int need[100][100];
int avail[100];
int n,r;
void input();
void show();
void cal();
int main()
{
int i,j;
printf("********** Banker's Algo ************\n");
input();
show();
cal();
return 0;
}
void input()
{
int i,j;
printf("Enter the no of Processes\t");
scanf("%d",&n);
printf("Enter the no of resources instances\t");
scanf("%d",&r);
printf("Enter the Max Matrix\n");
Department of Electronics and Telecommunication Engineering

OS/Sem V/ Aug- Dec 20 Page No


K. J. Somaiya College of Engineering, Mumbai-77
(An Autonomous College Affiliated to University of Mumbai)
for(i=0;i<n;i++)
{
for(j=0;j<r;j++)
{
scanf("%d",&max[i][j]);
}
}
printf("Enter the Allocation Matrix\n");
for(i=0;i<n;i++)
{
for(j=0;j<r;j++)
{
scanf("%d",&alloc[i][j]);
}
}
printf("Enter the available Resources\n");
for(j=0;j<r;j++)
{
scanf("%d",&avail[j]);
}
}
void show()
{
int i,j;
printf("Process\t Allocation\t Max\t Available\t");
for(i=0;i<n;i++)
{
printf("\nP%d\t ",i+1);
for(j=0;j<r;j++)
{
printf("%d ",alloc[i][j]);
}
printf("\t");
for(j=0;j<r;j++)
{
printf("%d ",max[i][j]);
}
printf("\t");
if(i==0)
{
for(j=0;j<r;j++)
printf("%d ",avail[j]);
}
}
}
void cal()
{

Department of Electronics and Telecommunication Engineering

OS/Sem V/ Aug- Dec 20 Page No


K. J. Somaiya College of Engineering, Mumbai-77
(An Autonomous College Affiliated to University of Mumbai)
int finish[100],temp,need[100][100],flag=1,k,c1=0;
int safe[100];
int i,j;
for(i=0;i<n;i++)
{
finish[i]=0;
}
//find need matrix
for(i=0;i<n;i++)
{
for(j=0;j<r;j++)
{
need[i][j]=max[i][j]-alloc[i][j];
}
}
printf("\n");
while(flag)
{
flag=0;
for(i=0;i<n;i++)
{
int c=0;
for(j=0;j<r;j++)
{
if((finish[i]==0)&&(need[i][j]<=avail[j]))
{
c++;
if(c==r)
{
for(k=0;k<r;k++)
{
avail[k]+=alloc[i][j];
finish[i]=1;
flag=1;
}
printf("P%d->",i);
if(finish[i]==1)
{
i=n;
}
}
}
}
}
}
for(i=0;i<n;i++)
{

Department of Electronics and Telecommunication Engineering

OS/Sem V/ Aug- Dec 20 Page No


K. J. Somaiya College of Engineering, Mumbai-77
(An Autonomous College Affiliated to University of Mumbai)
if(finish[i]==1)
{
c1++;
}
else
{
printf("P%d->",i);
}
}
if(c1==n)
{
printf("\n The system is in safe state");
}
else
{
printf("\n Process are in dead lock");
printf("\n System is in unsafe state");
}
}
Output:

Department of Electronics and Telecommunication Engineering

OS/Sem V/ Aug- Dec 20 Page No


K. J. Somaiya College of Engineering, Mumbai-77
(An Autonomous College Affiliated to University of Mumbai)
Prove that the safety algorithm requires an order of m × n2 operations.
A safety algorithm is an algorithm used to find whether or not a system is in its safe
state. The algorithm is as follows:

1. Let Work and Finish be vectors of length m and n, respectively. Initially,


Work = Available
Finish[i] =false for i = 0, 1, 2, ... , n-1
This means, initially, no process has finished and the number of available
resources is represented by the Available array.

2. Find an index i such that both


Finish[i] ==false
Needi <= Work
If there is no such i present, then proceed to step 4.
It means, we need to find an unfinished process whose needs can be satisfied by
the available resources. If no such process exists, just go to step 4.

3. Perform the following:


Work = Work + Allocation
Finish[i] = true
Go to step 2.
When an unfinished process is found, then the resources are allocated and the
process is marked finished. And then, the loop is repeated to check the same for
all other processes.

4. If Finish[i] == true for all i, then the system is in a safe state.


That means if all processes are finished, then the system is in safe state.

This algorithm may require an order of mxn² operations in order to determine


whether a state is safe or not.

Signature of faculty in-charge

Department of Electronics and Telecommunication Engineering

OS/Sem V/ Aug- Dec 20 Page No

You might also like