crack001

LRU Page Replacement Algorithm in C Language with Output

Posted by crack001 | Posted in Operating System | Posted on 07-12-2013

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 example . 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 Example

In this lru page replacement algorithm example only 3 page slot available. the process are given bellows.

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. if two or more are have same occurrence then FCFC scheduling algorithm is used.

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.

 

 LRU page replacement algorithm in c Language with Output

#include<stdio.h>

#include<conio.h>

#define high 25void 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;

}

}

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]);

}

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 framesEnter 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

thank you for reading LRU page replacement algorithm in c with example.

 

Write a comment