0% found this document useful (0 votes)
11 views10 pages

Feleke OS Page Replacement

Uploaded by

bereket.crow
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)
11 views10 pages

Feleke OS Page Replacement

Uploaded by

bereket.crow
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/ 10

Unity University

College of Engineering , Technology and Computational Sciences

Department of Computer Science

Course Title: Operating System (COSC 351 or MISY 332)

Topic: Memory management (Page Replacement Algorithms)


After completing this topic, the student will be able to:

 Classify types of “ local page replacement” algorithms;

 Differentiate FIFO , LRU, and OPT, page replacement algorithms;

 Explain the concept of Thrashing

 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.

 Types of page replacement algorithms


There are several algorithms to implement local page replacement policy. These include:

1. First In First Out (FIFO),

2. Least Recently Used (LRU) and

3. Optimal (Opt)

a) First In First Out (FIFO) algorithm

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.

Source: Feleke M. HaileMariam (Dr. ̶ Eng.) Page 1


Unity University
Example
Let us take a random “page reference strings” in a program. This is the sequence in which the
logical page numbers are referenced during the execution of that program.

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.

Source: Feleke M. HaileMariam (Dr. ̶ Eng.) Page 2


Unity University
Note
 FIFO algorithm has one anomaly known as Belady’s anomaly, named after the one who
discovered it.
 Normally, if the physical memory size and therefore, the number of frames available
increases, the number of page faults should decreases. This should enhance the
performance.
 But this is not necessarily the case if you use FIFO as the page replacement policy. This ,
in essence, is Belady’s anomaly.
Let us, for example, take the following reference strings:
2, 3, 4, 5, 2, 3, 6, 2, 3, 4, 5, 6
Fig. Y shows that with 3 frames, FIFO gives 9 page faults. However , fig, R shows us that if you
increase the number of frames from 3 to 4, the number of page faults increases and becomes ten
(10). This is clearly anomalous!

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 fault ( * means Yes) * * * * * * * * *


Fig Y: FIFO page replacement algorithm (total # of page fault equals 9)
Note:
 Fortunately, this anomalous behavior is rare and can be found only for specific types of
page reference strings.
 It does not always happen for all reference strings hence, FIFO is still a fairly good algorithm.

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

Page fault ( * means Yes) * * * * * * * * * *

Fig R FIFO page replacement algorithm (total # of page fault equals 10)

Source: Feleke M. HaileMariam (Dr. ̶ Eng.) Page 3


Unity University
b) Least recently used (LRU) replacement algorithm
 When a page fault occurs, LRU algorithm throws out a page that has been unused for the
longest time. (or LRU replaces the page that has not been used for the longest time.)
 When selecting a resident page for replacement, it takes into consideration the current
usage of a page.
 The algorithm is free from Belady’s anomaly.
 The limitation of LRU algorithm is that the algorithm has a lot of processing overheads,
which are needed to keep track of LRU page.

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.

Source: Feleke M. HaileMariam (Dr. ̶ Eng.) Page 4


Unity University
c) Optimal page replacement algorithm
 Removes a page that will be used not immediately but in the most distant future
 Because it depends upon future behaviors of a process in advance, the OPT algorithm is
of very little practical use. It is only used theoretically as a yardstick to compare other
algorithms with. This method was proposed by Belady.

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)

Fig D OPT page replacement algorithm (total # of page fault equals 9)

Source: Feleke M. HaileMariam (Dr. ̶ Eng.) Page 5


Unity University
Exercises(Page Replacement Algorithms)

 Assume the following reference string

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:

a) First-In-First-Out (FIFO) Algorithm (consider 3 empty frames, 4 empty frames)


 Replace the page that has been in memory the longest.
Answer

FIFO Replacement - Belady’s Anomaly -- more frames does not mean less page faults

b) Optimal Algorithm (consider 3 empty frames, 4 empty frames)


 Replace page that will not be used for longest period of time.
Answer

Source: Feleke M. HaileMariam (Dr. ̶ Eng.) Page 6


Unity University
c) Least Recently Used (LRU) Algorithm(consider 3 empty frames, 4 empty frames)
 Choose the page that has not been used for the longest period of time.
 May require hardware assistance to implement.

Answer

=================================================================
Graph of Page Faults Versus The Number of Frames

========================================================================

FIFO Illustrating Belady’s Anomaly

Source: Feleke M. HaileMariam (Dr. ̶ Eng.) Page 7


Unity University

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

 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: 9 page faults

Source: Feleke M. HaileMariam (Dr. ̶ Eng.) Page 8


Unity University
c) Least Recently Used (LRU) 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:

 12 faults – better than FIFO but worse than OPT

 Generally good algorithm and frequently used

 But how to implement?

Source: Feleke M. HaileMariam (Dr. ̶ Eng.) Page 9


Unity University
Thrashing
 If a process does not have enough pages, the page-fault rate is very high. This leads to:

 low CPU utilization.


 OS thinks that it needs to increase the degree of multiprogramming
 Another process is added to the system.
 System throughput plunges...

Thrashing = A process is busy swapping pages in and out.


In other words, a process is spending more time paging than executing.

Page fault Frequency Scheme

 Control thrashing by establishing acceptable page-fault rate.


* If page fault rate too low, process loses frame.
* If page fault rate too high, process needs and gains a frame.

‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐ The End! ‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐

Source: Feleke M. HaileMariam (Dr. ̶ Eng.) Page 10

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy