Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
445 views

C Program For LRU Replacement Algorithm Implementation

This C program implements the Least Recently Used (LRU) page replacement algorithm. It takes the number of frames and pages as input, along with a reference string of pages. It uses an array to track the frames and the last access time of each page. It finds the LRU page using a function that returns the position of the minimum time value. It simulates walking through the reference string and handles page hits and faults accordingly, updating the time array and frames. Finally it prints the total page faults.

Uploaded by

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

C Program For LRU Replacement Algorithm Implementation

This C program implements the Least Recently Used (LRU) page replacement algorithm. It takes the number of frames and pages as input, along with a reference string of pages. It uses an array to track the frames and the last access time of each page. It finds the LRU page using a function that returns the position of the minimum time value. It simulates walking through the reference string and handles page hits and faults accordingly, updating the time array and frames. Finally it prints the total page faults.

Uploaded by

Prathamesh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

//C program for LRU replacement algorithm implementation

#include <stdio.h>

//user-defined function

int findLRU(int time[], int n)

int i, minimum = time[0], pos = 0;

for (i = 1; i < n; ++i)

if (time[i] < minimum)

minimum = time[i];

pos = i;

return pos;

//main function

int main()

int no_of_frames, no_of_pages, frames[10], pages[30], counter = 0, time[10], flag1, flag2, i, j, pos,
faults = 0;

printf("Enter number of frames: ");

scanf("%d", &no_of_frames);

printf("Enter number of pages: ");

scanf("%d", &no_of_pages);
printf("Enter reference string: ");

for (i = 0; i < no_of_pages; ++i)

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

for (i = 0; i < no_of_frames; ++i)

frames[i] = -1;

for (i = 0; i < no_of_pages; ++i)

flag1 = flag2 = 0;

for (j = 0; j < no_of_frames; ++j)

if (frames[j] == pages[i])

counter++;

time[j] = counter;

flag1 = flag2 = 1;

break;

if (flag1 == 0)

for (j = 0; j < no_of_frames; ++j)


{

if (frames[j] == -1)

counter++;

faults++;

frames[j] = pages[i];

time[j] = counter;

flag2 = 1;

break;

if (flag2 == 0)

pos = findLRU(time, no_of_frames);

counter++;

faults++;

frames[pos] = pages[i];

time[pos] = counter;

printf("\n");

for (j = 0; j < no_of_frames; ++j)

printf("%d\t", frames[j]);

printf("\nTotal Page Faults = %d", faults);


return 0;

You might also like