You are on page 1of 44

VIRTUAL MEMORY

MANAGEMENT
OPERATING SYSTEM
Virtual Memory
 The main idea behind virtual memory is
to allow a combination of program, stack
and data to run on a smaller memory by
carefully choosing which part of the
process is to be placed in the physical
memory.
CONCEPT OF LOCALITY
 The principle of locality states that
program and data references within a
process tend to cluster.

 Clustering provides the system a good


chance of hitting or accessing the
needed page accurately and
immediately.
Two types of Locality
 Temporal Locality - simply suggest that
if a particular part was accessed before
then it could most likely be accessed
again.
 Spatial Locality - it suggest that
location near a region that was
accessed are candidates for access in
the near future.
Examples
 Temporal Locality
A[1], A[2000], A[1], A[1], A[2000], A[30], A[30],
A[2000], A[30], A[2000], A[30], A[4], A[4]

 Spatial Locality
 A[0][1], A[0][2], A[0][3]
 A[1], A[2], A[3]
PAGING
The idea is to divide the memory into
frames where programs can reside. The
programs, on the other hand, are divided
into equal sized pages. In this technique,
logical address is divided into a Page
Number and a Page Offset.
Conversion Process
Paging
 Partition memory into small equal fixed-
size chunks and divide each process into
the same size chunks
 The chunks of a process are called pages
and chunks of memory are called frames
 Operating system maintains a page table
for each process
Contains the frame location for each page in
the process
Memory address consist of a page number
and offset within the page

15
Paging Formula
p = U div P Where:
P = page size
d = U mod P V = physical address
U = logical address
U=p*P+d p = page number
f = frame number
V=f*P+d d = displacement
Paging Example
Page # Frame #

19
Paging Example
Page # Frame #

20
Paging Example
Page # Frame #

Page size = 4 bytes


Physical memory = 32 bytes
=> 32/4 = 8 partitions
21
Paging Example
Page 0
Frame 0
Page # Frame #
Page 1 Frame 1

Page 2 Frame 2

Page 3 Frame 3

Frame 4

Physical = frame # x Page size + Page offset Frame 5


address
Frame 6

Page size = 4 bytes Frame 7


Physical memory = 32 bytes
=> 32/4 = 8 partitions
22
Segmentation
 It is a technique similar to paging. However,
this memory management scheme looks into
program as segment and not pages. Each
segment has its own length and name. There
is a great difference since pages, like in a
book, are equal sizes whereas segment, like
the length of hair on a head, very
tremendously in sizes.
Segmentation
 Segment Table is used to index the base
and limit register essential in mapping
the logical address to the physical
memory.
 Limit (length) is the size of segment
 Base starting address of segment
Segmentation
 All segments of all programs do not
have to be of the same length
 There is a maximum segment length
 Addressing consist of two parts - a
segment number and an offset
 Since segments are not equal,
segmentation is similar to dynamic
partitioning
User’s View of a Program

35
Logical View of Segmentation
1

1 4

3
4
2

user space physical memory space


36
Example of Segmentation

37
Page entry is invalid; Page is not available in memory
(RAM)
Page fault occurs.

In case of page fault, Page will be loaded in memory


If virtual page found in page table, it is known as
Page hit.

If page is already available or found in Ram, it is Page hit.


When pages are shifted to and from main memory and
secondary storage excessively, Thrashing occurs.
Thrashing is a phenomenon in virtual memory schemes, in
which the processor uses most of its time performing
swapping instead of executing instructions.
Thrashing
Thrashing
To address the fetching of desired page and
swapping out a page already in memory, to
make room for the requested page, several
page replacement algorithms have been
developed.
Page Replacement Algorithms (cont)
•First In First Out Algorithm: when a new page must be
brought in, replace the page that has been in memory the
longest. Seldom used: even though a page has been in
memory a long time, it may still be needed frequently.
•Second Chance Algorithm: this is a modification of
FIFO. The Referenced bit of the page that has been in
memory longest is checked before that page is
automatically replaced. If the R bit has been set to 1, that
page must have been referenced during the previous
clock cycle. That page is placed at the rear of the list and
its R bit is reset to zero. A variation of this algorithm, the
‘clock’ algorithm keeps a pointer to the oldest page using
a circular list. This saves the time used in the Second
Chance Algorithm moving pages in the list
Page Replacement Algorithms (cont)
•Least Recently Used Algorithm (LRU) - keep track of each
memory reference made to each page by some sort of counter or
table. Choose a page that has been unused for a long time to be
replaced. This requires a great deal of overhead and/or special
hardware and is not used in practice. It is simulated by similar
algorithms:
•Not Frequently Used - keeps a counter for each page and at
each clock interrupt, if the R bit for that page is 1, the counter is
incremented. The page with the smallest counter is chosen for
replacement. What is the problem with this?
A page with a high counter may have been referenced a lot in one
phase of the process, but is no longer used. This page will be
overlooked, while another page with a lower counter but still being
used is replaced.
Page Replacement Algorithms (cont)

Aging -a modification of NFU that simulates LRU very well.


The counters are shifted right 1 bit before the R bit is added in.
Also, the R bit is added to the leftmost rather than the rightmost
bit. When a page fault occurs, the page with the lowest counter
is still the page chosen to be removed. However, a page that
has not been referenced for a while will not be chosen. It would
have many leading zeros, making its counter value smaller than
a page that was recently referenced.
How is a page fault actually handled?
1. Trap to the operating system ( also called page
fault interrupt).
2. Save the user registers and process state; i.e.
process goes into waiting state.
3. Determine that the interrupt was a page fault.
4. Check that the page reference was legal and, if
so, determine the location of the page on the disk.
5. Issue a read from the disk to a free frame and wait
in a queue for this device until the read request is
serviced. After the device seek completes, the disk
controller begins the transfer of the page to the
frame.
6. While waiting, allocate the CPU to some other
user.
7. Interrupt from the disk occurs when the I/O is
complete. Must determine that the interrupt was
from the disk.
8. Correct the page table /other tables to show
that the desired page is now in memory.
9. Take process out of waiting queue and put in
ready queue to wait for the CPU again.
10. Restore the user registers, process state
and new page table, then resume the interrupted
instruction.

You might also like