Thursday, April 22, 2010

PAGE REPLACEMENT ALGORiTHM(FIFO & LRU)

/*PROGRAM TO IMPLEMENT PAGE REPLACEMENT ALGORiTHM(FIFO & LRU)*/
// Add the header files stdlib.h      stdio.h      conio.h

#include
#include
#include

void fifo(int a[],int n)
{
    int i,m=n,x[3],v=0,y=0,next,j=0;
while(m!=0)
{
next=a[v];
j=0;
while(x[j]!=next&&j<3)
j++;

if(j>2)
{
printf("\nReplace page %d\n",x[y]);
x[y]=a[v];
v++;
y++;
m--;
}
else
{
printf("\n\nPage hit\n\n");
m--;
v++;
}
for(i=0;i<3;i++)
    printf(" %d",x[i]);

if(y==3)
y=0;
}
}
void lru (int b[],int n)
{
int i,m=n,x[3],v=0,y=0,next,j=0,min[3],min1=0,w,min2[3],min3;
for(i=0;i<3;i++)
{
x[i]=0;
min[i]=0;
}
for(i=0;i<3;i++)
{
j=0;
while(x[j]!=b[v]&&j<3)
j++;
if(j<3)
{
printf("\n\nPage hit\n\n");
m--;
v++;
}
else
{
printf("\nReplace page %d",x[y]);
x[y]=b[v];
v++;
m--;
y++;
for(j=0;j<3;j++)
printf(" %d",x[j]);
}
}

while(m!=0)
{
next=b[v];
j=0;
while(x[j]!=next&&j<3)
j++;
if(j<3)
{
printf("\n\nPage hit\n\n");
m--;
v++;
}
else
{
for(i=0;i<3;i++)
{
w=v;
while(b[w]!=x[i]&&w>0)
w--;
if(w>0)
{
min[i]=w;
min2[i]=b[w];
}
else
min[i]=w;
}
}
min3=min[0];
min1=0;
for(i=1;i<3;i++)
{
if(min[i]<=min3)
{
min3=min[i];
min1=i;
}
}
}
printf("\nReplace pager %d\n",x[min1]);
x[min1]=b[v];
v++;
m--;
for(i=0;i<3;i++)
printf(" %d", x[i]);
}

void main()
{
int n,i,a[25],b[25],c[25],ch;
clrscr();
printf("Enter how many pages:\n");
scanf("%d",&n);
printf("Enter sequence of pages\n");
for(i=0;i
{
scanf("%d",&a[i]);
b[i]=a[i];
c[i]=a[i];
}
do
{
printf("\nMENU\n");
printf("1.FIFO\n");
printf("2. LRU\n");
printf("3. EXIT\n");
printf("Enter choice\n");
scanf("%d",&ch);

switch(ch)
{
case 1: fifo(a,n);
break;
case 2: lru(c,n);
break;
case 3: break;
}
}
while(ch!=3);
getch();
}
/*Output:
Enter how many pages:
3                                                                              
Enter sequence of pages                                                        
1                                                                              
2                                                                              
3
                                                                               
MENU                                                                           
1.FIFO                                                                         
2. LRU                                                                         
3. EXIT                                                                        
Enter choice                                                                   
2                                                                              
                                                                               
Replace page 0 1 0 0                                                           
Replace page 0 1 2 0                                                           
Replace page 0 1 2 3                                                           
Replace pager 1                                                                
 0 2 3                                                                         
MENU
1.FIFO                                                                         
2. LRU                                                                         
3. EXIT                                                                        
Enter choice
1                                                                              
                                                                               
Replace page 8889                                                              
 1 10289 644                                                                   
Replace page 10289                                                             
 1 2 644                                                                       
Replace page 644                                                               
 1 2 3                                                                         
MENU                                                                           
1.FIFO                                                                         
2. LRU                                                                         
3. EXIT                                                                        
Enter choice
3
*/
                                                                       

0 comments:

Post a Comment

 
ShareThis

Visitor

Website counter
Copyright 2009 Code's. Powered by Blogger
Blogger Templates created by Deluxe Templates
Wordpress by Wpthemescreator
Blogger Showcase