Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

EX-NO:

IMPLEMENTATION OF PAGE REPLACEMENT ALGORITHM


DATE:

(a) FIFO

AIM :
To write a C program to implement the FIFO page replacement algorithm.

ALGORITHM :
Step 1: Start the program
Step 2: Declare reference string, frame .
Step 3: Get the reference string and its length.
Step 4: Get the number of frames and initialize them as -1.
Step 5: For each page in reference string
a) If page is not available in the frame.
i) Increment page fault
ii) Replace the oldest page by this current page.
Step 6: Display the reference string, frame contents.
Step 7: Stop the program.

PROGRAM :
#include<stdio.h>
int main()
{
int i,j,n,a[50],frame[10],no,k,avail,count=0;
printf("\n ENTER THE NUMBER OF PAGES:\n");
scanf("%d",&n);
printf("\n ENTER THE PAGE NUMBER :\n");
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
printf("\n ENTER THE NUMBER OF FRAMES :");
scanf("%d",&no);
for(i=0;i<no;i++)
frame[i]= -1;
j=0;
printf("\tref string\t page frames\n");
for(i=1;i<=n;i++)
{
printf("%d\t\t",a[i]);
avail=0;
for(k=0;k<no;k++)
if(frame[k]==a[i])
avail=1;
if (avail==0)
{
frame[j]=a[i];

|Page KGISL Institute of technology 711722104052


j=(j+1)%no;
count++;
for(k=0;k<no;k++)
printf("%d\t",frame[k]);
}
printf("\n");
}
printf("Page Fault Is %d",count);
return 0;
}

OUTPUT :

RESULT :
Thus the implementation of FIFO page replacement algorithm is executed and verified successfully.

(b) LRU

AIM :
To write a C program to implement the LRU page replacement algorithm.

ALGORITHM :
step1: Start the program.
step2: Get the number of pages, reference string and number of frames.
step3: Declare count variable.
step4: Copy the first page to the first frame and increment page fault count.
step5: For the remaining pages, select the least recently used page by counter value
step6: Replace them accordingly to the selection.
|Page KGISL Institute of technology 711722104052
step7: Display the values.
step8:Display the number of page faults.
step9:Stop the program

PROGRAM :
#include <stdio.h>
int arrmin(int[], int);
main()
{
int i,j,len,rs[50],frame[10],nf,k,avail,count=0;
int access[10], freq=0, dm;
printf("Length of Reference string : ");
scanf("%d", &len);
printf("Enter reference string :\n");
for(i=1; i<=len; i++)
scanf("%d", &rs[i]);
printf("Enter no. of frames : ");
scanf("%d", &nf);
for(i=0; i<nf; i++)
frame[i] = -1;
j = 0;
printf("\nRef. str Page frames");
for(i=1; i<=len; i++)
{
printf("\n%4d\t", rs[i]);
avail = 0;
for(k=0; k<nf; k++)
{
if(frame[k] == rs[i])
{
avail = 1;
access[k] = ++freq;
break;
}
}
if(avail == 0)
{
dm = 0;
for(k=0; k<nf; k++)
{
if(frame[k] == -1)
dm = 1;
break;
}
if(dm == 1)
{
frame[k] = rs[i];
access[k] = ++freq;
count++;
}else
{
j = arrmin(access, nf);
|Page KGISL Institute of technology 711722104052
frame[j] = rs[i];
access[j] = ++freq;
count++;}
for(k=0; k<nf; k++)
printf("%4d", frame[k]);}}
printf("\n\nTotal no. of page faults : %d\n", count);}
int arrmin(int a[], int n){
int i, min = a[0];
for(i=1; i<n; i++)
if (min > a[i])
min = a[i];
for(i=0; i<n; i++)
if (min == a[i])
return i;}

OUTPUT :

RESULT :
Thus the implementation of LRU page replacement algorithm is executed and verified successfully.

(C) LFU

AIM :
To write a C program to implement the LFU page replacement algorithm.
ALGORITHM :
step1: Start the program.
step2: Declare and initialize total_frames, total_pages, hit = 0
step3: Get the input for number of pages and frames
step4: Set frame[]=-1 for all frames and count[]=0 for all pages.
step5; Get the input for pages.
step6: For each page in reference string repeat steps from 7 to 13
step7: Increment the count for the page

|Page KGISL Institute of technology 711722104052


step8:Set the time for the page
step9:Set flag to 1.
step10:Set least to initial frame.
step11: For each frame
If frame is either not empty or filled with current page then
If frame is not empty then
Increment hit and decrement pgfault
Set flag to 0
load frame with the current page
Compare the count of least and current frame and set least to the minimum
step12:If flag is set to 1 then
Set mintime to 50
For each frame
If count of current frame is equal to least and time of current frame is less than mintime
Set temp as j
Set mintime as time[frame[]]
Set count for the new frame to 0
Load the page in to new frame
step13: For each frames
Print frame[]
step14: Print the hit and pgfault.
step15: Stop the program.

PROGRAM :
#include<stdio.h>
int main()
{
int total_frames, total_pages, hit = 0;
int pages[25], frame[10], arr[25], time[25];
int m, n, page, flag, k, minimum_time, temp;
printf("Enter Total Number of Pages:\t");
scanf("%d", &total_pages);
printf("Enter Total Number of Frames:\t");
scanf("%d", &total_frames);
for(m = 0; m < total_frames; m++)
{
frame[m] = -1;
}
for(m = 0; m < 25; m++)
{
arr[m] = 0;
}
printf("Enter Values of Reference String\n");
for(m = 0; m < total_pages; m++)
{
printf("Enter Value No.[%d]:\t", m + 1);
scanf("%d", &pages[m]);
}
printf("\n");
for(m = 0; m < total_pages; m++)
{
arr[pages[m]]++;

|Page KGISL Institute of technology 711722104052


time[pages[m]] = m;
flag = 1;
k = frame[0];
for(n = 0; n < total_frames; n++)
{
if(frame[n] == -1 || frame[n] == pages[m])
{
if(frame[n] != -1)
{
hit++;
}
flag = 0;
frame[n] = pages[m];
break;
}
if(arr[k] > arr[frame[n]])
{
k = frame[n];
}
}
if(flag)
{
minimum_time = 25;
for(n = 0; n < total_frames; n++)
{
if(arr[frame[n]] == arr[k] && time[frame[n]] < minimum_time)
{
temp = n;
minimum_time = time[frame[n]];
}
}
arr[frame[temp]] = 0;
frame[temp] = pages[m];
}
for(n = 0; n < total_frames; n++)
{
printf("%d\t", frame[n]);
}
printf("\n");
}
printf("Page Hit:\t%d\n", hit);
return 0;
}

|Page KGISL Institute of technology 711722104052


OUTPUT:

RESULT:
Thus the implementation of LFU page replacement algorithm is executed and verified
successfully.

|Page KGISL Institute of technology 711722104052

You might also like