Professional Documents
Culture Documents
Ex12 Ios
Ex12 Ios
(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];
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
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]]++;
RESULT:
Thus the implementation of LFU page replacement algorithm is executed and verified
successfully.