0% found this document useful (0 votes)
15 views8 pages

Experiment 9 Os

osxp3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views8 pages

Experiment 9 Os

osxp3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Experiment: FIFO Page Replacement, LRU Page Replacement, and Optimal Page Replacement

===============================================================================================

1. Aim of the Experiment


FIFO Page Replacement
Purpose:
To implement the FIFO (First-In-First-Out) page replacement algorithm and analyze its performance in terms of page
faults for a given reference string.
Example:
Simulate the FIFO page replacement algorithm and calculate the total number of page faults for a given reference
string and page frame size.

LRU Page Replacement


Purpose:
To learn the implementation of the LRU (Least Recently Used) page replacement algorithm and understand how it
improves page replacement efficiency by considering the recent usage of pages.
Example:
Simulate the LRU page replacement algorithm and calculate page faults for a given reference string and page frame
size.

Optimal Page Replacement


Purpose:
To simulate the Optimal page replacement algorithm, which minimizes page faults by predicting future page
references and replacing the page that will not be used for the longest time.
Example:
Simulate the Optimal page replacement algorithm and determine the total page faults for a given reference string
and page frame size.

===============================================================================================

2. Objective
FIFO Page Replacement
Purpose:
The objective is to understand the FIFO page replacement algorithm and evaluate its effectiveness in managing page
faults.
- Pages are replaced based on their arrival order in the page frame.
- Easy to implement but may not always yield optimal performance.
Example:
Implement FIFO and compute the page fault rate for various frame sizes.

LRU Page Replacement


Purpose:
To explore the LRU page replacement algorithm and its ability to minimize page faults by replacing the least recently
used page.
- Provides better performance compared to FIFO.
- Tracks page usage history.
Example:
Implement LRU to determine average page fault rates for specific page reference strings.

Optimal Page Replacement


Purpose:
The objective is to understand and implement the Optimal page replacement algorithm to achieve minimal page
faults theoretically.
- Requires knowledge of future page references.
- Serves as a benchmark for other algorithms.
Example:
Implement Optimal page replacement and compare its results with FIFO and LRU.

===============================================================================================

3. Problem Statement
FIFO Page Replacement
Purpose:
Simulate the FIFO page replacement algorithm for a given reference string and page frame size to calculate the total
page faults.
Example:
Design a program to input a page reference string and frame size, outputting the page faults using FIFO logic.

LRU Page Replacement


Purpose:
Simulate the LRU algorithm to determine page faults for a given reference string and frame size.
Example:
Write a program that tracks and replaces pages based on the least recent usage to calculate page faults.

Optimal Page Replacement


Purpose:
Implement the Optimal page replacement algorithm to minimize page faults and compare it with other algorithms.
Example:
Design a program to predict future page references and calculate the number of page faults using the Optimal
method.

===============================================================================================

4. Algorithm / Pseudocode
FIFO Page Replacement
Algorithm:
1. Input the page reference string and frame size.
2. Initialize an empty queue for the page frames.
3. For each page in the reference string:
- If the page is in the queue, do nothing.
- If the page is not in the queue and space is available, add it.
- If the page is not in the queue and space is full, remove the oldest page and add the new page.
4. Count page faults whenever a new page is added.
5. Output the total page faults.

LRU Page Replacement


Algorithm:
1. Input the page reference string and frame size.
2. Maintain a list for page frames and track the last usage time for each page.
3. For each page in the reference string:
- If the page is in the list, update its usage time.
- If the page is not in the list and space is available, add it.
- If the page is not in the list and space is full, replace the least recently used page.
4. Count page faults whenever a new page is added.
5. Output the total page faults.

Optimal Page Replacement


Algorithm:
1. Input the page reference string and frame size.
2. Maintain a list for page frames.
3. For each page in the reference string:
- If the page is in the list, do nothing.
- If the page is not in the list and space is available, add it.
- If the page is not in the list and space is full:
- Predict future usage for all pages in the list.
- Replace the page that will not be used for the longest time.
4. Count page faults whenever a new page is added.
5. Output the total page faults.

===============================================================================================

5. Code Implementation
Aim of the Experiment: FIFO page replacement
#include <stdio.h>
#define PAGE_FAULT 0
#define PAGE_HIT 1

typedef struct {
int page_number;
int status;
} frame_t;

int main() {
int frames_count, pages_count, page_fault_count = 0;

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


scanf("%d", &frames_count);

printf("Enter the number of pages: ");


scanf("%d", &pages_count);
int incoming_stream[pages_count];
printf("Enter the incoming page stream: ");
for (int i = 0; i < pages_count; i++) {
scanf("%d", &incoming_stream[i]);
}

frame_t frames[frames_count];
for (int i = 0; i < frames_count; i++) {
frames[i].page_number = -1; frames[i].status = PAGE_FAULT;
}

int oldest_frame_index = 0; // To track the oldest frame

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


int page_found = 0;

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


if (frames[j].page_number == incoming_stream[i]) {
page_found = 1;
frames[j].status = PAGE_HIT;
break;
}
}

if (!page_found) {
page_fault_count++;

frames[oldest_frame_index].page_number = incoming_stream[i];
frames[oldest_frame_index].status = PAGE_FAULT;

oldest_frame_index = (oldest_frame_index + 1) % frames_count;


}

printf("%d\t\t", incoming_stream[i]);
for (int j = 0; j < frames_count; j++) {
if (frames[j].page_number != -1) {
printf("%d\t", frames[j].page_number);
} else {
printf("-\t");
}
}
printf("\n");
}

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

return 0;
}
Q: LRU page replacement using c
#include <stdio.h>
#include <limits.h>

typedef struct {
int page_number;
int status; } frame_t;

void update_lru(frame_t *frames, int frame_count, int page_number) {


for (int i = 0; i < frame_count; i++) {
if (frames[i].page_number == page_number) {
for (int j = 0; j < frame_count; j++) {
if (frames[j].status > frames[i].status) {
frames[j].status--;
}
}
frames[i].status = frame_count - 1; // Set the current frame as most recently used
break;
}
}
}
int find_lru_page(frame_t *frames, int frame_count) {
int lru_page = 0;
int min_status = INT_MAX;
for (int i = 0; i < frame_count; i++) {
if (frames[i].status < min_status) {
min_status = frames[i].status;
lru_page = i;
}
}
return lru_page;
}

int main() {
int frame_count, page_count;

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


scanf("%d", &frame_count);

printf("Enter the number of pages: ");


scanf("%d", &page_count);

int incoming_stream[page_count];
printf("Enter the incoming page stream: ");
for (int i = 0; i < page_count; i++) {
scanf("%d", &incoming_stream[i]);
}

frame_t frames[frame_count];
for (int i = 0; i < frame_count; i++) {
frames[i].page_number = -1; // Empty frame
frames[i].status = -1; // Invalid status
}

int page_fault_count = 0;

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


int page_found = 0;

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


if (frames[j].page_number == incoming_stream[i]) {
page_found = 1;
update_lru(frames, frame_count, incoming_stream[i]); // Update LRU status
break;
}
}

if (!page_found) {
page_fault_count++;

int lru_page = find_lru_page(frames, frame_count);

frames[lru_page].page_number = incoming_stream[i];
update_lru(frames, frame_count, incoming_stream[i]);
}

printf("Incoming page: %d | Frames: ", incoming_stream[i]);


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

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

return 0;
}
Q:Optimal page replacement using typedef struct
#include <stdio.h>
#include <limits.h>

// Define a structure to represent a page frame


typedef struct {
int page_number; int next_use;
} PageFrame;
int findPageToReplace(PageFrame frames[], int frame_count) {
int max_next_use = -1;
int replace_index = -1;

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


if (frames[i].next_use == INT_MAX) {
return i;
}
if (frames[i].next_use > max_next_use) {
max_next_use = frames[i].next_use;
replace_index = i;
}
}

return replace_index;
}
void optimalPageReplacement(int pages[], int page_count, int frame_count) {
PageFrame frames[frame_count];
int page_faults = 0;

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


frames[i].page_number = -1; frames[i].next_use = INT_MAX;
}

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


int current_page = pages[i];
int found = 0;
for (int j = 0; j < frame_count; j++) {
if (frames[j].page_number == current_page) {
found = 1;
break;
}
}

if (!found) {
page_faults++;

int replace_index = findPageToReplace(frames, frame_count);

frames[replace_index].page_number = current_page;
}

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


if (frames[j].page_number != -1) {
frames[j].next_use = INT_MAX; for (int k = i + 1; k < page_count; k++) {
if (frames[j].page_number == pages[k]) {
frames[j].next_use = k;
break;
}
}
}
}

printf("Step %d: ", i + 1);


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

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


}

int main() {
int page_count, frame_count;

printf("Enter the number of pages: ");


scanf("%d", &page_count);

int pages[page_count];
printf("Enter the page reference stream:\n");
for (int i = 0; i < page_count; i++) {
scanf("%d", &pages[i]);
}
printf("Enter the number of frames: ");
scanf("%d", &frame_count);

optimalPageReplacement(pages, page_count, frame_count);

return 0;
}
===============================================================================================

6. Input / Output Details


FIFO Page Replacement
Input Example:
Reference String: 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2
Frame Size: 3
Output Example:
Page Faults: 9

LRU Page Replacement


Input Example:
Reference String: 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2
Frame Size: 3
Output Example:
Page Faults: 8

Optimal Page Replacement


Input Example:
Reference String: 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2
Frame Size: 3
Output Example:
Page Faults: 6

===============================================================================================

7. Discussion
FIFO Page Replacement
FIFO is simple but may lead to high page faults in cases like Belady’s anomaly.

LRU Page Replacement


LRU performs better than FIFO as it considers recent usage, but it has overhead for tracking page usage.

Optimal Page Replacement


Optimal minimizes page faults but is not feasible in real systems as it requires future knowledge.

===============================================================================================

8. Conclusion
FIFO: Simple to implement but may not be efficient for all cases.
LRU: Balances simplicity and efficiency, often used in practical scenarios.
Optimal: Serves as a theoretical benchmark for evaluating other algorithms.

You might also like