You are on page 1of 20

Chapter 9 VM

Virtual Memory: Part 1


In this session…
• We will cover:
– Virtual memory terminology
– Hardware control structures
– Execution of a process
– Thrashing
– Paging and why its used
– Page tables

2
Virtual Memory
• Virtual Memory is a storage allocation scheme in which secondary memory
can addressed thought it were part of the RAM
• The size of virtual memory is only limited by the size of the addressing
scheme of the computer system and by the amount of secondary memory
available
• The addresses assigned to a location in virtual memory to allow that location
to be accessed as though it were part of main memory is called virtual
address

3
Virtual Memory Terminology

4
Execution of a Process
• Operating system brings into main memory a few pieces of the program
this includes the initial program piece and the initial data piece to which
those instructions refer
• The portion of a process that is actually in main memory at any time is
defined to be the resident set of the process
• As the process executes, things proceed smoothly as long as all
memory references are to locations that are in the resident set
• Using the segment or page table, the processor is always able to
determine whether this is so
• An interrupt is generated when an address is needed that is not in main
memory

5
Implications
• More processes may be maintained in main memory
– Because only some of the pieces of any particular process are loaded,
there is room for more processes
– This leads to more efficient utilisation of the processor because it is more
likely that at least one of the more numerous processes will be in a Ready
state at any particular time

6
Implications
• A process may be larger than all of main memory
– If no virtual memory, if the program being written is too large, the
programmer must devise ways to structure the program into pieces that
can be loaded separately in some sort of overlay strategy
– With virtual memory based on paging, that job is left to the OS and the
hardware
– The OS automatically loads pieces of a process into main memory as
required

7
Real and Virtual Memory

Real Virtual
Memory Memory

Main memory, the


Memory on disk
actual RAM

Allows for effective


multiprogramming
and relieves the user
of tight constraints of
main memory

8
Thrashing

A state in which To avoid this, the


the system operating system
spends most of tries to guess,
its time swapping based on recent
process pieces history, which
rather than pieces are least
executing likely to be used
instructions in the near future

9
Principle of Locality
• Program and data references within a process tend to cluster
• Only a few pieces of a process will be needed over a short period of time
• Therefore it is possible to make intelligent guesses about which pieces will be
needed in the future
• This helps to avoid thrashing

10
Support Needed for Virtual Memory

For virtual memory to be practical


and effective:
Operating system must
include software for
Hardware must support managing the movement
paging of pages between
secondary memory and
main memory

11
Paging
• The term virtual memory is usually associated with systems that employ
paging
• Each process has its own page table
– Each page table entry (PTE) contains the frame number of the corresponding page
in main memory
• A page table is needed for a virtual memory scheme based on paging

12
Paging
• Paging provides an alternative solution to compaction
• Although compaction solves external fragmentation it has a significant CPU
overhead
• Paging and segmentation are two ways which allow a processes physical
address space to be non-contiguous
• This enables us to place processes in spare address spaces regardless of
their location

13
Page Tables
• Page tables rely on two
important things: Memory Address

– Page size
RAM
– Frame size 0 Divided into equal size
(2MB)
• Page size and Frame size
Frames Size
will always be equal
• The blocks inside the RAM Page Size =
Frame Size
are referred to as frames
• Pages are block inside our
logical address space

14
Paging

Process 1 Logical RAM


0 Divided into equal size
0 Address Space Frame 0
Divided into equal size (2KB)
Page 0 (2KB) Frames Size Frame 1 Page Size = 2KB
Page Page 1 Frame Size = 2KB
s Frame 2
Page 2
Frame 3
Page 3 Pages are Placed in Each Frame
Frame 4
Page 4
Frame 5
Page 5
...
...

15
Paging
This is difficult for the CPU as
pages are in non-contiguous
order
RAM
0 Processes in non-
Page 0 (P1) contiguous allocattion
Process 1 Logical
Occupied
0 Address Space
Page 0 Page 1 (P1)
Page 1 Occupied
Page 2 Page 2 (P1)
Page 3 Ocupied
Page 3 (P1)

16
Page Table
• With a page table we can store the mapping between virtual addresses and
physical addresses
• For every process we maintain a page table
RAM
Process 1 Logical
Address Space 0 Page 0 (P1)
0
Page 0 1 Occupied
Page 1 2 Page 1 (P1)
Page 2 3 Occupied
Page 3 4 Page 2 (P1)
5 Ocupied

6 Page 3 (P1)

Page Table of P1

Number of entries in the 0 Frame 0


page table will be equal to
1 Frame 2
the number of pages for Page table is placed inside the RAM
process P1 2 Frame 4
3 Frame 6 19
Paging Model of Logical and Physical Memory

The MMU Keeps track of all free frames,


for example, to run a program of size N
pages, need to find N free frames (may
not be contiguous) and load program

Page # Frame #
Paging Example
Summary
• In this session we have covered:
– Virtual memory terminology
– Hardware control structures
– Execution of a process
– Thrashing
– Paging and why its used
– Page tables

20

You might also like