M4 Virtual Memory PDF
M4 Virtual Memory PDF
SYSTEM
MODULE 4
VIRTUAL MEMORY
Upon completion of this module, you will be able to:
• Explain virtual memory and its benefits;
• Discuss the concept of demand paging and page replacement
algorithms;
• Apply different page-replacement algorithms such as First-In
First-Out (FIFO), Optimal Algorithm, and Least Recently Used
(LRU) Algorithm in solving demand paging problems;
The various memory management strategies that are used in
computer systems have the same goal: to keep many
processes in memory simultaneously to allow
multiprogramming.
virtual
memory
A demand-paging system 0
1
is similar to a paging system valid-invalid
bit
2
0 A 3
with swapping. However, 1 B 0
1
4 v
i 4 A
instead of swapping the 2 C 2
3
4
6 v
i
i
5
A B
3 D 5 9 v 6 C
entire process into memory, 4 E 6
7
i
i 7 C D E
physical
memory
• There is an additional bit in the page table which is the valid-
invalid bit.
• This bit is set to valid to indicate that a corresponding page is
in memory.
• This bit is set to invalid to indicate that the corresponding
page is in secondary storage.
• If a process tries to use a page that is not in physical memory,
then a page-fault will occur. This will cause a trap to the
operating system indicating an invalid address error.
• In the extreme case, the system could start executing a process
with no pages in memory. The process would immediately fault
for the page with the first instruction.
• After the first page is brought into memory, the process would
continue to execute, faulting as necessary until every page that
it needed was actually in memory.
• This is pure demand paging: never bring a page into memory
until it is required.
• The principle of locality of reference ensures that programs
do not access a new page of memory with each instruction
execution.
• The effectiveness of the demand paging is based on a
property of computer programs called the locality of reference.
• Analysis of programs shows that most of their execution time
is spent on routines in which many instructions are executed
repeatedly.
• These instructions may constitute a simple loop, nested loops,
or a few procedures or functions that repeatedly call each other.
• It is important to keep the page-fault rate low in a demand-
paging system.
• Otherwise, the effective access time increases, slowing down
process execution dramatically.
• A problem occurs if there is a need to transfer a page from disk
to memory but there is no memory space available (there are no
free frames). In other words, memory is over-allocated.
• The operating system has several options at this point:
• It could terminate the user process. However, demand
paging is the operating system’s attempt to improve the
computer system’s utilization and throughput.
• Users should not be aware their processes are running on a
paged system – paging should be logically transparent to the
user.
• Another possibility is page replacement. In this scheme, the
operating system removes or replaces one of the existing
pages in memory to give way for the incoming page.
• A page replacement algorithm is necessary to select which
among the pages currently residing in memory will be replaced.
• Page replacement takes the following approach:
• If no frame is free, the system finds one that is currently
being used and frees it.
• Freeing a frame means transferring its contents to the disk
and changing the page table (and all other tables) to
indicate that the page is no longer in memory.
1 swap out
2
f 0 v i change to victim page
invalid
f victim
f v 4 reset page
table for new
page
3 swap desired
page table page in
physical
memory
The page fault service routine is now modified to include page
replacement:
1. Find the location of the desired page on the disk.
2. Find a free frame:
3. If there is a free frame, use it.
4. Otherwise, use a page-replacement algorithm to select a victim
frame.
5. Write the victim page to the disk; change the page and frame
tables accordingly.
6. Read the desired page into the (newly) free frame; change the
page and frame tables.
7. Restart the user process.
• Notice that, if no frames are free, two page transfers (one out and
one in) are required. This situation effectively doubles the page-
fault service time and will increase the effective access time
accordingly.
• To reduce this overhead, a modify or dirty bit is necessary for
each page or frame.
• The modify bit for a page is set whenever any word or byte is
written into, indicating that the page has been modified.
• It is no longer necessary to swap out pages whose modify bit
is 0 (there was no modification). An incoming page may simply
overwrite an unchanged page.
• There are two major problems in the implementation of demand
paging:
• Frame Allocation
How many frames will the operating system allocate to a
process, particularly if there are multiple processes?
• Page Replacement
How will the operating system select pages that are to be
removed from memory to give way for incoming pages?
• Page replacement algorithms are the techniques using which
an OS decides which memory pages to swap out, write to disk
when a page of memory needs to be allocated.
• A good page-replacement algorithm is one with a low page-
fault rate.
• An algorithm is evaluated by running it on a particular string of
memory references and computing the number of page faults.
The string of memory references is called the reference string.
• Common Page-replacement algorithms or techniques are First-
In, First-Out (FIFO), Optimal, and Least Recently Used (LRU)
Page Algorithm.
First-In First-Out (FIFO) Algorithm
This is the simplest page-replacement algorithm. When a
page must be replaced, the oldest page is chosen.
Example:
Assume that there are three frames in memory and that the
following is the page reference string:
7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 1, 2, 0, 1, 7, 0, 1
First-In First-Out (FIFO) Algorithm
14
1 2 3 4 5 6
number of frames
Optimal Algorithm
• This algorithm has the lowest page-fault rate of all algorithms.
• In an optimal algorithm, the page that will be replaced is the one
that will not be used for the longest period of time.
Page Hit
There are 12 page faults.
Least Recently Used (LRU) Algorithm
• The LRU algorithm produced 12 page faults. Although it is not
as good as the 9 of the optimal algorithm (in fact, no other
algorithm will produce less than 9 page faults for this example), it
is much better than the 15 of the FIFO algorithm.
• The LRU policy is often used as a page replacement algorithm
and is considered to be quite good. The major problem is how to
implement LRU replacement.
• Siberschatz, A. (2018). Operating System Concepts,
Wiley.
• Tomsho, G. (2019). Guide to Operating Systems,
Cengage Learning.