You are on page 1of 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