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

ch10 Virtual Memory

The document discusses virtual memory and demand paging. It describes how pages are loaded into memory only when needed using demand paging. It also explains copy-on-write and different page replacement algorithms like FIFO, optimal and LRU. The performance of demand paging and factors affecting it are also covered.

Uploaded by

tmdgn0214
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)
28 views53 pages

ch10 Virtual Memory

The document discusses virtual memory and demand paging. It describes how pages are loaded into memory only when needed using demand paging. It also explains copy-on-write and different page replacement algorithms like FIFO, optimal and LRU. The performance of demand paging and factors affecting it are also covered.

Uploaded by

tmdgn0214
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

본 강의 자료는 Wiley 출판사에서 저작한 것을 강의용으로 수정 편집한 것이고, 본 영상은

명지대학교 데이터테크놀로지 전공 강의용으로 제작되었습니다. 본 강의 자료 및 영상의 무단


복제 및 배포는 저작권법에 위배되어 해당 법령에 의해 처벌될 수 있음을 유의 바랍니다.

Chapter 10
Virtual Memory

Operating System Concepts


Tenth Edition
Silberschatz, Galvin and Gagne
Chapter 10: Virtual Memory
◆ Background
◆ Demand Paging
◆ Copy-on-Write
◆ Page Replacement
◆ Allocation of Frames
◆ Thrashing
◆ Memory Compression
◆ Allocating Kernel Memory
◆ Other Considerations
◆ Operating-System Examples

Copyright © 2020 John Wiley & Sons, Inc. 2


Objectives
◆ Define virtual memory and describe its benefits
◆ Illustrate how pages are loaded into memory using demand paging
◆ Apply the FIFO, optimal, and LRU page-replacement algorithms
◆ Describe the working-set model of a process, and explain how it is
related to program locality
◆ To examine the relationship between shared memory and memory-
mapped files
◆ To explore how kernel memory is managed

Copyright © 2020 John Wiley & Sons, Inc. 3


Background
◆ Code needs to be in memory to execute, but entire program rarely
used
➢ Error code, unusual routines, large data structures
◆ Entire program code not needed at same time
◆ Only part of the program needs to be in memory for execution
◆ Benefits of executing partially-loaded program
➢ Program no longer constrained by limits of physical memory
➢ Each program takes less memory while running ➔ more programs run
at the same time ➔ Increased CPU utilization and throughput with no
increase in response time or turnaround time
➢ Less I/O needed to load or swap programs into memory ➔ each user
program runs faster

Copyright © 2020 John Wiley & Sons, Inc. 4


Virtual Memory - larger than Physical Memory
◆ Virtual memory
➢ separation of user logical memory from physical memory

➢ Logical address
space can therefore
be much larger than
physical address
space

backing store

Copyright © 2020 John Wiley & Sons, Inc. 5


Virtual Address Space
◆ Virtual address space – logical view of how process is stored in
memory
➢ Usually start at address 0, contiguous addresses until end of space
➢ Meanwhile, physical memory organized in page frames
➢ MMU must map logical to physical

◆ Sparse address spaces


➢ stack grows downward through function calls
➢ heap grows upward for dynamic memory allocation
➢ Unused address space between the two is hole
➢ No physical memory needed until heap or stack
grows to a given new page
➢ Holes can be filled for dynamically linked libraries,
etc

Copyright © 2020 John Wiley & Sons, Inc. 6


Shared Library using Virtual Memory
◆ Virtual memory allows files and memory to be shared by several
processes through page sharing

➢ System libraries can


be shared via
mapping into a
virtual address
space
➢ Processes can share
memory
➢ Pages can be
shared during fork(),
speeding process
creation

Copyright © 2020 John Wiley & Sons, Inc. 7


Demand Paging

◆ Demand paging – load a page in memory only when it is needed

◆ Valid–invalid bit is associated with each page table entry


➢ valid: both legal and in memory
➢ invalid: valid but not in memory or not valid(not in logical address
space of process)
◆ Initially valid–invalid bit is set to invalid on all entries

◆ During address translation, if invalid bit is set in page table entry


➔ page fault (trap to OS)

Copyright © 2020 John Wiley & Sons, Inc. 8


Page Table when some pages are Not in Main Memory

backing store

Copyright © 2020 John Wiley & Sons, Inc. 9


Page Fault
◆ If there is a reference to a page, first reference to that page will trap
to operating system: page fault

◆ Procedure for handling page fault


1) Checks an internal table(with PCB) to decide:
➢ Invalid reference ? ➔ terminate process
➢ Just not in memory ? ➔ page in
2) Find a free frame
3) Schedule a disk operation to read the page into frame
4) When read complete, modify the internal table & page table to
indicate that page is now in memory. Set validation bit = "valid"
5) Restart the instruction that interrupted by the page fault

Copyright © 2020 John Wiley & Sons, Inc. 10


Steps in Handling a Page Fault

backing store

Copyright © 2020 John Wiley & Sons, Inc. 11


Aspects of Demand Paging
◆ pure demand paging : never bring a page into memory until it is
required.
➢ start process with no pages in memory
➢ OS sets instruction pointer to first instruction of process, non-memory-
resident ➔ page fault
➢ And for every other process pages on first access
◆ locality of reference
➢ a given instruction could access multiple pages (one page for instruction
and many for data) ➔ multiple page faults ➔ unlikely behavior
◆ Hardware support needed for demand paging
➢ Page table with valid / invalid bit
➢ Secondary memory (swap device with swap space)
➢ Instruction restart

Copyright © 2020 John Wiley & Sons, Inc. 12


Free-Frame List
◆ Most OS maintain a free-frame list, a pool of free frames to bring
desired page into memory

◆ OS allocates free frames using zero-fill-on-demand


➢ frames are "zeroed-out" before allocated, erasing previous contents

◆ When starts up, all memory is placed on free-frame list


◆ Free frames are requested, shrinks

Copyright © 2020 John Wiley & Sons, Inc. 13


Performance of Demand Paging 1
◆ Stages to service a page fault
1. Trap to the operating system
2. Save the user registers and process state
3. Determine that the interrupt was a page fault
4. Check that the page reference was legal and determine the location of the
page on the disk
5. Issue a read from the disk to a free frame:
a. Wait in a queue for this device until the read request is serviced
b. Wait for the device seek and/or latency time
c. Begin the transfer of the page to a free frame
6. While waiting, allocate the CPU to some other user
7. Receive an interrupt from the disk I/O subsystem (I/O completed)
8. Save the registers and process state for the other user
9. Determine that the interrupt was from the disk
10. Correct the page table and other tables to show page is now in memory
11. Wait for the CPU to be allocated to this process again
12. Restore the user registers, process state, and new page table, and then
resume the interrupted instruction
Copyright © 2020 John Wiley & Sons, Inc. 14
Performance of Demand Paging 2
◆ Three major activities of page-fault service time
1) Service the page-fault interrupt: can be reduced with careful coding (just
several hundred instructions). 1~100 microseconds
2) Read in the page: lots of time. 8 milliseconds
3) Restart the process: again just a small amount of time
◆ page fault rate 0  p  1
➢ if p = 0 no page faults
➢ if p = 1, every reference is a fault

◆ Effective Access Time (EAT)


= (1 – p) x memory-access time + p x page-fault service time
(10~200 ns) (8 milliseconds)

Copyright © 2020 John Wiley & Sons, Inc. 15


Demand Paging Example
◆ Memory access time = 200 nanoseconds
◆ Average page-fault service time = 8 milliseconds
◆ EAT = (1 – p) x 200 + p (8 milliseconds)
= (1 – p) x 200 + p x 8,000,000
= 200 + p x 7,999,800
➔ Effective access time is directly proportional to page-fault rate
◆ If one access out of 1,000 causes a page fault, then
EAT = 8.2 microseconds.
This is a slowdown by a factor of 40!!
◆ If want performance degradation < 10 percent
➢ 220 > 200 + 7,999,800 x p
20 > 7,999,800 x p
➢ p < .0000025
➢ < one page fault in every 400,000 memory accesses

Copyright © 2020 John Wiley & Sons, Inc. 16


Copy-on-Write
◆ fork() system call use Copy-on-Write (COW)
➢ allows both parent and child processes to initially share the same pages
in memory
➢ Shared pages are marked as copy-on-write pages
➢ If either process modifies a shared page, only then is the page copied
◆ COW allows more efficient process creation as only modified pages
are copied
➢ used by Windows, Linux, and macOS

◆ vfork() - virtual memory fork - variation on fork() system call


has parent suspend and child uses address space of parent
➢ not use copy-on-write
➢ Designed to have child call exec()
➢ Very efficient

Copyright © 2020 John Wiley & Sons, Inc. 17


◆ Before process 1
modifies page C

◆ After process 1
modifies page C

Copyright © 2020 John Wiley & Sons, Inc. 18


Page Replacement
◆ Prevent 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

Copyright © 2020 John Wiley & Sons, Inc. 19


Need For Page Replacement

Copyright © 2020 John Wiley & Sons, Inc. 20


Page Replacement

◆ Note now potentially


2 page transfers for
page fault ➔
increasing EAT

Copyright © 2020 John Wiley & Sons, Inc. 21


Basic Page Replacement

1. Find the location of the desired page on disk


2. 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 victim frame to disk if dirty(modified). Change page & frame tables
3. Bring the desired page into the (newly) free frame; update the page
and frame tables
4. Continue the process by restarting the instruction that caused the
trap

Copyright © 2020 John Wiley & Sons, Inc. 22


Page Replacement Algorithms
◆ Frame-allocation algorithm
➢ How many frames to give each process
◆ Page-replacement algorithm
➢ Which frames to replace
➢ Want lowest page-fault rate on both first access and re-access
◆ Evaluate algorithm by running it on a particular string of memory
references (reference string) and computing the number of page
faults on that string
➢ String is just page numbers, not full addresses
➢ Repeated access to the same page does not cause a page fault
◆ In all our examples, the reference string of referenced page
numbers is
7,0,1,2,0,3,0,4,2,3,0,3,2,1,2,0,1,7,0,1

Copyright © 2020 John Wiley & Sons, Inc. 23


Graph of Page Faults vs Number of Frames
◆ As the number of frames available increases, the number of page
faults decreases

Copyright © 2020 John Wiley & Sons, Inc. 24


First-In-First-Out (FIFO) Page Replacement
◆ Reference string: 7,0,1,2,0,3,0,4,2,3,0,3,2,1,2,0,1,7,0,1
◆ 3 frames (3 pages can be in memory at a time)

➔15 page faults

◆ How to track ages of pages?


➢ Just use a FIFO queue

Copyright © 2020 John Wiley & Sons, Inc. 25


FIFO Illustrating Belady’s Anomaly
◆ Consider reference string: 1,2,3,4,1,2,5,1,2,3,4,5
◆ Adding more frames can cause more page faults! ➔ Belady’s Anomaly

Copyright © 2020 John Wiley & Sons, Inc. 26


Optimal Page Replacement
◆ Replace page that will not be used for longest period of time
➢ 9 is optimal for the example
◆ called OPT or MIN
◆ How do you know this?
➢ Can’t read the future
◆ Used for measuring how well your algorithm performs

➔9 page faults

Copyright © 2020 John Wiley & Sons, Inc. 27


Least Recently Used (LRU) Page Replacement 1
◆ Use past knowledge rather than future
◆ Replace page that has not been used in the most amount of time
◆ Associate time of last use with each page

➔12 page faults

◆ better than FIFO but worse than OPT


◆ Generally good algorithm and frequently used. How to implement?
◆ LRU and OPT are cases of stack algorithms that don’t have
Belady’s Anomaly
Copyright © 2020 John Wiley & Sons, Inc. 28
LRU Page Replacement 2
◆ Counter implementation
➢ Every page entry has a time-of-use field; A clock is added to CPU;
Whenever page is referenced, copy clock register into time-of-use field
➢ Replace the page with the smallest time value
✓ Search through table needed

◆ Stack implementation
➢ Keep a stack of page numbers
in a double link form:
➢ Whenever page referenced,
move it to the top
✓ requires 6 pointers to be
changed at worst
➢ Each update little more
expensive
➢ No search for replacement

Copyright © 2020 John Wiley & Sons, Inc. 29


LRU-Approximation Page Replacement
◆ LRU needs special hardware, but not many systems provide
sufficient

◆ Reference bit
➢ A bit associated with each entry in page table, initially = 0
➢ When page is referenced, the bit set to 1 by hardware
➢ Replace any with reference bit = 0 (if one exists)
➢ We can determine which pages have been used or not used, although
do not know the order

◆ Second-Chance algorithm
◆ Enhanced Second-Chance algorithm
◆ Additional-Reference-Bits algorithm

Copyright © 2020 John Wiley & Sons, Inc. 30


Second-Chance (clock) Algorithm

◆ Generally FIFO, plus


hardware-provided
reference bit

◆ If page to be replaced
has
➢ Reference bit = 0 ➔
replace it
➢ Reference bit = 1 ➔
✓ get second change
✓ set reference bit 0
✓ reset arrival time to
current time

Copyright © 2020 John Wiley & Sons, Inc. 31


Enhanced Second-Chance Algorithm
◆ Improve algorithm by using reference bit and modify bit

◆ Four classes with ordered pair (reference, modify)


1. (0, 0) neither recently used not modified – best page to replace
2. (0, 1) not recently used but modified – not quite as good, must write out
before replacement
3. (1, 0) recently used but clean – probably will be used again soon
4. (1, 1) recently used and modified – probably will be used again soon and
need to write out before replacement

◆ When page replacement called for, use the clock scheme but use
the four classes replace page in lowest non-empty class
➢ Might need to search circular queue several times

Copyright © 2020 John Wiley & Sons, Inc. 32


Additional-Reference-Bits Algorithm
◆ An 8-bit byte(shift register) for each page in a table contain history
of page use for the last eight time periods.
◆ At regular interval, OS shifts reference bit for each page into the
high-order bit of its 8-bit byte and shifts other bits right by 1 bit
➢ 00000000 – not used
➢ 11111111 – used at least once in each period
➢ 11000100 used more recently than 01110111
◆ Replace page with the lowest number
◆ The number of bits reduced to zero ➔ second-chance algorithm

Copyright © 2020 John Wiley & Sons, Inc. 33


Counting-based Page Replacement
◆ Keep a counter of the number of references that have been made to
each page
➢ Not common

◆ Least Frequently Used (LFU) Algorithm: replaces page with


smallest count

◆ Most Frequently Used (MFU) Algorithm: based on the argument


that the page with the smallest count was probably just brought in
and has yet to be used

Copyright © 2020 John Wiley & Sons, Inc. 34


Page-Buffering Algorithms
◆ Keep a pool of free frames, always
➢ Then frame available when needed, not found at fault time
➢ Read page into free frame and select victim to evict and add to free pool
➢ Allow process to restart as soon as possible
◆ Keep list of modified pages
➢ When backing store is idle, write modified pages there and reset modify
bit
◆ Keep free frame contents (which page was in each frame)
➢ If referenced again before reused, no need to load contents again from
disk
➢ Generally useful to reduce penalty if wrong victim frame selected

Copyright © 2020 John Wiley & Sons, Inc. 35


Allocation of Frames

◆ Each process needs minimum number of frames


◆ As number of frames allocated to each process decreases, page-
fault rate increases, slowing process execution
◆ Process must have enough frames to hold all different pages that
any single instruction can reference

◆ Maximum is total frames in the system

Copyright © 2020 John Wiley & Sons, Inc. 36


Allocation Algorithms
◆ Equal allocation
➢ For example, if there are 100 frames (after allocating frames for the OS)
and 5 processes, give each process 20 frames
➢ Keep some as free frame buffer pool
◆ Proportional allocation
➢ Allocate according to the size of process
si = size of process pi ➢ Ex: total 62 frames
S =  si ✓ P1 - 10 pages
✓ P2 - 127 pages
m = total number of frames
✓ P1 = 10/137 × 62 ≈ 4 frames
si ✓ P2 = 127/137 × 62 ≈ 57 frames
ai = allocation for pi = m
S
◆ Allocation may vary according to degree of multiprogramming
◆ One solution for high-priority process
➢ Use a proportional allocation scheme using priorities rather than size

Copyright © 2020 John Wiley & Sons, Inc. 37


Global vs. Local Allocation
◆ 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
number

◆ Global replacement – process selects a replacement frame from


the set of all frames; one process can take a frame from another
➢ But then process execution time can vary greatly
➢ But greater throughput so more common

◆ Local replacement – each process selects from only its own set of
allocated frames
➢ More consistent per-process performance
➢ But possibly underutilized memory

Copyright © 2020 John Wiley & Sons, Inc. 38


Reclaiming pages
◆ One strategy of global
replacement
◆ Keep the amount of
free memory above a
minimum threshold.
◆ When it drops below
the threshold, a kernel
routine(reaper) begins
reclaiming pages from
all processes.
◆ It continues until the
maximum threshold is
reached.
◆ LRU approximation
used

Copyright © 2020 John Wiley & Sons, Inc. 39


Non-Uniform Memory Access
◆ So far all memory accessed equally
◆ Many systems are NUMA – speed
of access to memory varies
➢ Consider system boards containing
CPUs and memory, interconnected
over a system bus
◆ Optimal performance comes from
allocating memory “close to” the
CPU on which the thread is
scheduled
➢ And modifying the scheduler to
schedule the thread on the same
system board when possible

Copyright © 2020 John Wiley & Sons, Inc. 40


Thrashing 1
◆ If a process does not have “enough” pages, the page-fault rate is
very high
➢ Page fault to get page
➢ Replace existing frame
➢ But quickly need replaced frame back
➢ This leads to:
✓ Low CPU utilization
✓ Operating system thinking that it needs to increase the degree of
multiprogramming
✓ Another process added to the system

◆ Thrashing : A process is spending more time paging than executing

Copyright © 2020 John Wiley & Sons, Inc. 41


Thrashing 2

◆ As degree of
multiprogramming
increases, CPU
utilization increases
until maximum is
reached
◆ If degree of
multiprogramming is
increased further,
thrashing sets in,
and CPU utilization
drops sharply

Copyright © 2020 John Wiley & Sons, Inc. 42


Cause of Thrashing
◆ Effects of thrashing can be limited by using local(or priority) page
replacement. But not entirely solved
◆ To prevent thrashing, provide a process with as many frames as it
needs. How many frames it "needs"?
◆ Locality model of process execution
➢ Process migrates from one locality to another
➢ Locality is a set of pages that are actively used together
➢ Localities may overlap
◆ Why does thrashing occur?
➢ If we don't allocate enough frames to accommodate the size of the
current locality, the process will thrash
➢  size of locality > total memory size

Copyright © 2020 John Wiley & Sons, Inc. 43


Locality
in a
Memory-Reference
Pattern

◆ Process's locality
changes over time
and may overlap

44
Working-Set Model 1
◆ based on assumption of locality
◆  : working-set window, a fixed number of page references
➢ Example: 10,000 instructions
◆ working set : set of pages in the most recent  page references
➢ Process is actively using the pages in its working set
➢ approximation of program's locality

Copyright © 2020 John Wiley & Sons, Inc. 45


Working-Set Model 2
◆ 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


➢ Approximation of locality
◆ If D > m (total number of available frames) ➔ Thrashing
◆ If sum of working-set sizes exceeds total number of available
frames (D > m), then suspend or swap out one of the processes
◆ Prevent thrashing
◆ keep the degree of multiprogramming
◆ Optimize CPU utilization

Copyright © 2020 John Wiley & Sons, Inc. 46


Working Sets and Page Fault Rates
◆ Direct relationship between working set of a process and its
page-fault rate
◆ Working set changes over time
◆ Peaks and valleys over time

Copyright © 2020 John Wiley & Sons, Inc. 47


Page-Fault Frequency
◆ More direct approach than working-set model
◆ Establish “acceptable” page-fault frequency (PFF) rate
➢ If page-fault rate exceeds upper limit, allocate process another frame
➢ If page-fault rate falls below lower limit, remove a frame from process
➢ If page-fault rate increases and no free frames, swap some process out

Copyright © 2020 John Wiley & Sons, Inc. 48


Other Considerations
◆ Prepaging
➢ To reduce the large number of page faults that occurs at process startup
➢ Prepage all or some of the pages a process will need, before they are
referenced
➢ But if prepaged pages are unused, I/O and memory was wasted
➢ Assume s pages are prepaged and α of the pages is used
✓ Is cost of s * α save pages faults > or < than the cost of prepaging
s * (1- α) unnecessary pages?
✓ α near zero  prepaging loses

Copyright © 2020 John Wiley & Sons, Inc. 49


Other Considerations – Page Size
◆ Sometimes OS designers have a choice
➢ Especially if running on custom-built CPU
◆ Page size selection must take into consideration:
➢ Fragmentation
➢ Page table size
➢ I/O overhead
➢ Number of page faults
➢ Locality
◆ Always power of 2, usually in the range 212 (4,096 bytes) to 222
(4,194,304 bytes)
◆ On average, growing over time

Copyright © 2020 John Wiley & Sons, Inc. 50


Other Considerations – Program Structure
◆ Data structures and program structures can increase locality and
lower page-fault rate
◆ page size – 128 words
➢ Each row is stored in one page
➢ Program 1
int[128,128] data;
for (j = 0; j <128; j++)
for (i = 0; i < 128; i++) ➔ 128 x 128
data[i,j] = 0; = 16,384 page faults

➢ Program 2
int[128,128] data;
for (i = 0; i < 128; i++)
for (j = 0; j < 128; j++) ➔ 128 page faults
data[i,j] = 0;

Copyright © 2020 John Wiley & Sons, Inc. 51


Summary

Copyright © 2020 John Wiley & Sons, Inc. 52


Summary

Copyright © 2020 John Wiley & Sons, Inc. 53

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy