C Program First in First Out [FIFO] Page Replacement Algorithm in c

   

Program for FIFO Page Replacement : Explanation [Click Here]

/*---------------------------------------------
Title : Page Replacement Algorithms
Program Developed By : Pritesh Abhiman Taral
----------------------------------------------*/

#include<stdio.h>
#include<conio.h>
#include<alloc.h>

struct node
{
int data;
struct node *next;
}*curr,*new_node,*current,*start=NULL,*temp,*last;

int arr[100],count,fault,fno;
//--------------------------------
void creat()
{
int i;

for(i=0;i< fno;i++)
{
new_node=(struct node *)malloc(sizeof(struct node));
new_node->data = -99; // -99 = NULL or Empty Characcter
new_node->next=NULL;

if(start==NULL)
{
start=new_node;
current=new_node;
}
else
{
current->next=new_node;
current=new_node;
}
}
current->next = start;
}
//--------------------------------
void display()
{
struct node *temp;
temp=start;
do
{
if(temp->data == -99)
printf("-t",temp->data);
else
printf("%dt",temp->data);
temp=temp->next;
}while(temp!=start);
}
//--------------------------------
int search(int number)
{
int flag;
struct node *temp;
temp=start;
do
{
if(temp->data==number)
return(1);
else
flag = 0;
temp=temp->next;
}while(temp!=start);

if(flag ==0)
return(0);
}
//--------------------------------
void fifo()
{
int res,i;
temp = start; //Temp Purpose
last = start; //Repl Purpose
curr = start; //Curr Purpose ->-99

for(i=0;i< count;i++)
{
res = search(arr[i]);

if(res == 0)
{
fault++;
if(curr->data == -99)
{
curr->data = arr[i];
curr = curr->next;
}
else
{
last ->data = arr[i];
last = last->next;
}
}//end outer if

printf("nnAfter Inserting (%d) :: ",arr[i]);
display();
printf(" Fault : %d",fault);
}//end for

printf("n--------------------------------------------n");
printf(" ¯ Total Number of Faults = %d",fault);
printf("n--------------------------------------------n");

}
//--------------------------------
void accept()
{
int i;
printf("nEnter the number of Frames : ");
scanf("%d",&fno);

printf("nEnter the number of Pages : ");
scanf("%d",&count);

printf("nEnter the Page No : ");
for(i=0;i< count;i++)
scanf("%d",&arr[i]);
}
//------------------------------------------------
void main()
{
char ch;
clrscr();
printf("nAccept Frame Number and Pages");
printf("n");

accept();
creat();
fifo();

getch();
}

Explanation :  [Click Here]


Output :

Accept Frame Number and Pages

Enter the number of Frames : 3

Enter the number of Pages : 7

Enter the Page No : 1 3 2 1 3 4 2


After Inserting (1) :: 1 - - Fault : 1

After Inserting (3) :: 1 3 - Fault : 2

After Inserting (2) :: 1 3 2 Fault : 3

After Inserting (1) :: 1 3 2 Fault : 3

After Inserting (3) :: 1 3 2 Fault : 3

After Inserting (4) :: 4 3 2 Fault : 4

After Inserting (2) :: 4 3 2 Fault : 4
═══════════════════════════════════════════════
» Total Number of Faults = 4
═══════════════════════════════════════════════

Explanation :  [Click Here]