Osy Part B
Osy Part B
Osy Part B
A micro - project
report
2 Internet Websites:
1. www.tutorialspoint.co -
m
4 Browser Chrome 1
5 Editor
In operating systems, memory management is an important aspect that determines the efficiency and
performance of a system. As modern applications demand more memory than ever before, it’s essential
to have effective strategies for managing memory, and one such strategy is page replacement
algorithms.
1. FIFO (first-in-first-out).
First, it checks if the requested page is already present in the main memory. If so, it detects a
page hit and proceeds to the next page request.
If the page is not found in the main memory, it triggers a page miss and checks if the main
memory is full.
When the main memory is not full, it loads the requested page into an empty slot and moves on
to the next page request.
If the main memory is full, it first makes room for the new page. To do so, it finds the page that
will not be accessed for the longest period in the future. After that, it replaces the selected page
with the newly requested page. This ensures that the main memory remains occupied by the
most relevant and frequently accessed pages.
Initially, the memory is empty, and the OPT algorithm inserts page in the memory. It is a page
miss:
Advantage: Provides the best theoretical performance by replacing the page
that will be used farthest in the future.
Disadvantage: Often not implementable in practice since it requires
knowledge of future memory access patterns.
First In First Out (FIFO): This is the simplest page replacement algorithm. In this
algorithm, the operating system keeps track of all pages in the memory in a queue, the
oldest page is in the front of the queue. When a page needs to be replaced page in the
front of the queue is selected for removal.
#include <stdio.h>
// Function to find the page that will be used farthest in the future
int findOptimalPage(int pages[], int frame[], int n, int current) {
int res = -1, farthest = current;
for (int i = 0; i < n; i++) {
int j;
for (j = current; j < n; j++) {
if (frame[i] == pages[j]) {
if (j > farthest) {
farthest = j;
res = i;
}
break;
}
}
if (j == n)
return i;
}
return (res == -1) ? 0 : res;
}
// Function to simulate the optimal page replacement algorithm
void optimalPageReplacement(int pages[], int n, int capacity) {
int frame[capacity];
int pageFaults = 0;
for (int i = 0; i < capacity; i++) {
frame[i] = -1;
}
for (int i = 0; i < n; i++) {
int page = pages[i];
int j;
for (j = 0; j < capacity; j++) {
if (frame[j] == page) {
break;
}
}
if (j == capacity) {
int replace = findOptimalPage(pages, frame, n,
i + 1);
frame[replace] = page;
pageFaults++;
}
}
printf("Page Faults: %d\n", pageFaults);
}
int main() {
int pages[] = {7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2};
int n = sizeof(pages) / sizeof(pages[0]);
int capacity = 3;
optimalPageReplacement(pages, n, capacity);
return 0;
}
2.1 Skills developed:
1. Communication Skills.
2. Group Discussion.
3. Basic knowledge of java.
.
.
2.2 Conclusion:
In this article, we discussed the OPT algorithm that minimizes page faults in theory and is a
benchmark for practical algorithms.
We then use an illustrative example to explain how the OPT algorithm works. In the end, we discuss
the potential advantages and disadvantages of the OPT algorithm.
3.1 References:
Websites:
1. http://www.geeksforgeeks.com
2. https://www.baeldung.com/cs/optimal-page-replacement-algorithm