FIFO Page Replacement Algorithm

Exploring FIFO Page Replacement Algorithm in C

TheLazyBusyCoder's Blog

Welcome to my coding adventures! In this post, we'll explore a C program.

FIFO Page Replacement Algorithm in C

Check out my YouTube video explaining this code: Watch now

        
#include <stdio.h>

int find(int frame[] , int framesize , int cur) {
	int i;
	for(i = 0; i < framesize; i++) {
		if(cur == frame[i]) return 1;
	}
	return 0;
}

void display(int frame[] , int framesize , int hit) {
	int i;
	for(i = 0; i < framesize; i++) {
		printf("%d | " , frame[i]);
	}
	if(hit == 1) printf("HIT\n");
	else printf("*\n");
}

void fifo(int ref[] ,int framesize , int n) {
	int frame[framesize]; 
	int i;
	for(i = 0; i < framesize; i++) frame[i] = -1;
	
	int hit = 0, pf = 0;
	int rep = 0;
	for(i = 0; i < n; i++) {
		int cur = ref[i];
		if(find(frame , framesize, cur) == 0) {
			pf++;
			frame[rep] = cur;
			rep++;
			if(rep == framesize) rep = 0;
			display(frame , framesize , 0);
		} else {
			hit++;
			display(frame , framesize , 1);
		}
	}
}

int main() {
  int ref[] = {7,0,1,2,0,3,0,4,2,3,0,3,2,1,2,0,1,7,0,1};
  int frameSize = 3;
  int n = 20;
  fifo(ref , frameSize , n);
  return 0;
}


        
    

Comments

Popular posts from this blog

Least Recently Used (LRU) page algorithm in c

Optimal Page Replacement (OPT) in C

LFU Page Replacement Algorithm in C