Page Replacement Algorithms

Page Replacement Algorithms

·

8 min read

Operating systems are definitely something that have of late started intriguing me more and more every time I learn about them in my Operating Systems class. I got to learn about how the operating system manages memory and what actually happens inside.

In this article, we will look at a part of memory management called replacement as well as what virtual memory is and the three of the commonly used algorithms that are used in determining page faults and page hits.

Table of Contents

1. Memory management

2. Virtual Memory

3. Paging

4. Page replacement algorithms

Memory Management

Memory management is of the main functions that the operating system performs. It handles primary memory and moves processes back and forth between the primary memory and disk during execution. Memory management is responsible for a lot of things such as: keeping track of each memory location regardless of if it has been allocated or not, checking how much memory has been allocated to a process and how much is free, it also decides which process will get memory and for how long. It needs to make sure that there is enough available at all times.

However, this isn’t easy as the computer has limited memory which means that the operating system has to find other alternatives on how it can still make sure that all the resources that require memory are provided with that memory. Most computers deal with this via the combination of both hardware and software to ensure that it always has available memory.

This is where the concept of virtual memory comes in.

Virtual Memory

Something that is virtual does not physically exist hence virtual memory is simply extra memory that there but does not physically exist on the computer. It allows programs to use memory larger than is physically available.

There are two purposes that virtual memory saves and these are:

  1. It allows the user to extend the use of physical memory by using a disk and

  2. Allows the user to have memory protection because each of the virtual addresses are translated into physical addresses.

Virtual memory can be implemented in one of two ways: paging or segmentation. However, this article will only look at paging as this is our main focus.

Paging

This is a technique that is used to read and write data to the secondary storage. Paging is used to retrieve processes from the secondary storage in the form of Pages and the store them in the primary memory.

Pages are usually of the same size and have the size of the power of 2 and range between 512 bytes and 8192 bytes. They are only brought to the main memory when they are needed and not before that. When the main memory receives the page, it breaks it into tiny pieces known as frames which are then used for page replacement.

If process in the CPU is executing and then it requests for a page that is in memory, a page hit will occur as that page will not have to be replaced with another one. However, if the page requested for is not in memory at that time, a page fault occurs and the operating system has to find a page to remove and replace it with the one requested for. This known as demand paging as the demanded page is loaded into memory while the one not needed at the moment is swapped ut and placed back in the secondary storage for later use or when it is needed again.

Page Hit: occurs when a page that has been requested is already present in memory. No page will be replaced at that moment.

Page Miss: also known as page fault, occurs when the requested for page is not available in memory at the time the request is made.

Page Replacement Algorithms

Page replacements are used to decide which page to replace or swap out when a page fault occurs and a new page is required to be loaded into memory.

When the page that was selected for replacement was paged out, and referenced again, it has to read in from disk, and this requires for I/O completion. This process determines the quality of the page replacement algorithm: the lesser the time waiting for page-ins, the better is the algorithm.

There are many page replacements algorithms that can be used, but in this article, we will only look at the three commonly used ones which are; first in first out page replacement algorithm, least recently used page replacement algorithm and optimal page replacement.

We will also look at the advantages and disadvantages of using each one as well as which one is most efficient in handling page replacements within the operating system.

a. OPTIMAL PAGE REPLACEMENT

Also known as OPT, Clairvoyant Replacement Algorithm or Belady’s Optimal Page Replacement Algorithm. Optimal page replacement is the best page replacement as it ensures that less page faults occur.

Optimal page replacement replaces pages that will not be used for the longest time in the future, which are the pages that will only be requested way later in the future. This algorithm works forwards and checks the pages that come after the page that has been requested.

Let’s take an example which has three frames and has the following reference string

Ref String = 5,7,6,0,7,1,7,2,0,1,7,1,0

At the start, the frames are all empty and we fill them in page 5,7 and 6 as they are first pages to be requested and then our frames are all filled up so we need to start swapping pages out.

Optimal-Page-Replacement-Algorithm.png

When page 0 gets requested by the executing process, the operating checks if there is free memory or if the page is already in memory and since all the frames are filled and there in no page 0, it needs to check which page it will swap out which happens to be page 5 as when we look at the pages that will be requested, 5 is not one of them and we won’t need it in the nearest future as hence can be replaced. However, when page 7 comes and the operating system sees that it is already in memory and will be needed in memory in the nearest future, a page hit will occur and the next frame will look identical to the previous one.

And when page 1 gets requested, the operating system continues to check the pages that come after it and then the ones in memory, and since page 6 will not be needed in the future, it can be swapped out. This continues until the entire reference string has been used up

The total number of page faults that occur using Optimal = 7

Total number of hits = 6

Page fault ratio = 7/13

Hit ratio = 6/13

Advantages:

• Produces the least number of page faults

• Used as a benchmark for the other algorithms • Easy to implement

• Highly efficient

• Uses simple data structures

• Doesn’t suffer from Belady’s anomaly

Disadvantages:

• Impossible to implement as you need to know the pages that will be requested but this is not possible

• Time consuming

b. FIRST IN, FIRST OUT (FIFO)

FIFO is the oldest page replacement algorithm and the simplest to implement. It works in such a way that the oldest page in memory (first in the queue) should be replaced when a page fault occurs because it was obviously the first one to be loaded into memory.

Using the same example as in the Optimal Page Replacement, you can see that the first page that was placed in memory is the first one to be removed (swapped out) and replaced with the newer page that was requested for. The top list of numbers is our reference string (pages that should be loaded in memory)

First-In-First-Out.png

Once all the frames are filled, the memory is no longer empty and when page 0 is requested for, the operating system will simply look for the page that has been in memory for the longest, which is 5 and swaps it out. And when page 7 is requested, the operating checks and sees that we already have it in memory hence a page hit will occur as no page will be swapped out.

The total number of page faults that occur using FIFO = 10

Total number of hits = 3

Page fault ratio = 10/13

Hit ratio = 3/13

Advantages:

• Easy to implement

• Low overhead

Disadvantages:

• Suffers from Belady’s Anomaly

• Usually produces the most page faults but this is never always the case

• Poor performance

• Doesn’t consider the frequency of use or last used time as it just replaces the oldest page

c. LEAST RECENTLY USED (LRU)

This algorithm keeps track of when a page was recently used over a short period of time. It considers the page that has not been used for the longest time almost the way that FIFO works but also incorporates some of the Optimal techniques.

However, in this algorithm, you have to check backwards in your reference string to find which page hasn’t been used for the longest period of time and not forwards the way you would when using Optimal algorithm. It assumes that the page that has heavily been used will most likely be heavily used in the future as well.

Least-Recently-Used.png

Using the same example as before, we can see that when page 0 is requested, the operating system first checks if page 6 is still in use and since it was recently added, we skip it and check page 7 and since it is still in use, the operating system moves backwards again and checks page 5. Page 5 has been in memory for the longest and is the least recently used, so the operating system will then replace it with page 0. This continues for every page that will be requested.

The total number of page faults that occur using LRU = 9

Total number of hits = 4

Page fault ratio = 9/13

Hit ratio = 4/13

Advantages:

• Easy to implement when dealing with a shorter reference string

• Efficient

• Doesn’t suffer from Belady’s anomaly

Disadvantages:

• Requires hardware support

• Expensive

• Requires complex implementation when dealing with longer reference strings

Summary

Memory is an integral part of an any computer as this determines how your processes will be executed. As computers have less memory than usually required, virtual memory and demand paging are necessary in order to ensure that each process can be executed fully which leads us to knowing how the operating system deals with demand paging such as by using the optimal, FIFO or LRU page replacement algorithms.

Connect with me on

Twitter

LinkedIn

GitHub