Virtual Memory Virtual Memory
Virtual Memory Virtual Memory
Virtual memory is a technique that permits processes Processes often only use a small part of their text.
to be executed even when they are not completely in
Also data structures, such as arrays, lists, and tables,
memory.
are often allocated more memory than they actually
use.
This has many advantages include:
virtual memory abstracts main memory into an Ovarlays have basically disappeared on systems which
extremely large logical storage area, and support virtual memory.
virtual memory increases the degree of Virtual memory should be transparent the the user.
multi-programming.
The diagram below shows virtual memory larger than Swapping is an approach that involve bringing an en-
physical memory. tire process from disk into main memory so it may be
executed.
Virtual Memory
3 4
Hardware Support Virtual Memory Demand-Paging Virtual Memory
If a logical address is access that is not in memory A page fault occurs when an invalid page is addressed.
then the pager must bring this page into memory.
space. 2
Page Table
3
0 5 RW v 4
1 RO i 5
2 8 RO v 6
3 i 7
Valid/Invalid Bit 8
5 6
Steps in dealing with a page fault Virtual Memory Pure demand paging Virtual Memory
Kernal
Error - Address outside logical address space.
One approach to paging is to only bring pages into
Logical Memory
Of Process A (dispatch another process)
memory when they are needed. This is call Pure de-
Invalid Get Page from disk
Page
Page Table
mand paging.
Reference
Trap
i Physical Memory
The very first instruction would cause a page fault.
Restart Instruction
Copy page into free frame
Update page table
7 8
Hardware constraints Virtual Memory Performance Virtual Memory
A page-fault may occur in the middle of an instruction. Demand paging can significantly decrease the perfor-
Paging requires that the process is restarted so it is in mance of a computer system by greatly increasing the
exactly the same state before the page fault occurred. effective access time of memory.
This imposes some architectural constraints.
9 10
Demand paging must manage the swap space. The Once the main memory fills up a page must be swapped
swap space is generally faster than the file system, out to make room for any pages to be swapped in.
This is know as page replacement.
as file lookups and indirect allocation methods are not
used.
The page-fault service routine will:
11 12
Page Replacement Virtual Memory Demand Paging Virtual Memory
13 14
The goal of the page-replacement algorithm is to min- For example suppose the pages are 1000 bytes each.
imise the page-fault rate. And if we trace a particular process it may reference
the following logical addresses:
Different algorithms may be compared by computing
the number of page faults on a particular reference 02001, 03234, 02002, 04345, 03345, 02998, 03987,
string. 07111, 02000, 02001,
Given the overhead of a page fault, small improve- This would reduce to the following reference string:
ments in the page replacement algorithm will greatly
improve the performance of the entire system. 2, 3, 2, 4, 3, 2, 3, 7, 2, 2,
15 16
Page-Replacement Algorithms Virtual Memory FIFO Algorithm Virtual Memory
Generally, increasing the number of frames reduces The FIFO is the simplest page-replacement algorithm.
the number of page faults. When a page fault occurs and the page frames are full
a victim must be selected. The FIFO algorithm selects
Page Faults the oldest frame(This is the frame that has been in
memory the longest.). This page is swapped out and
the required page is swapped into its location.
Frames
17 18
1 2 3 4 1 2 5 1 2 3 4 5
1 2 3 4 1 2 5 1 2 3 4 5
19 20
Optimal Algorithm Virtual Memory Optimal Algorithm Virtual Memory
An optimal page-replacement algorithm exists and called Consider the following reference string with 3 frames
is OPT or MIN. The approach is to simply replace the available:
page that will not be used for the longest period of
time. 1 3 1 2 4 1 4 3 4 4 5 6 4
21 22
The page references that occurred in the recent past Consider the following reference string with 3 frames
are good indicators of what page references will occur available:
in the future. That is if a page has just been reference
it is likely that it will be referenced again. This gives 1 3 1 2 4 1 4 3 4 4 5 6 4
rise to the least recently used (LRU) algorithm.
23 24
LRU Algorithm Virtual Memory LRU Approximation Algorithms Virtual Memory
Two implementations are feasible: Most architectures help the operating system approx-
imate the LRU algorithm by providing a reference bit.
The reference bit associated with pages and is lo-
counters, or
cated in each entry in the page table.
stack. Initially the operating system will set the reference bits
to 0. When a page is referenced the corresponding
reference bit is set to 1. This is all done in hardware.
Although it is possible to implement LRU it is imprac- After a period of time the operating system can exam-
tical due to the amount of work that needs to be done ine all the reference bit and determine which pages
for each memory reference. have been read from or written to in that period of
time. Note that, the order and frequency that pages
have been referenced is unknown.
25 26
Reference Bit 7
10 P3 A
11
27 28
Second-Chance Algorithm Virtual Memory Second-Chance Algorithm Virtual Memory
1
The second-chance page replacement algorithm is a 1
FIFO replacement algorithm where referenced pages 1
are given a second chance.
0
A circular queue may be used to implement the second- 1
chance algorithm. When a victim is required the pointer
advances around the queue until it finds a page with a
0 reference bit. As the pointer advances the reference 1
0
bits are set to zero. Pointer
If all bits are set then the second change is the same
1
as the FIFO replacement algorithm. 0 Page
Reference Bit
29 30
2
1
Victim Victim
Free Frame
1
31 32
Allocation of Frames Virtual Memory Allocation of Frames Virtual Memory
equal allocation,
33 34
A process will have a number of frames in active use You spend weeks writing the following program and
(working set). If a process does not have at least this for some reason it is very slow. What is the problem?
number of frames allocated to it then the process will How could you fix it? (Assuming pages are 2K and
be constantly paging. This is known as thrashing. integer are 4 bytes.)
Generally the problem is fixed by reducing the degree #define SIZE 512
of multiprogramming.
int data[SIZE][SIZE];
CPU utilization
thrashing
for (j = 0; j < SIZE; j++) {
for (k = 0; k < SIZE; k++) {
A[k][j] = 0;
}
}
degree of multiprogramming
35 36