Title: Chapter 9: VirtualMemory Management
1Chapter 9 Virtual-Memory Management
- Background
- Demand Paging
- Page Replacement
- Allocation of Frames
- Thrashing
- Operating System Examples
2Background
- Virtual memory separation of user logical
memory from physical memory, which allows the
execution of processes that may not be completely
in memory. - Only part of the program needs to be in memory
for execution. - Logical address space can therefore be much
larger than physical address space. - Allows address spaces to be shared by several
processes. - Allows for more efficient process creation.
- Virtual memory is commonly implemented with
demand paging.
3Virtual Memory That is Larger Than Physical Memory
4- The ability to execute a program that is only
partially in memory would provide many benefits - A program would no longer be constrained by the
amount of available physical memory. Users would
be able to write programs for a very large
virtual address space, simplifying the
programming task. - Because each user program could take less
physical memory, more programs could be run at
the same time, with a corres-ponding increase in
CPU utilization and throughput. - Less I/O would be needed to load or swap each
user program into memory, so each user program
would run faster.
5Demand Paging
- Bring a page into memory only when it is needed.
- Less I/O needed
- Less memory needed
- Faster response
- More users
- If there is a reference to a page, then the page
is needed. - invalid reference ? abort
- not-in-memory (page fault) ? bring to memory
6Transfer of a Paged Memory to Contiguous Disk
Space
7Valid-Invalid Bit
- With each page table entry, a validinvalid bit
is associated(1 ? in-memory, 0 ? not-in-memory) - Initially validinvalid bit is set to 0 on all
entries. - During address translation, if validinvalid bit
in page table entry is 0 ? page fault.
8Page Table When Some Pages Are Not in Main Memory
9Handling a Page Fault
- If there is ever a reference to a page marked
invalid, first reference will trap to OS ? page
fault trap - OS handles the page fault as follows
- 1. Check an internal table (kept with PCB) to
determine whether the reference was a valid or
invalid memory access. - 2. If the reference was invalid, terminate the
process. If it was valid, but the page need to be
brought in. - 3. Find a free frame.
- 4. Schedule a disk operation to read the desired
page into the newly allocated frame. - 5. When the disk read is complete, modify the
internal table in the PCB and the page table to
indicate that the page is now in memory. - 6. Restart the instruction that was interrupted
by the page fault trap. The process can now
access the page as though it had always been in
memory.
10Steps in Handling a Page Fault
11Performance of Demand Paging
- Page Fault Rate 0 ? p ? 1.0
- if p 0, no page faults
- if p 1, every reference is a fault
- Effective Access Time (EAT)
- EAT (1 p) x memory access time
- p x page fault time
-
- Three major components of the page-fault service
time - 1. Service the page-fault interrupt.
- 2. Read in the page.
- 3. Restart the process.
12What happens if there is no free frame?
- Page replacement find some page in memory, but
not really in use, swap it out. - A good algorithm is needed.
- Performance issue we want an algorithm which
will result in minimum number of page faults. - Same page may be brought into memory several
times.
13Need For Page Replacement
14Basic Page Replacement
- Find the location of the desired page on disk.
- Find a free frame - If there is a free frame,
use it. - If there is no free frame, use a page
replacement algorithm to select a victim
frame. - - Write the victim page to the disk update the
page and - frame tables accordingly.
- Read the desired page into the (newly) free
frame. Update the page and frame tables. - Restart the user process.
15Page Replacement
16Page Replacement
- Handle over-allocation of memory by modifying
page-fault service routine to include page
replacement. - Use modify (dirty) bit to reduce overhead of page
transfers only modified pages are written to
disk. - Page replacement completes separation between
logical memory and physical memory large
virtual memory can be provided on a smaller
physical memory.
17Page Replacement Algorithms
- Want lowest page-fault rate.
- Evaluate algorithm by running it on a particular
string of memory references (reference string)
and computing the number of page faults on that
string. - How is a reference string generated?
- For example, if we trace a particular process, we
might record the following address reference
sequence - 0100, 0432, 0101, 0612, 0102, 0103, 0104, 0101,
0611, 0102 - which, at 100 bytes per page, is reduced to the
following reference string - 1, 4, 1, 6, 1, 6, 1
18Expected (Ideal)Graph of Page Faults Versus The
Number of Frames
19First-In-First-Out (FIFO) Algorithm
- Reference string 1, 2, 3, 4, 1, 2, 5, 1, 2, 3,
4, 5 - 3 frames (3 pages can be in memory at a time per
process) - 4 frames
- This shows the Beladys Anomaly
- more frames ? more page faults
1
1
4
5
2
2
1
3
9 page faults
3
3
2
4
1
1
5
4
2
2
1
10 page faults
5
3
3
2
4
4
3
20FIFO Page Replacement Algorithmanother example
21FIFO Replacement Illustrating Beladys Anomaly
22Optimal Algorithm
- Replace the page that will not be used for the
longest period of time. - 4 frames example
- 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5
- Requires future knowledge of the reference
string. - Used for measuring how well your algorithm
performs.
1
4
2
6 page faults
3
4
5
23Optimal Page Replacement
24Least Recently Used (LRU) Algorithm
- Reference string 1, 2, 3, 4, 1, 2, 5, 1, 2, 3,
4, 5 - Replace the page that has not been used for the
longest period of time. - This strategy is the optimal page-replacement
algorithm looking backward in time, rather than
forward.
1
5
2
3
4
5
4
3
25LRU Page Replacement
26LRU Algorithm Implementation
- Counter implementation
- Every page entry has a counter every time page
is referenced through this entry, copy the clock
into the counter. - When a page needs to be changed, look at the
counters to determine which page to replace. (The
page with the smallest time value) - Stack implementation keep a stack of page
numbers in a double link form - Page referenced
- move it to the top
- The LRU page is at the bottom
- No search needed for replacement
27Use Of A Stack to Record The Most Recent Page
References
28LRU Approximation Algorithms
- Reference bit
- With each page, associate a bit, initially 0
- When a page is referenced, its reference bit is
set to 1. - Replace the one which is 0 (if one exists). We
do not know the order, however. - Second-chance (clock) algorithm
- It's a modified FIFO algorithm with reference
bit. - When a page has been selected to be replaced, we
inspect its reference bit. - If the bit is 0, we replace the page. If the bit
is 1, we give the page a second chance and move
on to select the next FIFO page with the same
rules. - When a page gets a second chance, its reference
bit is reset to 0 and its arrival time is reset
to the current time. - A page that is given a second chance will not be
replaced until all other pages are replaced or
given second chances.
29Second-Chance (clock) Page-Replacement Algorithm
30Page-Buffering Algorithm
- In addition to a page-replacement algorithm, the
system keeps a pool of free frames. - When a page fault occurs, a victim frame is
chosen as before. However, the desired page is
load into a free frame from the pool before the
victim page is written out. - This procedure allows the process to restart as
soon as possible, without waiting for the victim
page to be written out. - When the victim page is later written out, its
frame is added to the free-frame pool.
31Allocation of Frames
- How do we allocate the fixed amount of free
memory among the various processes? - Each process needs a minimum number of frames.
- The minimum number of frames is defined by the
computer architecture (instruction set). - The maximum number of frames is defined by the
amount of available physical memory. - Two major allocation schemes.
- fixed allocation
- priority allocation
32Fixed Allocation
- Equal allocation e.g., if there are 100 frames
and 5 processes, then each process gets 20
frames. - Proportional allocation Allocate according to
the size of process.
33Priority Allocation
- Use a proportional allocation scheme, using
priorities rather than size. - If process Pi generates a page fault,
- select for replacement one of its frames.
- select for replacement a frame from a process
with lower priority.
34Global vs. Local Allocation
- Global replacement process selects a
replacement frame from the set of all frames one
process can take a frame from another. - Local replacement each process selects from
only its own set of allocated frames.
35Thrashing
- If a process does not have enough pages, the
page-fault rate is very high. This leads to - low CPU utilization.
- operating system thinks that it needs to increase
the degree of multiprogramming. - another process added to the system, causing more
page faults. - Thrashing ? busy swapping pages in and out
- ? doing more paging than
executing - A process is thrashing if it is spending more
time paging than executing.
36Thrashing
- To prevent thrashing, we must provide a process
as many frames as it needs. - But how do we know how many frames it needs?
- By the locality model of process execution.
37The Locality Model
- The Locality model says that as a process
executes, it moves from one locality to another. - A locality is a set of pages that are actively
used together by a process. - A program is generally composed of several
localities, which may overlap. For example, when
a method is called, it defines a new locality. - Why does thrashing occur?If we allocate fewer
frames than the size of the current locality, the
process will thrash, since it cannot keep in
memory all the pages that it is actively using.
38Locality In A Memory-Reference Pattern
39Working-Set Model
- It is based on the assumption of locality.
- ? ? working-set window ? a fixed number of the
most recent page references Example 10,000
page references - The set of pages in the most recent ? page
references is the working set. - The working set is an approximation of the
program's current locality. - If a page is in active use, it will be in the
working set. If it is no longer being used, it
will drop from the working set ? time units after
its last reference.
40Working-set model
? 10
41- WSSi (working-set size of Process Pi) total
number of pages referenced in the most recent ?
(varies in time) - if ? too small, will not encompass entire
locality. - if ? too large, will encompass several
localities. - if ? ?, will encompass entire program.
- D ? WSSi ? total demand for frames
- If the total demand is greater than the total
number of available frames (D gt m) ? Thrashing - The OS monitors the working set of each process
and allocates enough frames to provide it with
its working-set size. If there are enough extra
frames, another process can be initiated. - Policy if D gt m, then suspend one of the
processes.
42Page-Fault Frequency Scheme
- Want to prevent thrashing.
- Establish acceptable page-fault rate.
- If actual rate too low, process loses frame.
- If actual rate too high, process gains frame.
43Prepaging
- In a pure demand-paging system, a large number of
page faults occur when a process is started. This
situation is a result of trying to get the
initial locality into memory. - The same situation may arise at other times. For
instance, when a swapped-out process is
restarted, all its pages are on the disk and each
must be brought in by its own page fault. - Prepaging is the strategy to bring into memory at
one time all the pages that will be needed. - Prepaging attempts to prevent the high level of
initial paging.
44Page Size
- Page size selection considerations
- to minimize internal fragmentation gt small page
size - to minimize the size of page table gt large page
size - to minimize I/O time gt large page size
- to reduce I/O overhead and wasted allocated
memory gt small page size - to minimize the number of page faults gt large
page size - The trend is toward larger page size. This is the
result of CPU speeds and main memory capacity
increasing faster than disk speeds. Pentium II
allows page sizes to be either 4K or 4M bytes.
45Other Considerations
- Program structure
- int A new int10241024
- Assume that page size is 1024 words
- Each row is stored in one page
- Program 1 for (j 0 j lt A.length j) for
(i 0 i lt A.length i) Aij 11024
x 1024 page faults - Program 2 for (i 0 i lt A.length i) for
(j 0 j lt A.length j) Aij 1 - 1024 page faults
46Other Considerations
- I/O Interlock Pages must sometimes be locked
into memory. - Consider I/O. Pages that are used for copying a
file from a device must be locked from being
selected for eviction by a page replacement
algorithm.
47Real-Time Processing
- Real-time processes expect to gain control of the
CPU, and to run to completion with a minimum of
delays. - Virtual memory is against real-time computing,
because it can introduce unexpected, long-term
delays in the execution of a process while pages
are brought into memory. - Therefore, real-time systems almost never have
virtual memory.
48OS Example Windows NT
- Uses demand paging with clustering. Clustering
brings in pages surrounding the faulting page. - Processes are assigned working set minimum and
working set maximum. - Working set minimum is the minimum number of
pages the process is guaranteed to have in
memory. - A process may be assigned as many pages up to its
working set maximum. - When the amount of free memory in the system
falls below a threshold, automatic working set
trimming is performed to restore the amount of
free memory. - Working set trimming removes pages from processes
that have pages in excess of their working set
minimum.