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

Fall 2024 - CS604P - 2

Uploaded by

mirzamubashar535
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)
14 views3 pages

Fall 2024 - CS604P - 2

Uploaded by

mirzamubashar535
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

Operating Systems (Practical)

(CS604P)
Assignment # 02
Fall 2024

Question 1: 20 Marks
Write a c program to implement LRU Page Replacement algorithm. If we consider the recent
past as a predictor of the near future, we will replace the page that has been unused for the
longest time. Pages that are rarely used are likely to remain infrequently accessed in the
future.
Also write and display your Student ID in the program at the end of the program output same
as shown in the figure.

Solution:

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

#define MAX_FRAMES 100

int getLRUIndex(int time[], int n) {


int min = time[0], index = 0;
for (int i = 1; i < n; i++) {
if (time[i] < min) {
min = time[i];
index = i;
}
}
return index;
}

void LRUPageReplacement(int frames[], int reference[], int numFrames, int numPages) {


int time[numFrames];
int pageFaults = 0;

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


frames[i] = -1;
time[i] = -1;
}

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


int page = reference[i];
int found = 0;

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


if (frames[j] == page) {
time[j] = i;
found = 1;
break;
}
}

if (!found) {
int lruIndex = getLRUIndex(time, numFrames);
frames[lruIndex] = page;
time[lruIndex] = i;
pageFaults++;

printf("Page Accessed: %d\tFrames: ", page);


for (int j = 0; j < numFrames; j++) {
if (frames[j] != -1)
printf("%d ", frames[j]);
}
printf("\n");
}

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


}

int main() {
int numFrames, numPages;

// Input for total number of frames


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

printf("Enter number of Pages: ");


scanf("%d", &numPages);

int reference[numPages];

printf("Enter Reference string:\n");


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

int frames[numFrames];

LRUPageReplacement(frames, reference, numFrames, numPages);

printf("\nStudent ID: BC123456789\n");


return 0;
}

Output:

You might also like