0% found this document useful (0 votes)
62 views46 pages

Chapter 8: Virtual Memory: CS 472 Operating Systems Indiana University - Purdue University Fort Wayne

Virtual memory allows processes to have a logical address space larger than physical memory by swapping pages between memory and disk as needed. When a process accesses a page not in memory, a page fault occurs and the OS loads the page. Replacement policies like LRU and clock determine which page to swap out for the new one. Hardware structures like TLBs and inverted page tables optimize the two-level address translation needed for virtual memory.

Uploaded by

pradeep8184
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views46 pages

Chapter 8: Virtual Memory: CS 472 Operating Systems Indiana University - Purdue University Fort Wayne

Virtual memory allows processes to have a logical address space larger than physical memory by swapping pages between memory and disk as needed. When a process accesses a page not in memory, a page fault occurs and the OS loads the page. Replacement policies like LRU and clock determine which page to swap out for the new one. Hardware structures like TLBs and inverted page tables optimize the two-level address translation needed for virtual memory.

Uploaded by

pradeep8184
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 46

Chapter 8: Virtual Memory

CS 472 Operating Systems Indiana University Purdue University Fort Wayne

Virtual memory
Load on demand paging and/or segmentation Allows more processes to be active at a time, since only part of each process is in memory at a time One process may be larger than all of main memory The resident set is the set of pages or segments currently loaded in memory Virtual memory is possible because of the principle of locality
2

Principle of locality
3

Principle of locality
References to program instructions and data within a process tend to cluster Only a few pages of a process are needed over a short period of time It is possible to make intelligent guesses about which pages will be needed in the future This suggests that virtual memory may work efficiently
4

Virtual memory with paging


A valid bit must be added to each PTE to indicate if the page has been loaded into a page frame A page fault is an internal interrupt caused when there is a reference to a program page that has not been loaded into a page frame A page fault causes the OS to load the needed page into an available page frame

Thrashing
Thrashing is when there is excessive page fault activity
. . . particularly when pages are frequently replaced just before they are needed

The processor spends most of its time swapping pages rather than executing user instructions

Page fault activity


The page fault handler first moves the running process to the blocked state It then identifies a page frame to hold the needed page Typically, a loaded page needs to be overwritten The fault handler then directs the DMA to load the needed page from disk into the available frame When the DMA finishes, the interrupt handler moves the process that caused the fault from the blocked state to the ready queue
7

Hardware support for virtual memory


Additional hardware support is needed in addition to that for simple paging The valid bit in each PTE mentioned earlier A modify bit in each PTE to indicate if the page has been altered since it was last loaded into main memory If no change has been made, the page does not need to be written back to disk before it is overwritten by a new page
8

VAX system PTE


31 30 27 26 25 21 20 0

frame number

Holds 26-bit disk address if page not loaded protection (each page)

Valid bit (v): If the page is not loaded, generate a page fault and get disk address from bits 0-25 Modify bit (m): Set the bit with the first write to the page in memory Avoids write-back of clean page to disk
9

VAX system PTE


Virtual memory size = 232 bytes = 4 GB Page size = frame size = 512 bytes (29 bytes) Real memory with up to 221 frames is possible = 221 frames x 29 bytes/frame = 230 bytes = 1 GB
user processes
0 230 231 232

user space

stack space

system space

unused

shared 10

Note that each logical memory reference now requires two physical references.
11

Page tables: real or virtual?


The entire page table may take up too much main memory VAX example for each process: Potentially, 221 PTEs (4 bytes each) = 8 MB Therefore, page tables are also stored in virtual memory When a process is running, only part of its page table is in main memory
12

Page tables: real or virtual?


The system root page table for the process page tables must be locked in real memory The root page table may never swapped out Address translation now becomes a 2-step lookup
Get the PTE for the process page table Get the PTE for the reference page Get the instruction or data

Note that each logical memory reference now requires three physical references
A hardware solution is needed to keep things from running to slow
13

Translation Lookaside Buffer (TLB)


A high-speed cache for page table entries Contains PTEs that have been most recently used Hardware algorithm
Given a virtual address, processor examines the TLB If PTE is present (TLB hit), retrieve frame number and form real address If PTE is not found (TLB miss), do the 2-step lookup and update the TLB with the missing PTE for next time

Note on step 1 of the 2-step lookup


This uses the TLB again for the process page table lookup This might result in an additional page fault on the process page table page (independent of using a TLB). If so, block waiting for the process page table page and then resume
14

Page #s

PTEs

15

Inverted page table


Found on PowerPC, UltraSPARC, and IA-64 architecture Page number portion of a virtual address is hashed Hash value is an index into the inverted page table There is one PTE per available frame, in contrast to one for each virtual page (many fewer PTEs) There is only one table for all processes Inverted page table is of fixed size If hash succeeds, use frame #, else generate page fault
16

17

Other issues
Cache memory
PTE may be in TLB cache, real memory, or disk Instructions and data may be in real memory cache, real memory, or disk

Page size
Large

more internal fragmentation on the last page resident set less able to adapt to principle of locality larger page tables more page faults

Small

18

19

Example page sizes

20

Segmented virtual memory


Segments may be of unequal, dynamic size
Simplifies handling of growing data structures Allows a segment to be altered, recompiled, and relinked without linking and loading all segments Lends itself to sharing data among processes Lends itself to protection

21

Segmented virtual memory tables


Segment entry for each segment
Each entry contains the length of the segment along with the base address A valid bit is needed to determine if segment is already in main memory A modify bit is needed to determine if the segment has been modified since it was loaded

22

23

Combined paging/segmentation
Paging is transparent to the programmer Segmentation is visible to the programmer Each segment is broken into fixed-size pages

24

25

OS policies for virtual memory

26

Fetch policy
Determines when a page should be brought into memory Demand paging brings a page into main memory when a reference is made to a location on the page
This results in many page faults when process first started

Prepaging anticipates pages will be needed


It is efficient to bring in pages that reside contiguously on the disk Pointless if the pages are never referenced Prepaging is usually not warranted
27

Placement and replacement policy


A placement policy determines where in real memory a process piece is to reside
This is important in a segmentation system

A replacement policy determines which page is replaced


The page removed should be the page least likely to be referenced in the near future

Most policies predict the future behavior on the basis of past behavior
28

Replacement policy
Consider only the set of pages available to be swapped out
Ignore locked frames

Locked frames contain . . .


Kernel of the operating system Control structures System root page tables, for example I/O buffers

Associate a lock bit with each frame

29

Replacement policy algorithms


Optimal replacement policy
Selects for replacement that page for which the time to the next reference is the longest (if ever) Not practical

Impossible to have perfect knowledge of future events

So, use the principle of locality to improve the chances that the replaced page is unlikely to be referenced soon

30

Replacement policy algorithms


Least Recently Used (LRU)
Replaces the page that has not been referenced for the longest time By the principle of locality, this should be the page least likely to be referenced in the near future Inefficient to implement Each frame would need to be tagged with the time of last reference (much overhead)

31

Replacement policy algorithms


First-in, first-out (FIFO)
Logically treat page frames allocated to a process as arranged in a circular buffer Pages are replaced in round-robin style Simplest replacement policy to implement Page that has been in memory the longest is replaced But, these pages may be needed again very soon Imagine a grocery store eliminating bread and milk just because they had been stocked a long time
32

Replacement policy algorithms


Clock
Requires hardware use bit for each frame When a page is first loaded, the use bit is set to 1 When the page is referenced, the use bit is set to 1 When it is time to replace a page, the OS does a round-robin scan, choosing the first frame encountered which still has a clear use bit During the search for replacement, the OS clears each use bit it encounters
33

34

Replacement policy algorithms


Clock (continued)
The frame has to earn the right to stay the next time around This is the most effective practical method It can be enhanced by using the modify bit in the PTE Unmodified pages are preferred for replacement

35

36

Comparison of Placement Algorithms

37

Page buffering
A replaced page is added to a pool of free frames which have been replaced but not yet overwritten The pool is separated into two lists
Free page list of unmodified (clean) free pages Modified page list of pages that need to be written back to disk

Modified pages can be cleaned in batches and moved to the free page list
This decouples cleaning from replacement

Some page faults can be handled by simple bookkeeping Page buffering is best used in conjunction with the FIFO page replacement policy
Used by the VAX/VMS operating system
38

Resident set size


Fixed-allocation for each process
Gives a process a fixed number of page frames within which to execute When a page fault occurs, a frame is reclaimed from the current process Difficult to allocate the optimal number of frames

39

Resident set size


Variable-allocation for each process
Size of the resident set of a process varies and adapts over the lifetime of the process Involves OS overhead Global scope: a page fault gives the process a new frame taken from any process Local scope: frames are reclaimed from the current process, but the allocation size is adjusted periodically based on the fault rate

40

Cleaning policy
Demand cleaning
A page is written out only when it has been selected for replacement

Precleaning
Pages are written out in batches (cleaned) It works best to combine precleaning with page buffering

41

Working set
Definition The working set W(t,) of a process is the set of process pages at virtual time t that have been referenced in the previous virtual time units The page fault rate is low if | resident set | > | W(t,) | Goal: Allow as many processes as possible consistent with a low fault rate
42

43

Load control (L=S Criterion)


Adjust the number of processes so that
mean time between faults = mean time needed to process a fault

Determines the optimal multiprogramming level


= the number of processes resident in main memory

Processor use is maximized at this point


If too few processes, all processes may be blocked from time to time If too many processes, thrashing will occur

To reduce the number or processes, some could be suspended


44

Multiprogramming level

45

Process Suspension: choose the . . .


Lowest priority process Faulting process - does not have much of its working set in main memory so it will be blocked anyway Last process activated - unlikely to have its working set resident Process with smallest resident set - requires the least future effort to reload Largest process yields the most free frames
46

You might also like