0% found this document useful (0 votes)
6 views3 pages

LRUCODE

The document contains a C program that implements the Least Recently Used (LRU) page replacement algorithm. It initializes frames and tracks the time of page access, processes a reference string of pages, and replaces the least recently used page when a page fault occurs. The program outputs the current frame content and the total number of page faults after processing all pages.

Uploaded by

jekitoc589
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)
6 views3 pages

LRUCODE

The document contains a C program that implements the Least Recently Used (LRU) page replacement algorithm. It initializes frames and tracks the time of page access, processes a reference string of pages, and replaces the least recently used page when a page fault occurs. The program outputs the current frame content and the total number of page faults after processing all pages.

Uploaded by

jekitoc589
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/ 3

LRU

#include <stdio.h>

#include <limits.h>

#define MAX 100

void lru(int pages[], int n, int frames) {

int frame[frames], time[frames], page_faults = 0;

// Initialize frames and time trackers

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

frame[i] = -1;

time[i] = -1; // -1 indicates that the frame is empty

// Process each page in the reference string

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

int found = 0, replace_index = -1;

// Check if the page is already in any frame

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

if (frame[j] == pages[i]) {

found = 1;

time[j] = i; // Update the time the page was last accessed

break;

// If the page is not found, replace the least recently used page

if (!found) {
int min_time = INT_MAX;

// Find the least recently used page to replace

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

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

replace_index = j;

break;

if (time[j] < min_time) {

min_time = time[j];

replace_index = j;

// Replace the page and update time

frame[replace_index] = pages[i];

time[replace_index] = i; // Set the access time for the new page

page_faults++;

// Display frame content after each page reference

printf("Frame: ");

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

if (frame[j] != -1)

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

else

printf("- ");

printf("\n");

// Display the total number of page faults


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

int main() {

int frames = 3;

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

int n = sizeof(pages) / sizeof(pages[0]);

printf("Number of frames: %d\n", frames);

printf("Reference string: ");

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

printf("%d ", pages[i]);

printf("\n\n");

// Call LRU page replacement algorithm

lru(pages, n, frames);

return 0;

You might also like