0% found this document useful (0 votes)
58 views7 pages

LFU and LRU

The document contains code implementing the Least Frequently Used (LFU) page replacement algorithm. It takes in a reference string and number of frames as input. It iterates through the reference string, checks if each page is already in frames, and if not, replaces the least frequently used page based on a counter array. It tracks and prints the number of page faults and hits.

Uploaded by

mooezshakir56
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views7 pages

LFU and LRU

The document contains code implementing the Least Frequently Used (LFU) page replacement algorithm. It takes in a reference string and number of frames as input. It iterates through the reference string, checks if each page is already in frames, and if not, replaces the least frequently used page based on a counter array. It tracks and prints the number of page faults and hits.

Uploaded by

mooezshakir56
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Q1: LFU

#include <stdio.h>
#include <stdlib.h>

#define MAX_FRAMES 10

int main() {
int frames[MAX_FRAMES];
int counter[MAX_FRAMES];
int referenceString[MAX_FRAMES];
int numFrames, numReferences, numFaults = 0, numHits = 0;

printf("Least Frequently Used Page Replacement Algorithm\n");


printf("Enter the number of frames: ");
scanf("%d", &numFrames);

printf("Enter the length of the reference string: ");


scanf("%d", &numReferences);

printf("Enter the reference string: ");


for (int i = 0; i < numReferences; i++) {
scanf("%d", &referenceString[i]);
}

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


frames[i] = -1;
counter[i] = 0;
}

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


int currentPage = referenceString[i];
int pageFault = 1;

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


if (frames[j] == currentPage) {
pageFault = 0;
numHits++;
counter[j]++;
break;
}
}

if (pageFault) {
int minCounter = counter[0];
int index = 0;

for (int j = 1; j < numFrames; j++) {


if (counter[j] < minCounter) {
minCounter = counter[j];
index = j;
}
}

frames[index] = currentPage;
counter[index] = 1;
numFaults++;
}

printf("Reference np ");
for (int j = 0; j < numFrames; j++) {
printf("%d ", frames[j]);
if (frames[j] == -1) {
printf("-1 ");
}
}
printf("-> ");
if (pageFault) {
printf("Page Fault %d = 1", currentPage);
} else {
printf("Hit %d = %d", currentPage, counter[j]);
}
printf("\n");
}

printf("Number of Page Faults = %d\n", numFaults);


printf("Number of Hits = %d\n", numHits);

printf("Final values are:\n");


for (int i = 0; i < numFrames; i++) {
printf("%d => %d ", frames[i], counter[i]);
}
return 0;
}
OUTPUT:

Q2:
#include <stdio.h>
#define Frame_SIZE 3
int main() {
int cache[Frame_SIZE];
int used[Frame_SIZE];
int i, j, faults = 0, hits = 0;

for (i = 0; i < Frame_SIZE; i++) {


cache[i] = -1;
used[i] = 0;
}

// int pageReferences[] = {7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2};


int pageReferences[] = {6, 5, 4, 2, 3, 1};

for (i = 0; i < sizeof(pageReferences) / sizeof(pageReferences[0]); i++) {


int page = pageReferences[i];
int pageFound = 0;

// Check if the page is already in the cache


for (j = 0; j < Frame_SIZE; j++) {
if (cache[j] == page) {
pageFound = 1;
used[j] = i + 1; // Update usage information
hits += 1;
break;
}
}

// If the page is not in the frame, find the least recently used page
if (!pageFound) {
int lruIndex = 0;
int minUsage = used[0];

// Find the least recently used page


for (j = 1; j < Frame_SIZE; j++) {
if (used[j] < minUsage) {
minUsage = used[j];
lruIndex = j;
}
}

// Replace the least recently used page with the new page
cache[lruIndex] = page;
used[lruIndex] = i + 1; // Update usage information
faults += 1;
}

// Print the current cache contents


printf("Reference np %d: ", page);
for (j = 0; j < Frame_SIZE; j++) {
if (cache[j] == -1) {
printf("-1 ");
} else {
printf("%d ", cache[j]);
}
}
printf("\n");
}

printf("\nNumber of Page Faults = %d\n", faults);


printf("Number of Hits = %d\n", hits);

return 0;
}
OUTPUT:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy