Friday, June 20, 2014

SIMULATE PAGE REPLACEMENT ALGORITHMS


Simulate the following Page Replacement algorithms :
a) FIFO b) LRU c) LFU
Page Replacement algorithm-FIFO:-
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{ char prs[40],fp[10],ps;
int fs,i,j,k=0,flg1,flg2,x=5,y,pfc=0;
clrscr();
printf("\n enter page reference string:");
gets(prs);
printf("\n enter the frame size:");
scanf("%d",&fs);
for(i=0;i<fs;i++)
fp[i]='x';
clrscr();
printf("\n page replacement technique :: FIFO algorithm:");
printf("\n .............................................");
printf("\n F-Page Fault \t H- Page Hit \n");
for(i=0;i<strlen(prs);i++,x+=2)
{
flg1=0;
ps='F';
for(j=0;j<fs;j++)
if(fp[j]==prs[i]){
ps='H';
flg1=1;
break;
}
if(flg1==0)
{
flg2=0;
for(j=0;j<fs;j++)
if(fp[j]=='x')
{
fp[j]=prs[i];
pfc++;
flg2=1;
break;
}
if(flg2==0)
{
pfc++;
fp[k]=prs[i];
k++;
if(k==fs)
k=0; } }
y=5;
gotoxy(x,y);
printf("%c",prs[i]);
y++;
gotoxy(x,y);
printf("--");
y++;
for(j=0;j<fs;y++,j++)
{
gotoxy(x,y);
printf("%c",fp[j]);
}
y++;
gotoxy(x,y);
printf("--");
y++;
gotoxy(x,y);
printf("%c",ps);
}
printf("\n \n\n\n\n Total page Faults=%d",pfc);
getch();
}
output:-
Image
___________________________________________________________________________________________
Page Replacement algorithm-LRU:-
#include<dos.h>
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{ char prs[40],fp[10],ps;
int fs,i,j,k,flg1,flg2,x=5,y,pfc=0,ru[10],min;
clrscr();
printf("enter the page reference string:");
gets(prs);
printf("enter the frame size:");
scanf("%d",&fs);
for(i=0;i<fs;i++)
{
fp[i]='x';
ru[i]=0;
}
clrscr();
printf("PAGE REPLACEMENT TECHNIQUE::LRU algorithm\n");
printf("-----------------------------------\n");
printf("F-Page fault\tH-Page Hit\n");
for(i=0;i<strlen(prs);i++,x+=2)
{
flg1=0;
ps='F';
for(j=0;j<fs;j++)
{
if(fp[j]==prs[i])
{
ps='H';
ru[j]=i;
flg1=1;
break;
} }
if(flg1==0)
{
pfc++;
flg2=0;
for(j=0;j<fs;j++)
{
if(fp[j]=='X')
{
fp[j]=prs[i];
ru[j]=i;
flg2=1;
break;
} }
if(flg2==0)
{
min=0;
for(j=1;j<fs;j++)
{
if(ru[min]>ru[j])
min=j;
}
fp[min]=prs[i];
ru[min]=i;
} }
y=5;
gotoxy(x,y);
printf("%c",prs[i]);
y++;
gotoxy(x,y);
printf("- -");
y++;
for(j=0;j<fs;y++,j++)
{
gotoxy(x,y);
printf("%c",fp[j]);
}
y++;
gotoxy(x,y);
printf("--");
y++;
gotoxy(x,y);
printf("%c",ps);
}
printf("\n\n\n\n\n\n total page faults=%d",pfc);
getch();
}
output:-
Image
___________________________________________________________________________________________
Page Replacement algorithm-LFU:-
#include<dos.h>
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{ char prs[40],fp[10],ps;
int fs,i,j,k,flg1,flg2,x=5,y,pfc=0,ru[10],min;
clrscr();
printf("\n ENTER THE PAGE REFERENCE STRING:");
gets(prs);
printf("\n enter the frame size:");
scanf("%d",&fs);
for(i=0;i<fs;i++)
{
fp[i]='X';
ru[i]=0;
} clrscr();
printf("\n PAGE REPLACEMENT TECHNIQUE ::LFU ALGORITHM \n");
printf("\n .......................................... \n");
printf("F-Page Fault \t H-Page Hit\n");
for(i=0;i<strlen(prs);i++,x+=5)
{
flg1=0;
ps='F';
for(j=0;j<fs;j++)
{
if(fp[j]==prs[i])
{
ps='H';
(ru[j])++;
flg1=1;
break;
}}
if(flg1==0)
{
pfc++;
flg2=0;
for(j=0;j<fs;j++)
{
if(fp[j]=='X')
{
fp[j]=prs[i];
ru[j]=1;
flg2=1;
break;
}}
if(flg2==0)
{
min=0;
for(j=1;j<fs;j++)
{
if(ru[min]>=ru[j])
{
if(ru[min]>ru[j])
min=j;
else
{
for(k=0;k<i;k++)
{
if(prs[k]==fp[min])
break;
if(prs[k]==fp[j])
{
min=j;
break;
}}}}}
fp[min]=prs[i];
ru[min]=1;
}}
y=5;
gotoxy(x,y);
printf("%c",prs[i]);
y++;
gotoxy(x,y);
printf("-----");
y++;
for(j=0;j<fs;y++,j++)
{
gotoxy(x,y);
printf("%c(%d)",fp[j],ru[j]);
}
y++;
gotoxy(x,y);
printf("-----");
y++;
gotoxy(x,y);
printf("%c",ps);
}
printf("\n\n\n\n\n TOTAL PAGE FAULTS =%d",pfc);
getch();
}
output:-
Image
___________________________________________________________________________________________

0 comments:

Post a Comment