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

Fall 2024 - CS604P - 2

The document provides a C program implementation of the LRU Page Replacement algorithm, which replaces the least recently used page in memory. It includes a sample input and output, detailing how to track page access and calculate page faults. The program also displays the student's ID at the end of the output.

Uploaded by

Tayyab Najeeb
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 views5 pages

Fall 2024 - CS604P - 2

The document provides a C program implementation of the LRU Page Replacement algorithm, which replaces the least recently used page in memory. It includes a sample input and output, detailing how to track page access and calculate page faults. The program also displays the student's ID at the end of the output.

Uploaded by

Tayyab Najeeb
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/ 5

CS604P Assignment Solution

Student Name: Tayyab Najeeb


Student ID: Bc230206185

Question Statement:
Write a C program to implement the LRU Page Replacement algorithm. If we consider the
recent past as a predictor of the near future, LRU 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 at the end of the program output.
Sample Input:
 Enter Total number of frames: 4
 Enter number of Pages: 8
 Enter Reference string
Output Sample:
 My Student ID: BC81902320
 Total Page Faults: 7

Analysis:
To implement the LRU Page Replacement algorithm, we need to keep track of the order in which
pages are accessed. When a page fault occurs, we replace the page that was accessed the longest
time ago.
C Program Implementation:
#include <stdio.h>

int main() {

int frames, pages, page_faults = 0, i, j, k, l, flag;

int reference_string[20], frames_array[10], temp[10], counter[10];


printf("Enter Total number of frames: ");

scanf("%d", &frames);

printf("Enter number of Pages: ");

scanf("%d", &pages);

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

for(i = 0; i < pages; i++) {

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

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

frames_array[i] = -1;

counter[i] = 0;

for(i = 0; i < pages; i++) {

flag = 0;

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

if(frames_array[j] == reference_string[i]) {

counter[j] = i;

flag = 1;

break;

}
if(flag == 0) {

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

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

page_faults++;

frames_array[j] = reference_string[i];

counter[j] = i;

flag = 1;

break;

if(flag == 0) {

page_faults++;

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

temp[j] = counter[j];

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

for(k = j + 1; k < frames; k++) {

if(temp[j] > temp[k]) {

int t = temp[j];

temp[j] = temp[k];

temp[k] = t;

t = frames_array[j];

frames_array[j] = frames_array[k];

frames_array[k] = t;
}

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

if(frames_array[j] != reference_string[i]) {

frames_array[j] = reference_string[i];

counter[j] = i;

break;

printf("\nMy Student ID: BC81902320\n");

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

return 0;

Explanation:
1. Initialization:
 Declare variables for frames, pages, page_faults, arrays for reference string,
frames, counters, and a temporary array.
 Initialize frames_array with -1 to indicate empty frames.
 Initialize counter array to 0.

2. Page Fault Check:


 For each page in the reference string:
 Check if the page is already present in the frames_array. If found, update the
counter for that frame.
 If not found:
 If there is an empty frame, add the page to the empty frame and update the
counter.
 If all frames are occupied, find the frame with the oldest access (least recent
counter value) and replace it with the current page. Update the counter for the
new page.

3. Calculate Page Faults:

 Increment the page_faults counter for each page fault.

4. Display Output:

 Print the student ID and the total number of page faults.

Output:

You might also like