LRU Page Replacement Algorithm in C | LRU Scheduling

lru page replacement algorithm in cToday we implement least Recently used LRU page replacement algorithm in c. with this LRU page replacement algorithm in OS used for managing processes waiting time. this algorithm is use least recently used processes for the replacement of the page fault in Operating System.

In this section we can discuss lru page replacement algorithm with explanation. the Least Recently used Scheduling algorithm may be beneficial. replacement the pages with recently used if they are not longer available for processing.

LRU page replacement algorithm in c with explanation

In this lru page replacement algorithm with explanation only 3 page slot available. by using those three slot we have to schedule lru program. Simultaneously we need to calculate total number of page fault required for lru algorithm c. the working of least recently used page replacement algorithm in c replaces the least used resource up-to. Based on these intimation page replacement lru page replacement in c work.

We Now now how least recently used algorithm in c work ? the lru algorithm c help to replace pages for faster performance by replacing unused pages. Lru scheduling example is shown below to get more information related to lru page replacement algorithm in operating system link on download pdf link.

In the Example 10 element are taken has input and page available is 3. Now we need c program to implement lru page replacement algorithm for solving example below.

5 6 9 5 4 3 9 9 3 5

5 5 5 5 5 5 5 5
NULL 6 6 6 4 4 9 9
NULL NULL 9 9 9 3 3 3

In this above table the pages fault can be replaced only when it will least recently used and program for lru page replacement in c given below. if two or more are have same occurrence then FCFS scheduling algorithm is used. lru algorithm in c require more memory to store not used field.

download PDF :

lru Page Replacement Algorithm in c

 

lru page replacement algorithm in c

 

Watch this video to Now more about this page replacement algorithm. Make your concept clear and step by step with example now start watching this video. Beneficial lru page replacement algorithm example is also given. by this it’s easier to understand program for lru page replacement algorithm .

 

LRU page replacement algorithm in c with Output

/* c program to implement lru page replacement algorithm */

#include<stdio.h>
#include<conio.h>
#define high 37
void main()
{
int fframe[10],used[10],index;
int count,n1,k,nf,np=0,page[high],tmp;
int flag=0,pf=0;
clrscr();
printf(“Enter no. of frames:”);
scanf(“%d”,&nf);
for(i=0;count<nf;count++)
fframe[count]=-1;
printf(” lru page replacement algorithm in c “);
printf(“Enter pages (press -999 to exit):\n”);
for(count=0;count<high;count++)
{
scanf(“%d”,&tmp);
if(tmp==-999) break;
page[count]=tmp;
np++;
}
for(count=0;count<np;count++)
{
flag=0;
for(n1=0;n1<nf;n1++)
{
if(fframe[n1]==page[count])
{
printf(“\n\t”);
flag=1;break;
}
}
/* program for lru page replacement algorithm in c */
if(flag==0)
{
for(n1=0;n1<nf;n1++) used[n1]=0;
for(n1=0,tmp=count-1;n1<nf-1;n1++,tmp–)
{
for(k=0;k<nf;k++)
{
if(fframe[k]==page[tmp])
used[k]=1;
}
}
for(n1=0;n1<nf;n1++)
if(used[n1]==0)
index=n1;
fframe[index]=page[count];
printf(“\nFault: “);
pf++;
}
for(k=0;k<nf;k++)
printf(“%d\t”,fframe[k]);
} // lru algorithm in c
printf(“\nnumber of total page faults is: %d “,pf);
getch();
}

Output of LRU page replacement algorithm in c

lru page replacement algorithm in c
Enter no. of frames Enter pages (press -999 to exit):
2 3 2 1
5 2 4 5
3 2 5 -999
-1 -1 2 Fault:
-1 -1 2 Fault:
-1 3 2 Fault:
1 3 2 Fault:
1 3 2
1 5 2 Fault:
4 5 2 Fault:
4 5 2
4 5 3 Fault:
2 5 3 Fault:
2 5 3
2 5 3

 

output of program for lru c program is require input for which we require to find lru scheduling. by this c program for lru page replacement algorithm we can easily find page fault count. also keep track where and when page fault occur. thank you for reading LRU page replacement algorithm in c with example.

If you have any query related to c program for lru page replacement you can ask query by commenting below. we hope lru program in c like you if yes then share on social website to know your friends least recently used page replacement algorithm in c.