You are on page 1of 9

Introduction Virtual Memory Introduction 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:

The programming task is simplified by not being con-


programs can be larger than physical memory, strained by the physical memory.

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.

However, it is complex to implement and can dramati-


cally decrease performance if it is used carelessly.
1 2

Introduction Virtual Memory Demand Paging Virtual Memory

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

Secondary Storage Demand-paging is a lazy “swapper” which brings into


Physical Memory

 

 

 

 

 

 

  memory the pages of the process that are needed.


   
    
    
    
    
    
   The pager is concerned with the manipulation of the
Memory Map

 

 
  
  
  
  
  
  
  
  
  
   pages to and from disk.


  
  
  
    
    
    
    
  

  
  
  
  
  
 
 


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.

This requires some hardware support to distinguish


between pages that are in memory and those that are Logical Address Space

on disk. A valid-invalid bit in the page table may be


    
     
Memory
used to achieve this. If a page is invalid then either it
 

 

 

 


0
is on disk or the page is not within the logical address 1

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

Read Only/Read Write Bit

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.

This generally involves restarting the instruction fetch-


Given  
is the memory access time. Let be the
probability of a page fault occurring. The effective ac-
execute cycle.
cess time is then:
effective access time    

 page fault time
Problems arise when one instruction modifies several
different locations. Examples of this are:

E XAMPLE . Suppose the average page fault service


IBM System 360/370 – instruction that can move
time is 20 milliseconds and a memory access time
up to 256 bytes.
is 200 nanoseconds. Suppose we wish to have less
than 10% degradation what must the page fault rate
PDP-11 – auto-increment and auto-decrement ad- be less than?
dressing modes.

9 10

Swap Space Virtual Memory Page Replacement Virtual Memory

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:

When a process is started, one approach is to copy


the entire program into the swap space and then to Find the location of the desired page on disk.
demand page the pages from the swap space.
Either find a free frame or create a free frame by:
Another approach is to page the program directly form selecting a victim frame, writing it to disk, and up-
the file system the first time a page is used, and then dating the page and frame tables.
to use the swap space for the following page-faults.

Read the desired page into the free frame.

Restart the process.

11 12
Page Replacement Virtual Memory Demand Paging Virtual Memory

Page replacement requires two page transfers. Demand paging requires a:

One approach to reduce this overhead is by using a


frame-allocation algorithm — How many frames
dirty bit in the page table. When a page is modified
do we allocate to each process?
the dirty bit is set. Suppose a page fault occurs and
a victim is selected. If the dirty bit of the victim is set
then the service routine must write this page to disk, page-replacement algorithm — How do we select
however, if the bit is not set the there is no need to the victim to be replaced?
write the page to disk as there already exists the iden-
tical page on disk.

13 14

Page-Replacement Algorithms Virtual Memory Page-Replacement Algorithms Virtual Memory

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,

Given the number of available frames and the page re-


placement algorithm used we can determine the num-
ber of page faults.

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.

The performance of FIFO is generally poor, as, heav-


ily used frames become old and are paged out, only
to be page immediately back in.

Frames
17 18

FIFO Algorithm Virtual Memory FIFO Algorithm Virtual Memory

Consider the following reference string with 3 frames


available:
FIFO suffers from Belady’s anomaly where an increase
1 3 1 2 4 1 4 3 4 4 5 6 4 in the number of available frames may in fact increase
the number of page faults.

Consider the below reference string with either three


or four frames available:

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

The optimal algorithm, like SJF algorithm for CPU schedul-


ing, is impossible to implement as it requires the future
knowledge of the reference string.

21 22

LRU Algorithm Virtual Memory LRU Algorithm Virtual Memory

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.

The LRU keeps track of how recently a page has been


used and keeping the pages that have been recently
used. LRU selects the page that reference is the most
distant past.

Note that, the LRU is identical to running OPT on the


reference string in reverse order.

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 Virtual Memory Reference Bit History Virtual Memory

Physical Address Space


Frame
The reference bit may be sampled at regular intervals
Logical Address Space Number
and shifted into a register. A timer interrupt may be
P2 A
Process A Page Table A 0 used to undertake this task regularly.
1
P0 0 3 v 1
2 eg A page with a history register 01100111 was not
P1 1 i
accessed in the previous time interval, however is was
2 0 v 0 3 P0 A
P2 access in the time interval before that, etc
3 10 v 1 4
P3
5 These history registers may be compared to select a
Frame
6 victim. Thus approximating the LRU algorithm.
Valid-Invalid

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

Enhanced Second-Chance Algorithm Virtual Page Buffering Algorithms Virtual Memory


Memory

A pool of free frames may be maintain. When a page


If the victim has not been modified then it is not re- fault occurs, a victim is chosen as before. However,
quired to be copied to disk. This makes these victims the required page is read into a free frame from the
more attractive as the required page can be obtained pool of free frames. The process may continue once
in half the time. this is complete without waiting for the victim to be
written out to the swap space. The victim may be
This gives rise to the enhanced second-chance algo- copied to the swap space at the pagers leisure and
rithm which selects unmodified pages over modified it frame will form part of the pool of free frames.
pages.
Frames Frames

Swap Space Swap Space

2
1

Victim Victim

Free Frame
1

31 32
Allocation of Frames Virtual Memory Allocation of Frames Virtual Memory

Frame allocation to processes may be either:


There exists a minimum number of frames that must
be allocated to each process. this number is depen-
dent on the instruction set, as, one instruction may global, or
require access to several memory locations.
local.

A variety of approaches may be used when locally


allocating the number of frame to give to a process.
These include:

equal allocation,

proportional allocation (size of the process), or

proportional allocation (priority).

33 34

Thrashing Virtual Memory Program Structure Virtual Memory

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

You might also like