0% found this document useful (0 votes)
12 views6 pages

OS Lab Experiment 9

oxexp2
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)
12 views6 pages

OS Lab Experiment 9

oxexp2
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/ 6

1.

Aim of the Experiment


• Purpose: The goal of this experiment is to understand and implement three different page
replacement algorithms—FIFO (First-In-First-Out), LRU (Least Recently Used), and Optimal
page replacement. These algorithms simulate the process of managing memory in a system
where the number of frames (pages) is limited, and pages are replaced based on certain
policies when a page fault occurs.
• Example: Develop programs to simulate memory management using FIFO, LRU, and Optimal
page replacement algorithms to handle page faults efficiently in a computer system.

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");

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("\nTotal Page Faults: %d\n", page_fault_count);
return 0;
}

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");

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]);
break;
}
}
if (!page_found) {
page_fault_count++;
int lru_page = find_lru_page(frames, frame_count);
frames[lru_page].page_number = incoming_stream[i];
frames[lru_page].status = frame_count - 1;
for (int j = 0; j < frame_count; j++) {
if (j != lru_page) {
frames[j].status--;
}
}
}
printf("%d\t\t", incoming_stream[i]);
for (int j = 0; j < frame_count; j++) {
if (frames[j].page_number != -1) {
printf("%d\t", frames[j].page_number);
} else {
printf("-\t");
}
}
printf("\n");
}
printf("\nTotal Page Faults: %d\n", page_fault_count);
return 0;
}

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;

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


if (page_table[j].page_number == current_page) {
found = 1;
break;
}
}
if (!found) {
page_fault_count++;
int furthest_future_reference = -1;
int furthest_future_index = -1;
for (int j = 0; j < frames; j++) {
if (page_table[j].page_number == -1) {
furthest_future_index = j;
break;
}
int future_reference_time = -1;
for (int k = i + 1; k < pages; k++) {
if (incoming_stream[k] == page_table[j].page_number) {
future_reference_time = k;
break;
}
}
if (future_reference_time == -1) {
furthest_future_index = j;
break;
}
if (future_reference_time > furthest_future_reference) {
furthest_future_reference = future_reference_time;
furthest_future_index = j;
}
}
page_table[furthest_future_index].page_number = current_page;
}
printf("Incoming Page: %d\n", current_page);
printf("Page Table: ");
for (int j = 0; j < frames; j++) {
if (page_table[j].page_number != -1) {
printf("%d ", page_table[j].page_number);
} else {
printf("- ");
}
}
printf("\n");
}
printf("\nTotal Page Faults: %d\n", page_fault_count);
return 0;
}

6. Input / Output Details


• Purpose: To specify the format of inputs and outputs.
• Example: Input: Number of frames, number of pages, incoming page stream.
Output: Total number of page faults.
• Example Input:Number of frames: 3
Number of pages: 6
Incoming page stream: 1 2 3 4 2 5
• Example Output: Total Page Faults: 5

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.

You might also like