3 - Memory Management (Virtual)
3 - Memory Management (Virtual)
Memory
WXES1110 (3) 1
Introduction
● 4 memory allocation schemes:
● Paged allocation
● Demand paging allocation
● Segmented allocation
● Segmented/demand paged allocation
● Remove the restriction of storing programs
contiguously.
● Eliminate the requirement that the entire
program reside in memory during execution.
WXES1110 (3) 2
Paged Memory Allocation (1)
● An incoming job is divided into pages of
equal size.
● Sector/block: a section of a disk.
● Page frame: a section of main memory.
● For efficiency, pages, sectors and page
frames are of the same size.
● One sector holds one page of job instruction
and fits into one page frame of memory.
WXES1110 (3) 3
Paged Memory Allocation (2)
● Before executing a job, the Memory Mgr:
● Determines the number of pages;
● Locates enough empty page frames in main
memory;
● Loads the pages (not necessarily
contiguously).
WXES1110 (3) 4
Paged Memory Allocation (3)
● Disadvantage: Memory Mgr must keep track
of pages of a program – increases size and
complexity of OS.
● Memory Mgr uses three tables to keep
track of the pages:
● Job table (JT);
● Page map table (PMT);
● Memory map table (MMT).
WXES1110 (3) 5
WXES1110 (3) 6
Paged Memory Allocation (4)
WXES1110 (3) 7
Paged Memory Allocation (5)
0 8
1 10
2 5
3 11
WXES1110 (3) 8
WXES1110 (3) 9
Demand Paging (1)
● Demand paging loads only a part of the
program into memory for processing.
● A job is divided into equally sized pages
that initially reside in secondary storage.
● When the jobs run, its pages are brought
into memory as needed.
● Allows a user to run jobs with less main
memory.
WXES1110 (3) 10
Demand Paging (2)
● Requires high-speed direct access storage
device that can work directly with the CPU.
● Important because pages must be passed
quickly from secondary storage to main
memory and back again.
● How and when pages are swapped depends
on pre-defined policies that determine
when to make room for needed pages and
how to do so.
WXES1110 (3) 11
WXES1110 (3) 12
Page Replacement Policies & Concepts
● The page replacement policy is crucial to
the efficiency of the system.
● 2 popular policies:
● First-in first-out (FIFO);
● Least-recently-used (LRU).
WXES1110 (3) 13
FIFO (1)
WXES1110 (3) 14
FIFO (2)
WXES1110 (3) 15
LRU (1)
WXES1110 (3) 16
LRU (2)
● A variation of LRU is the clock page
replacement policy.
● It is implemented as a circular queue and uses
a pointer to step through the reference bits of
the active pages, simulating a clockwise motion.
● A page is selected for replacement if its
reference bit is 0.
● If its reference bit is 1, the page is given a
second chance and its reference bit is set to 0.
WXES1110 (3) 17
LRU (3)
WXES1110 (3) 18
LRU (4)
● Another variation of LRU uses a 8-bit
reference and a bit shifting technique to
track the usage of pages in memory.
● When a page is first copied into memory, the
leftmost bit is set to 1; remaining bits are
set to 0.
● At each time tick, the Memory Mgr shifts
every page’s reference byte to the right by
one.
WXES1110 (3) 19
LRU (5)
● Each time a page is referenced, the
leftmost bit is set to 1.
● The shifting and setting the leftmost bit
when a page is referenced gives a history
of page usage.
WXES1110 (3) 20
LRU(6)
WXES1110 (3) 21
The Mechanics of Paging (1)
● The Memory Mgr needs specific info about
each page in memory in order to swap pages
out.
WXES1110 (3) 22
The Mechanics of Paging (2)
WXES1110 (3) 23
The Mechanics of Paging (3)
WXES1110 (3) 25
Working Set (2)
● 2 issues:
● How many pages comprise the working set?
● What is the max number of pages the OS
allow for a working set?
● Solution: begin by identifying each job’s
working set and then loading them into
memory in its entirety before allowing
execution to begin.
WXES1110 (3) 26
Working Set (3)
WXES1110 (3) 27
Working Set (4)
● In time-sharing systems, the OS must keep
track of the size and identity of each
working set and makes sure jobs for
processing do not exceed available memory.
● Some OSs use a variable working set size
and increase/decrease it as necessary.
WXES1110 (3) 28
Advantages of Demand Paging
● It introduced the concept of virtual
memory, and hence, jobs are no longer
constrained by the size of the physical
memory.
● Memory is utilized more efficiently because
sections of a job that are seldom used or
not at all are not loaded into memory unless
they are requested.
WXES1110 (3) 29
Disadvantages of Demand Paging
● Increased overhead caused by the tables
and page interrupts.
WXES1110 (3) 30
Segmented Memory Allocation (1)
● Each job is divided into segments of
different sizes – one for each module that
contains pieces that perform related
functions.
● This approach reduces page faults that
resulted from having a segment’s loop split
over two or more pages.
● Main memory is not divided into page frames
– memory is allocated dynamically.
WXES1110 (3) 31
Segmented Memory Allocation (2)
WXES1110 (3) 32
Segmented Memory Allocation (3)
● The Memory Mgr keeps track of segments
in the memory using three tables:
● Job Table
● Segment Map Table
● Memory Map Table
WXES1110 (3) 33
Segmented Memory Allocation (4)
Segment Map Table
WXES1110 (3) 34
Segmented Memory Allocation (5)
WXES1110 (3) 35
Segmented Memory Allocation (6)
● What is the difference between paging and
segmentation?
● Pages are physical units that are invisible to
the user’s program and of fixed sizes.
● Segments are logical units that are visible to
the user’s program and of variable sizes.
WXES1110 (3) 36
Segmented/Demand Paged Memory Allocation (1)
● Requires 4 tables:
● Job Table
● Segment Map Table
● Page Map Table
● Memory Map Table
WXES1110 (3) 38
WXES1110 (3) 39
Segmented/Demand Paged Memory Allocation (3)
● Major disadvantages:
● The overhead required for the extra tables;
● The time required to reference the SMT
and PMT.
● Associative memory is used to minimize the
number of references and speed up the
process.
WXES1110 (3) 40
Segmented/Demand Paged Memory Allocation (4)
WXES1110 (3) 42
Segmented/Demand Paged Memory Allocation (6)
WXES1110 (3) 43
Segmented/Demand Paged Memory Allocation (7)
WXES1110 (3) 44
Segmented/Demand Paged Memory Allocation (8)
● Example:
● A system with 8 associative registers per job
will use them to store the SMT and PMT for the
last eight pages referenced by that job.
● When an address needs to be translated from
segment and page numbers to a memory location,
the system look first in the 8 associative
registers.
● If a match is found, the memory location is
taken from the associative register. Otherwise,
the search in PMT and SMT continues.
WXES1110 (3) 46
Virtual Memory (1)
● Virtual memory makes it possible to swap
pages between main memory and secondary
storage.
● It gives users the impression that their
entire program is loaded in main memory
when only a portion of it is actually loaded.
● Prior to virtual memory, programmers are
responsible for making sure programs fits
into available memory.
WXES1110 (3) 47
Virtual Memory (2)
Virtual Memory with Paging Virtual Memory with
Segmentation
Allows internal fragmentation Doesn’t allow internal
within page frame. fragmentation.
Doesn’t allow external Allows external fragmentation.
fragmentation.
Programs are divided into equal- Programs are divided into
sized pages. unequal-sized segments.
Absolute address calculated using Absolute address calculated
page number and displacement. using segment number and
displacement.
Requires PMT. Requires SMT.
WXES1110 (3) 48
Virtual Memory (3)
● Segmentation allows users to share
program code.
● A shared segment contains:
● An area where unchangeable code (reentrant
code) is stored;
● Several data areas, one for each user.
WXES1110 (3) 49
Virtual Memory (4)
● Before virtual memory, sharing involves
storing copies of files in each user’s
account.
● Users load their own copy and work on it at
any time.
● Creates a great deal of unnecessary cost –
overhead of loading the copies and the extra
secondary storage needed.
WXES1110 (3) 50
Virtual Memory (5)
● Using virtual memory involves cooperation
btwn:
● Memory Mgr (tracks each page/segment),
and
● Processor hardware (issues the interrupt
and resolves the virtual address).
WXES1110 (3) 51
Virtual Memory (6)
● Advantages of virtual memory mgmt:
● A job’s size is not restricted to the size of
main memory.
● Memory is used more efficiently because
only sections of a job that are immediately
needed are stored in memory.
● Allows an unlimited amount of
multiprogramming.
WXES1110 (3) 52
Virtual Memory (7)
● Advantages of virtual memory mgmt:
● Eliminates external fragmentation and
minimizes internal fragmentation by
combining segmentation and paging.
● Allows sharing of code and data.
● Facilitates dynamic linking of program
segments.
WXES1110 (3) 53