Friday, 8 May 2015

FIFO and LPU pages replacement Algorithm implementation using C

Coding:

#include<stdio.h>
#include<conio.h>
int m,n,i,j,k,flag,count=0,refer[100],page_frame[100][2],fault=0,min,no_frame;
void replace(int z)
{
for(i=0;i<n;i++)
{
flag=1;
for(j=0;j<no_frame;j++)
if(refer[i]==page_frame[j][0])
{
m=j;
flag=0;
}
if(flag)
{
fault++;
min=32000;
for(j=0;j<no_frame;j++)
if(page_frame[j][1]<min)
{
min=page_frame[j][1];
k=j;
}
page_frame[k][0]=refer[i];
page_frame[k][1]=++count;
for(j=0;j<no_frame;j++)
printf(" %d ",page_frame[j][0]);
printf("\n");
}
else
{
printf("\n No pagefault\n\n");
if(z==2)
page_frame[m][1]=++count;
}
}
printf("\nNumber of pagefault is:%d\n",fault);
}
int main()
{
clrscr();
printf("\n Enter the number of reference:");
scanf("%d",&n);
printf("\n Enter the number of frame:");
scanf("%d",&no_frame);
printf("\n Enter the reference~string:");
for(i=0;i<n;i++)
scanf("%d",&refer[i]);
printf("\t\tFIFO ALGORITHM\n");
printf("\n=======================\n");
for(i=0;i<no_frame;i++)
{
page_frame[i][0]=-1;
page_frame[i][1]=count;
}
replace(1);
fault=0;
count=0;
printf("\n\n\nLRU ALGORITHM\n");
printf("\n=======================\n");
for(i=0;i<no_frame;i++)
{
page_frame[i][0]=-1;
page_frame[i][1]=count;
}
replace(2);
getch();
return 0;
}










Output:

Enter the number of reference: 20
Enter the number of frames:     3
Enter the reference string:    7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1

                        FIFO ALGORITHM
7     7    -1
7     0    -1
2     0      1
NO page fault
2      3      -1
2      3       0
4       3      0
4       2      0
4       2      3
0       2      3
NO page fault
NO page fault
0      1      3
0      1      2
NO page fault
NO page fault
7      1      2
7      0      2
7      0      1
Number of page fault  is: 15


                                   LRU ALGORITHM
7      -1      -1
7       0       -1
2       0        1
NO page fault
2      0     3
NO page fault
4    0     3
4    0      2
4    3      2
0    3      2
NO page fault
NO page fault 

1      3      2
NO page fault
 1     0       2
NO page fault
  1     0       7

NO page fault
NO page fault

Number of pages fault is: 12

No comments:

Post a Comment