#include<iostream.h>
#include<iomanip.h>
#include<conio.h>
struct process
{
int finish,request[6],all[6],max[6],need[6];
}p[10];
int avail[10],safeseq[10],work[6],ss=0,n=0,nor=0,c1=0,c2=0;
int seq()
{
int i,j,k;
ss=0;
//copy avail into work
for(j=0;j<nor;j++)
work[j]=avail[j];
//finished initialised to zero
for(j=0;j<n;j++)
p[j].finish=0;
//select a process s.t finish=false and need < work
for(int m=0;m<nor;m++)
{
for(j=0;j<n;j++)
{
if(p[j].finish==0)
{
c1=0;
for(k=0;k<nor;k++)
{
if(p[j].need[k]<=work[k])
c1++;
}
if(c1==nor)
{
for(i=0;i<nor;i++)
work[i]=p[j].all[i]+work[i]; //work =work + allocation
p[j].finish=1;
safeseq[ss]=j;
ss++;
}
}
}
}
c2=0;
for(i=0;i<n;i++)
{
if(p[i].finish==1)
c2++;
}
if(c2==n)
{
cout<<"\n System in safe state!!!\n";
for(i=0;i<n;i++)
cout<<"\n\t"<<i<<". P"<<safeseq[i];
return 1;
}
else
{
cout<<"\n System in unsafe state!!!";
return 0;
}
}
void main()
{
int ch,i=0,j=0,pid,ch1;
int violationcheck=0,waitcheck=0;
clrscr();
do
{
clrscr();
cout<<"\n 1. Input";
cout<<"\n 2. New Request";
cout<<"\n 3. Safe State";
cout<<"\n 4. Print";
cout<<"\n 5. Exit";
cout<<"\n\t Enter ur choice : ";
cin>>ch;
switch(ch)
{
case 1:
//no of processes and resources
cout<<"\nEnter the no of processes = ";
cin>>n;
cout<<"\nEnter the no of resouces = ";
cin>>nor;
cout<<"\nEnter the max available resources\n";
//max available resources
for(i=0;i<nor;i++)
{
cout<<"Enter max availbale for resource "<<i<<" = ";
cin>>avail[i];
}
//initialise request,allocated,max and need matrices
for(i=0;i<n;i++)
{
for(j=0;j<nor;j++)
{
p[i].all[j]=p[i].request[j]=p[i].max[j]=p[i].need[j]=0;
}
p[i].finish=0;
}
//enter allocation and max matrices and calculate need matrices
for(i=0;i<n;i++)
{
cout<<"\nEnter max and allocated resouces for process "<<i<<"\n";
for(j=0;j<nor;j++)
{
cout<<"Enter Max of resource "<<j<<" =";
cin>>p[i].max[j];
cout<<"Enter allocattion of resource "<<j<<" =";
cin>>p[i].all[j];
if(p[i].all[j]>p[i].max[j])
j--;
else
{
p[i].need[j]=p[i].max[j]-p[i].all[j];
}
avail[j]=avail[j]-p[i].all[j];
}
}
break;
case 2:
//input request
violationcheck=0;
waitcheck=0;
cout<<"\nEnter Pid of requesting process = ";
cin>>pid;
for(i=0;i<nor;i++)
{
cout<<"\nEnter no of request for resource "<<i<<" =";
cin>>p[pid].request[i];
if(p[pid].request[j]>p[pid].need[i])
violationcheck=1;
if(p[pid].request[j]>avail[i])
waitcheck=1;
}
if(violationcheck==1)
cout<<"\nREQUEST DENIED::request exceeds need!!!";
else if(waitcheck==1)
cout<<"\nProcess in WAIT::Lack of resources!!!";
else
{
for(j=0;j<nor;j++)
{
avail[j]=avail[j]-p[pid].request[j];
p[pid].need[j]=p[pid].need[j]-p[pid].request[j];
p[pid].all[j]=p[pid].all[j]+p[pid].request[j];
}
}
ch1=seq();
if(ch1==0)
{
cout<<"\nRequest denied!!!";
//undo the changes made to matrices
for(j=0;j<nor;j++)
{
avail[j]=avail[j]+p[pid].request[j];
p[pid].all[j]=p[pid].all[j]-p[pid].request[j];
p[pid].need[j]=p[pid].need[j]+p[pid].request[j];
}
}
else if(ch1==1)
cout<<"\n Request Granted!!!";
break;
case 3:
ch1=seq();
break;
case 4:
cout<<"\n No of resources = "<<nor;
cout<<"\n No of process = "<<n<<"\n\n";
cout<<setw(12)<<"Allocation "<<"\tMaximun Need "<<"\tNeed";
for(i=0;i<n;i++)
{
cout<<endl;
for(j=0;j<nor;j++)
{
cout<<setw(2)<<p[i].all[j];
}
cout<<"\t\t";
for(j=0;j<nor;j++)
{
cout<<setw(2)<<p[i].max[j];
}
cout<<"\t\t";
for(j=0;j<nor;j++)
{
cout<<setw(2)<<p[i].need[j];
}
}
break;
}
getch();
}while(ch!=5);
}
#include<iomanip.h>
#include<conio.h>
struct process
{
int finish,request[6],all[6],max[6],need[6];
}p[10];
int avail[10],safeseq[10],work[6],ss=0,n=0,nor=0,c1=0,c2=0;
int seq()
{
int i,j,k;
ss=0;
//copy avail into work
for(j=0;j<nor;j++)
work[j]=avail[j];
//finished initialised to zero
for(j=0;j<n;j++)
p[j].finish=0;
//select a process s.t finish=false and need < work
for(int m=0;m<nor;m++)
{
for(j=0;j<n;j++)
{
if(p[j].finish==0)
{
c1=0;
for(k=0;k<nor;k++)
{
if(p[j].need[k]<=work[k])
c1++;
}
if(c1==nor)
{
for(i=0;i<nor;i++)
work[i]=p[j].all[i]+work[i]; //work =work + allocation
p[j].finish=1;
safeseq[ss]=j;
ss++;
}
}
}
}
c2=0;
for(i=0;i<n;i++)
{
if(p[i].finish==1)
c2++;
}
if(c2==n)
{
cout<<"\n System in safe state!!!\n";
for(i=0;i<n;i++)
cout<<"\n\t"<<i<<". P"<<safeseq[i];
return 1;
}
else
{
cout<<"\n System in unsafe state!!!";
return 0;
}
}
void main()
{
int ch,i=0,j=0,pid,ch1;
int violationcheck=0,waitcheck=0;
clrscr();
do
{
clrscr();
cout<<"\n 1. Input";
cout<<"\n 2. New Request";
cout<<"\n 3. Safe State";
cout<<"\n 4. Print";
cout<<"\n 5. Exit";
cout<<"\n\t Enter ur choice : ";
cin>>ch;
switch(ch)
{
case 1:
//no of processes and resources
cout<<"\nEnter the no of processes = ";
cin>>n;
cout<<"\nEnter the no of resouces = ";
cin>>nor;
cout<<"\nEnter the max available resources\n";
//max available resources
for(i=0;i<nor;i++)
{
cout<<"Enter max availbale for resource "<<i<<" = ";
cin>>avail[i];
}
//initialise request,allocated,max and need matrices
for(i=0;i<n;i++)
{
for(j=0;j<nor;j++)
{
p[i].all[j]=p[i].request[j]=p[i].max[j]=p[i].need[j]=0;
}
p[i].finish=0;
}
//enter allocation and max matrices and calculate need matrices
for(i=0;i<n;i++)
{
cout<<"\nEnter max and allocated resouces for process "<<i<<"\n";
for(j=0;j<nor;j++)
{
cout<<"Enter Max of resource "<<j<<" =";
cin>>p[i].max[j];
cout<<"Enter allocattion of resource "<<j<<" =";
cin>>p[i].all[j];
if(p[i].all[j]>p[i].max[j])
j--;
else
{
p[i].need[j]=p[i].max[j]-p[i].all[j];
}
avail[j]=avail[j]-p[i].all[j];
}
}
break;
case 2:
//input request
violationcheck=0;
waitcheck=0;
cout<<"\nEnter Pid of requesting process = ";
cin>>pid;
for(i=0;i<nor;i++)
{
cout<<"\nEnter no of request for resource "<<i<<" =";
cin>>p[pid].request[i];
if(p[pid].request[j]>p[pid].need[i])
violationcheck=1;
if(p[pid].request[j]>avail[i])
waitcheck=1;
}
if(violationcheck==1)
cout<<"\nREQUEST DENIED::request exceeds need!!!";
else if(waitcheck==1)
cout<<"\nProcess in WAIT::Lack of resources!!!";
else
{
for(j=0;j<nor;j++)
{
avail[j]=avail[j]-p[pid].request[j];
p[pid].need[j]=p[pid].need[j]-p[pid].request[j];
p[pid].all[j]=p[pid].all[j]+p[pid].request[j];
}
}
ch1=seq();
if(ch1==0)
{
cout<<"\nRequest denied!!!";
//undo the changes made to matrices
for(j=0;j<nor;j++)
{
avail[j]=avail[j]+p[pid].request[j];
p[pid].all[j]=p[pid].all[j]-p[pid].request[j];
p[pid].need[j]=p[pid].need[j]+p[pid].request[j];
}
}
else if(ch1==1)
cout<<"\n Request Granted!!!";
break;
case 3:
ch1=seq();
break;
case 4:
cout<<"\n No of resources = "<<nor;
cout<<"\n No of process = "<<n<<"\n\n";
cout<<setw(12)<<"Allocation "<<"\tMaximun Need "<<"\tNeed";
for(i=0;i<n;i++)
{
cout<<endl;
for(j=0;j<nor;j++)
{
cout<<setw(2)<<p[i].all[j];
}
cout<<"\t\t";
for(j=0;j<nor;j++)
{
cout<<setw(2)<<p[i].max[j];
}
cout<<"\t\t";
for(j=0;j<nor;j++)
{
cout<<setw(2)<<p[i].need[j];
}
}
break;
}
getch();
}while(ch!=5);
}
No comments:
Post a Comment