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

CS604P SOLUTION Assignment No 2

Uploaded by

noorsarfraz83
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)
10 views3 pages

CS604P SOLUTION Assignment No 2

Uploaded by

noorsarfraz83
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
Question no 1;
Solution;
The c program to implement LRU Page Replacement algorithm is,
#include <stdbool.h>
#include <stdio.h>
void displayPages(int frames[], int n) {
for (int i = 0; i < n; i++) {
if (frames[i] == -1)
printf("-1\t");
else
printf("%d\t", frames[i]);
}
printf("\n");
}
int findLRU(int time[], int n) {
int minimum = time[0], pos = 0;
for (int i = 1; i < n; i++) {
if (time[i] < minimum) {
minimum = time[i];
pos = i;
}
}
return pos;
}
int main() {
int frames[10], pages[30], time[10];
int n, m, page_faults = 0, flag, counter = 0;
printf("Enter Total number of frames: ");
scanf("%d", &n);
printf("Enter number of Pages: ");
scanf("%d", &m);
printf("Enter Reference string:\n");
for (int i = 0; i < m; i++) {
scanf("%d", &pages[i]);
}
printf("\n");
// Initialize frames array with -1
for (int i = 0; i < n; i++) {
frames[i] = -1;
}
// Process each page request
for (int i = 0; i < m; i++) {
flag = 0;
// Check if page already exists in frames
for (int j = 0; j < n; j++) {
if (frames[j] == pages[i]) {
counter++;
time[j] = counter;
flag = 1;
break;
}
}
// If page not found and empty frame exists
if (flag == 0) {
int j;
for (j = 0; j < n; j++) {
if (frames[j] == -1) {
counter++;
page_faults++;
frames[j] = pages[i];
time[j] = counter;
break;
}
}
// If no empty frame found, find LRU page
if (j == n) {
int pos = findLRU(time, n);
counter++;
page_faults++;
frames[pos] = pages[i];
time[pos] = counter;
}
}
// Display current state of frames
displayPages(frames, n);
}
printf("\nMy Student ID = BC230204456\n");
printf("\nTotal Page Faults = %d\n", page_faults);
return 0;
}
Screen shot of output;

You might also like