#include<stdio.h>
#include<iostream.h>
#define true 1
#define false 0
typedef struct process
{
int alloc[4],max[4],curneed[4];
int finish;
};
int main()
{
process p[5];
int res,n,i,j,avail[4];
cout<<"Enter no of process: ";
cin>>n;
cout<<"Enter no of Resources: ";
cin>>res;
for(i=0;i<n;i++)
{
cout<<"Enter Allocations of Resources for process "<<i+1<<"\n";
for(j=0;j<res;j++)
{
cout<<"Resource "<<j+1<<": ";
cin>>p[i].alloc[j];
}
for(j=0;j<res;j++)
{
cout<<"Maximum Need "<<j+1<<": ";
cin>>p[i].max[j];
p[i].curneed[j]=p[i].max[j]-p[i].alloc[j];
}
p[i].finish = false;
}
cout<<"Enter the resouces available: "<<endl;
for(i=0;i<res;i++)
{
cout<<"Resources "<<i+1<<": ";
cin>>avail[i];
}
cout<<"Printing Details\n\n";
for(i=0;i<n;i++)
{
for(j=0;j<res;j++)
cout<<p[i].alloc[j]<<" ";
cout<<"\t";
for(j=0;j<res;j++)
cout<<p[i].max[j]<<" ";
cout<<"\t";
for(j=0;j<res;j++)
cout<<p[i].curneed[j]<<" ";
cout<<"\t";
cout<<"\n";
}
//calculations
int flag,process_flag=true;
int no=0;
int count = 0;
while(process_flag)
{
count++;
for(i=0;i<n;i++)
{
count++;
flag = true;
if(p[i].finish == false)
{
count++;
for(j=0;j<res;j++)
{
count++;
if(p[i].curneed[j] > avail[j])
{
count++;
flag = false;
break;
}
}
if(flag == true)
{
count++;
for(j=0;j<res;j++)
{
avail[j] += p[i].alloc[j];
count++;
}
p[i].finish = true;
no++;
process_flag = false;
}
}
}
if((process_flag == false) && (no!=n))
{
process_flag = true;
count++;
}
}
if(no == n)
cout<<"\n\n\nSAFE"<<count;
else
cout<<"\n\n\nNOT SAFE"<<count;
return 0;
}
#include<iostream.h>
#define true 1
#define false 0
typedef struct process
{
int alloc[4],max[4],curneed[4];
int finish;
};
int main()
{
process p[5];
int res,n,i,j,avail[4];
cout<<"Enter no of process: ";
cin>>n;
cout<<"Enter no of Resources: ";
cin>>res;
for(i=0;i<n;i++)
{
cout<<"Enter Allocations of Resources for process "<<i+1<<"\n";
for(j=0;j<res;j++)
{
cout<<"Resource "<<j+1<<": ";
cin>>p[i].alloc[j];
}
for(j=0;j<res;j++)
{
cout<<"Maximum Need "<<j+1<<": ";
cin>>p[i].max[j];
p[i].curneed[j]=p[i].max[j]-p[i].alloc[j];
}
p[i].finish = false;
}
cout<<"Enter the resouces available: "<<endl;
for(i=0;i<res;i++)
{
cout<<"Resources "<<i+1<<": ";
cin>>avail[i];
}
cout<<"Printing Details\n\n";
for(i=0;i<n;i++)
{
for(j=0;j<res;j++)
cout<<p[i].alloc[j]<<" ";
cout<<"\t";
for(j=0;j<res;j++)
cout<<p[i].max[j]<<" ";
cout<<"\t";
for(j=0;j<res;j++)
cout<<p[i].curneed[j]<<" ";
cout<<"\t";
cout<<"\n";
}
//calculations
int flag,process_flag=true;
int no=0;
int count = 0;
while(process_flag)
{
count++;
for(i=0;i<n;i++)
{
count++;
flag = true;
if(p[i].finish == false)
{
count++;
for(j=0;j<res;j++)
{
count++;
if(p[i].curneed[j] > avail[j])
{
count++;
flag = false;
break;
}
}
if(flag == true)
{
count++;
for(j=0;j<res;j++)
{
avail[j] += p[i].alloc[j];
count++;
}
p[i].finish = true;
no++;
process_flag = false;
}
}
}
if((process_flag == false) && (no!=n))
{
process_flag = true;
count++;
}
}
if(no == n)
cout<<"\n\n\nSAFE"<<count;
else
cout<<"\n\n\nNOT SAFE"<<count;
return 0;
}
No comments:
Post a Comment