Friday, 29 June 2012

.Write the simulation program for demand paging and show the page

.Write the simulation program for demand paging and show the page scheduling and total number of page faults according to MFU page replacement algorithm. Assume the memory of ‘n’ frames.

#include<stdio.h>
int RefString[20],PT[10],nof,nor;
void Accept()
{
int i;
printf("\n enter the reference sting:");
for(i=0;i<nor;i++)
{
printf("[%d]=",i);
scanf("%d",&RefString[i]);
}
}
int search(int s)
{
int i;
for(i=0;i<nof;i++)
if(PT[i]==s)
return(i);
return(-1);
}
int GETMFU(int e,int s)
{
int i,j,cnt1=0,cnt2,posi,pos,pos1;
i=s;
do
{
cnt2=0;
for(j=e-1;j>=0;j--)
{
if(PT[i]==RefString[j])
{
cnt2++;
                                  pos=j;
}
}

                     for(j=e-1;j>=0;j--)
                {
                        if(PT[i]==RefString[j])
                        {
                             
                                  pos=j;
                                   break;
                        }
                }

                       //  printf("\nr=%d",RefString[i]);
if(cnt2>cnt1)
{
cnt1=cnt2;
posi=i;
                        pos1=pos;
}
                if(cnt2==cnt1 && pos1>pos)
                 {
                      cnt1=cnt2;
                      posi=i;
                 
}

i=(i+1)%nof;
}
while(i!=s);
return(posi);
}
void MFU()
{
int i,j,k,faults=0;
for(k=0,i=0;k<nof && i<nor;i++)
{
printf("%2d",RefString[i]);
if(search(RefString[i])==-1)
{
PT[k]=RefString[i];
for(j=0;j<nof;j++)
{
if(PT[j])
printf("%2d\t",PT[j]);
}
faults++;
k++;
}
printf("\n");
}
k=0;
while(i<nor)
{
printf("%2d",RefString[i]);
if(search(RefString[i])==-1)
{
k=GETMFU(i,k);
PT[k]=RefString[i];
k=(k+1)%nof;
for(j=0;j<nof;j++)
{
if(PT[j])
printf("%2d\t",PT[j]);
}
faults++;

}                      
i++;
printf("\n");
}
printf("total page faults:%d",faults);
}
int main()
{
printf("\n enter the length of Reference string:");
scanf("%d",&nor);
printf("\n enter the number of frames:");
scanf("%d",&nof);
Accept();
MFU();
}


No comments: