AdriasJ MIT VirtualMemory Segmentation Paging Page Replacement

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 41

Memory and Virtual Management

-Virtual Memory Management


-Segmentation
-Paging
-Page Replacement

Prepared by: JOY R. ADRIAS-MIT


Submitted to: DR. FRANCIS F. BALAHADIA
Virtual Memory Management

With the help of the memory management technology known as virtual memory,
secondary memory can be used as like it were a component of the main memory. A
frequent method in an operating system for a computer is virtual memory (OS).

What is virtual memory?


Virtual memory is a memory management technique that allows you to use
secondary memory as if it were part of your main memory. Virtual memory is a
common technique used by the computer's operating system (OS) in used.
Virtual memory uses both hardware and software to allow a computer to
compensate for a lack of physical memory by temporarily moving data from random
access memory (RAM) to disk storage. By mapping memory blocks to disk files, your
computer can treat secondary storage like main memory.
Most personal computers (PCs) today have at least 8 GB (gigabytes) of RAM.
But running multiple programs at the same time may not be enough. This is where
virtual memory comes into play. Virtual memory frees up RAM by swapping data that
has not been used recently to a storage device such as a hard drive or solid state drive
(SSD).
Virtual memory is important for improving system performance, multitasking,
and using large programs. However, virtual memory is much slower than main
memory, so you shouldn't rely on it too much. If your operating system has to
exchange data between virtual memory and RAM frequently, your computer will slow
down. This is called thrashing.

Figure 1:
Virtual
memory
can act in
concert
with a

computer’s main memory to enable faster, more fluid operations.


Source:https://www.enterprisestorageforum.com/wp-content/uploads/2021/02/virtual-
memory_6019c62e91446.png.webp

Virtual memory was developed at a time when physical memory, also known
as RAM, was expensive. Since the computer has a limited amount of RAM, running
2 | Memory and Virtual Management
Prepared by: JOY R. ADRIAS-MIT
Submitted to: DR. FRANCIS F. BALAHADIA
multiple programs at the same time will eventually run out of memory. Systems that
use virtual memory use sections of the hard drive to emulate RAM. Virtual memory
allows your system to run larger programs or multiple programs simultaneously, each
temporary as if they had more memory without having to buy more RAM.
Here are some situations where the entire program does not need to be fully
loaded into main memory.
 User-written error handlers are used only when there is an error in data or
calculations.
 Program may be used rarely at a certain options and features.
 Many tables are assigned a fixed address space even if only a small portion of
the table is actually used.
 Being able to run a program that is only partially in memory negates many
benefits.
 Less I/O is required to load or swap each user program into memory.
 Programs are no longer limited by the amount of physical memory available.
 Each user program used less physical memory, allowing more programs to run
simultaneously, with a corresponding increase
How virtual memory works
Virtual memory works using both hardware and software. When an application
is used, data from that program is stored at physical addresses using RAM. The
memory management unit (MMU) maps addresses to RAM and translates addresses
automatically. For example, an MMU can map a logical address space to a
corresponding physical address.

Figure 2:
MMU can
map a logical
address
space to a

corresponding physical address.

Source: https://www.baeldung.com/wp-content/uploads/sites/4/2021/05/Capture.png
If at some point you need RAM space for something more pressing, it can swap
data out of RAM into virtual memory. A computer's memory manager is responsible
3 | Memory and Virtual Management
Prepared by: JOY R. ADRIAS-MIT
Submitted to: DR. FRANCIS F. BALAHADIA
for keeping track of movements between physical and virtual memory. When that data
is needed again, MMU on the computer uses a context switch to continue execution.

The main advantage of virtual memory is that the operating system can load
programs larger than its physical memory. This gives the user the impression that the
computer has unlimited memory. It also provides memory protection.
Implementing mapping operations requires the use of page tables and
translations in virtual memory. A page table is a data structure that stores page tables
called page table entries (PTEs). The purpose of page tables is to map virtual
addresses to physical addresses. This is a contiguous block, the smallest unit of virtual
memory.
To create a memory page, divide the memory page into frames of equal size.
Each frame references a respective physical address frame number, frame offset, or
absolute address.
Translation is the process of translating virtual addresses into physical
addresses using page tables:

Figure 3:
Translation
Process

Source: https://www.baeldung.com/wp-content/uploads/sites/4/2021/05/pava.png

When copying virtual memory to physical memory, the operating system shares
memory with a fixed number of paging or paging file addresses. Each page is stored
on disk, and when the page is needed, the operating system copies it from disk to
main memory, translating the virtual address to a real address.
However, the process of swapping virtual memory to physical memory is very
slow. This usually means that using virtual memory will degrade performance
4 | Memory and Virtual Management
Prepared by: JOY R. ADRIAS-MIT
Submitted to: DR. FRANCIS F. BALAHADIA
significantly. Because of swapping, computers with more RAM are believed to perform
better. To speed up the process of reading data, virtual memory can use cache
memory. Use virtual cache because physical cache does not guarantee fast
processing. The CPU is directly connected to the cache and looks up virtual addresses
in the cache avoiding translations.
Translation is only done if the address is not found. Finally, each program has
its own virtual cache:

Figure 4:
Translation
Process

Source: https://www.baeldung.com/wp-content/uploads/sites/4/2021/05/cache-1.png

Modern microprocessors intended for general-purpose use, a memory


management unit, or MMU, is built into the hardware. The MMU's job is to translate
virtual addresses into physical addresses. A basic example is given below –

Figure 5 Memory
Management Unit
translate Virtual
Address into
Physical
Addresses.

5 | Memory and Virtual Management


Prepared by: JOY R. ADRIAS-MIT
Submitted to: DR. FRANCIS F. BALAHADIA
How to manage virtual memory
Managing virtual memory within an OS can be straightforward, as there are
default settings that determine the amount of hard drive space to allocate for virtual
memory. Those settings will work for most applications and processes, but there may
be times when it is necessary to manually reset the amount of hard drive space
allocated to virtual memory -- for example, with applications that depend on fast
response times or when the computer has multiple hard disk drives (HDDs).
When manually resetting virtual memory, the minimum and maximum amount
of hard drive space to be used for virtual memory must be specified. Allocating too
little HDD space for virtual memory can result in a computer running out of RAM. If a
system continually needs more virtual memory space, it may be wise to consider
adding RAM. Common OSes may generally recommend users not increase virtual
memory beyond 1 ½ times the amount of RAM.
Managing virtual memory differs by OS. For this reason, IT professionals should
understand the basics when it comes to managing physical memory, virtual memory
and virtual addresses.
RAM cells in SSDs also have a limited lifespan. RAM cells have a limited number
of writes, so using them for virtual memory often reduces the lifespan of the drive.
What are the benefits of using virtual memory?
The advantages to using virtual memory include:
• It can handle twice as many addresses as main memory.
• It enables more applications to be used at once.
• It frees applications from managing shared memory and saves users from
having to add memory modules when RAM space runs out.
• It has increased speed when only a segment of a program is needed for
execution.
• It has increased security because of memory isolation.
• It enables multiple larger applications to run simultaneously.
• Allocating memory is relatively inexpensive.
• It does not need external fragmentation.
• CPU use is effective for managing logical partition workloads.
• Data can be moved automatically.
• Pages in the original process can be shared during a fork system call operation
that creates a copy of itself.
In addition to these benefits, in a virtualized computing environment,
administrators can use virtual memory management techniques to allocate additional
memory to a virtual machine (VM) that has run out of resources. Such virtualization

6 | Memory and Virtual Management


Prepared by: JOY R. ADRIAS-MIT
Submitted to: DR. FRANCIS F. BALAHADIA
management tactics can improve VM performance and management flexibility.

Source: https://www.techtarget.com/searchstorage/definition/virtual-memory.
Figure 6. This image shows an example of how physical memory is separated by a virtual memory
manager.

What are the limitations of using virtual memory?


Although the use of virtual memory has its benefits, it also comes with some
tradeoffs worth considering, such as:
• Applications run slower if they are running from virtual memory.
• Data must be mapped between virtual and physical memory, which requires
extra hardware support for address translations, slowing down a computer
further.
• The size of virtual storage is limited by the amount of secondary storage, as
well as the addressing scheme with the computer system.
• Thrashing can occur if there is not enough RAM, which will make the computer
perform slower.
• It may take time to switch between applications using virtual memory.
• It lessens the amount of available hard drive space.

Virtual memory (virtual RAM) vs. physical memory (RAM)

7 | Memory and Virtual Management


Prepared by: JOY R. ADRIAS-MIT
Submitted to: DR. FRANCIS F. BALAHADIA
When talking about the differences between virtual and physical memory, the
biggest distinction commonly made is to speed. RAM is considerably faster than
virtual memory. RAM, however, tends to be more expensive.
When a computer requires storage, RAM is the first used. Virtual memory,
which is slower, is used only when the RAM is filled.

Source: https://www.techtarget.com/searchstorage/definition/virtual-memory.
Figure 7.This chart shows how virtual RAM (virtual memory) compares to RAM
(physical memory).
Users can actively add RAM to a computer by buying and installing more RAM
chips. This is useful if they are experiencing slowdowns due to memory swaps
happening too often. The amount of RAM depends on what is installed on a computer.
Virtual memory, on the other hand, is limited by the size of the computer's hard drive.
Virtual memory settings can often be controlled through the OS.
In addition, RAM uses swapping techniques, while virtual memory uses paging.
While physical memory is limited to the size of the RAM chip, virtual memory is limited
by the size of the hard disk. RAM also has direct access to the CPU, while virtual RAM
does not.

Types of virtual memory

A computer's MMU manages virtual memory operations. In most computers,


the MMU hardware is integrated into the central processing unit (CPU). The CPU also

8 | Memory and Virtual Management


Prepared by: JOY R. ADRIAS-MIT
Submitted to: DR. FRANCIS F. BALAHADIA
generates the virtual address space. In general, virtual memory is either paged or
segmented. Virtual memory can be managed in a number of different ways by a
system’s operating system.
Two most common approaches are:
 paging and
 segmentation

Paging VS Segmentation (source: https://www.javatpoint.com/os-paging-vs-segmentation)

Source: https://www.gatevidyalay.com/wp-content/uploads/2018/11/Non-Contiguous-Memory-
Allocation-Techniques.png
Paging divides memory into sections or paging files. When a computer uses
up its available RAM, pages not in use are transferred to the hard drive using a swap
file. A swap file is a space set aside on the hard drive to be used as the virtual memory
extension for the computer's RAM. When the swap file is needed, it is sent back to
RAM using a process called page swapping. This system ensures the computer's OS
and applications do not run out of real memory. The maximum size of the page file
can be 1 ½ to four times the physical memory of the computer.
The virtual memory paging process uses page tables, which translate the virtual
addresses that the OS and applications use into the physical addresses that the MMU
uses. Entries in the page table indicate whether the page is in RAM. If the OS or a
program does not find what it needs in RAM, then the MMU responds to the missing
9 | Memory and Virtual Management
Prepared by: JOY R. ADRIAS-MIT
Submitted to: DR. FRANCIS F. BALAHADIA
memory reference with a page fault exception to get the OS to move the page back to
memory when it is needed. Once the page is in RAM, its virtual address appears in
the page table.
Segmentation is also used to manage virtual memory. This approach divides
virtual memory into segments of different lengths. Segments not in use in memory can
be moved to virtual memory space on the hard drive. Segmented information or
processes are tracked in a segment table, which shows if a segment is present in
memory, whether it has been modified and what its physical address is. In addition,
file systems in segmentation are only made up of segments that are mapped into a
process's potential address space.
Segmentation and paging differ as a memory model in terms of how memory is
divided; however, the processes can also be combined. In this case, memory gets
divided into frames or pages. The segments take up multiple pages, and the virtual
address includes both the segment number and the page number.
Other page replacement methods include first-in-first-out (FIFO), optimal
algorithm and least recently used (LRU) page replacement. The FIFO method has
memory select the replacement for a page that has been in the virtual address for the
longest time. The optimal algorithm method selects page replacements based on
which page is unlikely to be replaced after the longest amount of time; although difficult
to implement, this leads to less page faults. The LRU page replacement method
replaces the page that has not been used for the longest time in the main memory.
How to Increase Virtual Memory in a System
Most operating systems allow users to increase virtual memory from a
configuration page.
In Windows, users can also allow the system to manage the amount of virtual
memory provided dynamically.
Please note that on Windows 11, you can take a less circuitous route to your
Advanced System Settings.
1. Press Windows key + I to
open the Settings app.
2. Head to System > About.
3. Select Advanced system
settings.
4. Under Performance,
select Settings. Open
the Advanced tab.
Under Virtual memory,
select Change. Here are your
Virtual Memory options.

Source: https://www.makeuseof.com/tag/virtual-memory-low-heres-fix/

10 | Memory and Virtual Management


Prepared by: JOY R. ADRIAS-MIT
Submitted to: DR. FRANCIS F. BALAHADIA
The default option is to Automatically manage paging file size for all drives.
Uncheck this to enable the currently greyed-out section below. Select the drive you
want to edit the paging file size for. Usually, this is your C: drive.

Source: https://www.makeuseof.com/tag/virtual-memory-low-heres-fix/

Now, select Custom size. Set the


Maximum size you want for your paging file,
following the recommended size for your
system. Remember, Windows restricts the
paging file size to three times the size of your
installed RAM. This is to ensure system
stability. Set the Initial size to the currently
allocated size

To calculate the "general rule"


recommended size of virtual memory in
Windows 10 per the 8 GB your system has,
here's the equation:
1024 x 8 x 1.5 = 12,288 MB.

("There is a formula for calculating the correct pagefile size. The Initial size is one and
a half (1.5) x the amount of total system memory. The Maximum size is three (3) x the
initial size. So let's say you have 4 GB (1 GB = 1,024 MB x 4 = 4,096 MB) of memory.
The initial size would be 1.5 x 4,096 = 6,144 MB and the maximum size would be 3 x
6,144 = 18,432 MB.".)

11 | Memory and Virtual Management


Prepared by: JOY R. ADRIAS-MIT
Submitted to: DR. FRANCIS F. BALAHADIA
Additional readings:
The history of virtual memory

Before virtual memory was developed, computers had RAM and secondary
memory. Early computers used magnetic core memory for main memory and magnetic
drums for their secondary memory. Computer memory was expensive and, usually, in
short supply back in the 1940s and 1950s. As computer programs grew in size and
complexity, developers had to worry that their programs would use up all of a
computer's main memory and run out of memory.
In those early days, programmers used a process called overlaying to run
programs that were larger than available memory. Parts of a program that were not
continually in use were set up as overlays that, when needed, would overwrite the
existing overlay in memory. It required extensive programming to make overlaying
work, and that was a key impetus for the development of automated virtual memory.
German physicist Fritz-Rudolf Güntsch has been credited with developing the
concept of virtual memory in 1956 -- though this point has been contested. Güntsch
did, however, end up describing a form of cache memory.
The first apparent real instance of a virtual memory system came from the
University of Manchester in Manchester, England, in its attempt to develop a one-level
storage system for the Atlas computer. The system used paging to map virtual
addresses to a programmer onto the primary memory. Atlas was developed in 1959
and later commissioned in 1962.
In 1961, the first commercial computer with virtual memory was released by the
Burroughs Corp. This version of virtual memory used segmentation, as opposed to
paging.
In 1969, IBM researchers demonstrated that virtual memory overlay systems
worked better than the earlier manual systems. Up until this point, there was still a
debate over this. Mainframes and minicomputers in the 1970s generally used virtual
memory. Virtual memory technology was not included in early PCs because
developers thought running out of memory would not be a problem in those machines.
That assumption proved incorrect. Intel introduced virtual memory in the protected
mode of the 80286 processor in 1982, and paging support when the 80386 came out
in 1985.

12 | Memory and Virtual Management


Prepared by: JOY R. ADRIAS-MIT
Submitted to: DR. FRANCIS F. BALAHADIA
Segmentation

Segmentation is a memory management technique whereby data items are


stored in segments on the storage media. It divides the process or user-accessible
space into fixed-sized blocks, called segments.
Memory management is the process of allocating and controlling the virtual
address spaces of computers. In plain language, it is how computers keep track of the
things they are working on simultaneously. Segmentation is a memory management
technique that allows different parts of the kernel to run in separate address spaces.

What is Segmentation?
Segmentation is a memory management technique that splits up the virtual
address space of an application into chunks.
By splitting the memory up into manageable chunks, the operating system can
track which parts of the memory are in use and which parts are free. This makes
allocating and de-allocating memory much faster and simpler for the operating system.
The segments are of unequal size and are not placed in a contiguous way. As it’s a
non-contiguous memory allocation technique, internal fragmentation doesn’t occur.
The length is decided on the base of the purpose of the segment in a user program.

Why is Segmentation technique required?


Segmentation is a memory management technique which divides the program
from the user’s view of memory. That means the program is divided into
modules/segments, unlike paging in which the program was divided into different
pages, and those pages may or may not be loaded into the memory simultaneously.
Segmentation prevents internal fragmentation.

13 | Memory and Virtual Management


Prepared by: JOY R. ADRIAS-MIT
Submitted to: DR. FRANCIS F. BALAHADIA
Difference between paging and Segmentation with example

Some of you must be confusing the segmentation concept with


the paging concept. So let me clear that. The page concept depicts the paging
concept. In which we have different functions in a program. These functions will be
fragmented into equal sizes in case of paging and stored in the main memory. And the
Segmentation Concept depicts segmentations in which the program will be divided
into different functions (modules) without fragmenting these functions. Segmentation
allows users to partition the user programs into modules. These modules are nothing
but the independent codes of the current process/program.

14 | Memory and Virtual Management


Prepared by: JOY R. ADRIAS-MIT
Submitted to: DR. FRANCIS F. BALAHADIA
Other differences are-

S.NO Paging Segmentation

In paging, the program or process is In segmentation, the program or process


1.
divided into fixed-size pages. is divided into variable size sections.

Here, the segment size is decided by the


2. Page size is decided by hardware.
user.

Paging could result in internal Segmentation could result in external


3.
fragmentation. fragmentation.

The logical address in paging is Here, the logical address is divided into
4. divided into a page offset and page section number and limit(size of the
number. segment).

5. Fast as compared to segmentation Slow as compared to the paging.

6. Paging has a page table While segmentation has a segment table.

7. Paging is invisible to the user. Segmentation is visible to the developers.

Paging consists of static linking and Segmentation consists of dynamic linking


8.
dynamic loading and dynamic loading

In paging, to calculate the absolute In segmentation, to calculate the absolute


9. address the processor needs the address the processor uses the segment
page number, and offset number, and offset.

Paging is managed by the system In segmentation, the programmer should


10
automatically. have prior knowledge of memory limits.

The size of the page is always equal


11. The size of the segments can vary.
to the size of the frames.

15 | Memory and Virtual Management


Prepared by: JOY R. ADRIAS-MIT
Submitted to: DR. FRANCIS F. BALAHADIA
Advantages of using segmentation technique
 It can improve the system’s efficiency by allowing different kernel parts to run on
separate processor cores.
 It can also improve the system’s responsiveness by allowing different threads to
run in parallel.
 Better CPU utilization
 Provide a solution to internal fragmentation.
 The segment table is there for storing records of segments. This segment table
also consumes some memory to get stored.
 Security procedures can be separated from data.
 Segmentation is near to the user’s view of physical memory. Segmentation allows
users to partition the user programs into modules. These modules are nothing but
the independent codes of the current process.
 The user specifies the Segment size compared to paging, where hardware
decides the page size.

Disadvantages of using segmentation technique


 It can increase the cost of system performance. This is because the kernel must
be able to detect when a segment is being used and allocate new memory space
for it.
 The system may require more resources to manage Segmentation than if it used a
more straightforward memory management technique.
 It suffers from external fragmentation.
 Overhead of maintaining a segment table for each process.
 Access time to fetch the instruction increases since two memory accesses are
now required, one for the segment table and the other for the main memory.
 Unequal size segments are not suited for swapping.

16 | Memory and Virtual Management


Prepared by: JOY R. ADRIAS-MIT
Submitted to: DR. FRANCIS F. BALAHADIA
How Does Segmentation Work?
Segmentation is a memory management technique that breaks down data into
manageable chunks. When you want to store or access data, the system first looks
for the segment that contains that data.

This concept is the same as the paging concept. But unlike paging in
Segmentation, the process is not divided into pages but into large chunks
called segments or modules. The CPU generates this logical address. This logical
address is to access secondary memory. But CPU has to access the main memory. So
what to do now? In this case, address translation is required to convert a logical
address into a physical one.
Segmentation gives user’s view of the process which paging does not give.
Here the user’s view is mapped to physical memory.
There are types of segmentation:

1. Virtual memory segmentation –Each process is divided into a number of


segments, not all of which are resident at any one point in time.
2. Simple segmentation –Each process is divided into a number of segments, all of
which are loaded into memory at run time, though not necessarily contiguously.

A physical address is an address for finding data in the main memory. So now
the CPU will take the help of the segment table. A segment table is a data structure
for storing the information of all process segments. CPU use a segment table to map
the logical address to a physical address.
In the segment table, there are two types of information
 Limit: Specify the length of Segment. (Actual size of a segment)

17 | Memory and Virtual Management


Prepared by: JOY R. ADRIAS-MIT
Submitted to: DR. FRANCIS F. BALAHADIA
 Base Address: Contain the starting physical address (the address of the
segment in the main memory)

Address generated by the CPU is divided into:

 Segment number (s): Number of bits required to represent the segment.


 Segment offset (d): Number of bits required to represent the size of the
segment.

Then if the value of offset(d)<=Limit. Then only the CPU can read that segment;
else, the error will be there. Offset (d) depicts the size of that segment CPU wants to
read.

Figure Segmentation in the x86 operating system which means accessing the
memory through segments.
Source: https://miro.medium.com/max/876/1*t8a-wUISu7k7Rt9kOEgoZQ.png

18 | Memory and Virtual Management


Prepared by: JOY R. ADRIAS-MIT
Submitted to: DR. FRANCIS F. BALAHADIA
1. Segmentation Example

Suppose the CPU wants to access segment 3. It will generate a logical


address(as shown below) specifying the segment number and size of the segment it
wants to read. The segment size is supposed 300. This means the CPU wants to
read 300 instructions from segment 3. So after consulting the segment table, the
CPU comes to know that the base address is 2500. But the size of the segment is
200 instructions. So here offset(d)>Limit. So this will generate an error.

Segmentation is a memory management technique that can be used to


improve the performance of an operating system. By dividing physical memory into
segments, the operating system can better manage its memory use, improving
efficiency and performance.

19 | Memory and Virtual Management


Prepared by: JOY R. ADRIAS-MIT
Submitted to: DR. FRANCIS F. BALAHADIA
Paging in Operating Systems
Paging permits the physical address space of a process to be non-
contiguous. It is a fixed-size partitioning scheme. In the Paging technique, the
secondary memory and main memory are divided into equal fixed-size partitions.
Paging solves the problem of fitting memory chunks of varying sizes onto the
backing store and this problem is suffered by many memory management schemes.
Paging helps to avoid external fragmentation and the need for compaction.

Basic Method of Paging

The paging technique divides the physical memory (main memory) into fixed-size
blocks that are known as Frames and also divide the logical memory(secondary
memory) into blocks of the same size that are known as Pages.
This technique keeps the track of all the free frames.
The Frame has the same size as that of a Page. A frame is basically a place where
a (logical) page can be (physically) placed.

Source: https://s3.ap-south-1.amazonaws.com/s3.studytonight.com/tutorials/uploads/pictures/
1608530491-71449.png

Each process is mainly divided into parts where the size of each part is the same as
the page size.
There is a possibility that the size of the last part may be less than the page size.
 Pages of a process are brought into the main memory only when there is a
requirement otherwise they reside in the secondary storage.
 One page of a process is mainly stored in one of the frames of the memory.
Also, the pages can be stored at different locations of the memory but always
the main priority is to find contiguous frames.

20 | Memory and Virtual Management


Prepared by: JOY R. ADRIAS-MIT
Submitted to: DR. FRANCIS F. BALAHADIA
Source: https://s3.ap-south-1.amazonaws.com/s3.studytonight.com/tutorials/uploads
/pictures/1608532433-71449.png

Translation of Logical Address into Physical Address

Before moving on further there are some important points to note:


 The CPU always generates a logical address.
 In order to access the main memory always a physical address is needed.

The logical address generated by CPU always consists of two parts:


1. Page Number (p) - is used to specify the specific page of the process from
which the CPU wants to read the data and it is also used as an index to the
page table.
2. Page Offset (d) - is mainly used to specify the specific word on the page that
the CPU wants to read.
Logical Address = Page number + page offset
Page Table in OS

The Page table mainly contains the base address of each page in the Physical
memory. The base address is then combined with the page offset in order to define
the physical memory address which is then sent to the memory unit.
Thus page table mainly provides the corresponding frame number (base address
of the frame) where that page is stored in the main memory.
As we have told you above that the frame number is combined with the page
offset and forms the required physical address.
So, The physical address consists of two parts:
1. Page offset(d)
2. Frame Number(f)
where,
 Frame number is used to indicate the specific frame where the required page
is stored.
21 | Memory and Virtual Management
Prepared by: JOY R. ADRIAS-MIT
Submitted to: DR. FRANCIS F. BALAHADIA
 Page Offset indicates the specific word that has to be read from that page.
The Page size (like the frame size) is defined with the help of hardware. It is
important to note here that the size of the page is typically the power of 2 that
varies between 512 bytes and 16 MB per page and it mainly depends on the
architecture of the computer.
If the size of logical address space is 2 raised to the power m and page
size is 2 raised to the power n addressing units then the high order m-n bits
of logical address designates the page number and the n low-order bits
designate the page offset.
Physical Address = Frame number + page offset
The logical address is as follows:

where p indicates the index into the page table, and d indicates the displacement within the
page.

Source: https://s3.ap-south-
1.amazonaws.com/s3.studytonight.com/tutorials/uploads/pictures/ 1608534972-71449.png
The above diagram indicates the translation of the Logical address into the
Physical address. The PTBR (Page Table Base Register ) in the above diagram
means page table base register and it basically holds the base address for the page
table of the current process.
The PTBR is mainly a processor register and is managed by the operating
system. Commonly, each process running on a processor needs its own logical
address space.
But there is a problem with this approach and that is with the time required to
access a user memory location. Suppose if we want to find the location i, we must first
find the index into the page table by using the value in the PTBR offset by the page
22 | Memory and Virtual Management
Prepared by: JOY R. ADRIAS-MIT
Submitted to: DR. FRANCIS F. BALAHADIA
number for I. And this task requires memory access. It then provides us the frame
number which is combined with the page offset in order to produce the actual address.
After that, we can then access the desired place in the memory.
With the above scheme, two memory accesses are needed in order to access
a byte( one for the page-table entry and one for byte). Thus memory access is slower
by a factor of 2 and in most cases, this scheme slowed by a factor of 2.

Translation of look-aside buffer (TLB)

There is the standard solution for the above problem that is to use a special, small,
and fast-lookup hardware cache that is commonly known as Translation of look-aside
buffer(TLB).
 TLB is associative and high-speed memory.
 Each entry in the TLB mainly consists of two parts: a key(that is the tag) and a
value.
 When associative memory is presented with an item, then the item is compared
with all keys simultaneously. In case if the item is found then the corresponding
value is returned.
 The search with TLB is fast though the hardware is expensive.
 The number of entries in the TLB is small and generally lies in between 64 and
1024.

TLB is used with Page Tables in the following ways:


The TLB contains only a few of the page-table entries. Whenever the logical address
is generated by the CPU then its page number is presented to the TLB.
 If the page number is found, then its frame number is immediately available and
is used in order to access the memory. The above whole task may take less
than 10 percent longer than would if an unmapped memory reference were
used.
 In case if the page number is not in the TLB (which is known as TLB miss),
then a memory reference to the Page table must be made.
 When the frame number is obtained it can be used to access the memory.
Additionally, page number and frame number is added to the TLB so that they
will be found quickly on the next reference.
 In case if the TLB is already full of entries then the Operating system must
select one for replacement.
 TLB allows some entries to be wired down, which means they cannot be
removed from the TLB. Typically TLB entries for the kernel code are wired
down.

23 | Memory and Virtual Management


Prepared by: JOY R. ADRIAS-MIT
Submitted to: DR. FRANCIS F. BALAHADIA
Paging Hardware With TLB

Source: https://s3.ap-south-
1.amazonaws.com/s3.studytonight.com/tutorials/uploads/pictures/ 1608632310-71449.png

Advantages of Paging
Given below are some advantages of the Paging technique in the operating system:
 Paging mainly allows to storage of parts of a single process in a non-
contiguous fashion.
 With the help of Paging, the problem of external fragmentation is solved.
 Paging is one of the simplest algorithms for memory management.

Disadvantages of Paging
Disadvantages of the Paging technique are as follows:
 In Paging, sometimes the page table consumes more memory.
 Internal fragmentation is caused by this technique.
 There is an increase in time taken to fetch the instruction since now two
memory accesses are required.

24 | Memory and Virtual Management


Prepared by: JOY R. ADRIAS-MIT
Submitted to: DR. FRANCIS F. BALAHADIA
Paging Hardware

Every address generated by CPU mainly consists of two parts:


1. Page Number(p)
2. Page Offset (d)
where,
 Page Number is used as an index into the page table that generally
contains the base address of each page in the physical memory.
 Page offset is combined with base address in order to define the physical
memory address which is then sent to the memory unit.

If the size of logical address space is 2 raised to the power m and page size is 2
raised to the power n addressing units then the high order m-n bits of logical
address designates the page number and the n low-order bits designate the
page offset.
The logical address is as follows:

where p indicates the index into the page table, and d indicates the displacement
within the page. The Page size is usually defined by the hardware. The size of the
page is typically the power of 2 that varies between 512 bytes and 16 MB per page.

Source: https://s3.ap-south-
1.amazonaws.com/s3.studytonight.com/tutorials/uploads/pictures /1608616966-71449.png

25 | Memory and Virtual Management


Prepared by: JOY R. ADRIAS-MIT
Submitted to: DR. FRANCIS F. BALAHADIA
Paging Example

In order to implement paging, one of the simplest methods is to implement the


page table as a set of registers. As the size of registers is limited but the size of the
page table is usually large thus page table is kept in the main memory.
There is no External fragmentation caused due to this scheme; Any frame that
is free can be allocated to any process that needs it. But the internal fragmentation is
still there.
 If any process requires n pages then at least n frames are required.
 The first page of the process is loaded into the first frame that is listed on
the free-frame list, and then the frame number is put into the page table.

Source: https://s3.ap-south-
1.amazonaws.com/s3.studytonight.com/tutorials/uploads/ pictures/1608619676-
71449.png

The frame table is a data structure that keeps the information of which frames
are allocated or which frames are available and many more things. This table mainly
has one entry for each physical page frame.
26 | Memory and Virtual Management
Prepared by: JOY R. ADRIAS-MIT
Submitted to: DR. FRANCIS F. BALAHADIA
Source: https://s3.ap-south-
1.amazonaws.com/s3.studytonight.com/tutorials/uploads/ pictures/1608622355-
71449.png

The Operating system maintains a copy of the page table for each process in
the same way as it maintains the copy of the instruction counter and register
contents. Also, this copy is used to translate logical addresses to physical addresses
whenever the operating system maps a logical address to a physical address
manually.
This copy is also used by the CPU dispatcher in order to define the hardware
page table whenever a process is to be allocated to the CPU.

27 | Memory and Virtual Management


Prepared by: JOY R. ADRIAS-MIT
Submitted to: DR. FRANCIS F. BALAHADIA
Structure of Page Table in Operating Systems

The data structure that is used by the virtual memory system in the operating
system of a computer in order to store the mapping between physical and logical
addresses is commonly known as Page Table.

As we had already told you that the logical address that is generated by the CPU
is translated into the physical address with the help of the page table.

 Thus page table mainly provides the corresponding frame number (base address of
the frame) where that page is stored in the main memory.

The above diagram shows the paging model of Physical and logical memory.

Characteristics of the Page Table

Some of the characteristics of the Page Table are as follows:


 It is stored in the main memory.
 Generally; the Number of entries in the page table = the Number of Pages in
which the process is divided.
 PTBR means page table base register and it is basically used to hold the
base address for the page table of the current process.
 Each process has its own independent page table.

28 | Memory and Virtual Management


Prepared by: JOY R. ADRIAS-MIT
Submitted to: DR. FRANCIS F. BALAHADIA
Techniques used for Structuring the Page Table
Some of the common techniques that are used for structuring the Page table are as
follows:
1. Hierarchical Paging
2. Hashed Page Tables
3. Inverted Page Tables

1. Hierarchical Paging
Another name for Hierarchical Paging is multilevel paging.
 There might be a case where the page table is too big to fit in a contiguous
space, so we may have a hierarchy with several levels.
 In this type of Paging the logical address space is broke up into Multiple page
tables.
 Hierarchical Paging is one of the simplest techniques and for this purpose, a
two-level page table and three-level page table can be used.

Two Level Page Table

Consider a system having 32-bit logical address space and a page size of 1 KB and
it is further divided into:
 Page Number consisting of 22 bits.
 Page Offset consisting of 10 bits.

As we page the Page table, the page number is further divided into :
 Page Number consisting of 12 bits.
 Page Offset consisting of 10 bits.

Thus the Logical address is as follows:

In the above diagram,


P1 is an index into the Outer Page table.
P2 indicates the displacement within the page of the Inner page Table.
As address translation works from outer page table inward so is known as forward-
mapped Page Table.
Below given figure below shows the Address Translation scheme for a two-level
page table

29 | Memory and Virtual Management


Prepared by: JOY R. ADRIAS-MIT
Submitted to: DR. FRANCIS F. BALAHADIA
A Two-level page Table scheme
Source: chrome-
extension://efaidnbmnnnibpcajpcglclefindmkaj/https://os.ecci.ucr.ac.cr
/slides/Abraham-Silberschatz-Operating-System-Concepts-10th-2018.pdf

Three Level Page Table


For a system with 64-bit logical address space, a two-level paging scheme is not
appropriate. Let us suppose that the page size, in this case, is 4KB.If in this case, we
will use the two-page level scheme then the addresses will look like this:

30 | Memory and Virtual Management


Prepared by: JOY R. ADRIAS-MIT
Submitted to: DR. FRANCIS F. BALAHADIA
Thus in order to avoid such a large table, there is a solution and that is to divide the
outer page table, and then it will result in a Three-level page table:

2. Hashed Page Tables


This approach is used to handle address spaces that are larger than 32 bits.
 In this virtual page, the number is hashed into a page table.
 This Page table mainly contains a chain of elements hashing to the same
elements.
Each element mainly consists of :
1. The virtual page number
2. The value of the mapped page frame.
3. A pointer to the next element in the linked list.
Given below figure shows the address translation scheme of the Hashed Page
Table:

The above Figure shows Hashed Page Table


The Virtual Page numbers are compared in this chain searching for a match; if the
match is found then the corresponding physical frame is extracted.
In this scheme, a variation for 64-bit address space commonly uses clustered page
tables.

31 | Memory and Virtual Management


Prepared by: JOY R. ADRIAS-MIT
Submitted to: DR. FRANCIS F. BALAHADIA
Clustered Page Tables
 These are similar to hashed tables but here each entry refers to several
pages (that is 16) rather than 1.
 Mainly used for sparse address spaces where memory references are non-
contiguous and scattered

3. Inverted Page Tables


The Inverted Page table basically combines A page table and A frame table into a
single data structure.
 There is one entry for each virtual page number and a real page of memory
 And the entry mainly consists of the virtual address of the page stored in that
real memory location along with the information about the process that owns
the page.
 Though this technique decreases the memory that is needed to store each
page table; but it also increases the time that is needed to search the table
whenever a page reference occurs.

Given below figure shows the address translation scheme of the Inverted Page
Table:

In this, we need to keep the track of process id of each entry, because many
processes may have the same logical addresses.
Also, many entries can map into the same index in the page table after going
through the hash function. Thus chaining is used in order to handle this.

Example-of Paging

 Consider a process is divided into 4 pages P0, P1, P2 and P3.


 Depending upon the availability, these pages may be stored in the main memory
frames in a non-contiguous fashion as shown-

32 | Memory and Virtual Management


Prepared by: JOY R. ADRIAS-MIT
Submitted to: DR. FRANCIS F. BALAHADIA
Translating Logical Address into Physical Address-

 CPU always generates a logical address.


 A physical address is needed to access the main memory.

Following steps are followed to translate logical address into physical address-

Step-01:

CPU generates a logical address consisting of two parts-


1. Page Number
2. Page Offset

 Page Number specifies the specific page of the process from which CPU wants to
read the data.
 Page Offset specifies the specific word on the page that CPU wants to read.

Step-02:

For the page number generated by the CPU,


 Page Table provides the corresponding frame number (base address of the frame)
where that page is stored in the main memory.

Step-03:

 The frame number combined with the page offset forms the required physical address.

33 | Memory and Virtual Management


Prepared by: JOY R. ADRIAS-MIT
Submitted to: DR. FRANCIS F. BALAHADIA
 Frame number specifies the specific frame where the required page is stored.
 Page Offset specifies the specific word that has to be read from that page.

Diagram-

The following diagram illustrates the above steps of translating logical address into
physical address-

Demand Paging
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
34 | Memory and Virtual Management
Prepared by: JOY R. ADRIAS-MIT
Submitted to: DR. FRANCIS F. BALAHADIA
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.

While executing a program, if the program references a page which is not available in
the main memory because it was swapped out a little ago, the processor treats this
invalid memory reference as a page fault and transfers control from the program to
the operating system to demand the page back into the memory.
Advantages
Following are the advantages of Demand Paging −

 Large virtual memory.


 More efficient use of memory.
 There is no limit on degree of multiprogramming.
Disadvantages
 Number of tables and the amount of processor overhead for handling page
interrupts are greater than in the case of the simple paged management
techniques.

If you want to know more about Paging, Please watch YouTube link:
https://youtu.be/LKYKp_ZzlvM (Paging Os) https://youtu.be/pQxZxPFunTo
(PAGING IN OPERATING SYSTEM | OS | NON-CONTIGUOUS MEMORY
ALLOCATION)

35 | Memory and Virtual Management


Prepared by: JOY R. ADRIAS-MIT
Submitted to: DR. FRANCIS F. BALAHADIA
Page Replacement

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. Paging happens whenever a page fault occurs and a
free page cannot be used for allocation purpose accounting to reason that pages are
not available or the number of free pages is lower than required pages.
When the page that was selected for replacement and was paged out, is
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.
A page replacement algorithm looks at the limited information about accessing the
pages provided by hardware, and tries to select which pages should be replaced to
minimize the total number of page misses, while balancing it with the costs of primary
storage and processor time of the algorithm itself. There are many different page
replacement algorithms. We evaluate an algorithm by running it on a particular string
of memory reference and computing the number of page faults,
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. The latter choice produces a large number of data, where we
note two things.
 For a given page size, we need to consider only the page number, not the entire
address.
 If we have a reference to a page p, then any immediately following references
to page p will never cause a page fault. Page p will be in memory after the first
reference; the immediately following references will not fault.
 For example, consider the following sequence of addresses −
123,215,600,1234,76,96
 If page size is 100, then the reference string is 1,2,6,12,0,0

A. First In First Out (FIFO) algorithm


 Oldest page in main memory is the one which will be selected for replacement.
 Easy to implement, keep a list, replace pages from the tail and add new pages
at the head.

36 | Memory and Virtual Management


Prepared by: JOY R. ADRIAS-MIT
Submitted to: DR. FRANCIS F. BALAHADIA
Example FIFO: Consider page reference string 1, 3, 0, 3, 5, 6, 3 with 3 page
frames. Find the number of page faults.

Initially, all slots are empty, so when 1, 3, 0 came they are allocated to the
empty slots —> 3 Page Faults.
when 3 comes, it is already in memory so —> 0 Page Faults. Then 5 comes,
it is not available in memory so it replaces the oldest page slot i.e 1. —>1
Page Fault. 6 comes, it is also not available in memory so it replaces the
oldest page slot i.e 3 —>1 Page Fault. Finally, when 3 come it is not available
so it replaces 0 1 page fault.
Belady’s anomaly proves that it is possible to have more page faults when
increasing the number of page frames while using the First in First Out (FIFO)
page replacement algorithm. For example, if we consider reference strings
3, 2, 1, 0, 3, 2, 4, 3, 2, 1, 0, 4, and 3 slots, we get 9 total page faults, but if we
increase slots to 4, we get 10-page faults.
FIFO explaines in this video: https://www.youtube.com/watch?v=ET43MRKRuYM

B. Optimal Page algorithm


 An optimal page-replacement algorithm has the lowest page-fault rate of all
algorithms. An optimal page-replacement algorithm exists, and has been called
OPT or MIN.
 Replace the page that will not be used for the longest period of time. Use the
time when a page is to be used.

37 | Memory and Virtual Management


Prepared by: JOY R. ADRIAS-MIT
Submitted to: DR. FRANCIS F. BALAHADIA
Example OPT: Consider the page references 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2,
3 with 4 page frame. Find number of page fault.

Initially, all slots are empty, so when 7 0 1 2 are allocated to the empty slots —
> 4 Page faults
0 is already there so —> 0 Page fault. when 3 came it will take the place of 7
because it is not used for the longest duration of time in the future.—>1 Page
fault. 0 is already there so —> 0 Page fault. 4 will takes place of 1 —> 1
Page Fault.
Now for the further page reference string —> 0 Page fault because they are
already available in the memory.
Optimal page replacement is perfect, but not possible in practice as the
operating system cannot know future requests. The use of Optimal Page
replacement is to set up a benchmark so that other replacement algorithms
can be analyzed against it.
OPTIMAL Explain in this video:
https://www.youtube.com/watch?v=L8BEoRRUVRE

38 | Memory and Virtual Management


Prepared by: JOY R. ADRIAS-MIT
Submitted to: DR. FRANCIS F. BALAHADIA
C. Least Recently Used (LRU) algorithm
 Page which has not been used for the longest time in main memory is the one
which will be selected for replacement.
 Easy to implement, keep a list, replace pages by looking back into time.

Page Buffering algorithm


 To get a process start quickly, keep a pool of free frames.
 On page fault, select a page to be replaced.
 Write the new page in the frame of free pool, mark the page table and restart the
process.
 Now write the dirty page out of disk and place the frame holding replaced page in free
pool.

39 | Memory and Virtual Management


Prepared by: JOY R. ADRIAS-MIT
Submitted to: DR. FRANCIS F. BALAHADIA
Least frequently Used (LFU) algorithm
 The page with the smallest count is the one which will be selected for
replacement.
 This algorithm suffers from the situation in which a page is used heavily during
the initial phase of a process, but then is never used again.

Most frequently Used(MFU) algorithm


 This algorithm is based on the argument that the page with the smallest count
was probably just brought in and has yet to be used.

40 | Memory and Virtual Management


Prepared by: JOY R. ADRIAS-MIT
Submitted to: DR. FRANCIS F. BALAHADIA
Reference:

 Gaurav, Sushant: 2022: Segmentation in Operating System (OS): Retrieve


from https://www.scaler.com/topics/operating-system/segmentation-in-os/
 Gillis, Alexander S.: 2021: Virtual Memory: retrieve from
https://www.techtarget.com/searchstorage/definition/virtual-memory
 Madaan, Mohita:2022: Segmentation technique in Operating System: retrieve
from https://www.naukri.com/learning/articles/segmentation-technique-in-
operating-system/
 Neha T: 2021: Segmentation in OS: retrieve from
https://binaryterms.com/segmentation-in-os.html
 Phillips Gavin: 2022: Is Your Virtual Memory Too Low? Here's How to Fix It!:
Retrieve from https://www.makeuseof.com/tag/virtual-memory-low-heres-fix/
 Rubens, Paul: 2019: What Is Virtual Memory? How Does It Work?: retrieve from
https://www.enterprisestorageforum.com/hardware/virtual-memory/
 Silberschatz, A., Galvin, P. B., & Gagne, G. (2018). Operating System
Concepts. Copyright © 2018, 2013, 2012, 2008 John Wiley & Sons, Inc.
 Studytonight Technologies: 2022: Page Replacement Algorithms in Operating
Systems: Retrieve from https://www.studytonight.com/operating-
system/structure-of-page-table-in-operating-systems

41 | Memory and Virtual Management


Prepared by: JOY R. ADRIAS-MIT
Submitted to: DR. FRANCIS F. BALAHADIA

You might also like