0% found this document useful (0 votes)
9 views53 pages

3 - Memory Management (Virtual)

The document discusses various memory management techniques, including paged allocation, demand paging, segmented allocation, and segmented/demand paged allocation, emphasizing the removal of contiguous storage restrictions and the introduction of virtual memory. It details the mechanics of paging, page replacement policies, and the concept of working sets, highlighting their advantages and disadvantages. Additionally, it explains how virtual memory allows efficient memory usage and sharing of code while minimizing fragmentation.

Uploaded by

huixin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views53 pages

3 - Memory Management (Virtual)

The document discusses various memory management techniques, including paged allocation, demand paging, segmented allocation, and segmented/demand paged allocation, emphasizing the removal of contiguous storage restrictions and the introduction of virtual memory. It details the mechanics of paging, page replacement policies, and the concept of working sets, highlighting their advantages and disadvantages. Additionally, it explains how virtual memory allows efficient memory usage and sharing of code while minimizing fragmentation.

Uploaded by

huixin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 53

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)

Page Map Table (PMT)


Job Page # Page Frame #

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)

Page Map Table for Job 1 (Slide 12)


Page Status Reference Modified Page
Bit
Bit Bit Frame
0 1 1 1 5
1 1 0 0 9
2 1 0 0 7
3 1 1 0 12

WXES1110 (3) 23
The Mechanics of Paging (3)

Modified Referenced Meaning


Case 1 0 0 Not modified AND
not referenced.
Case 2 0 1 Not modified BUT
was referenced.
Case 3 1 0 Was modified BUT
not referenced.
Case 4 1 1 Was modified AND
was referenced.
WXES1110 (3) 24
Working Set (1)
● A failure to find a page in memory is called a
page fault.
● The working set of a job is the set of pages
residing in memory that can be accessed
directly without incurring a page fault.
● It would be convenient if we could load all
the pages of a job’s working set to minimize
page faults and to speed up processing.

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)

● Is a combination of segmentation and demand


paging.
● Offers the logical benefits of segmentation
and the physical benefits of paging.
● A segment is sub-divided into pages of equal
size, smaller than most segment – easier to
manipulate.
● Overcomes the problems of segmentations
such as compaction, external fragmentation
and secondary storage handling.
WXES1110 (3) 37
Segmented/Demand Paged Memory Allocation (2)

● 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)

● Associative memory is a set of registers


allocated to each active job.
● The associative registers associate several
segments and page numbers belonging to
the job being processed with their main
memory address.
● Reside in main memory.
● The number of registers varies between
systems.
WXES1110 (3) 41
Segmented/Demand Paged Memory Allocation (5)

● When a job is allocated to the CPU:


● Its SMT is loaded into main memory.
● Its PMT is loaded as needed.
● As pages are swapped btwn. main memory
and secondary storage, all tables are
updated.

WXES1110 (3) 42
Segmented/Demand Paged Memory Allocation (6)

● When a page is first requested:


● The job’s SMT is searched to locate its
PMT.
● The PMT is loaded and searched to
determine the page’s location in memory.
● If the page is not in memory, a page
interrupt is issued, the page is brought into
memory, and the table is updated.

WXES1110 (3) 43
Segmented/Demand Paged Memory Allocation (7)

● Since this segment’s PMT now resides in


memory, another request for pages within
this segment can be quickly accommodated.
● Accessing SMT and PMT is time-consuming.
● Associative memory overcomes this
drawback by storing info about the most-
recently-used pages in memory.

WXES1110 (3) 44
Segmented/Demand Paged Memory Allocation (8)

● When a page request is issued, two


searches are initiated:
● Through SMT and PMT;
● Through the content of associative
registers.
● If the search of the associative registers
is successful:
● the search through the tables are stopped;
● address translation is performed using the
info in the associative registers.
WXES1110 (3) 45
Segmented/Demand Paged Memory Allocation (9)

● 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

You might also like