#include<stdio.h>
int main()
{
int data[15],page[6],n,s,c,hit=0,i,j,index=0,cnt,k,arr[5],entry,x;
printf("\nEnter the frame size:");
scanf("%d",&n);
printf("\nEnter the string size:");
scanf("%d",&s);
printf("\nEnter the data bits:>");
for(i=0;i<s;i++)
{
printf("\nEnter data %d:",i+1);
scanf("%d",&data[i]);
}
for(i=0;i<n;i++)
{
page[i]=999;
}
//FIFOFIFOFIFOFIFOFIFOFIFOFIFOFIFOFIFOFIFOFIFOFIFOFIFOFIFOFIFOFIFOFIFOFIFOFIFOFIFOFIFOFIFOFIFO
printf("\n**************FIFO******************");
for(i=0;i<s;i++)
{
c=0;
if(index==0)
index=n;
for(j=0;j<n;j++)
{
if(page[j]==data[i])
{
c++;
break;
}
}
if(c==1)
hit++;
else
page[n-index--]=data[i];
}
printf("\nPage Fault=%d\n",s-hit);
printf("\nHit ratio=%f\n",hit/(float)s);
////FIFOFIFOFIFOFIFOFIFOFIFOFIFOFIFOFIFOFIFOFIFOFIFOFIFOFIFOFIFOFIFOFIFOFIFOFIFOFIFOFIFOFIFOFIFO
////LRULRULRULRULRULRULRULRULRULRULRULRULRULRULRULRULRULRULRULRULRULRULRULRULRULRULRULRULRULRULRU
hit=0;
printf("\n**************LRU******************");
for(i=0;i<n;i++)
{
page[i]=999;
}
index=0;
for(i=0;i<s;i++)
{
cnt=0;
c=0;
for(j=0;j<n;j++)
{
if(page[j]==data[i])
{
c++;
break;
}
}
if(c==1)
hit++;
else
{
if(index<n)
page[index++]=data[i];
else
{
for(j=i-n;j>0;j--)
{
for(k=j+1;k<i;k++)
{
if(data[j]==data[k])
cnt++;
}
j=cnt==0?j:(j-cnt+1);
for(k=0;k<n;k++)
{
if(data[j]==page[k])
{page[k]=data[i];break;}
}
}
}
}
}
printf("\nPage Fault=%d\n",s-hit);
printf("\nHit ratio=%f\n",hit/(float)s);
//LRULRULRULRULRULRULRULRULRULRULRULRULRULRULRULRULRULRULRULRULRULRULRULRULRULRULRULRULRULRULRU
//OPTIMALOPTIMALOPTIMALOPTIMALOPTIMALOPTIMALOPTIMALOPTIMALOPTIMALOPTIMALOPTIMALOPTIMALOPTIMALOPTIMAL
index=0;
hit=0;
printf("\n**************OPTIMAL******************");
for(i=0;i<n;i++)
{
page[i]=999;
}
for(i=0;i<s;i++)
{
c=0;
cnt=0;
for(j=0;j<n;j++)
{
if(page[j]==data[i])
{
c++;
break;
}
}
if(c==1)
hit++;
else
{
if(index<n)
page[index++]=data[i];
else
{
entry=0;
x=0;
for(k=i+1;k<s;k++)
{
if(cnt==n)
break;
for(j=0;j<n;j++)
{
if(data[k]==page[j])
{
if(cnt++==0)
arr[cnt-1]=k;
else
{
for(x=0;x<cnt;x++)
{
if(data[arr[x]]==data[k])
entry++;
}
if(entry==0)
{arr[cnt++]=k;}
}
break;
}
}
}
if(cnt==n)
page[cnt]=data[i];
else
page[0]=data[i];
}
}
}
printf("\nPage Fault=%d\n",s-hit);
printf("\nHit ratio=%f\n",hit/(float)s);
//OPTIMALOPTIMALOPTIMALOPTIMALOPTIMALOPTIMALOPTIMALOPTIMALOPTIMALOPTIMALOPTIMALOPTIMALOPTIMALOPTIMAL
return 0;
}
int main()
{
int data[15],page[6],n,s,c,hit=0,i,j,index=0,cnt,k,arr[5],entry,x;
printf("\nEnter the frame size:");
scanf("%d",&n);
printf("\nEnter the string size:");
scanf("%d",&s);
printf("\nEnter the data bits:>");
for(i=0;i<s;i++)
{
printf("\nEnter data %d:",i+1);
scanf("%d",&data[i]);
}
for(i=0;i<n;i++)
{
page[i]=999;
}
//FIFOFIFOFIFOFIFOFIFOFIFOFIFOFIFOFIFOFIFOFIFOFIFOFIFOFIFOFIFOFIFOFIFOFIFOFIFOFIFOFIFOFIFOFIFO
printf("\n**************FIFO******************");
for(i=0;i<s;i++)
{
c=0;
if(index==0)
index=n;
for(j=0;j<n;j++)
{
if(page[j]==data[i])
{
c++;
break;
}
}
if(c==1)
hit++;
else
page[n-index--]=data[i];
}
printf("\nPage Fault=%d\n",s-hit);
printf("\nHit ratio=%f\n",hit/(float)s);
////FIFOFIFOFIFOFIFOFIFOFIFOFIFOFIFOFIFOFIFOFIFOFIFOFIFOFIFOFIFOFIFOFIFOFIFOFIFOFIFOFIFOFIFOFIFO
////LRULRULRULRULRULRULRULRULRULRULRULRULRULRULRULRULRULRULRULRULRULRULRULRULRULRULRULRULRULRULRU
hit=0;
printf("\n**************LRU******************");
for(i=0;i<n;i++)
{
page[i]=999;
}
index=0;
for(i=0;i<s;i++)
{
cnt=0;
c=0;
for(j=0;j<n;j++)
{
if(page[j]==data[i])
{
c++;
break;
}
}
if(c==1)
hit++;
else
{
if(index<n)
page[index++]=data[i];
else
{
for(j=i-n;j>0;j--)
{
for(k=j+1;k<i;k++)
{
if(data[j]==data[k])
cnt++;
}
j=cnt==0?j:(j-cnt+1);
for(k=0;k<n;k++)
{
if(data[j]==page[k])
{page[k]=data[i];break;}
}
}
}
}
}
printf("\nPage Fault=%d\n",s-hit);
printf("\nHit ratio=%f\n",hit/(float)s);
//LRULRULRULRULRULRULRULRULRULRULRULRULRULRULRULRULRULRULRULRULRULRULRULRULRULRULRULRULRULRULRU
//OPTIMALOPTIMALOPTIMALOPTIMALOPTIMALOPTIMALOPTIMALOPTIMALOPTIMALOPTIMALOPTIMALOPTIMALOPTIMALOPTIMAL
index=0;
hit=0;
printf("\n**************OPTIMAL******************");
for(i=0;i<n;i++)
{
page[i]=999;
}
for(i=0;i<s;i++)
{
c=0;
cnt=0;
for(j=0;j<n;j++)
{
if(page[j]==data[i])
{
c++;
break;
}
}
if(c==1)
hit++;
else
{
if(index<n)
page[index++]=data[i];
else
{
entry=0;
x=0;
for(k=i+1;k<s;k++)
{
if(cnt==n)
break;
for(j=0;j<n;j++)
{
if(data[k]==page[j])
{
if(cnt++==0)
arr[cnt-1]=k;
else
{
for(x=0;x<cnt;x++)
{
if(data[arr[x]]==data[k])
entry++;
}
if(entry==0)
{arr[cnt++]=k;}
}
break;
}
}
}
if(cnt==n)
page[cnt]=data[i];
else
page[0]=data[i];
}
}
}
printf("\nPage Fault=%d\n",s-hit);
printf("\nHit ratio=%f\n",hit/(float)s);
//OPTIMALOPTIMALOPTIMALOPTIMALOPTIMALOPTIMALOPTIMALOPTIMALOPTIMALOPTIMALOPTIMALOPTIMALOPTIMALOPTIMAL
return 0;
}
No comments:
Post a Comment