Experiment 9 Os
Experiment 9 Os
===============================================================================================
===============================================================================================
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.
===============================================================================================
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.
===============================================================================================
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.
===============================================================================================
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;
frame_t frames[frames_count];
for (int i = 0; i < frames_count; i++) {
frames[i].page_number = -1; frames[i].status = PAGE_FAULT;
}
if (!page_found) {
page_fault_count++;
frames[oldest_frame_index].page_number = incoming_stream[i];
frames[oldest_frame_index].status = PAGE_FAULT;
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");
}
return 0;
}
Q: LRU page replacement using c
#include <stdio.h>
#include <limits.h>
typedef struct {
int page_number;
int status; } frame_t;
int main() {
int frame_count, 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;
if (!page_found) {
page_fault_count++;
frames[lru_page].page_number = incoming_stream[i];
update_lru(frames, frame_count, incoming_stream[i]);
}
return 0;
}
Q:Optimal page replacement using typedef struct
#include <stdio.h>
#include <limits.h>
return replace_index;
}
void optimalPageReplacement(int pages[], int page_count, int frame_count) {
PageFrame frames[frame_count];
int page_faults = 0;
if (!found) {
page_faults++;
frames[replace_index].page_number = current_page;
}
int main() {
int page_count, frame_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);
return 0;
}
===============================================================================================
===============================================================================================
7. Discussion
FIFO Page Replacement
FIFO is simple but may lead to high page faults in cases like Belady’s anomaly.
===============================================================================================
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.