OS Lab
OS Lab
Tittle: Write a program to display the number of page fault for user input page reference
string and frame size using FIFO.
FIFO:
A simple page replacement algorithm where the page that has been in memory the longest is
replaced when a page fault occurs.
It operates on a first-in-first-out (FIFO) basis, similar to a queue.
Steps:
1. Maintain a queue of page numbers currently in memory.
2. When a page fault occurs:
Replace the page at the front of the queue (oldest page).
Bring the required page into the newly freed frame.
Add the new page to the rear of the queue.
Page Fault:
An event that occurs when a process accesses a page that is not currently loaded into physical memory. The
operating system must bring the required page from secondary storage (like disk) into a free frame in
physical memory.
Algorithm:
Step 1: Input the processes along with their burst time (bt).
Step 3: As first process that comes need not to wait so waiting time for process 1 will be 0 i.e., wt[0] = 0.
Step 4: Find waiting time for all other processes i.e., for process i -> wt[i] = bt[i-1] + wt[i-1].
#include <stdio.h>
int main() {
int n, frames, page_faults = 0, page_hits = 0;
Compiler: VS CODE
Language: C
OUTPUT:
Lab no: 2 Date: 2024/08/14
Tittle: Write a program to display the number of page fault for user input page reference
string and frame size using LRU.
Algorithm:
Source Code:
#include <stdio.h>
int findLRU(int time[], int n) {
int i, minimum = time[0], pos = 0;
for (i = 1; i < n; i++) {
if (time[i] < minimum) {
minimum = time[i];
pos = i;
}
}
return pos;
}
int main() {
printf("\t\t****************************************\n");
printf("\t\t\t LRU Page Replacement\n");
printf("\t\t*****************************************\n");
int n, frames, page_faults = 0, page_hits = 0, counter = 0;
printf("Enter the number of reference strings: ");
scanf("%d", &n);
int reference_string[n];
printf("Enter the reference string:\n");
for (int i = 0; i < n; i++) {
scanf("%d", &reference_string[i]);
}
printf("Enter the number of page frames: ");
scanf("%d", &frames);
int page_frames[frames], time[frames];
for (int i = 0; i < frames; i++) {
page_frames[i] = -1;
}
printf("\nRef String | Page Frames | Page Fault | Page Hit\n");
printf("-----------|------------------------|------------|---------\n");
for (int i = 0; i < n; i++) {
int flag1 = 0, flag2 = 0;
for (int j = 0; j < frames; j++) {
if (page_frames[j] == reference_string[i]) {
counter++;
time[j] = counter;
flag1 = flag2 = 1;
page_hits++;
break;
}
}
if (flag1 == 0) {
for (int j = 0; j < frames; j++) {
if (page_frames[j] == -1) {
counter++;
page_faults++;
page_frames[j] = reference_string[i];
time[j] = counter;
flag2 = 1;
break;
}
}
}
if (flag2 == 0) {
int pos = findLRU(time, frames);
counter++;
page_faults++;
page_frames[pos] = reference_string[i];
time[pos] = counter;
}
printf("%10d |", reference_string[i]);
for (int j = 0; j < frames; j++) {
if (page_frames[j] != -1) {
printf("%4d", page_frames[j]);
} else {
printf(" ");
}
}
printf(" |");
if (flag1 == 0 && flag2 == 0) {
printf(" Yes | No\n");
} else if (flag1 == 1) {
printf(" No | Yes\n");
} else {
printf(" Yes | No\n");
}
}
printf("\nTotal number of page faults: %d\n", page_faults);
printf("Total number of page hits: %d\n", page_hits);
return 0;
}
Compiler: VS Code
Language: C
Output:
Lab no: 3 Date: 2024/08/14
Tittle: Write a program to display the number of page fault for user input page reference
string and frame size using OPR.
Algorithm:
Step 2: Let frame size be n. Add first n elements from reference string
to frame directly. Print frame and increment page fault.
Step 3: For the elements left do the following:
a. There may be two cases. Either the element in the reference string may be in the frame
already or not present in the frame
b. If the element is not in the frame do the following
(a) For all the elements in the frame find respective length from current index in the
reference string.
(b) Find the maximum of the length.
(c) Replace that index of frame with the element of reference string and increment
page fault. Else do nothing.
Step 4: Print page faults.
Source Code:
#include <stdio.h>
int findOptimal(int reference_string[], int page_frames[], int n, int current_index, int frames) {
int farthest = current_index;
int pos = -1;
for (int i = 0; i < frames; i++) {
int j;
for (j = current_index; j < n; j++) {
if (page_frames[i] == reference_string[j]) {
if (j > farthest) {
farthest = j;
pos = i;
}
break;
}
}
if (j == n) {
return i;
}
}
if (pos == -1) {
return 0;
}
return pos;
}
int main() {
printf("\t\t****************************************\n");
printf("\t\t\t Optimal Page Replacement\n");
printf("\t\t*****************************************\n");
int n, frames, page_faults = 0, page_hits = 0;
printf("Enter the number of reference strings: ");
scanf("%d", &n);
int reference_string[n];
printf("Enter the reference string:\n");
for (int i = 0; i < n; i++) {
scanf("%d", &reference_string[i]);
}
printf("Enter the number of page frames: ");
scanf("%d", &frames);
int page_frames[frames];
for (int i = 0; i < frames; i++) {
page_frames[i] = -1;
}
printf("\nRef String | Page Frames | Page Fault | Page Hit\n");
printf("-----------|------------------------|------------|---------\n");
Compiler: VS Code
Language: C
Output:
Lab no: 4 Date: 2024/08/14
Tittle: Write a program to display the number of page fault for user input page reference
string and frame size using Belady’s Anamoly.
Belady's Anomaly:
Belady's Anomaly is a counterintuitive phenomenon in page replacement algorithms where
increasing the number of page frames can lead to an increase in the number of page faults. This
behavior is typically observed in the FIFO (First In, First Out) page replacement algorithm.
Algorithm:
Step 1: Read the number of pages, the reference string, and the frame size.
Step 2:
Add the first n elements from the reference string directly to the frames.
Step 3: For each remaining element in the reference string, do the following:
i. Identify the element in the frame that has been there the longest (FIFO approach).
ii. Replace this element with the new element from the reference string.
Step 4: After processing all elements in the reference string, print the total number of page faults.
Source Code:
#include <stdio.h>
int main() {
printf("\t\t*************************************\n");
printf("\t\t\t Belady's Anamoly\n");
printf("\t\t**************************************\n");
int n;
printf("Enter the number of reference strings: ");
scanf("%d", &n);
int reference_string[n];
printf("Enter the reference string:\n");
for (int i = 0; i < n; i++) {
scanf("%d", &reference_string[i]);
}
int frame_sizes[] = {2, 3, 4};
int num_frame_sizes = sizeof(frame_sizes) / sizeof(frame_sizes[0]);
printf("\nFrame Size | Page Faults | Page Hits\n");
printf("-----------|-------------|-----------\n");
for (int i = 0; i < num_frame_sizes; i++) {
int page_hits;
int page_faults = simulateFIFO(reference_string, n, frame_sizes[i], &page_hits);
printf("%10d | %11d | %9d\n", frame_sizes[i], page_faults, page_hits);
}
return 0;
}
Compiler: VS Code
Language: C
Output: