AIM:
To write a LINUX/UNIX C Program for the Implementation of Page Replacement Algorithm in CS1254 - Operating Systems Laboratory.
int m,n,I,j,k,flag,count=0,refer[100],page_frame[100][2],fault=0,min,no_frames;
void replaceme(int z)
{
for(i=0;i<n;i++)
{
flag=1;
for(j=0;i<no_frames;j++)
if(refer[i]==page_frame[j][0])
{
m=j;
flag=0;
}
if(flag)
{
fault++;
min=32000;
for(j=0;i<no_frames;j++)
if(page_frame[j][1]<min)
{
min=page_frame[j][1];
k=j;
}
page_frame[k][0]=refer[i];
page_frame[j][1]=++count;
for(j=0;i<no_frames;j++)
printf(“%d”,page_frame[j][0]);
printf(“\n”);
}
else
printf(“No page fault\n”);
if(z==2)
page_frame[m][1]=++count;
}
}
printf(“Number of page faults is: %d\n”,fault);
}
int main()
{
printf("\nEnter the number of reference:");
scanf("%d",&n);
printf("\nEnter the number of frames:");
scanf("%d",&no_frames);
printf("\nEnter the reference string: ");
for(i=0;i<n;i++)
scanf("%d",&refer[i]);
printf("\t\t\t fifo algorithm ");
for(i=0;i<no_frames;i++)
{
page_frame[i][0]=-1;
page_frame[i][1]=count;
}
replace(1);
fault=0;
count=0;
printf("\t\t\t LRU ALGORITHM\n");
for(i=0;i<no_frames;i++)
{
page_frame[i][0]=-1;
page_frame[i][1]=count;
}
replace(2);
return 0;
}
[examuser56@localhost ~]$ cc pagereplacement.c
[examuser56@localhost ~]$ ./a.out
Enter the number of reference: 6
Enter the number of frames:3
Enter the reference string: 1
3
0
5
1
2
fifo algorithm 1-1-1
13-1
130
530
510
512
Number of page faults is: 6
LRU ALGORITHM
1-1-1
13-1
130
530
510
512
Number of page faults is: 6
To write a LINUX/UNIX C Program for the Implementation of Page Replacement Algorithm in CS1254 - Operating Systems Laboratory.
SOURCE CODE:
#include<stdio.h>int m,n,I,j,k,flag,count=0,refer[100],page_frame[100][2],fault=0,min,no_frames;
void replaceme(int z)
{
for(i=0;i<n;i++)
{
flag=1;
for(j=0;i<no_frames;j++)
if(refer[i]==page_frame[j][0])
{
m=j;
flag=0;
}
if(flag)
{
fault++;
min=32000;
for(j=0;i<no_frames;j++)
if(page_frame[j][1]<min)
{
min=page_frame[j][1];
k=j;
}
page_frame[k][0]=refer[i];
page_frame[j][1]=++count;
for(j=0;i<no_frames;j++)
printf(“%d”,page_frame[j][0]);
printf(“\n”);
}
else
printf(“No page fault\n”);
if(z==2)
page_frame[m][1]=++count;
}
}
printf(“Number of page faults is: %d\n”,fault);
}
int main()
{
printf("\nEnter the number of reference:");
scanf("%d",&n);
printf("\nEnter the number of frames:");
scanf("%d",&no_frames);
printf("\nEnter the reference string: ");
for(i=0;i<n;i++)
scanf("%d",&refer[i]);
printf("\t\t\t fifo algorithm ");
for(i=0;i<no_frames;i++)
{
page_frame[i][0]=-1;
page_frame[i][1]=count;
}
replace(1);
fault=0;
count=0;
printf("\t\t\t LRU ALGORITHM\n");
for(i=0;i<no_frames;i++)
{
page_frame[i][0]=-1;
page_frame[i][1]=count;
}
replace(2);
return 0;
}
OUTPUT:
[examuser56@localhost ~]$ cc pagereplacement.c
[examuser56@localhost ~]$ ./a.out
Enter the number of reference: 6
Enter the number of frames:3
Enter the reference string: 1
3
0
5
1
2
fifo algorithm 1-1-1
13-1
130
530
510
512
Number of page faults is: 6
LRU ALGORITHM
1-1-1
13-1
130
530
510
512
Number of page faults is: 6
EmoticonEmoticon