C program for LRU page replacement algorithm

Saturday, September 19th, 2009

OUTPUT :
2 -1 -1
2  3 -1
2  3 -1
2  3  1
2  5  1
2  5  1
2  5  4
2  5  4
3  5  4
3  5  2
3  5  2
3  5  2
no of page faults : 4

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include<stdio.h>
#include<conio.h>
int fr[3];
void main()
{
void display();
int p[12]={2,3,2,1,5,2,4,5,3,2,5,2},i,j,fs[3];
int index,k,l,flag1=0,flag2=0,pf=0,frsize=3;
clrscr();
for(i=0;i<3;i++)
{
fr[i]=-1;
}
for(j=0;j<12;j++)
{
flag1=0,flag2=0;
for(i=0;i<3;i++)
{
if(fr[i]==p[j])
{
flag1=1;
flag2=1;
break;
}
}
if(flag1==0)
{
for(i=0;i<3;i++)
{
if(fr[i]==-1)
{
fr[i]=p[j];
flag2=1;
break;
}
}
}
if(flag2==0)
{
for(i=0;i<3;i++)
fs[i]=0;
for(k=j-1,l=1;l<=frsize-1;l++,k--)
{
for(i=0;i<3;i++)
{
if(fr[i]==p[k])
fs[i]=1;
}
}
for(i=0;i<3;i++)
{
if(fs[i]==0)
index=i;
}
fr[index]=p[j];
pf++;
}
display();
}
printf("\n no of page faults :%d",pf);
getch();
}
void display()
{
int i;
printf("\n");
for(i=0;i<3;i++)
printf("\t%d",fr[i]);
}

OUTPUT :
2 -1 -1
2  3 -1
2  3 -1
2  3  1
2  5  1
2  5  1
2  5  4
2  5  4
3  5  4
3  5  2
3  5  2
3  5  2
no of page faults : 4

Author Name :
Ranjith

Total : 23 Comments


23 Responses to “C program for LRU page replacement algorithm”

  1. student_from_politechnika says:

    good job :)
    if you want I send you my project in cpp.

  2. mery says:

    good job

    can u send me your projuct… I think that it can help me

    thainks a lot

  3. usha says:

    Hi
    This pgm sounds good. If u have page replacement algorithm project can u
    u mail to the above e mail address

  4. paul says:

    why only 4 page faults? i think there are 7 page faults…

  5. Akansha says:

    paul u r correct..there r 7 page faults..he hasnt included the initial page faults while inserting the pages initially in d stack

  6. Hariswaroop says:

    this code has 6 page faults…

  7. shashank singh says:

    yup akansha that’s correct…………he hasn’t included d initial page faults

  8. bhagya says:

    ya dis pogram is crct but page faults are 7 he did not include first 3

  9. SRINATH says:

    KKKK…. I CAN……

  10. kiran says:

    i want the page values as the increasing or dec order so modify the pgm

  11. ani says:

    after 8 pages/(output lines) why 3 is replacing 2(since 2 is most frequently use) the least frequently used at that page is 4 so why not 4 is replaced by 3… can anyone explain what lfu actually is…?????

  12. nagapavan says:

    mee ammamama raaaa ranjith

  13. Prabhu.K says:

    this prg is correct . very good

  14. Prabhu.K says:

    Please the send the implement the mutual exclusion problem using dekker’s algorthim program

  15. kk says:

    that’s correct…………he hasn’t included d initial page faults..

  16. sohail says:

    no of page fault is wrong…..
    u have to increment pf in first if condition{if (flag1==0)}…
    then it will fine
    the no of pf is 7…………
    thank you…….
    Heart Hacker…

  17. blanken says:

    program was wrong. EX: 7,0,1,2,0,3,0,4,2,3,0,3,2,1,2,0,1,7,0,1 has 16 error but program has only 9 error. you can fix again.thanks

  18. marry says:

    please u don’t have the same pg but writing with JAVA

  19. ashita garg says:

    Hii..

    this program is absolutely correct.
    if it’ll not bother u then plzzz send me ur project i really need it.
    i feel heartly thnkfull if u’ll send me this project.

    thnkuu

  20. Omkar says:

    Hey,its very correct program,but; just need one modification..
    if two same elements appear altogether then it results in wrong output.
    eg. 1 2 3 2 3 4 5 will give correct output.
    but;1 2 3 3 3 4 5 will yield wrong output.
    I agree its a very rare case..but;look for it if you can..!!
    Thank you..!!

  21. imran says:

    This program gives wrong output. I guess 7 page faults according to given input.

  22. Well says:

    @ashita copy paste it

  23. Lib says:

    Hello, can u give the C code for MRU page replement algorithm?. Pls…

Leave a Reply

Question and Answer
C/C++ Unix & Linux Wordpress
Source codes
C C++ Java

Free email signup

Email: