8 - OS - CH 08 - Virtual Memory - OS8e
8 - OS - CH 08 - Virtual Memory - OS8e
8 - OS - CH 08 - Virtual Memory - OS8e
Operating
Systems:
Internals
and Design Chapter 8
Principles Virtual Memory
Eighth Edition
William Stallings
١
١٦/٠١/١٤٣٨
Resident set
portion of process that is in main memory
Continued . . .
٢
١٦/٠١/١٤٣٨
Execution of a Process
Piece of process that contains the logical address is brought into
main memory
operating system issues a disk I/O Read request
another process is dispatched to run while the disk I/O takes
place
an interrupt is issued when disk I/O is complete, which causes
the operating system to place the affected process in the Ready
state
Implications
More processes may be maintained in main memory
only load in some of the pieces of each process
with so many processes in main memory, it is very likely a
process will be in the Ready state at any particular time
٣
١٦/٠١/١٤٣٨
main memory,
the actual memory on disk
RAM
٤
١٦/٠١/١٤٣٨
Principle of Locality
Program and data references within a process tend to cluster
Avoids thrashing
٥
١٦/٠١/١٤٣٨
Paging
The term virtual memory is usually associated with systems that
employ paging
٦
١٦/٠١/١٤٣٨
Virtual Address
Page Number Offset
Virtual Address
Segment Number Offset
Virtual Address
Segment Number Page Number Offset
Register
n bits Page Table Ptr
Page Table
m bits
Offset
Page
Page#
Frame
+
Frame #
٧
١٦/٠١/١٤٣٨
4-kbyte root
page table
4-Mbyte user
page table
4-Gbyte user
address space
Virtual Address
10 bits 10 bits 12 bits Frame # Offset
Root page
table ptr
Page
Frame
+ +
4-kbyte page
Root page table table (contains
(contains 1024 PTEs) 1024 PTEs)
٨
١٦/٠١/١٤٣٨
Virtual Address
n bits
Page # Offset
Control
n bits bits
Process
hash m bits Page # ID Chain
function 0
2m 1 Frame # Offset
m bits
Inverted Page Table Real Address
(one entry for each
physical memory frame)
٩
١٦/٠١/١٤٣٨
Translation Lookaside
Buffer (TLB)
Each virtual memory To overcome the effect of
reference can cause two doubling the memory
physical memory accesses: access time, most virtual
one to fetch the page
memory schemes make
table entry use of a special high-speed
cache called a translation
one to fetch the data
lookaside buffer
١٠
١٦/٠١/١٤٣٨
Secondary
Main Memory Memory
Virtual Address
Page # Offset
Translation
Lookaside Buffer
TLB hit
Offset
Load
page
Page Table
TLB miss
Frame # Offset
Real Address
Page fault
Start
Return to
Faulted Instruction
CPU checks the TLB
Yes
CPU Activates
I/O Hardware Update TLB
Page Transferred
from Disk to CPU Generates
Main Memory Physical Address
Memory Yes
Full?
No Perform Page
Replacement
Page Tables
Updated
Figure 8.7 Operation of Paging and Translation Lookaside Buffer (TLB) [FURH87]
١١
١٦/٠١/١٤٣٨
Associative Mapping
The TLB only contains some of the page table entries so we
cannot simply index into the TLB based on page number
each TLB entry must include the page number as well as the
complete page table entry
Page # PT Entries
19
511
37
27
14
37 1
211
5 37
90
37 502 37 502
Frame # Offset Translation Lookaside Buffer Frame # Offset
Real Address Real Address
Page Table
Figure 8.8 Direct Versus Associative Lookup for Page Table Entries
١٢
١٦/٠١/١٤٣٨
TLB Operation
Virtual Address
Page # Offset
TLB
TLB miss
TLB
hit Cache Operation
Real Address
Main
Memory
Page Table
Value
Page Size
The smaller the page size, the lesser the amount of internal
fragmentation
however, more pages are required per process
more pages per process means larger page tables
for large programs in a heavily multiprogrammed
environment some portion of the page tables of active
processes must be in virtual memory instead of main memory
the physical characteristics of most secondary-memory
devices favor a larger page size for more efficient block
transfer of data
١٣
١٦/٠١/١٤٣٨
١٤
١٦/٠١/١٤٣٨
Page Size
the design issue of main memory is
page size is related to getting larger and
the size of physical address space used by
main memory and applications is also
program size growing
Contemporary programming
techniques used in large
programs tend to decrease the most obvious on
personal computers
locality of references within a where applications are
process becoming increasingly
complex
Segmentation
Advantages:
Segmentation • simplifies handling
allows the of growing data
programmer to structures
view memory as • allows programs to
consisting of be altered and
multiple address recompiled
independently
spaces or
• lends itself to
segments sharing data
among processes
• lends itself to
protection
١٥
١٦/٠١/١٤٣٨
Segment Organization
Each segment table entry contains the starting address of the
corresponding segment in main memory and the length of the
segment
Register
Seg Table Ptr
Segment table
d
Segment
Seg #
+
Length Base
١٦
١٦/٠١/١٤٣٨
In a combined
paging/segmentation system Segmentation is visible to the
a user’s address space is programmer
broken up into a number of
segments. Each segment is
broken up into a number of
fixed-sized pages which are Paging is transparent to the
equal in length to a main programmer
memory frame
Virtual Address
Seg # Page # Offset Frame # Offset
Segment Page
Table Table
Page#
Offset
Page
Seg#
Frame
+ +
١٧
١٦/٠١/١٤٣٨
Virtual Address
Segment Number Page Number Offset
١٨
١٦/٠١/١٤٣٨
١٩
١٦/٠١/١٤٣٨
Determines when a
page should be Two main
brought into types:
memory
Demand
Prepaging
Paging
٢٠
١٦/٠١/١٤٣٨
Demand Paging
Demand Paging
only brings pages into main memory when a reference is made
to a location on the page
many page faults when process is first started
principle of locality suggests that as more and more pages are
brought in, most future references will be to pages that have
recently been brought in, and page faults should drop to a very
low level
Prepaging
Prepaging
pages other than the one demanded by a page fault are brought
in
exploits the characteristics of most secondary memory devices
if pages of a process are stored contiguously in secondary
memory it is more efficient to bring in a number of pages at
one time
ineffective if extra pages are not referenced
should not be confused with “swapping”
٢١
١٦/٠١/١٤٣٨
Placement Policy
Determines where in real memory a process
piece is to reside
Important design issue in a segmentation system
Paging or combined paging with segmentation
placing is irrelevant because hardware performs
functions with equal efficiency
For NUMA systems an automatic placement
strategy is desirable
Replacement Policy
Deals with the selection of a page in main memory
to be replaced when a new page must be brought in
objective is that the page that is removed be the page
least likely to be referenced in the near future
٢٢
١٦/٠١/١٤٣٨
٢٣
١٦/٠١/١٤٣٨
Page address
stream 2 3 2 1 5 2 4 5 3 2 5 2
2 2 2 2 2 2 4 4 4 2 2 2
OPT 3 3 3 3 3 3 3 3 3 3 3
1 5 5 5 5 5 5 5 5
F F F
2 2 2 2 2 2 2 2 3 3 3 3
LRU 3 3 3 5 5 5 5 5 5 5 5
1 1 1 4 4 4 2 2 2
F F F F
2 2 2 2 5 5 5 5 3 3 3 3
FIFO 3 3 3 3 2 2 2 2 2 5 5
1 1 1 4 4 4 4 4 2
F F F F F F
2* 2* 2* 2* 5* 5* 5* 5* 3* 3* 3* 3*
CLOCK 3* 3* 3* 3 2* 2* 2* 2 2* 2 2*
1* 1 1 4* 4* 4 4 5* 5*
F F F F F
Difficult to implement
one approach is to tag each page with the time of last
reference
this requires a great deal of overhead
٢٤
١٦/٠١/١٤٣٨
First--in-
First in-First-
First-out (FIFO)
Treats page frames allocated to a process as a circular buffer
Clock Policy
Requires the association of an additional bit with each frame
referred to as the use bit
٢٥
١٦/٠١/١٤٣٨
40
Page Faults per 1000 References
FIFO
35
30 CLOCK
25 LRU
20
OPT
15
10
5
0
6 8 10 12 14
٢٦
١٦/٠١/١٤٣٨
a simpler page
replacement
policy
Free page list Modified page list
٢٧
١٦/٠١/١٤٣٨
The OS must decide how many pages to bring into main memory
the smaller the amount of memory allocated to each process,
the more processes can reside in memory
small number of pages loaded increases page faults
beyond a certain size, further allocations of pages will not
effect the page fault rate
٢٨
١٦/٠١/١٤٣٨
Local
• chooses only among the resident pages of the process that generated
the page fault
Global
• considers all unlocked pages in main memory
٢٩
١٦/٠١/١٤٣٨
Variable Allocation
Global Scope
Easiest to implement
adopted in a number of operating systems
٣٠
١٦/٠١/١٤٣٨
When a page fault occurs, select the page to replace from among
the resident set of the process that suffers the fault
Variable Allocation
Local Scope
Decision to increase or decrease a resident set size is based
on the assessment of the likely future demands of active
processes
Key elements:
٣١
١٦/٠١/١٤٣٨
Sequence of
Page
Window Size, ∆
References
2 3 4 5
24 24 24 24 24
15 24 15 24 15 24 15 24 15
18 15 18 24 15 18 24 15 18 24 15 18
23 18 23 15 18 23 24 15 18 23 24 15 18 23
24 23 24 18 23 24 • •
17 24 17 23 24 17 18 23 24 17 15 18 23 24 17
18 17 18 24 17 18 • 18 23 24 17
24 18 24 • 24 17 18 •
18 • 18 24 • 24 17 18
17 18 17 24 18 17 • •
17 17 18 17 • •
15 17 15 17 15 18 17 15 24 18 17 15
24 15 24 17 15 24 17 15 24 •
17 24 17 • • 17 15 24
24 • 24 17 • •
18 24 18 17 24 18 17 24 18 15 17 24 18
٣٢
١٦/٠١/١٤٣٨
When a page fault occurs, the OS notes the virtual time since the
last page fault for that process
Does not perform well during the transient periods when there is
a shift to a new locality
the number of
the minimum the maximum page faults that
duration of the duration of the are allowed to
sampling sampling occur between
interval interval sampling
instances
٣٣
١٦/٠١/١٤٣٨
Cleaning Policy
Concerned with determining when a modified page should be
written out to secondary memory
Demand Cleaning
a page is written out to secondary memory only when it has been selected for
replacement
Precleaning
allows the writing of pages in batches
Load Control
Determines the number of processes that will be resident in main
memory
multiprogramming level
٣٤
١٦/٠١/١٤٣٨
Processor Utilization
Multiprogramming Level
٣٥
١٦/٠١/١٤٣٨
UNIX
Intended to be machine independent so its memory
management schemes will vary
early UNIX: variable partitioning with no virtual memory
scheme
current implementations of UNIX and Solaris make use of
SVR4 and Solaris use
paged virtual memory
Kernel Memory
Paging System
Allocator
٣٦
١٦/٠١/١٤٣٨
Copy
Page frame number Age on Mod- Refe- Valid Pro-
write ify rence tect
Reference Page/storage
count unit number
Age
Indicates how long the page has been in memory without being referenced. The length and contents of this
Table 8.6
field are processor dependent.
Copy on write
Set when more than one process shares a page. If one of the processes writes into the page, a separate copy
of the page must first be made for all other processes that share the page. This feature allows the copy
operation to be deferred until necessary and avoided in cases where it turns out not to be necessary.
UNIX SVR4
Modify Memory
Indicates page has been modified.
Reference
Management
Indicates page has been referenced. This bit is set to 0 when the page is first loaded and may be periodically
reset by the page replacement algorithm. Parameters
Valid (page 1 of 2)
Indicates page is in main memory.
Protect
Indicates whether write operation is allowed.
Type of storage
Storage may be swap unit or executable file. In the latter case, there is an indication as to whether the (Table can be found on page 379
virtual memory to be allocated should be cleared first. in the textbook)
٣٧
١٦/٠١/١٤٣٨
Page state
Indicates whether this frame is available or has an associated page. In the latter case, the Table 8.6
status of the page is specified: on swap device, in executable file, or DMA in progress.
Pfdata pointer
Pointer to other pfdata table entries on a list of free pages and on a hash queue of pages.
Reference count
Number of page table entries that point to a page on the swap device.
٣٨
١٦/٠١/١٤٣٨
End of Beginning
page list of page list
fr
on
th
handspread
an
d
nd
ha
ck
ba
٣٩
١٦/٠١/١٤٣٨
Initial value of Di is 0
After an operation, the value of Di is updated as follows
٤٠
١٦/٠١/١٤٣٨
Linux
Memory Management
Shares many characteristics with UNIX
Is quite complex
• process virtual
memory
Two main • kernel memory
aspects allocation
each entry points to one page each entry points to one page each entry refers to one
of the page middle directory in the page table virtual page of the process
٤١
١٦/٠١/١٤٣٨
Virtual address
Global Directory Middle Directory Page Table Offset
+
Page table
Page middle +
directory Page frame
in physical
memory
Page
directory
+
+
cr3
register
٤٢
١٦/٠١/١٤٣٨
Inactive Active
timeout
ed
used timeout used timeout
us
PG_active = 0 PG_active = 1
PG_referenced = 1 PG_referenced = 1
used
٤٣
١٦/٠١/١٤٣٨
Windows
Memory Management
Virtual memory manager controls how memory is allocated and
how paging is performed
٤٤
١٦/٠١/١٤٣٨
0
64-Kbyte region for
NULL-pointer assignments
(inaccessible)
2-Gbyte user
address space
(unreserved, usable)
0xFFFFFFFF
Windows Paging
On creation, a process can make use of the entire user space of
almost 2 Gbytes
٤٥
١٦/٠١/١٤٣٨
Android Memory
Management
Android includes a number of extensions to the normal Linux kernel memory
management facility
These include:
ASHMem
this feature provides anonymous shared memory, which abstracts
memory as file descriptors
a file descriptor can be passed to another process to share memory
Pmem
this feature allocates virtual memory so that it is physically contiguous
this feature is useful for hardware that does not support virtual memory
Low Memory Killer
this feature enables the system to notify an app or apps that they need to
free up memory
if an app does not cooperate, it is terminated
٤٦
١٦/٠١/١٤٣٨
Summary
Hardware and control structures UNIX and Solaris memory
Locality and virtual memory management
Paging Paging system
Segmentation Kernel memory allocator
Combined paging and Linux memory management
segmentation
Linux virtual memory
Protection and sharing
Kernel memory allocation
OS software
Windows memory management
Fetch policy
Windows virtual address map
Placement policy
Windows paging
Replacement policy
Windows 8 swapping
Resident set management
Cleaning policy Android memory management
Load control
٤٧