Sunday, November 25, 2012

Program fifo and lru page replacement algorithm | Operating System


Code for first in first out as FIFO and Least Recently Used as LRU page replacement algorithm using structure in C programming with output | Operating System

Code for first in first out as FIFO and Least Recently Used as LRU page replacement algorithm using struture in C programming with output | Operating System



Solution : In this C program for Operating System's algorithms as FIFO and LRU for page replacement, i have created serveral functions as follows :

void fifo() : This function uses First In First Out Algorithm to Fill the page frame frame[10]

void lru(): This function uses Least recently Used Algorithm to Fill page framel[10]

int leastrecent():This function will finding out leastrecent element in the framel[]

void display(int i):This function used to display elements in Page Frames with parameter i used to identify which frame to display in this program if i=0 then it will print fifo frame[] else lru framel[]

int check(int i,int c,int refindex): This function used to check coming elements from refs[i] as reference string is present in frame or not if it is then ignore .In this function i have taken 3 parameters first i used to identify the frame,c is element from refs[i],refindex used to identify last used index in case of LRU

void frameinitial(): This function used to initialize the frame[10] and framel[10] as both frames belongs to structure so we to initialize the value of its variables as ele=-1,lastused=0


Some of the Variable and arrays :
refs[100] : Holds reference string
refsize :Size of reference string
framesize :Size of frames

Code for fifo and lru page replacement algorithm

/* Program for Page Segmentation using FIFO LRU from Nullpointer.in*/

#include<stdio.h>
int refs[100],refsize,framesize;
void fifo();
void lru();
int leastrecent();
struct Frame
{
int lastused;
int ele;
};
void display(int i);
int check(int i,int c,int refindex);
struct Frame frame[10],framel[10];
void frameinitial()
{
int z;
for(z=0;z<10;z++)
{
        frame[z].ele=-1;
        framel[z].ele=-1;
        frame[z].lastused=0;
        framel[z].lastused=0;
}

}

int main()
{
printf("\nPlease Give Size for Reference String:-\n");
scanf("%d",&refsize);
printf("\nPlease Give Frame Size:-\n");
scanf("%d",&framesize);
frameinitial();
printf("\nPlease Enter Reference String:-");
int r;
for(r=0;r<refsize;r++)
{
scanf("%d",&refs[r]);
}
printf("\nPAGE FRAMES FOR FIFO PAGE REPLACEMENT:\n\n");
fifo();
printf("\n\nPAGE FRAMES FOR LRU PAGE REPLACEMENT:\n\n");
lru();
printf("\n");
return 0;
}
/* First in First Out Function's Logic */
void fifo()
{
int i,p;
p=0;
 for(i=0;i<refsize;i++)
 {
 if(p==framesize)
 {
  p=0;
  if(check(0,refs[i],i))
  {
  frame[p].ele=refs[i];
  p++;
  display(0);
  }
 }//run when  frame become full
 else
 {
  if(check(0,refs[i],i))
   {
  frame[p].ele=refs[i];
  p++;
  display(0);
   }

 }
  
 }//exit for
}

void lru()
{
int i,p,l;
p=0;
        for(i=0;i<refsize;i++)
        {
          if(p==framesize)
        {
         
                if(check(1,refs[i],i))
                {
  l=leastrecent();
                framel[l].ele=refs[i];
                framel[l].lastused=i;
                display(1);
                }
        }//run when  framel become full
        else
        {
                if(check(1,refs[i],i))
                {
                framel[p].ele=refs[i];
  framel[p].lastused=i;
                p++;
                display(1);
                }

        }

        }//exit for

}

void display(int i)
{
int d;
if(i==0)
{
printf("[");
for(d=0;d<framesize;d++)
{
if(frame[d].ele==-1)
     printf(" "); 
else printf("%d",frame[d].ele);
if(d<framesize-1)
 printf("|");
}
printf("] ");
}
else
{
printf("[");
for(d=0;d<framesize;d++)
{
if(framel[d].ele==-1)
     printf(" ");
else printf("%d",framel[d].ele);
if(d<framesize-1)
        printf("|");
}
printf("] ");
}
}


//Method to check Is element present or not
int check(int i,int c,int refindex)
{
int ck,flag;
flag=1;
if(i==0)
{
for(ck=0;ck<framesize;ck++)
{
 if(frame[ck].ele==c)
 {
 flag=0;
 break;
 }
}
}
else
{
for(ck=0;ck<framesize;ck++)
{
        if(framel[ck].ele==c)
        {
        framel[ck].lastused=refindex;
        flag=0;
        break;
        }
}
}
return flag;
}

int leastrecent()
{
int i,min;
min=0;
for(i=0;i<framesize;i++)
{
if(framel[i].lastused<framel[min].lastused)
{
min=i;
}
}
return min;
}


Output of This Program is :-

Please Give Size for Reference String:-
8

Please Give Frame Size:-
3

Please Enter Reference String:-7 0 1 2 0 3 0 4

PAGE FRAMES FOR FIFO PAGE REPLACEMENT:

[7| | ] [7|0| ] [7|0|1] [2|0|1] [2|3|1] [2|3|0] [4|3|0] 

PAGE FRAMES FOR LRU PAGE REPLACEMENT:

[7| | ] [7|0| ] [7|0|1] [2|0|1] [2|0|3] [4|0|3] 



0 comments:

Post a Comment