0% found this document useful (0 votes)
20 views

Page Replacement

This C program implements three page replacement algorithms - FIFO, LRU, and optimal - to simulate memory page faults on a given reference string. It defines functions for each algorithm that take the reference string, string size, and number of frames as parameters. The functions simulate replacing pages based on the specific algorithm, track page faults, and output the reference string at each step and total faults. The main function gets user input for the reference string and frames and calls each algorithm function.

Uploaded by

Deepika Padukon
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Page Replacement

This C program implements three page replacement algorithms - FIFO, LRU, and optimal - to simulate memory page faults on a given reference string. It defines functions for each algorithm that take the reference string, string size, and number of frames as parameters. The functions simulate replacing pages based on the specific algorithm, track page faults, and output the reference string at each step and total faults. The main function gets user input for the reference string and frames and calls each algorithm function.

Uploaded by

Deepika Padukon
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

#include <stdio.

h>

#include <stdlib.h>

void fifo(int reference_string[],int REF_STRING_SIZE,int FRAME_SIZE) {

int page_frame[FRAME_SIZE];

for(int i=0;i<FRAME_SIZE;i++){

page_frame[i]=-1;

int page_faults = 0;

int pointer = 0;

printf("FIFO Algorithm:\n");

for (int i = 0; i < REF_STRING_SIZE; i++) {

int page = reference_string[i];

int hit = 0;

for (int j = 0; j < FRAME_SIZE; j++) {

if (page_frame[j] == page) {

hit = 1;

break;

if (!hit) {

page_faults++;

page_frame[pointer] = page;

pointer = (pointer + 1) % FRAME_SIZE;

printf("Page %d: ", page);

for (int j = 0; j < FRAME_SIZE; j++) {

printf("%d ", page_frame[j]);

printf("\n");
}

printf("Total Page Faults: %d\n", page_faults);

void lru(int reference_string[],int REF_STRING_SIZE,int FRAME_SIZE) {

int page_frame[FRAME_SIZE];

for(int i=0;i<FRAME_SIZE;i++){

page_frame[i]=-1;

int page_faults = 0;

int time_stamp[FRAME_SIZE];

for(int i=0;i<FRAME_SIZE;i++){

time_stamp[i]=-1;

printf("LRU Algorithm:\n");

for (int i = 0; i < REF_STRING_SIZE; i++) {

int page = reference_string[i];

int hit = 0;

int lru_page = 0;

for (int j = 0; j < FRAME_SIZE; j++) {

if (page_frame[j] == page) {

hit = 1;

time_stamp[j] = i;

break;

if (page_frame[j] == -1) {

page_frame[j] = page;

time_stamp[j] = i;

hit = 1;

page_faults++;
break;

if (time_stamp[j] < time_stamp[lru_page]) {

lru_page = j;

if (!hit) {

page_faults++;

page_frame[lru_page] = page;

time_stamp[lru_page] = i;

printf("Page %d: ", page);

for (int j = 0; j < FRAME_SIZE; j++) {

printf("%d ", page_frame[j]);

printf("\n");

printf("Total Page Faults: %d\n", page_faults);

void optimal(int reference_string[],int REF_STRING_SIZE,int FRAME_SIZE) {

int page_frame[FRAME_SIZE];

for(int i=0;i<FRAME_SIZE;i++){

page_frame[i]=-1;

int page_faults = 0;

int next_empty_slot = 0;

printf("Optimal Algorithm:\n");

for (int i = 0; i < REF_STRING_SIZE; i++) {


int page = reference_string[i];

int hit = 0;

for (int j = 0; j < FRAME_SIZE; j++) {

if (page_frame[j] == page) {

hit = 1;

break;

if (!hit) {

page_faults++;

if (next_empty_slot < FRAME_SIZE) {

page_frame[next_empty_slot] = page;

next_empty_slot++;

} else {

int replace_page = -1;

for (int j = 0; j < FRAME_SIZE; j++) {

int future = 0;

for (int k = i + 1; k < REF_STRING_SIZE; k++) {

if (page_frame[j] == reference_string[k]) {

future = k - i;

break;

if (future == 0) {

replace_page = j;

break;

if (future > replace_page || replace_page == -1) {

replace_page = j;

}
page_frame[replace_page] = page;

printf("Page %d: ", page);

for (int j = 0; j < FRAME_SIZE; j++) {

printf("%d ", page_frame[j]);

printf("\n");

printf("Total Page Faults: %d\n", page_faults);

int main() {

int REF_STRING_SIZE,FRAME_SIZE=0;

printf("\nEnter the length of page reference sequence:");

scanf("%d",&REF_STRING_SIZE);

int reference_string[REF_STRING_SIZE];

printf("\nEnter the number of frames:");

scanf("%d",&FRAME_SIZE);

printf("\nEnter the referenced sequence:");

for(int i=0;i<REF_STRING_SIZE;i++){

//{4 , 7, 6, 1, 7, 6, 1, 2, 7, 2};

scanf("%d",&reference_string[i]);

fifo(reference_string,REF_STRING_SIZE,FRAME_SIZE);

printf("\n");

lru(reference_string,REF_STRING_SIZE,FRAME_SIZE);

printf("\n");

optimal(reference_string,REF_STRING_SIZE,FRAME_SIZE);

return 0;

}
OUTPUT:

You might also like