0% found this document useful (0 votes)
16 views97 pages

Virtual Memory OSedition 7 Stallings Book

The document discusses virtual memory and how it allows processes to have memory larger than physical RAM. It covers key concepts like paging, segmentation, page tables, and policies for bringing pages into memory like demand paging and prepaging. Virtual memory relies on hardware and software to map logical addresses to physical addresses and move pages between RAM and disk as needed.

Uploaded by

lyumengyikr
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)
16 views97 pages

Virtual Memory OSedition 7 Stallings Book

The document discusses virtual memory and how it allows processes to have memory larger than physical RAM. It covers key concepts like paging, segmentation, page tables, and policies for bringing pages into memory like demand paging and prepaging. Virtual memory relies on hardware and software to map logical addresses to physical addresses and move pages between RAM and disk as needed.

Uploaded by

lyumengyikr
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/ 97

Operating

Systems:
Internals
and Design Chapter 8
Principles Virtual Memory
Seventh Edition
William Stallings
Operating Systems:
Internals and Design Principles

You’re gonna need a bigger boat.

— Steven Spielberg,
JAWS, 1975
Hardware and Control Structures

n Two characteristics fundamental to memory


management:
1) all memory references are logical addresses that are
dynamically translated into physical addresses at run time
2) a process may be broken up into a number of pieces that
don’t need to be contiguously located in main memory
during execution
n If these two characteristics are present, it is not
necessary that all of the pages or segments of a
process be in main memory during execution
Terminology
n Operating system brings into main memory a few pieces of the
program

n Resident set - portion of process that is in main memory

n An interrupt is generated when an address is needed that is not


in main memory

n Operating system places the process


in a blocking state

Continued . . .
Execution of a Process
n Piece of process that contains the logical address is brought into
main memory
n operating system issues a disk I/O Read request
n another process is dispatched to run while the disk I/O takes
place
n 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
n More processes may be maintained in main memory
n only load in some of the pieces of each process
n with so many processes in main memory, it is very likely a
process will be in the Ready state at any particular time

n A process may be larger than all of main memory


Real and Virtual Memory

Real memory
• main memory, the actual RAM

Virtual memory
• memory on disk
• allows for effective multiprogramming and relieves the
user of tight constraints of main memory
Table 8.2

Characteristics of

Paging and

Segmentation
To avoid this, the
A state in which operating system tries
the system spends to guess, based on
most of its time recent history, which
swapping process pieces are least likely
pieces rather than to be used in the near
executing future
instructions
Principle of Locality
n Program and data references within a process tend to cluster

n Only a few pieces of a process will be needed over a short


period of time

n Therefore it is possible to make intelligent guesses about which


pieces will be needed in the future

n Avoids thrashing
Paging Behavior

n During the lifetime of the


process, references are
confined to a subset of pages
For virtual memory to be practical and
effective:
• hardware must support paging and
segmentation
• operating system must include software for
managing the movement of pages and/or
segments between secondary memory and
main memory
Paging
n The term virtual memory is usually associated with systems that
employ paging

n Use of paging to achieve virtual memory was first reported for


the Atlas computer

n Each process has its own page table


n each page table entry contains the frame number of the
corresponding page in main memory
Memory
Management
Formats
Address Translation
Two-Level
Two-
Hierarchical Page Table
Address Translation
n Page number portion of a virtual address is mapped into a hash
value
n hash value points to inverted page table

n Fixed proportion of real memory is required for the tables


regardless of the number of processes or virtual pages supported

n Structure is called inverted because it indexes page table entries by


frame number rather than by virtual page number
Inverted Page Table
Each entry in the page table includes:

Page Process Control Chain


number identifier bits pointer
• the process • includes • the index
that owns flags and value of the
this page protection next entry
and locking in the chain
information
Translation Lookaside
Buffer (TLB)
n Each virtual memory n To overcome the effect of
reference can cause two doubling the memory
physical memory accesses: access time, most virtual
n one to fetch the page
memory schemes make
table entry use of a special high-speed
cache called a translation
n one to fetch the data
lookaside buffer
Use of a TLB
TLB
Operation
Associative Mapping
n The TLB only contains some of the page table entries so we
cannot simply index into the TLB based on page number
n each TLB entry must include the page number as well as the
complete page table entry

n The processor is equipped with hardware that allows it to


interrogate simultaneously a number of TLB entries to
determine if there is a match on page number
Direct Versus
Associative Lookup
TLB and Cache Operation
Page Size
n The smaller the page size, the lesser the amount of internal
fragmentation
n however, more pages are required per process
n more pages per process means larger page tables
n 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
n the physical characteristics of most secondary-memory devices
favor a larger page size for more efficient block transfer of data
Paging Behavior of a Program
Example: Page Sizes
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

n 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:
n 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
n Each segment table entry contains the starting address of the
corresponding segment in main memory and the length of the
segment

n A bit is needed to determine if the segment is already in main


memory

n Another bit is needed to determine if the segment has been


modified since it was loaded in main memory
Address Translation
Combined Paging and
Segmentation

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
Address Translation
Combined Segmentation
and Paging
Protection and Sharing
n Segmentation lends itself to the implementation of protection
and sharing policies

n Each entry has a base address and length so inadvertent memory


access can be controlled

n Sharing can be achieved by segments referencing multiple


processes
Protection
Relationships
Operating System Software

The design of the memory management


portion of an operating system depends on
three fundamental areas of choice:
• whether or not to use virtual memory techniques
• the use of paging or segmentation or both
• the algorithms employed for various aspects of
memory management
Policies for Virtual Memory
n Key issue: Performance
§ minimize page faults
n Determines when a
page should be Two main
brought into types:
memory

Demand
Prepaging
Paging
Demand Paging
n Demand Paging
n only brings pages into main memory when a reference is made
to a location on the page
n many page faults when process is first started
n 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
n Prepaging
n pages other than the one demanded by a page fault are brought
in
n exploits the characteristics of most secondary memory devices
n 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
n ineffective if extra pages are not referenced
n should not be confused with “swapping”
Placement Policy
n Determines where in real memory a process
piece is to reside
n Important design issue in a segmentation system
n Paging or combined paging with segmentation
placing is irrelevant because hardware performs
functions with equal efficiency
n For NUMA systems an automatic placement
strategy is desirable
Replacement Policy
n Deals with the selection of a page in main memory
to be replaced when a new page must be brought in
n objective is that the page that is removed be the page
least likely to be referenced in the near future

n The more elaborate the replacement policy the


greater the hardware and software overhead to
implement it
§ When a frame is locked the page currently stored in that frame
may not be replaced
§ kernel of the OS as well as key control structures are held
in locked frames
§ I/O buffers and time-critical areas may be locked into
main memory frames
§ locking is achieved by associating a lock bit with each
frame
Algorithms used for
the selection of a
page to replace:
• Optimal
• Least recently used (LRU)
• First-in-first-out (FIFO)
• Clock
§ Selects the page for which the time to the
next reference is the longest
§ Produces three page faults after the frame
allocation has been filled
Least Recently Used
(LRU)
n Replaces the page that has not been referenced for the longest
time

n By the principle of locality, this should be the page least likely


to be referenced in the near future

n Difficult to implement
n one approach is to tag each page with the time of last
reference
n this requires a great deal of overhead
LRU Example
First--in-
First in-First-
First-out (FIFO)
n Treats page frames allocated to a process as a circular buffer

n Pages are removed in round-robin style


§ simple replacement policy to implement

n Page that has been in memory the longest is replaced


Clock Policy
n Requires the association of an additional bit with each frame
n referred to as the use bit

n When a page is first loaded in memory or referenced, the use bit


is set to 1

n The set of frames is considered to be a circular buffer

n Any frame with a use bit of 1 is passed over by the algorithm

n Page frames visualized as laid out in a circle


Clock
Policy
Clock
Policy
Combined Examples
n Improves paging A replaced page is
not lost, but
performance and rather assigned to
allows the use of one of two lists:

a simpler page
replacement
policy
Free page list Modified page list

list of page frames


pages are written
available for
out in clusters
reading in pages
Replacement Policy and Cache Size
n With large caches, replacement of pages can have a performance
impact
n if the page frame selected for replacement is in the cache, that
cache block is lost as well as the page that it holds
n in systems using page buffering, cache performance can be
improved with a policy for page placement in the page buffer
n most operating systems place pages by selecting an arbitrary
page frame from the page buffer
n The OS must decide how many pages to bring into main memory
n the smaller the amount of memory allocated to each process,
the more processes can reside in memory
n small number of pages loaded increases page faults
n beyond a certain size, further allocations of pages will not
effect the page fault rate
Resident Set Size
Fixed-allocation Variable-allocation
n gives a process a fixed n allows the number of page
number of frames in main frames allocated to a
memory within which to process to be varied over
execute the lifetime of the process
n when a page fault occurs,
one of the pages of that
process must be replaced
n The scope of a replacement strategy can be categorized as
global or local
n both types are activated by a page fault when there are no free
page frames

Local
• chooses only among the resident pages of the process that generated
the page fault

Global
• considers all unlocked pages in main memory
Fixed Allocation, Local Scope
n Necessary to decide ahead of time the amount of
allocation to give a process
n If allocation is too small, there will be a high page fault
rate

If allocation is too • increased processor idle time


large, there will be
too few programs • increased time spent in
in main memory swapping
Variable Allocation
Global Scope
n Easiest to implement
n adopted in a number of operating systems

n OS maintains a list of free frames

n Free frame is added to resident set of process when a page fault


occurs

n If no frames are available the OS must choose a page currently in


memory

n One way to counter potential problems is to use page buffering


n When a new process is loaded into main memory, allocate to it a
certain number of page frames as its resident set

n When a page fault occurs, select the page to replace from among
the resident set of the process that suffers the fault

n Reevaluate the allocation provided to the process and increase or


decrease it to improve overall performance
Variable Allocation
Local Scope
n Decision to increase or decrease a resident set size is based
on the assessment of the likely future demands of active
processes

Key elements:

• criteria used to determine


resident set size
• the timing of changes
Figure 8.19

Working Set
of Process as
Defined by
Window Size
Page Fault Frequency
(PFF)
n Requires a use bit to be associated with each page in memory

n Bit is set to 1 when that page is accessed

n When a page fault occurs, the OS notes the virtual time since the
last page fault for that process

n Does not perform well during the transient periods when there is
a shift to a new locality
n Evaluates the working set of a process at sampling instances based
on elapsed virtual time

n Driven by three parameters:

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
n 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
n Determines the number of processes that will be resident in main
memory
n multiprogramming level

n Critical in effective memory management

n Too few processes, many occasions when all processes will be


blocked and much time will be spent in swapping

n Too many processes will lead to thrashing


Multiprogramming
n If the degree of multiprogramming is to be reduced, one or more
of the currently resident processes must be swapped out

Six possibilities exist:


• Lowest-priority process
• Faulting process
• Last process activated
• Process with the smallest resident set
• Largest process
• Process with the largest remaining execution window
Unix
n Intended to be machine independent so its memory
management schemes will vary
n early Unix: variable partitioning with no virtual memory
scheme
n current implementations of UNIX and Solaris make use of
SVR4 and Solaris use
paged virtual memory

two separate schemes:


• paging system
• kernel memory allocator
Kernel Memory
Paging system
Allocator

provides a virtual memory


capability that allocates page frames allocates memory for the kernel
in main memory to processes

allocates page frames to disk block


buffers
UNIX SVR4
Memory
Management
Formats
Table 8.6

UNIX SVR4
Memory
Management
Parameters
(page 1 of 2)
Table 8.6

UNIX SVR4
Memory
Management
Parameters
(page 2 of 2)
n The page frame data table is used for page replacement

n Pointers are used to create lists within the table


n all available frames are linked together in a list of free frames
available for bringing in pages
n when the number of available frames drops below a certain
threshold, the kernel will steal a number of frames to
compensate
“Two Handed”
Clock
Page
Replacement
n The kernel generates and destroys small tables and buffers
frequently during the course of execution, each of which requires
dynamic memory allocation.

n Most of these blocks are significantly smaller than typical pages


(therefore paging would be inefficient)

n Allocations and free operations must be made as fast as possible


n Technique adopted for SVR4

n UNIX often exhibits steady-state behavior in kernel memory


demand
n i.e. the amount of demand for blocks of a particular size
varies slowly in time

n Defers coalescing until it seems likely that it is needed, and


then coalesces as many blocks as possible
Lazy Buddy System Algorithm
Linux
Memory Management
n Shares many characteristics with Unix
n Is quite complex

• process virtual
memory
Two main • kernel memory
aspects allocation
n Three level page table structure:

Page directory Page middle directory Page table

process has a single page


may span multiple pages may also span multiple pages
directory

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

must be in main memory for


an active process
Address Translation
n Based on the clock algorithm

n The use bit is replaced with an 8-bit age variable


n incremented each time the page is accessed

n Periodically decrements the age bits


n a page with an age of 0 is an “old” page that has not been
referenced is some time and is the best candidate for
replacement

n A form of least frequently used policy


n Kernel memory capability manages physical main memory page frames
n primary function is to allocate and deallocate frames for particular
uses
Possible owners of a frame include:
• user-space processes
• dynamically allocated kernel data
• static kernel code
• page cache

n A buddy algorithm is used so that memory for the kernel can be


allocated and deallocated in units of one or more pages
n Page allocator alone would be inefficient because the kernel requires
small short-term memory chunks in odd sizes
n Slab allocation
n used by Linux to accommodate small chunks
Windows
Memory Management
n Virtual memory manager controls how memory is allocated and
how paging is performed

n Designed to operate over a variety of platforms

n Uses page sizes ranging from 4 Kbytes to 64 Kbytes


Windows Virtual Address Map
n On 32 bit platforms each user process sees a separate 32 bit
address space allowing 4 Gbytes of virtual memory per process
§ by default half is reserved for the OS
n Large memory intensive applications run more effectively using
64-bit Windows
n Most modern PCs use the AMD64 processor architecture which
is capable of running as either a 32-bit or 64-bit system
32
32--Bit
Windows
Address
Space
Windows Paging
n On creation, a process can make use of the entire user space of
almost 2 Gbytes

n This space is divided into fixed-size pages managed in


contiguous regions allocated on 64 Kbyte boundaries

n Regions may be in one of three states:

available reserved committed


n Windows uses variable allocation, local scope

n When activated, a process is assigned a data structure to manage


its working set

n Working sets of active processes are adjusted depending on the


availability of main memory
Summary
n Desirable to:
n maintain as many processes in main memory as possible
n free programmers from size restrictions in program
development

n With virtual memory:


n all address references are logical references that are translated
at run time to real addresses
n a process can be broken up into pieces
n two approaches are paging and segmentation
n management scheme requires both hardware and software
support

You might also like