0% found this document useful (0 votes)
3 views1 page

4) Lru Page

This C program implements a Least Recently Used (LRU) page replacement algorithm. It takes the number of pages, a reference string, and the number of frames as input, then simulates page faults while tracking memory state. The program outputs the memory state after each page reference and the total number of page faults at the end.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views1 page

4) Lru Page

This C program implements a Least Recently Used (LRU) page replacement algorithm. It takes the number of pages, a reference string, and the number of frames as input, then simulates page faults while tracking memory state. The program outputs the memory state after each page reference and the total number of page faults at the end.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

#include <stdio.

h>
#define MAX 100

int main() {
int pages[MAX], mem[10], recent[10], n, f, faults = 0;

printf("Pages: ");
scanf("%d", &n);

printf("Ref String: ");


for (int i = 0; i < n; i++)
scanf("%d", &pages[i]);

printf("Frames: ");
scanf("%d", &f);

for (int i = 0; i < f; i++)


mem[i] = recent[i] = -1;

printf("\nPage\tMemory\t\tFault\n");

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


int p = pages[i], found = 0;

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


if (mem[j] == p) {
found = 1;
recent[j] = i;
break;
}
}

if (!found) {
int lru = 0;
for (int j = 1; j < f; j++)
if (recent[j] < recent[lru]) lru = j;

mem[lru] = p;
recent[lru] = i;
faults++;
}

printf("%d\t", p);
for (int j = 0; j < f; j++)
mem[j] == -1 ? printf("- ") : printf("%d ", mem[j]);
printf("\t%s\n", found ? "No" : "Yes");
}

printf("\nTotal Faults: %d\n", faults);


return 0;
}

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