Program for FIFO page replacement algorithm

Saturday, September 19th, 2009

OUTPUT :
2 -1 -1
2  3 -1
2  3 -1
2  3  1
5  3  1
5  2  1
5  2  4
5  2  4
3  2  4
3  2  4
3  5  4
3  5  2

Number of page faults  : 6

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
#include<stdio.h>
#include<conio.h>
int fr[3];
void main()
{
void display();
int i,j,page[12]={2,3,2,1,5,2,4,5,3,2,5,2};
int flag1=0,flag2=0,pf=0,frsize=3,top=0;
clrscr();
for(i=0;i<3;i++)
{
fr[i]=-1;
}
for(j=0;j<12;j++)
{
flag1=0;
flag2=0;
for(i=0;i<12;i++)
{
if(fr[i]==page[j])
{
flag1=1;
flag2=1;
break;
}
}
if(flag1==0)
{
for(i=0;i<frsize;i++)
{
if(fr[i]==-1)
{
fr[i]=page[j];
flag2=1;
break;
}
}
}
if(flag2==0)
{
fr[top]=page[j];
top++;
pf++;
if(top>=frsize)
top=0;
}
display();
}
printf("Number of page faults  : %d ",pf);
getch();
}
void display()
{
int i;
printf("\n");
for(i=0;i<3;i++)
printf("%d\t",fr[i]);
}

OUTPUT :
2 -1 -1
2  3 -1
2  3 -1
2  3  1
5  3  1
5  2  1
5  2  4
5  2  4
3  2  4
3  2  4
3  5  4
3  5  2

Number of page faults  : 6

Avatar Image

Author Name :
Ranjith

Total : 17 Comments


17 Responses to Program for FIFO page replacement algorithm

  1. sp

    can you give me C Program for clock page replacement Algorithm?

  2. ankit

    this code is not working……… here page faults are 9 but u say 6.

  3. prachi

    the code in line 18 is wrong……
    the correct code will be
    for(i=0;i<3;i++)

  4. murthy

    can uplz give me the algorithm steps for scheduling algorithms(fcfs,sjf,priority,round robin0
    and also for page replacement algorithms(fifo,lru)

  5. iqra

    plz send mai fcfs and roud robib

  6. liyta

    can i get java code????

  7. dpk

    @ankit: first learn properly,then only comment,,page faults are 6..it is corect

  8. amit

    hey yr prog is giving page fault as 6
    but if u manually calculate it u will find it is 9

  9. hariya

    program is not correct

  10. RAJ SHARMA

    it is very good effort ,keep it up

  11. Sandy

    Here fr[3] means there will b 4 frames i.e.,0,1,2,3 so thats y we r getting 6 as page faults

  12. Anuja

    actually different books give different info for calculation of page faults..
    here first 3 page faults ( ie wen frame is empty or partially empty ) are not considered..

  13. sourav

    incorrect code

  14. ravi

    thankyou

  15. ravi

    @prachi you are correct

  16. iram

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace OS_lab___FIFO
    {
    class Program
    {

    static void Main(string[] args)
    {
    int[] fr_mainMemory = new int[3];
    int i,j;
    int [] page={2,3,2,1,5,2,4,5,3,2,5,2};
    int flag1=0,flag2=0,pf=0,frsize=3,top=0;

    Console.WriteLine(“\n\n\t\t***BEHAVIOUR OF FIFO PAGE REPLACEMENT ALGORITHM***\n\n”);
    Console.WriteLine(“These are pages in resident set having fixed frame allocation size of 3 frames\n\n”);

    Console.WriteLine(“\tPage Address Stream : “);
    for (i = 0; i < 12; i++)
    {
    Console.Write(" "+page[i]+" ");
    }
    Console.WriteLine("\n\n");

    for(i=0;i<3;i++)
    {
    fr_mainMemory[i]=-1;
    }
    for(j=0;j<12;j++)
    {
    flag1=0;
    flag2=0;
    for(i=0;i<frsize;i++)
    {
    if(fr_mainMemory[i]==page[j])
    {
    flag1=1;
    flag2=1;
    break;
    }
    }
    if(flag1==0)
    {
    for(i=0;i=frsize)
    top=0;
    }

    display(fr_mainMemory);
    }
    Console.WriteLine(“\tNumber of page faults : “+pf);

    }

    static void display(int [] f)
    {
    int i;
    int[] residentset = new int[3];
    int frsize = 3;
    for (i = 0; i < frsize; i++)
    {
    residentset[i] = f[i];

    }
    for (i = 0; i < frsize; i++)
    {
    Console.Write("\t"+residentset[i]+" ");

    }
    Console.WriteLine("\n");
    }
    }
    }

  17. iram

    its the correct code and it works

Leave a Reply

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

Free email signup