Mod-1 3
Mod-1 3
Seventh Edition
Chapter 3
Memory Management:
Virtual Memory Systems
Learning Objectives
(table 3.1)
This section of the Job Table initially has one entry for each job (a).
When the second job ends (b), its entry in the table is released and then
replaced by the entry for the next job (c).
© Cengage Learning 2014
Advantages:
Pages do not have to be loaded contiguously
and are loaded into any available page frame
Memory is used more efficiently, because an
empty page frame can be used by any job
No compaction is really necessary as there is
no external fragmentation
Paged Memory Allocation
Disadvantages:
Some internal fragmentation in the last pages of some
jobs loaded
Memory manager now needs a mechanism to keep
track of a job’s pages => enlarging the size and
complexity the OS software => overhead
Entire job still needs to be loaded during execution
The program must be divided into pages by the
compiler during compilation time
There is an additional effort necessary to access the
bytes in the page frames (calculation of the page
frame number and offset)
Paged Memory Allocation
The Memory Manager uses tables to keep
track of which job pages are loaded into
which memory page frames
Three tables that perform this function are:
• Job Table (JT)
• Page Map Table (PMT)
• Memory Map Table (MMT)
All 3 tables reside in the part of main
memory reserved for the OS
Paged allocation example
Main Memory Page Frame No.
0
Job 1 OS
1
First 100 bytes Page 0 2
3
Second 100 bytes Page 1 4
Job 1 - Page 2 5
Third 100 bytes Page 2
6
Remaining 50 bytes 7
Wasted space Page 3
Job 1 - Page 0 8
9
Job 1 - Page 1 10
Job 1 - Page 3 11
12
Paged Memory Allocation
Example
Page 3 of job 1 uses only 50 of 100 bytes
available in page frame 11, thus causing
some internal fragmentation
After Job 1 is loaded, there are 7 free
page frames:
Thus, a job larger than 700 bytes cannot
be loaded until Job 1 ends
JOB TABLE
Example
PMT
Job Size
location
350 3096
200 3100
500 3150
Page Map Table for Job1 Memory Map Table (Example)
Job Page No. Page Frame No. Page Frame Location Status
No. (starting line)
0 8 0 0 Busy
1 100 Busy
1 10 2 200 Free
3 300 Free
2 5
4 400 Free
3 11 5 500 Busy
6 600 Free
7 700 Free
8 800 Busy
9 900 Free
10 1000 Busy
11 1100 Busy
12 1200 Free
Calculating page frame address and offset
Compiler divides each job into pages:
• page 0: bytes 0 - 99
• page 1: bytes 100 - 199
• page 2: bytes 200 - 299
• page 3: bytes 300 - 399
e.g. suppose the OS needs to access byte 214
When accessing a byte, the OS calculates from the
absolute address of a byte the page and offset address
• page = bytes / page size => 214 / 100 => page 2
• offset = byte % page size => 214 % 100 = 14
(% = Modulo Division)
Calculating page frame address and offset
The OS refers to the job’s PMT and sorts out which page
frame in memory contains which job page
page 2 => page frame 5
Get the address of the beginning of the page frame by
multiplying the page frame number by the page frame size
addr_page_frame = page_frame_num * page_size
= 5 * 100 = 500
Now add the offset to the starting address of the page frame
to compute the precise location in memory of the byte
instr_addr_in_mem = addr_page_frame + offset
= 500 + 14 = 514
Calculating page frame address and offset
(table 3.3)
Page Map Table for Job 1 shown in Figure 3.5.
A 1 = Yes and 0 = No.
© Cengage Learning 2014
(table 3.5)
Four possible combinations of modified and referenced bits and the
meaning of each.
© Cengage Learning 2014
Page Interrupts: * * * * * * * * *
Contents of
page frame 1: A A A C C B B B A A D
Contents of
page frame 2: B B B A A D D D C C
Contents of A
secondary B B
storage: C C C C A B C A A B B A
D D D D D D D C C C D B
FIFO
9 page interrupts for 11 page requests
Failure rate:
= page interrupts / page requests = 9 / 11 = 82%
Success rate:
= successful page requests / page requests = 2/11 = 18%
No guarantee that adding more memory will always result
in better performance (FIFO anomaly). In some cases,
performance can decrease !!
Least Recently Used (LRU)
Used by Windows 95 / 98
The LRU page replacement policy swaps
out the pages that show the least amount of
recent activity
LRU
Page requests: A B A C A B D B A C D
Page Interrupts: * * * * * * * *
Contents of
page frame 1: A A A A A A D D A A D
Contents of
page frame 2: B B C C B B B B C C
Contents of A
secondary B B
storage: C C C C B B C A A C B A
D D D D D D D C C D D B
LRU
A B A C A B D B A C D B
PF1
PF2
PF3
• Perform a FIFO page replacement policy. (hint: Redraw the table and
use * to represent a page interrupt). Calculate the Failure Rate.
• Perform a LRU page replacement policy. (hint: Redraw the table and
use * to represent a page interrupt). Calculate the Failure Rate.
Mechanics of Paging
99
Segment 2
199
Segment 1
349
Segment 0
How does Segmented Memory Allocation work ?
• Segment number
• Length of segment
• Access rights of the segment
• Status, referenced, modified
• Address in memory (if loaded)
Segmented Map Table
0 Segment Size Status Access Memory 0
Main Program no. Address
0
1
350
200
Y
Y
E
E
4000
7000
OS
2 100 N E ______
3000
Empty
349
4000
Main
0 Subroutine A program
Other
programs
199 7000
Subroutine A
0 Subroutine B
Other
99
programs
Segmented Memory Allocation
To access a byte within a segment the OS
performs an operation similar to the one used
for paged memory management (segments
instead of pages)
A 2-dimensional addressing scheme is used:
Segment Number Offset
Segments are logical units that are visible to
the user’s program
Pages are physical units that are invisible to the
user’s program
Disadvantage: need for compaction because of
external fragmentation
Calculating segment address and offset
Compiler divides each job into segments:
• Segment 0: bytes 0-349
• Segment 1: bytes 350 - 549
• Segment 2: bytes 550 - 649
Suppose the main program needs to load into
register 1, the instruction at address 353
When accessing a byte the OS determines from
the absolute address of a byte the segment and
calculates the offset
• Segment = 1
• Offset = 353 – 350 = 3
Calculating segment address and offset
The OS refers to the job’s SMT to find out which
address in memory contains which job segment
• Segment 1 => address 7000
Now the OS adds the offset to the starting address
of the segment to compute the precise location of
the byte in memory
• seg_start-addr + offset = act_mem_loc
7000 + 3 = 7003
Memory Management Schemes
Paged Memory Allocation
Segmented / Demand
Paged Memory Allocation
Segmented / Demand Paged
Memory Allocation
The segments are divided into pages of equal
size (smaller than the segments)
The problems of external fragmentation, and
thus of compaction are solved
Disadvantages:
• Table-handling overhead
• Memory needed for the management tables
(space)
• Internal fragmentation may return in last page
How does segmented / demand paged
memory allocation work ?
The algorithms used by demand paging and
segmented memory management schemes
are applied with only minor modifications
Modifications are necessary for the four
tables this scheme is using:
• JT, Job Table, lists every job
• MMT, Memory Map Table, monitors the
allocation of the page frames
How does segmented / demand paged
memory allocation work ?
• PMT, Page Map Table, lists details about every
page:
• page number
• page frame number
• status, modified, referenced
• SMT, Segment MapTable, lists details about
every segment:
• segment number
• length
• address of PMT
• access rights
Segmented / Paging Scheme
How does segmented / demand paged
memory allocation work ?
To access a byte in memory the OS uses a 3-
dimensional addressing scheme:
segment number page number offset
Disadvantages:
=> memory overhead for tables
=> time overhead for referencing an address
How does segmented / demand paged
memory allocation work ?
To speed up the access time many systems
are using associative memory
This is a name given to several registers
that are allocated to each job that is active
Their task is to associate several segments
and page numbers belonging to the active
job with their main memory addresses
Working Set
These associative registers reside in main
memory and the exact number varies from
system to system
How does segmented / demand
paged memory allocation work ?
In general, when a job is allocated to the CPU, its
SMT is loaded into memory, while the PMTs are
loaded only as needed
As pages are swapped, all tables are updated
When a page is first requested, the SMT is searched to
locate its PMT
The PMT is loaded and searched to determine the
page’s location in memory
If the page isn’t in memory, a page interrupt is issued,
the page is loaded, and the table is updated
The associative memory contains the information
related to the most-recently-used pages
How does segmented / demand paged
memory allocation work ?
When further page requests are issued, two searches
begin:
• through associative memory
• through SMTs and PMTs
If the search through associative memory is successful
then the search through the tables is stopped
A system with 8 associative registers per job will, for
example, use them to store the SMT and PMT for the last
8 pages referenced by that job
Details kept by associative memory might include
segment number, page number, page frame number, etc
Disadvantage: Complex, expensive HW is required for
associative memory
Virtual Memory
• Made possible by swapping pages in/out of memory
• Program execution: only a portion of the program in
memory at any given moment
• Requires cooperation between:
– Memory Manager: tracks each page or segment
– Processor hardware: issues the interrupt and resolves
the virtual address