OS Lab Experiment 9
OS Lab Experiment 9
2. Objective
• Purpose: To gain a deeper understanding of memory management in operating systems and
how different page replacement algorithms can affect system performance, particularly in
terms of page faults.
• Example: The objective is to simulate the page replacement process using three algorithms:
FIFO, LRU, and Optimal, and compare their behavior by tracking the number of page faults in
each algorithm.
3. Problem Statement
• Purpose: Implementing and comparing three page replacement algorithms—FIFO, LRU, and
Optimal—on a fixed number of memory frames. The system should process a stream of page
references, replacing pages in memory when necessary and counting the total number of
page faults.
• Example: Design a program where the system simulates a set number of frames in memory.
When a page is referenced, the program checks whether it is already in memory (page hit). If
not, a page fault occurs, and one of the algorithms (FIFO, LRU, or Optimal) decides which
page to replace.
4. Algorithm / Pseudocode
• FIFO Algorithm:
1. Maintain a queue of frames in memory.
2. When a page is referenced, check if it is already in memory (page hit).
3. If not, replace the oldest page (FIFO logic) and update the page fault count.
• LRU Algorithm:
1. Track the recency of pages using a status value.
2. When a page is referenced, update its recency status.
3. If the page is not in memory, replace the least recently used (LRU) page.
• Optimal Algorithm:
1. For each page fault, find the page that will not be used for the longest time in the
future and replace it.
2. Track the reference times of pages and choose the page that will not be used for the
longest time.
5. Code Implementation
FIFO Algorithm
#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;
printf("\nIncoming Page\tFrames\n");
LRU Algorithm
#include <stdio.h>
#define PAGE_FAULT 0
#define PAGE_HIT 1
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;
break;
}
}
}
int find_lru_page(frame_t *frames, int frame_count) {
int lru_page = 0;
for (int i = 1; i < frame_count; i++) {
if (frames[i].status < frames[lru_page].status) {
lru_page = i;
}
}
return lru_page;
}
int main() {
int frame_count, page_count, page_fault_count = 0;
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;
frames[i].status = -1;
}
printf("\nIncoming Page\tFrames\n");
Optimal Algorithm
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int page_number;
int reference_time;
} page_t;
int compare(const void *a, const void *b) {
page_t *page1 = (page_t *)a;
page_t *page2 = (page_t *)b;
return page1->reference_time - page2->reference_time;
}
int main() {
int frames, pages, page_fault_count = 0;
printf("Enter the number of frames: ");
scanf("%d", &frames);
printf("Enter the number of pages: ");
scanf("%d", &pages);
int incoming_stream[pages];
printf("Enter the incoming page stream: ");
for (int i = 0; i < pages; i++) {
scanf("%d", &incoming_stream[i]);
}
page_t page_table[frames];
for (int i = 0; i < frames; i++) {
page_table[i].page_number = -1;
page_table[i].reference_time = -1;
}
for (int i = 0; i < pages; i++) {
int current_page = incoming_stream[i];
int found = 0;
7. Discussion
• Purpose: The FIFO, LRU, and Optimal page replacement algorithms are key concepts in
operating system memory management. FIFO follows the principle of replacing the oldest
page, LRU replaces the least recently used page, and Optimal replaces the page that will be
used farthest in the future.
• Example: In a real-world scenario, the FIFO algorithm might be likened to the "first come,
first served" system in a queue, while LRU is like remembering which item you last used, and
Optimal would involve planning the best strategy based on future needs.
8. Conclusion
• Purpose: By implementing the FIFO, LRU, and Optimal algorithms, we gained insights into
how operating systems handle memory management and deal with page faults. Each
algorithm has its strengths and weaknesses depending on the system’s workload.
• Example: FIFO is easy to implement but may result in more page faults than LRU or Optimal.
LRU is more efficient for most cases, while the Optimal algorithm offers the best theoretical
performance, though it is difficult to implement in practice.