This program complie and run using turbo C++
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class Algo
{
public:
int p,r,alc[10][10],max[10][10],need[10][10];
int i,j,t_r[10],wait[10],t_r1[10],k,m;
Algo()
{
k=0;
m=0;
}
void input()
{
cout<<"Enter the type of processes in number:"<<endl;
cin>>p;
cout<<"Enter the type of resourses in number:"<<endl;
cin>>r;
for(i=0;i<r;i++)
{
cout<<"Enter the total number of available resourses in A"<<i<<endl;
cin>>t_r[i];
}
for(i=0;i<r;i++)
{
t_r1[i]=t_r[i];
}
for(i=0;i<p;i++)
{
for(j=0;j<r;j++)
{
cout<<"Enter the allocated resourses of process type P"<<i<<" and resourse type A"<<j<<endl;
cin>>alc[i][j];
}
}
for(i=0;i<p;i++)
{
for(j=0;j<r;j++)
{
cout<<"Enter the Maximum allocated resourses of process type P"<<i<<"and resourse type A"<<j<<endl;
cin>>max[i][j];
}
}
}
void need1()
{
for(i=0;i<p;i++)
{
for(j=0;j<r;j++)
{
need[i][j]=max[i][j]-alc[i][j];
}
}
}
void show()
{
cout<<"Max matrix :"<<endl;
for(i=0;i<p;i++)
{
for(j=0;j<r;j++)
{
cout<<max[i][j];
}
cout<<endl;
}
cout<<endl;
cout<<"Allocated matrix :"<<endl;
for(i=0;i<p;i++)
{
for(j=0;j<r;j++)
{
cout<<alc[i][j];
}
cout<<endl;
}
cout<<endl;
cout<<"Need Matrix :"<<endl;
for(i=0;i<p;i++)
{
for(j=0;j<r;j++)
{
cout<<need[i][j];
}
cout<<endl;
}
cout<<endl;
}
void calculate()
{
for(i=0;i<p;i++)
{
int p1=0;
for(j=0;j<r;j++)
{
if(need[i][j]<=t_r[j])
p1++;
}
if(p1==r)
{
cout<<"p"<<i<<" ";
for(j=0;j<r;j++)
t_r[j]=t_r[j]+alc[i][j];
}
else
{
wait[k]=i;
k++;
}
}
for(int u=0;u<k;u++)
{
if(m<=k)
{
i=wait[m];
m++;
int p1=0;
for(j=0;j<r;j++)
{
if(need[i][j]<=t_r[j])
p1++;
}
if(p1==r)
{
cout<<"p"<<i<<" ";
for(j=0;j<r;j++)
t_r[j]=t_r[j]+alc[i][j];
}
}
}
cout<<">"<<endl;
cout<<endl;
for(j=0;j<r;j++)
{
cout<<"Total Resourses in A"<<j<<" : ";
cout<<t_r[j]<<endl;
}
}
};
void main()
{
Algo ob;
clrscr();
ob.input();
ob.need1();
ob.show();
cout<<"The safe sequence : "<<'\t'<<"<";
ob.calculate();
exit(1);
getch();
}
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class Algo
{
public:
int p,r,alc[10][10],max[10][10],need[10][10];
int i,j,t_r[10],wait[10],t_r1[10],k,m;
Algo()
{
k=0;
m=0;
}
void input()
{
cout<<"Enter the type of processes in number:"<<endl;
cin>>p;
cout<<"Enter the type of resourses in number:"<<endl;
cin>>r;
for(i=0;i<r;i++)
{
cout<<"Enter the total number of available resourses in A"<<i<<endl;
cin>>t_r[i];
}
for(i=0;i<r;i++)
{
t_r1[i]=t_r[i];
}
for(i=0;i<p;i++)
{
for(j=0;j<r;j++)
{
cout<<"Enter the allocated resourses of process type P"<<i<<" and resourse type A"<<j<<endl;
cin>>alc[i][j];
}
}
for(i=0;i<p;i++)
{
for(j=0;j<r;j++)
{
cout<<"Enter the Maximum allocated resourses of process type P"<<i<<"and resourse type A"<<j<<endl;
cin>>max[i][j];
}
}
}
void need1()
{
for(i=0;i<p;i++)
{
for(j=0;j<r;j++)
{
need[i][j]=max[i][j]-alc[i][j];
}
}
}
void show()
{
cout<<"Max matrix :"<<endl;
for(i=0;i<p;i++)
{
for(j=0;j<r;j++)
{
cout<<max[i][j];
}
cout<<endl;
}
cout<<endl;
cout<<"Allocated matrix :"<<endl;
for(i=0;i<p;i++)
{
for(j=0;j<r;j++)
{
cout<<alc[i][j];
}
cout<<endl;
}
cout<<endl;
cout<<"Need Matrix :"<<endl;
for(i=0;i<p;i++)
{
for(j=0;j<r;j++)
{
cout<<need[i][j];
}
cout<<endl;
}
cout<<endl;
}
void calculate()
{
for(i=0;i<p;i++)
{
int p1=0;
for(j=0;j<r;j++)
{
if(need[i][j]<=t_r[j])
p1++;
}
if(p1==r)
{
cout<<"p"<<i<<" ";
for(j=0;j<r;j++)
t_r[j]=t_r[j]+alc[i][j];
}
else
{
wait[k]=i;
k++;
}
}
for(int u=0;u<k;u++)
{
if(m<=k)
{
i=wait[m];
m++;
int p1=0;
for(j=0;j<r;j++)
{
if(need[i][j]<=t_r[j])
p1++;
}
if(p1==r)
{
cout<<"p"<<i<<" ";
for(j=0;j<r;j++)
t_r[j]=t_r[j]+alc[i][j];
}
}
}
cout<<">"<<endl;
cout<<endl;
for(j=0;j<r;j++)
{
cout<<"Total Resourses in A"<<j<<" : ";
cout<<t_r[j]<<endl;
}
}
};
void main()
{
Algo ob;
clrscr();
ob.input();
ob.need1();
ob.show();
cout<<"The safe sequence : "<<'\t'<<"<";
ob.calculate();
exit(1);
getch();
}
No comments:
Post a Comment