OSEXP9
OSEXP9
9
Memory Management: Virtual Memory
A. Write a program in C demonstrate the concept of page
replacement policies for handling page faults eg: FIFO, LRU,
Optimal
Experiment No. 9
Aim: Memory Management: Virtual Memory
Objective:
Theory:
Demand Paging
1
A demand paging system is quite similar to a paging system with swapping where
processes reside in secondary memory and pages are loaded only on demand, not in
advance. When a context switch occurs, the operating system does not copy any of
the old program’s pages out to the disk or any of the new program’s pages into the
main memory Instead, it just begins executing the new program after loading the
first page and fetches that program’s pages as they are referenced.
Page Replacement Algorithm
Page replacement algorithms are the techniques using which an Operating System
decides which memory pages to swap out, write to disk when a page of memory
needs to be allocated.
Reference String
The string of memory references is called reference string. Reference strings are
generated artificially or by tracing a given system and recording the address of each
memory reference.
When there is a need for page replacement, the FIFO algorithm, swaps out the page
at the front of the queue, that is the page which has been in the memory for the
longest time.
In LRU, whenever page replacement happens, the page which has not been used for
the longest amount of time is replaced.
2
Optimal Page Replacement algorithm is the best page replacement algorithm as it
gives the least number of page faults. It is also known as OPT, clairvoyant
replacement algorithm, or Belady’s optimal page replacement policy.
In this algorithm, pages are replaced which would not be used for the longest
duration of time in the future, i.e., the pages in the memory which are going to be
referred farthest in the
future are replaced.
This algorithm was introduced long back and is difficult to implement because it
requires
future knowledge of the program behavior. However, it is possible to implement
optimal page replacement on the second run by using the page reference information
collected on the first run.
Program:
⦁ FIFO:-
#include
<stdio.h>
#include
<stdlib.h>
#include
<stdbool.h
>
int
3
pageFaul
ts = 0; int
pageHits
= 0; int
pageMiss
es = 0; int
index =
0;
int page =
referenceString[i];
bool pageFound =
false;
if (frames[j] ==
page)
{ pageFound =
true; break;
}
4
if
(pag
eFou
nd)
{ pag
eHits
++;
} else {
frames[index] = page;
pageFaults++;
pageMisses++;
// Display current
frames
printf("Frames: ");
for (int j = 0; j
(frames[j] != -1) {
printf("%d ", frames[j]);
printf("\n");
5
printf("Total Page Hits: %d\n", pageHits);
\n", pageMisses);
}
int main() {
int n = sizeof(referenceString) /
FIFO(referenceString, n,
frameCount); return 0;
}
//Output
FIFO Page
Replacement:
Frames: 5
Frames: 5 0
Frames: 5 0 2
Frames: 3 0 2
Frames: 3 0 2
Frames: 3 1 2
Frames: 3 1 2
Frames: 3 1 4
6
Frames: 5 1 4
Frames: 5 1 4
Frames: 5 2 4
Frames: 5 2 0
Frames: 3 2 0
Frames: 3 4 0
Frames:
340
Total
Page
Hits: 4
Total Page
Faults: 11
Total Page
Misses: 11
⦁ LRU:-
#include
<stdio.h>
#include
<stdlib.h>
#include
<stdbool.h
>
int
pageFaul
ts = 0; int
pageHits
= 0; int
pageMiss
es = 0;
int page =
referenceString[i];
bool pageFound =
false;
8
if (frames[j] ==
page)
{ pageFound =
true;
pageHits++;
if (!pageFound) {
if (recentUse[j]
j;
}
frames[lruIndex] = page;
9
// Display current
frames
printf("Frames: ");
for (int j = 0; j
(frames[j] != -1) {
printf("%d ", frames[j]);
printf("\n");
\n", pageMisses);
}
int main() {
int n = sizeof(referenceString) /
LRU(referenceString, n,
frameCount); return 0;
}
10
//Output
LRU Page
Replacement:
Frames: 5
Frames: 5 0
Frames: 5 0 2
Frames: 3 0 2
Frames: 3 0 2
Frames: 3 0 1
Frames: 3 0 1
Frames: 3 4 1
Frames: 3 4 5
Frames: 3 4 5
Frames: 2 4 5
Frames: 2 4 0
Frames: 2 3 0
Frames: 4 3 0
Frames:
430
Total
Page
Hits: 4
Total Page Faults: 11
11
⦁ OPTIMAL:-
#include
<stdio.h>
#include
<stdlib.h>
#include
<stdbool.h
>
if (referenceString[i] ==
page) { return i;
}
int
pageFaul
12
ts = 0; int
pageHits
= 0; int
pageMiss
es = 0;
int page =
referenceString[i];
bool pageFound =
false;
if (frames[j] ==
page)
{ pageFound =
true;
pageHits++;
break;
13
if (!pageFound) {
for (int j = 0; j
(frames[j] == -1) {
frames[j]
= page;
pageFaults
++;
pageMisse
s++;
placed =
true;
break;
}
// If no empty frame, replace the page that will not be used for the
indexToR
eplace = j;
14
break;
farthest) { farthest =
nextUse;
indexToReplace = j;
}
frames[indexToReplac
e] = page;
pageFaults++;
pageMisses++;
// Display current
frames
printf("Frames: ");
for (int j = 0; j
(frames[j] != -1) {
printf("%d ", frames[j]);
printf("\n");
15
printf("Total Page Hits: %d\n", pageHits);
\n", pageMisses);
}
int main() {
int n = sizeof(referenceString) /
Optimal(referenceString, n,
frameCount); return 0;
}
//output
Optimal Page
Replacement: Frames: 7
Frames: 7 0
Frames: 7 0 1
Frames: 2 0 1
Frames: 2 0 1
Frames: 2 0 3
Frames: 2 0 3
Frames: 2 4 3
Frames: 2 4 3
Frames: 2 4 3
16
Frames: 0 4 3
Frames:
043
Total
Page
Hits: 5
Total Page
Faults: 7
Total Page
Misses: 7
Conclusion:
Ans:- age replacement strategies are essential in operating systems that utilize virtual
memory. Here's why:
17
secondary storage, which is a relatively slow process.
⦁ Page replacement algorithms aim to minimize the number of
page faults by selecting the "best" page to remove.
⦁
⦁ Optimizing Performance:
⦁ A well-designed page replacement strategy can significantly
improve system performance by reducing the frequency of page
faults.
⦁
⦁ This leads to faster program execution and a more responsive user
experience.
⦁ Efficient Memory Utilization:
⦁ Page replacement algorithms help the operating system utilize RAM
efficiently by keeping frequently used pages in memory and
removing those that are less likely to be needed.
⦁
In essence, page replacement strategies are crucial for effectively managing virtual
memory and ensuring that computer systems can handle large programs and multitask
efficiently.
18