Feleke OS Page Replacement
Feleke OS Page Replacement
Basic concept
If all the frames are occupied and when a new page has to be brought in (due to the page
fault), then the operating system has to choose a candidate frame to be overwritten.
If this page is necessarily chosen from the same process, it is called local replacement
policy.
If it can be from any process, it is called global replacement policy.
For our discussion, we will use local replacement policy, because the operating system
require very complicated system-wise logic to implement global replacement policy.
3. Optimal (Opt)
In this scheme, the page that is removed from the memory is the one that entered it first.
Replace the page that has been in memory the longest.
Assume that there are only three frames 0, 1, and 2 in our system.
Also assume that they are empty to begin with, and, the list of free frames also maintains them in
that order 0, 1, and 2. Therefore, they will be allocated in that order.
Again assume that the page reference strings are given below:
8, 1, 2, 3, 1, 4, 1, 5, 3, 4, 1, 4, 3, 2, 3, 1, 2, 8, 1, 2.
Reference string refers to the sequence of page numbers referenced by a program during its
execution. Figure x shows the state of the three frames after each page reference. The figure also
indicates for each page reference, whether it causes a page fault or not.
Page fault is indicated by * symbol and space means no page fault for that page reference.
Discussion on fig X.
For instance, initially all frames are empty.
When page 8 is referenced, a page fault will occur (indicated by * symbol in last row for
that column).
This page now will be brought into memory (frame zero) first column.
The next two columns are similar.
Page reference 8 1 2 3 1 4 1 5 3 4 1 4 3 2 3 1 2 8 1 2
Frame 0 8 8 8 3 3 3 3 5 5 5 1 1 1 1 1 1 1 8 8 8
Frame 1 1 1 1 1 4 4 4 3 3 3 3 3 2 2 2 2 2 1 1
Frame 2 2 2 2 2 1 1 1 4 4 4 4 4 3 3 3 3 3 2
Page fault * * * * * * * * * * * * * * *
( * means Yes)
Fig X: FIFO page replacement algorithm (total # of page fault equals 15)
In the fourth reference of page 3, a page fault results, and the FIFO algorithms throws
out page 8 because it was the first one to be brought in.
The fifth page reference does not cause page fault as page one is already in the memory.
The sixth page reference is for page 4. Here, FIFO will throw out page one.
Following this logic, the table can be completed.
Page reference 2 3 4 5 2 3 6 2 3 4 5 6
Frame 0 2 2 2 5 5 5 6 6 6 6 6 6
Frame 1 3 3 3 2 2 2 2 2 4 4 4
Frame 2 4 4 4 3 3 3 3 3 5 5
Page reference 2 3 4 5 2 3 6 2 3 4 5 6
Frame 0 2 2 2 2 6 6 6 6 5 5
Frame 1 3 3 3 3 2 2 2 2 6
Frame 2 4 4 4 4 3 3 3 3
Frame 3 5 5 5 5 4 4 4
Fig R FIFO page replacement algorithm (total # of page fault equals 10)
Example:
To find the number of total number of page faults (for LRU case), let us take the same page
reference string as shown earlier for FIFO.
Page reference 8 1 2 3 1 4 1 5 3 4 1 4 3 2 3 1 2 8 1 2
Frame 0 8 8 8 3 3 3 3 5 5 5 1 1 1 2 2 2 2 2 2 2
Frame 1 1 1 1 1 1 1 1 1 4 4 4 4 4 4 1 1 1 1 1
Frame 2 2 2 2 4 4 4 3 3 3 3 3 3 3 3 3 8 8 8
Page fault * * * * * * * * * * * *
( * means Yes)
Fig C LRU page replacement algorithm (total # of page fault equals 12)
Description of fig. C
In this scheme(LRU), when page five is referenced (8th reference from the beginning),
there are already three frames in the memory, namely 3, 1, and 4.
Studying the reference strings, it is found that the last page 3 is referenced in 4th
reference, the last page 1 is referenced in the seventh reference and the last page 4 is
referenced in the 6th reference.
The operating system wants to bring in page five, but it needs to throw out a page because
there is no free frame available. So page 3 was used least recently, it is thrown out.
Example
Assume that the following page reference string are given:
8, 1, 2, 3, 1, 4, 1, 5, 3, 4, 1, 4, 3, 2, 3, 1, 2, 8, 1, 2.
Describing the OPT algorithm:
Fig. D shows that the state of the 3 frames after each reference.
Initially all frames are empty.
When page 8 is referenced, a page fault will occur (indicated by * symbol).
This page (page 8) now will be brought into the memory (page frame zero). This is
indicated by the first column.
The next two columns are similar and hence, need no explanation.
The fourth page reference is for page 3. This is not in the memory, but no frame is also
free. Thus, a page removal is necessary.
The operating system has the choice of throwing out page 8 residing in frame zero. Why?
If you study the page reference list, you will realize that page 8 is going to be used far
later than page one or page 2. This exactly was Belady’s algorithm for OPT page
replacement. You should trace the figure through all the page references to understand
this completely.
Page reference 8 1 2 3 1 4 1 5 3 4 1 4 3 2 3 1 2 8 1 2
Frame 0 8 8 8 3 3 3 3 3 3 3 3 3 3 3 3 3 3 8 8 8
Frame 1 1 1 1 1 1 1 5 5 5 1 1 1 1 1 1 1 1 1 1
Frame 2 2 2 2 4 4 4 4 4 4 4 4 2 2 2 2 2 2 2
Page fault * * * * * * * * *
( * means Yes)
1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5.
Assume x frames ( x pages can be in memory at a time per process)
Required: Determine the number of page faults for the following algorithms:
FIFO Replacement - Belady’s Anomaly -- more frames does not mean less page faults
Answer
=================================================================
Graph of Page Faults Versus The Number of Frames
========================================================================
Class Exercises
a) First-In-First-Out (FIFO) Algorithm
Reference string: 7,0,1,2,0,3,0,4,2,3,0,3,0,3,2,1,2,0,1,7,0,1
3 frames (3 pages can be in memory at a time per process)
Required: Total number of page faults
Answer: 15 page faults
======================================================================
b) Optimal Algorithm
Answer: