/******************************************************
Program :- Banker's Algorithm......
******************************************************/
#include<iostream.h>
#include<conio.h>
/******************************************************/
int procno,res,cnt,i,j,k,n,index;
int allocation[20][20],need[20][20],available[20],max[20][20],flag[20];
/******************************************************/
void input()
{
cout<<"\n Enter No. of Resources => ";cin>>res;
cout<<"\n Enter No. of Processes => ";cin>>procno;
cout<<"\n Enter Resources => ";
for(i=0;i<20;i++)
flag[i]=0;
for(i=0;i<res;i++)
{
cout<<"\n Resource R"<<i<<"=> ";
cin>>available[i];
}
cout<<"\n Enter Allocation Matrix => ";
for(i=0;i<procno;i++)
{
cout<<"\n Enter Instances for Process P"<<i<<" => ";
for(j=0;j<res;j++)
cin>>allocation[i][j];
}
cout<<"\n Enter Max Matrix => ";
for(i=0;i<procno;i++)
{
cout<<"\n Enter Instances for Process P"<<i<<" => ";
for(j=0;j<res;j++)
cin>>max[i][j];
}
}
/******************************************************/
void display()
{
clrscr();
cout<<"\n Allocation Matrix ==> \n";
for(i=0;i<procno;i++)
{
cout<<"\n ";
for(j=0;j<res;j++)
cout<<" "<<allocation[i][j];
}
cout<<"\n Max Matrix ==> \n";
for(i=0;i<procno;i++)
{
cout<<"\n ";
for(j=0;j<res;j++)
cout<<" "<<max[i][j];
}
for(i=0;i<procno;i++)
for(j=0;j<res;j++)
need[i][j]=max[i][j]-allocation[i][j];
cout<<"\n Need Matrix ==> \n";
for(i=0;i<procno;i++)
{
cout<<"\n ";
for(j=0;j<res;j++)
cout<<" "<<need[i][j];
}
cout<<"\n Available Matrix ==> \n";
for(j=0;j<res;j++)
cout<<" "<<available[j];
getch();
clrscr();
}
/******************************************************/
void calculate_available()
{
for(i=0;i<res;i++)
{
for(j=0;j<procno;j++)
available[i]=available[i]-allocation[j][i];
}
}
/******************************************************/
void bank()
{
cout<<"\n Sequence Of Program Execution => ";
k=0;
while(k<procno)
{
for(i=0;i<procno;i++)
{
cnt=0;
for(j=0;j<res;j++)
{
if(need[i][j]<=available[j])
cnt++;
index=i;
}
if(flag[i]==0 && cnt==3)
{
cout<<" "<<index;
flag[i]=1;
k++;
for(j=0;j<procno;j++)
available[j]=available[j]+allocation[i][j];
goto A;
}
}
A:
}
}
/******************************************************/
void main()
{
clrscr();
input();
calculate_available();
display();
bank();
getch();
}
No comments:
Post a Comment