SlideShare a Scribd company logo
Chapter 12
Virtual Memory
Copyright © 2008
Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 12.2
Operating Systems, by Dhananjay Dhamdhere 2
Introduction
• Virtual Memory Basics
• Demand Paging
• The Virtual Memory Manager
• Page Replacement Policies
• Controlling Memory Allocation to a Process
• Shared Pages
• Memory-Mapped Files
• Case Studies of Virtual Memory Using Paging
• Virtual Memory Using Segmentation
Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 12.3
• MMU translates logical address into physical one
• Virtual memory manager is a software component
– Uses demand loading
– Exploits locality of reference to improve performance
Operating Systems, by Dhananjay Dhamdhere 3
Virtual Memory Basics
Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 12.4
Operating Systems, by Dhananjay Dhamdhere 4
Virtual Memory Basics (continued)
Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 12.5
Operating Systems, by Dhananjay Dhamdhere 5
Virtual Memory Using Paging
• MMU performs address translation using page table
Effective memory address of logical address (pi, bi)
= start address of the page frame containing page pi + bi
Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 12.6
Operating Systems, by Dhananjay Dhamdhere 6
Demand Paging Preliminaries
Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 12.7
Operating Systems, by Dhananjay Dhamdhere 7
Demand Paging Preliminaries
(continued)
• Memory Management Unit (MMU) raises a page fault
interrupt if page containing logical address not in
memory
Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 12.8
Operating Systems, by Dhananjay Dhamdhere 8
Demand Paging Preliminaries
(continued)
A page fault interrupt
is raised because
Valid bit of page 3 is 0
Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 12.9
Operating Systems, by Dhananjay Dhamdhere 9
Demand Paging Preliminaries
(continued)
• At a page fault, the required page is loaded in a free
page frame
• If no page frame is free, virtual memory manager
performs a page replacement operation
– Page replacement algorithm
– Page-out initiated if page is dirty (modified bit is set)
• Page-in and page-out: page I/O or page traffic
• Effective memory access time in demand paging:
Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 12.10
• (Empirical) law of locality of reference: logical
addresses used by process in a short interval tend to
be grouped in certain portions of its logical address
space
Operating Systems, by Dhananjay Dhamdhere 10
Page Replacement
Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 12.11
• How much memory to allocate to a process
Operating Systems, by Dhananjay Dhamdhere 11
Memory Allocation to a Process
Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 12.12
Operating Systems, by Dhananjay Dhamdhere 12
Optimal Page Size
• Size of a page is defined by computer hardware
• Page size determines:
– No of bits required to represent byte number in a page
– Memory wastage due to internal fragmentation
– Size of the page table for a process
– Page fault rates when a fixed amount of memory is
allocated to a process
• Use of larger page sizes than optimal value implies
somewhat higher page fault rates for a process
– Tradeoff between HW cost and efficient operation
Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 12.13
Operating Systems, by Dhananjay Dhamdhere 13
Paging Hardware
• Page-table-address-register (PTAR) points to the start
of a page table
Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 12.14
Operating Systems, by Dhananjay Dhamdhere 14
Paging Hardware (continued)
Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 12.15
Operating Systems, by Dhananjay Dhamdhere 15
Memory Protection
• Memory protection violation raised if:
– Process tries to access a nonexistent page
– Process exceeds its (page) access privileges
• It is implemented through:
– Page table size register (PTSR) of MMU
• Kernel records number of pages contained in a process in
its PCB
– Loads number from PCB in PTSR when process is scheduled
– Prot info field of the page’s entry in the page table
Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 12.16
• Translation look-aside buffer (TLB): small and fast
associative memory used to speed up address
translation
Operating Systems, by Dhananjay Dhamdhere 16
Address Translation and Page Fault
Generation
Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 12.17
Operating Systems, by Dhananjay Dhamdhere 17
Address Translation and Page Fault
Generation (continued)
• TLBs can be HW or SW managed
Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 12.18
Operating Systems, by Dhananjay Dhamdhere 18
Address Translation and Page Fault
Generation (continued)
• Some mechanisms used to improve performance:
– Wired TLB entries for kernel pages: never replaced
– Superpages
TLB hit ratio
Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 12.19
Superpages
• TLB reach is stagnant even though memory sizes
increase rapidly as technology advances
– TLB reach = page size x no of entries in TLB
– It affects performance of virtual memory
• Superpages are used to increase the TLB reach
– A superpage is a power of 2 multiple of page size
– Its start address (both logical and physical) is aligned on
a multiple of its own size
– Max TLB reach = max superpage size x no of entries in
TLB
– Size of a superpage is adapted to execution behavior of a
process through promotions and demotions
Operating Systems, by Dhananjay Dhamdhere 19
Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 12.20
Operating Systems, by Dhananjay Dhamdhere 20
Support for Page Replacement
• Virtual memory manager needs following information
for minimizing page faults and number of page-in and
page-out operations:
– The time when a page was last used
• Expensive to provide enough bits for this purpose
• Solution: use a single reference bit
– Whether a page is dirty
• A page is clean if it is not dirty
• Solution: modified bit in page table entry
Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 12.21
Operating Systems, by Dhananjay Dhamdhere 21
Practical Page Table Organizations
• A process with a large address space requires a large
page table, which occupies too much memory
• Solutions:
– Inverted page table
• Describes contents of each page frame
– Size governed by size of memory
– Independent of number and sizes of processes
– Contains pairs of the form (program id, page #)
• Con: information about a page must be searched
– Multilevel page table
• Page table of process is paged
Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 12.22
Operating Systems, by Dhananjay Dhamdhere 22
Inverted Page Tables
Use of hash table
Speeds up search
Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 12.23
Operating Systems, by Dhananjay Dhamdhere 23
Multilevel Page Tables
• If size of a table entry is 2e
bytes, number of page
table entries in one PT
page is 2nb/2e
• Logical address (pi , bi) is
regrouped into three fields:
– PT page with the number
pi
1 contains entry for pi
– pi
2 is entry number for pi
in PT page
– bi
Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 12.24
Operating Systems, by Dhananjay Dhamdhere 24
I/O Operations in a Paged
Environment
• Process makes system call for I/O operations
– Parameters include: number of bytes to transfer, logical
address of the data area
• Call activates I/O handler in kernel
– I/O subsystem does not contain an MMU, so I/O handler
replaces logical address of data area with physical
address, using information from process page table
– I/O fix (bit in misc info field) ensures pages of data area
are not paged out
– Scatter/gather feature can deposit parts of I/O operation’s
data in noncontiguous memory areas
– Alternatively, data area pages put in contiguous areas
Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 12.25
Operating Systems, by Dhananjay Dhamdhere 25
Example: I/O Operations in Virtual
Memory
Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 12.26
Operating Systems, by Dhananjay Dhamdhere 26
The Virtual Memory Manager
Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 12.27
Operating Systems, by Dhananjay Dhamdhere 27
Example: Page Replacement
Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 12.28
Operating Systems, by Dhananjay Dhamdhere 28
Overview of Operation of the Virtual
Memory Manager
• Virtual memory manager makes two important
decisions during its operation:
– Upon a page fault, decides which page to replace
– Periodically decides how many page frames should be
allocated to a process
Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 12.29
Operating Systems, by Dhananjay Dhamdhere 29
Page Replacement Policies
• A page replacement policy should replace a page not
likely to be referenced in the immediate future
• Examples:
– Optimal page replacement policy
• Minimizes total number of page faults; infeasible in practice
– First-in first-out (FIFO) page replacement policy
– Least recently used (LRU) page replacement policy
• Basis: locality of reference
• Page reference strings
– Trace of pages accessed by a process during its
operation
– We associate a reference time string with each
Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 12.30
Operating Systems, by Dhananjay Dhamdhere 30
Example: Page Reference String
• A computer supports instructions that are 4 bytes in
length
– Uses a page size of 1KB
– Symbols A and B are in pages 2 and 5
Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 12.31
Operating Systems, by Dhananjay Dhamdhere 31
Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 12.32
Operating Systems, by Dhananjay Dhamdhere 32
Page Replacement Policies
(continued)
• To achieve desirable page fault characteristics, faults
shouldn’t increase when memory allocation is increased
– Policy must have stack (or inclusion) property
Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 12.33
Operating Systems, by Dhananjay Dhamdhere 33
FIFO page replacement policy does not exhibit stack property.
Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 12.34
Operating Systems, by Dhananjay Dhamdhere 34
Page Replacement Policies
(continued)
• Virtual memory manager cannot use FIFO policy
– Increasing allocation to a process may increase page
fault frequency of process
• Would make it impossible to control thrashing
Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 12.35
Operating Systems, by Dhananjay Dhamdhere 35
Practical Page Replacement Policies
• Virtual memory manager has two threads
– Free frames manager implements page replacement
policy
– Page I/O manager performs page-in/out operations
Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 12.36
Operating Systems, by Dhananjay Dhamdhere 36
Practical Page Replacement Policies
(continued)
• LRU replacement is not feasible
– Computers do not provide sufficient bits in the ref info
field to store the time of last reference
• Most computers provide a single reference bit
– Not recently used (NRU) policies use this bit
• Simplest NRU policy: Replace an unreferenced page and
reset all reference bits if all pages have been referenced
• Clock algorithms provide better discrimination between
pages by resetting reference bits periodically
– One-handed clock algorithm
– Two-handed clock algorithm
» Resetting pointer (RP) and examining pointer (EP)
Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 12.37
Operating Systems, by Dhananjay Dhamdhere 37
Example: Two-Handed Clock
Algorithm
• Both pointers are advanced simultaneously
• Algorithm properties defined by pointer distance:
– If pointers are close together, only recently used pages
will survive in memory
– If pointers are far apart, only pages that have not been
used in a long time would be removed
Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 12.38
Operating Systems, by Dhananjay Dhamdhere 38
Controlling Memory Allocation to a
Process
• Process Pi is allocated alloci number of page frames
• Fixed memory allocation
– Fixes alloc statically; uses local page replacement
• Variable memory allocation
– Uses local and/or global page replacement
– If local replacement is used, handler periodically
determines correct alloc value for a process
• May use working set model
• Sets alloc to size of the working set
Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 12.39
Operating Systems, by Dhananjay Dhamdhere 39
Implementation of a Working Set
Memory Allocator
• Swap out a process if alloc page frames cannot be
allocated
• Expensive to determine WSi(t,∆) and alloci at every time
instant t
– Solution: Determine working sets periodically
• Sets determined at end of an interval are used to decide
values of alloc for use during the next interval
Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 12.40
Operating Systems, by Dhananjay Dhamdhere 40
Shared Pages
• Static sharing results from static binding performed by a
linker/loader before execution of program
• Dynamic binding conserves memory by binding same
copy of a program/data to several processes
– Program or data shared retains its identity
– Two conditions should be satisfied:
• Shared program should be coded as reentrant
– Can be invoked by many processes at the same time
• Program should be bound to identical logical addresses in
every process that shared it
Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 12.41
Operating Systems, by Dhananjay Dhamdhere 41
Shared pages
should have same
page numbers in
all processes
Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 12.42
Operating Systems, by Dhananjay Dhamdhere 42
Copy-on-Write
• Feature used to conserve memory when data in shared
pages could be modified
– Copy-on-write flag in page table entries Memory
allocation decisions are performed statically
A private copy of
page k is made
when A modifies it
Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 12.43
Operating Systems, by Dhananjay Dhamdhere 43
Memory-Mapped Files
• Memory mapping of a file by a process binds file to a
part of the logical address space of the process
– Binding is performed when process makes a memory
map system call
– Analogous to dynamic binding of programs and data
Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 12.44
Operating Systems, by Dhananjay Dhamdhere 44
Memory-Mapped Files (continued)
Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 12.45
Operating Systems, by Dhananjay Dhamdhere 45
Case Studies of Virtual Memory Using
Paging
• Unix Virtual Memory
• Linux Virtual Memory
• Virtual Memory in Solaris
• Virtual Memory in Windows
Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 12.46
Operating Systems, by Dhananjay Dhamdhere 46
Unix Virtual Memory
• Paging hardware differs in architectures
• Pages can be: resident, unaccessed, swapped-out
• Allocation of as little swap space as possible
• Copy-on-write for fork
• Lack reference bit in some HW architectures;
compensated using valid bit in interesting manner
• Process can fix some pages in memory
• Pageout daemon uses a clock algorithm
– Swaps out a process if all required pages cannot be in
memory
– A swap-in priority is used to avoid starvation
Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 12.47
Operating Systems, by Dhananjay Dhamdhere 47
Linux Virtual Memory
• Page size of 4 KB
• On 64-bit architectures, uses three-level page table
• States for page frames: free, active, inactive dirty,
inactive laundered, inactive clean
• Page replacement based on a clock algorithm
– Uses two lists called active list and inactive list
• Buddy system allocator for allocating page frames
• Several virtual memory regions for a process:
– Zero-filled, file-backed, private memory
Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 12.48
Operating Systems, by Dhananjay Dhamdhere 48
Virtual Memory in Solaris
• Supports normal pages and superpages
– Superpages:
• Automatically for processes with large address spaces
• Can be requested using memcntl system call
• Not used for memory-mapped files
• Solaris 6 introduced priority paging to avoid interference
between file processing and virtual memory
• Page scanner tries to keep a sufficient number of page
frames on cyclic page cache (since Solaris 8)
– lotsfree parameter indicates how many page frames
should be free
– Uses two-handed clock algorithm on a global basis
Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 12.49
Operating Systems, by Dhananjay Dhamdhere 49
Virtual Memory in Windows
• Supports both 32-bit and 64-bit logical addresses
• Page size is 4 KB
• Process address space is either 2 GB or 3 GB
• Two-, three- or four-level page tables and various page
table entry formats
– On the X-86 architecture:
• Page frame can be in one of eight states, including:
valid, free, zeroed, standby, modified, and bad
• A process must first reserve virtual address space and
then commit it for specific entities
Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 12.50
Operating Systems, by Dhananjay Dhamdhere 50
Virtual Memory in Windows
(continued)
• A section object represents a section of memory that
can be shared
– A process maps a view to access part of a section
– Copy-on-write feature used for sharing pages
– Prototype PTE is set-up for shared pages
• TLBs are managed by HW (32-bit) or SW (64-bit)
• Exploits reference locality: loads a few pages before
and after a page-faulted page into memory
• Uses notion of working sets (for memory allocation)
– Clock algorithm
• Page lists: free, zero-initiated, modified, standby
Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 12.51
Operating Systems, by Dhananjay Dhamdhere 51
Virtual Memory Using Segmentation
• A segment is a logical entity in a program, such as a
function, a data structure, or an object
– Or, a module that consists of some or all of these
– Convenient unit for sharing and protection
Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 12.52
Operating Systems, by Dhananjay Dhamdhere 52
Example: Effective Address
Calculation in Segmentation
• Logical address (si, bi) can be specified as ids
– (alpha, beta)
• alpha: name of a segment
• beta: id associated with a byte contained in alpha
Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 12.53
Operating Systems, by Dhananjay Dhamdhere 53
Management of Memory
• Similarities to paging:
– Segment fault indicates segment is not in memory
– Segment-in operation is performed to load segment
• Segment-out operations may be needed first
– Can use working set of segment for allocation
• Segments can be replaced on NRU basis
• Differences to paging:
– Can lead to external fragmentation
• Tackled through compaction or through memory reuse
techniques (first fit, best fit, etc.)
– Segments can dynamically grow or shrink in size
Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 12.54
Operating Systems, by Dhananjay Dhamdhere 54
Sharing and Protection
• Two important issues in protection and sharing of
segments are:
– Static and dynamic sharing of segments
– Detecting use of invalid addresses
• Protection exception if bi exceeds size of si
Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 12.55
• External fragmentation exists in a virtual memory using
segmentation
– Solution: segmentation with paging
Operating Systems, by Dhananjay Dhamdhere 55
Segmentation with Paging
Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 12.56
Operating Systems, by Dhananjay Dhamdhere 56
Summary
• Basic actions in virtual memory using paging: address
translation and demand loading of pages
– Implemented jointly by
• Memory Management Unit (MMU): Hardware
• Virtual memory manager: Software
• Memory is divided into page frames
• Virtual memory manager maintains a page table
– Inverted and multilevel page tables use less memory but
are less efficient
– A fast TLB is used to speed up address translation
Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 12.57
Operating Systems, by Dhananjay Dhamdhere 57
Summary (continued)
• Which page should VM manager remove from memory
to make space for a new page?
– Page replacement algorithms exploit locality of reference
• LRU has stack property, but is expensive
• NRU algorithms are used in practice
– E.g., clock algorithms
• How much memory should manager allocate?
– Use working set model to avoid thrashing
• Copy-on-write can be used for shared pages
• Memory mapping of files speeds up access to data

More Related Content

Similar to Chapter_12 Virtual memory.pptx

Distributed Operating System_3
Distributed Operating System_3Distributed Operating System_3
Distributed Operating System_3
Dr Sandeep Kumar Poonia
 
Lect 27 28_29_30_virtual_memory
Lect 27 28_29_30_virtual_memoryLect 27 28_29_30_virtual_memory
Lect 27 28_29_30_virtual_memory
Soumendu Sarkar
 
memory
memorymemory
Computer memory management
Computer memory managementComputer memory management
Computer memory management
Kumar
 
Unit 5Memory management.pptx
Unit 5Memory management.pptxUnit 5Memory management.pptx
Unit 5Memory management.pptx
SourabhRaj29
 
Lecture 8- Virtual Memory Final.pptx
Lecture 8- Virtual Memory Final.pptxLecture 8- Virtual Memory Final.pptx
Lecture 8- Virtual Memory Final.pptx
Amanuelmergia
 
4 (1)
4 (1)4 (1)
4 (1)
Mothi R
 
chap.4.memory.manag.ppt
chap.4.memory.manag.pptchap.4.memory.manag.ppt
chap.4.memory.manag.ppt
amadayshwan
 
OS_Ch10
OS_Ch10OS_Ch10
Ch10 OS
Ch10 OSCh10 OS
Ch10 OS
C.U
 
OSCh10
OSCh10OSCh10
Virtual Memory in Windows
Virtual Memory in Windows Virtual Memory in Windows
Virtual Memory in Windows
HanzlaRafique
 
Understanding operating systems 5th ed ch03
Understanding operating systems 5th ed ch03Understanding operating systems 5th ed ch03
Understanding operating systems 5th ed ch03
BarrBoy
 
Mca ii os u-4 memory management
Mca  ii  os u-4 memory managementMca  ii  os u-4 memory management
Mca ii os u-4 memory management
Rai University
 
OS Presentation 2023.pptx
OS Presentation 2023.pptxOS Presentation 2023.pptx
OS Presentation 2023.pptx
ShayanAli68
 
CSI-503 - 9. Virtual Memory
CSI-503 - 9. Virtual MemoryCSI-503 - 9. Virtual Memory
CSI-503 - 9. Virtual Memory
ghayour abbas
 
ch9_virMem.pdf
ch9_virMem.pdfch9_virMem.pdf
ch9_virMem.pdf
HoNguyn746501
 
Chap8 Virtual Memory. 1997-2003.ppt
Chap8 Virtual Memory. 1997-2003.pptChap8 Virtual Memory. 1997-2003.ppt
Chap8 Virtual Memory. 1997-2003.ppt
Muteebulhassan
 
cache
cachecache
Operating system 38 page replacement
Operating system 38 page replacementOperating system 38 page replacement
Operating system 38 page replacement
Vaibhav Khanna
 

Similar to Chapter_12 Virtual memory.pptx (20)

Distributed Operating System_3
Distributed Operating System_3Distributed Operating System_3
Distributed Operating System_3
 
Lect 27 28_29_30_virtual_memory
Lect 27 28_29_30_virtual_memoryLect 27 28_29_30_virtual_memory
Lect 27 28_29_30_virtual_memory
 
memory
memorymemory
memory
 
Computer memory management
Computer memory managementComputer memory management
Computer memory management
 
Unit 5Memory management.pptx
Unit 5Memory management.pptxUnit 5Memory management.pptx
Unit 5Memory management.pptx
 
Lecture 8- Virtual Memory Final.pptx
Lecture 8- Virtual Memory Final.pptxLecture 8- Virtual Memory Final.pptx
Lecture 8- Virtual Memory Final.pptx
 
4 (1)
4 (1)4 (1)
4 (1)
 
chap.4.memory.manag.ppt
chap.4.memory.manag.pptchap.4.memory.manag.ppt
chap.4.memory.manag.ppt
 
OS_Ch10
OS_Ch10OS_Ch10
OS_Ch10
 
Ch10 OS
Ch10 OSCh10 OS
Ch10 OS
 
OSCh10
OSCh10OSCh10
OSCh10
 
Virtual Memory in Windows
Virtual Memory in Windows Virtual Memory in Windows
Virtual Memory in Windows
 
Understanding operating systems 5th ed ch03
Understanding operating systems 5th ed ch03Understanding operating systems 5th ed ch03
Understanding operating systems 5th ed ch03
 
Mca ii os u-4 memory management
Mca  ii  os u-4 memory managementMca  ii  os u-4 memory management
Mca ii os u-4 memory management
 
OS Presentation 2023.pptx
OS Presentation 2023.pptxOS Presentation 2023.pptx
OS Presentation 2023.pptx
 
CSI-503 - 9. Virtual Memory
CSI-503 - 9. Virtual MemoryCSI-503 - 9. Virtual Memory
CSI-503 - 9. Virtual Memory
 
ch9_virMem.pdf
ch9_virMem.pdfch9_virMem.pdf
ch9_virMem.pdf
 
Chap8 Virtual Memory. 1997-2003.ppt
Chap8 Virtual Memory. 1997-2003.pptChap8 Virtual Memory. 1997-2003.ppt
Chap8 Virtual Memory. 1997-2003.ppt
 
cache
cachecache
cache
 
Operating system 38 page replacement
Operating system 38 page replacementOperating system 38 page replacement
Operating system 38 page replacement
 

Recently uploaded

按照学校原版(ArtEZ文凭证书)ArtEZ艺术学院毕业证快速办理
按照学校原版(ArtEZ文凭证书)ArtEZ艺术学院毕业证快速办理按照学校原版(ArtEZ文凭证书)ArtEZ艺术学院毕业证快速办理
按照学校原版(ArtEZ文凭证书)ArtEZ艺术学院毕业证快速办理
evnum
 
欧洲杯外围-欧洲杯外围赛程-欧洲杯外围压注|【​网址​🎉ac99.net🎉​】
欧洲杯外围-欧洲杯外围赛程-欧洲杯外围压注|【​网址​🎉ac99.net🎉​】欧洲杯外围-欧洲杯外围赛程-欧洲杯外围压注|【​网址​🎉ac99.net🎉​】
欧洲杯外围-欧洲杯外围赛程-欧洲杯外围压注|【​网址​🎉ac99.net🎉​】
karimimorine448
 
美洲杯买球-美洲杯买球下注平台-美洲杯买球投注平台|【​网址​🎉ac55.net🎉​】
美洲杯买球-美洲杯买球下注平台-美洲杯买球投注平台|【​网址​🎉ac55.net🎉​】美洲杯买球-美洲杯买球下注平台-美洲杯买球投注平台|【​网址​🎉ac55.net🎉​】
美洲杯买球-美洲杯买球下注平台-美洲杯买球投注平台|【​网址​🎉ac55.net🎉​】
ahmedendrise81
 
Connect to Grow: The power of building networks
Connect to Grow: The power of building networksConnect to Grow: The power of building networks
Connect to Grow: The power of building networks
Eirini SYKA-LERIOTI
 
欧洲杯投注-欧洲杯投注投注官方网站-欧洲杯投注买球投注官网|【​网址​🎉ac99.net🎉​】
欧洲杯投注-欧洲杯投注投注官方网站-欧洲杯投注买球投注官网|【​网址​🎉ac99.net🎉​】欧洲杯投注-欧洲杯投注投注官方网站-欧洲杯投注买球投注官网|【​网址​🎉ac99.net🎉​】
欧洲杯投注-欧洲杯投注投注官方网站-欧洲杯投注买球投注官网|【​网址​🎉ac99.net🎉​】
mukeshomran942
 
Switching Careers Slides - JoyceMSullivan SocMediaFin - 2024Jun11.pdf
Switching Careers Slides - JoyceMSullivan SocMediaFin -  2024Jun11.pdfSwitching Careers Slides - JoyceMSullivan SocMediaFin -  2024Jun11.pdf
Switching Careers Slides - JoyceMSullivan SocMediaFin - 2024Jun11.pdf
SocMediaFin - Joyce Sullivan
 
欧洲杯足彩-欧洲杯足彩体育投注-欧洲杯足彩投注网站|【​网址​🎉ac99.net🎉​】
欧洲杯足彩-欧洲杯足彩体育投注-欧洲杯足彩投注网站|【​网址​🎉ac99.net🎉​】欧洲杯足彩-欧洲杯足彩体育投注-欧洲杯足彩投注网站|【​网址​🎉ac99.net🎉​】
欧洲杯足彩-欧洲杯足彩体育投注-欧洲杯足彩投注网站|【​网址​🎉ac99.net🎉​】
lemike859
 
一比一原版布拉德福德大学毕业证(bradford毕业证)如何办理
一比一原版布拉德福德大学毕业证(bradford毕业证)如何办理一比一原版布拉德福德大学毕业证(bradford毕业证)如何办理
一比一原版布拉德福德大学毕业证(bradford毕业证)如何办理
taqyea
 
一比一原版(uwm毕业证书)美国威斯康星大学密尔沃基分校毕业证如何办理
一比一原版(uwm毕业证书)美国威斯康星大学密尔沃基分校毕业证如何办理一比一原版(uwm毕业证书)美国威斯康星大学密尔沃基分校毕业证如何办理
一比一原版(uwm毕业证书)美国威斯康星大学密尔沃基分校毕业证如何办理
aweuwyo
 
在线办理(UOIT毕业证书)安大略省理工大学毕业证在读证明一模一样
在线办理(UOIT毕业证书)安大略省理工大学毕业证在读证明一模一样在线办理(UOIT毕业证书)安大略省理工大学毕业证在读证明一模一样
在线办理(UOIT毕业证书)安大略省理工大学毕业证在读证明一模一样
yhkox
 
BUKU PENJAGAAN BUKU PENJAGAAN BUKU PENJAGAAN
BUKU PENJAGAAN BUKU PENJAGAAN BUKU PENJAGAANBUKU PENJAGAAN BUKU PENJAGAAN BUKU PENJAGAAN
BUKU PENJAGAAN BUKU PENJAGAAN BUKU PENJAGAAN
cahgading001
 
体育博彩论坛-十大体育博彩论坛-体育博彩论坛|【​网址​🎉ac55.net🎉​】
体育博彩论坛-十大体育博彩论坛-体育博彩论坛|【​网址​🎉ac55.net🎉​】体育博彩论坛-十大体育博彩论坛-体育博彩论坛|【​网址​🎉ac55.net🎉​】
体育博彩论坛-十大体育博彩论坛-体育博彩论坛|【​网址​🎉ac55.net🎉​】
waldorfnorma258
 
一比一原版(surrey毕业证书)英国萨里大学毕业证成绩单修改如何办理
一比一原版(surrey毕业证书)英国萨里大学毕业证成绩单修改如何办理一比一原版(surrey毕业证书)英国萨里大学毕业证成绩单修改如何办理
一比一原版(surrey毕业证书)英国萨里大学毕业证成绩单修改如何办理
gnokue
 
Community Skills Building Workshop | PMI Silver Spring Chapter | June 12, 2024
Community Skills Building Workshop | PMI Silver Spring Chapter  | June 12, 2024Community Skills Building Workshop | PMI Silver Spring Chapter  | June 12, 2024
Community Skills Building Workshop | PMI Silver Spring Chapter | June 12, 2024
Hector Del Castillo, CPM, CPMM
 
欧洲杯买球-欧洲杯买球买球推荐-欧洲杯买球买球推荐网站|【​网址​🎉ac10.net🎉​】
欧洲杯买球-欧洲杯买球买球推荐-欧洲杯买球买球推荐网站|【​网址​🎉ac10.net🎉​】欧洲杯买球-欧洲杯买球买球推荐-欧洲杯买球买球推荐网站|【​网址​🎉ac10.net🎉​】
欧洲杯买球-欧洲杯买球买球推荐-欧洲杯买球买球推荐网站|【​网址​🎉ac10.net🎉​】
ahmedendrise81
 
办理阿卡迪亚大学毕业证(uvic毕业证)本科文凭证书原版一模一样
办理阿卡迪亚大学毕业证(uvic毕业证)本科文凭证书原版一模一样办理阿卡迪亚大学毕业证(uvic毕业证)本科文凭证书原版一模一样
办理阿卡迪亚大学毕业证(uvic毕业证)本科文凭证书原版一模一样
kkkkr4pg
 
一比一原版美国西北大学毕业证(NWU毕业证书)学历如何办理
一比一原版美国西北大学毕业证(NWU毕业证书)学历如何办理一比一原版美国西北大学毕业证(NWU毕业证书)学历如何办理
一比一原版美国西北大学毕业证(NWU毕业证书)学历如何办理
1wful2fm
 
LinkedIn for Your Job Search June 17, 2024
LinkedIn for Your Job Search June  17, 2024LinkedIn for Your Job Search June  17, 2024
LinkedIn for Your Job Search June 17, 2024
Bruce Bennett
 
一比一原版(EUR毕业证)鹿特丹伊拉斯姆斯大学毕业证如何办理
一比一原版(EUR毕业证)鹿特丹伊拉斯姆斯大学毕业证如何办理一比一原版(EUR毕业证)鹿特丹伊拉斯姆斯大学毕业证如何办理
一比一原版(EUR毕业证)鹿特丹伊拉斯姆斯大学毕业证如何办理
nguqayx
 
A Guide to a Winning Interview June 2024
A Guide to a Winning Interview June 2024A Guide to a Winning Interview June 2024
A Guide to a Winning Interview June 2024
Bruce Bennett
 

Recently uploaded (20)

按照学校原版(ArtEZ文凭证书)ArtEZ艺术学院毕业证快速办理
按照学校原版(ArtEZ文凭证书)ArtEZ艺术学院毕业证快速办理按照学校原版(ArtEZ文凭证书)ArtEZ艺术学院毕业证快速办理
按照学校原版(ArtEZ文凭证书)ArtEZ艺术学院毕业证快速办理
 
欧洲杯外围-欧洲杯外围赛程-欧洲杯外围压注|【​网址​🎉ac99.net🎉​】
欧洲杯外围-欧洲杯外围赛程-欧洲杯外围压注|【​网址​🎉ac99.net🎉​】欧洲杯外围-欧洲杯外围赛程-欧洲杯外围压注|【​网址​🎉ac99.net🎉​】
欧洲杯外围-欧洲杯外围赛程-欧洲杯外围压注|【​网址​🎉ac99.net🎉​】
 
美洲杯买球-美洲杯买球下注平台-美洲杯买球投注平台|【​网址​🎉ac55.net🎉​】
美洲杯买球-美洲杯买球下注平台-美洲杯买球投注平台|【​网址​🎉ac55.net🎉​】美洲杯买球-美洲杯买球下注平台-美洲杯买球投注平台|【​网址​🎉ac55.net🎉​】
美洲杯买球-美洲杯买球下注平台-美洲杯买球投注平台|【​网址​🎉ac55.net🎉​】
 
Connect to Grow: The power of building networks
Connect to Grow: The power of building networksConnect to Grow: The power of building networks
Connect to Grow: The power of building networks
 
欧洲杯投注-欧洲杯投注投注官方网站-欧洲杯投注买球投注官网|【​网址​🎉ac99.net🎉​】
欧洲杯投注-欧洲杯投注投注官方网站-欧洲杯投注买球投注官网|【​网址​🎉ac99.net🎉​】欧洲杯投注-欧洲杯投注投注官方网站-欧洲杯投注买球投注官网|【​网址​🎉ac99.net🎉​】
欧洲杯投注-欧洲杯投注投注官方网站-欧洲杯投注买球投注官网|【​网址​🎉ac99.net🎉​】
 
Switching Careers Slides - JoyceMSullivan SocMediaFin - 2024Jun11.pdf
Switching Careers Slides - JoyceMSullivan SocMediaFin -  2024Jun11.pdfSwitching Careers Slides - JoyceMSullivan SocMediaFin -  2024Jun11.pdf
Switching Careers Slides - JoyceMSullivan SocMediaFin - 2024Jun11.pdf
 
欧洲杯足彩-欧洲杯足彩体育投注-欧洲杯足彩投注网站|【​网址​🎉ac99.net🎉​】
欧洲杯足彩-欧洲杯足彩体育投注-欧洲杯足彩投注网站|【​网址​🎉ac99.net🎉​】欧洲杯足彩-欧洲杯足彩体育投注-欧洲杯足彩投注网站|【​网址​🎉ac99.net🎉​】
欧洲杯足彩-欧洲杯足彩体育投注-欧洲杯足彩投注网站|【​网址​🎉ac99.net🎉​】
 
一比一原版布拉德福德大学毕业证(bradford毕业证)如何办理
一比一原版布拉德福德大学毕业证(bradford毕业证)如何办理一比一原版布拉德福德大学毕业证(bradford毕业证)如何办理
一比一原版布拉德福德大学毕业证(bradford毕业证)如何办理
 
一比一原版(uwm毕业证书)美国威斯康星大学密尔沃基分校毕业证如何办理
一比一原版(uwm毕业证书)美国威斯康星大学密尔沃基分校毕业证如何办理一比一原版(uwm毕业证书)美国威斯康星大学密尔沃基分校毕业证如何办理
一比一原版(uwm毕业证书)美国威斯康星大学密尔沃基分校毕业证如何办理
 
在线办理(UOIT毕业证书)安大略省理工大学毕业证在读证明一模一样
在线办理(UOIT毕业证书)安大略省理工大学毕业证在读证明一模一样在线办理(UOIT毕业证书)安大略省理工大学毕业证在读证明一模一样
在线办理(UOIT毕业证书)安大略省理工大学毕业证在读证明一模一样
 
BUKU PENJAGAAN BUKU PENJAGAAN BUKU PENJAGAAN
BUKU PENJAGAAN BUKU PENJAGAAN BUKU PENJAGAANBUKU PENJAGAAN BUKU PENJAGAAN BUKU PENJAGAAN
BUKU PENJAGAAN BUKU PENJAGAAN BUKU PENJAGAAN
 
体育博彩论坛-十大体育博彩论坛-体育博彩论坛|【​网址​🎉ac55.net🎉​】
体育博彩论坛-十大体育博彩论坛-体育博彩论坛|【​网址​🎉ac55.net🎉​】体育博彩论坛-十大体育博彩论坛-体育博彩论坛|【​网址​🎉ac55.net🎉​】
体育博彩论坛-十大体育博彩论坛-体育博彩论坛|【​网址​🎉ac55.net🎉​】
 
一比一原版(surrey毕业证书)英国萨里大学毕业证成绩单修改如何办理
一比一原版(surrey毕业证书)英国萨里大学毕业证成绩单修改如何办理一比一原版(surrey毕业证书)英国萨里大学毕业证成绩单修改如何办理
一比一原版(surrey毕业证书)英国萨里大学毕业证成绩单修改如何办理
 
Community Skills Building Workshop | PMI Silver Spring Chapter | June 12, 2024
Community Skills Building Workshop | PMI Silver Spring Chapter  | June 12, 2024Community Skills Building Workshop | PMI Silver Spring Chapter  | June 12, 2024
Community Skills Building Workshop | PMI Silver Spring Chapter | June 12, 2024
 
欧洲杯买球-欧洲杯买球买球推荐-欧洲杯买球买球推荐网站|【​网址​🎉ac10.net🎉​】
欧洲杯买球-欧洲杯买球买球推荐-欧洲杯买球买球推荐网站|【​网址​🎉ac10.net🎉​】欧洲杯买球-欧洲杯买球买球推荐-欧洲杯买球买球推荐网站|【​网址​🎉ac10.net🎉​】
欧洲杯买球-欧洲杯买球买球推荐-欧洲杯买球买球推荐网站|【​网址​🎉ac10.net🎉​】
 
办理阿卡迪亚大学毕业证(uvic毕业证)本科文凭证书原版一模一样
办理阿卡迪亚大学毕业证(uvic毕业证)本科文凭证书原版一模一样办理阿卡迪亚大学毕业证(uvic毕业证)本科文凭证书原版一模一样
办理阿卡迪亚大学毕业证(uvic毕业证)本科文凭证书原版一模一样
 
一比一原版美国西北大学毕业证(NWU毕业证书)学历如何办理
一比一原版美国西北大学毕业证(NWU毕业证书)学历如何办理一比一原版美国西北大学毕业证(NWU毕业证书)学历如何办理
一比一原版美国西北大学毕业证(NWU毕业证书)学历如何办理
 
LinkedIn for Your Job Search June 17, 2024
LinkedIn for Your Job Search June  17, 2024LinkedIn for Your Job Search June  17, 2024
LinkedIn for Your Job Search June 17, 2024
 
一比一原版(EUR毕业证)鹿特丹伊拉斯姆斯大学毕业证如何办理
一比一原版(EUR毕业证)鹿特丹伊拉斯姆斯大学毕业证如何办理一比一原版(EUR毕业证)鹿特丹伊拉斯姆斯大学毕业证如何办理
一比一原版(EUR毕业证)鹿特丹伊拉斯姆斯大学毕业证如何办理
 
A Guide to a Winning Interview June 2024
A Guide to a Winning Interview June 2024A Guide to a Winning Interview June 2024
A Guide to a Winning Interview June 2024
 

Chapter_12 Virtual memory.pptx

  • 2. Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 12.2 Operating Systems, by Dhananjay Dhamdhere 2 Introduction • Virtual Memory Basics • Demand Paging • The Virtual Memory Manager • Page Replacement Policies • Controlling Memory Allocation to a Process • Shared Pages • Memory-Mapped Files • Case Studies of Virtual Memory Using Paging • Virtual Memory Using Segmentation
  • 3. Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 12.3 • MMU translates logical address into physical one • Virtual memory manager is a software component – Uses demand loading – Exploits locality of reference to improve performance Operating Systems, by Dhananjay Dhamdhere 3 Virtual Memory Basics
  • 4. Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 12.4 Operating Systems, by Dhananjay Dhamdhere 4 Virtual Memory Basics (continued)
  • 5. Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 12.5 Operating Systems, by Dhananjay Dhamdhere 5 Virtual Memory Using Paging • MMU performs address translation using page table Effective memory address of logical address (pi, bi) = start address of the page frame containing page pi + bi
  • 6. Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 12.6 Operating Systems, by Dhananjay Dhamdhere 6 Demand Paging Preliminaries
  • 7. Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 12.7 Operating Systems, by Dhananjay Dhamdhere 7 Demand Paging Preliminaries (continued) • Memory Management Unit (MMU) raises a page fault interrupt if page containing logical address not in memory
  • 8. Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 12.8 Operating Systems, by Dhananjay Dhamdhere 8 Demand Paging Preliminaries (continued) A page fault interrupt is raised because Valid bit of page 3 is 0
  • 9. Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 12.9 Operating Systems, by Dhananjay Dhamdhere 9 Demand Paging Preliminaries (continued) • At a page fault, the required page is loaded in a free page frame • If no page frame is free, virtual memory manager performs a page replacement operation – Page replacement algorithm – Page-out initiated if page is dirty (modified bit is set) • Page-in and page-out: page I/O or page traffic • Effective memory access time in demand paging:
  • 10. Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 12.10 • (Empirical) law of locality of reference: logical addresses used by process in a short interval tend to be grouped in certain portions of its logical address space Operating Systems, by Dhananjay Dhamdhere 10 Page Replacement
  • 11. Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 12.11 • How much memory to allocate to a process Operating Systems, by Dhananjay Dhamdhere 11 Memory Allocation to a Process
  • 12. Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 12.12 Operating Systems, by Dhananjay Dhamdhere 12 Optimal Page Size • Size of a page is defined by computer hardware • Page size determines: – No of bits required to represent byte number in a page – Memory wastage due to internal fragmentation – Size of the page table for a process – Page fault rates when a fixed amount of memory is allocated to a process • Use of larger page sizes than optimal value implies somewhat higher page fault rates for a process – Tradeoff between HW cost and efficient operation
  • 13. Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 12.13 Operating Systems, by Dhananjay Dhamdhere 13 Paging Hardware • Page-table-address-register (PTAR) points to the start of a page table
  • 14. Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 12.14 Operating Systems, by Dhananjay Dhamdhere 14 Paging Hardware (continued)
  • 15. Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 12.15 Operating Systems, by Dhananjay Dhamdhere 15 Memory Protection • Memory protection violation raised if: – Process tries to access a nonexistent page – Process exceeds its (page) access privileges • It is implemented through: – Page table size register (PTSR) of MMU • Kernel records number of pages contained in a process in its PCB – Loads number from PCB in PTSR when process is scheduled – Prot info field of the page’s entry in the page table
  • 16. Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 12.16 • Translation look-aside buffer (TLB): small and fast associative memory used to speed up address translation Operating Systems, by Dhananjay Dhamdhere 16 Address Translation and Page Fault Generation
  • 17. Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 12.17 Operating Systems, by Dhananjay Dhamdhere 17 Address Translation and Page Fault Generation (continued) • TLBs can be HW or SW managed
  • 18. Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 12.18 Operating Systems, by Dhananjay Dhamdhere 18 Address Translation and Page Fault Generation (continued) • Some mechanisms used to improve performance: – Wired TLB entries for kernel pages: never replaced – Superpages TLB hit ratio
  • 19. Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 12.19 Superpages • TLB reach is stagnant even though memory sizes increase rapidly as technology advances – TLB reach = page size x no of entries in TLB – It affects performance of virtual memory • Superpages are used to increase the TLB reach – A superpage is a power of 2 multiple of page size – Its start address (both logical and physical) is aligned on a multiple of its own size – Max TLB reach = max superpage size x no of entries in TLB – Size of a superpage is adapted to execution behavior of a process through promotions and demotions Operating Systems, by Dhananjay Dhamdhere 19
  • 20. Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 12.20 Operating Systems, by Dhananjay Dhamdhere 20 Support for Page Replacement • Virtual memory manager needs following information for minimizing page faults and number of page-in and page-out operations: – The time when a page was last used • Expensive to provide enough bits for this purpose • Solution: use a single reference bit – Whether a page is dirty • A page is clean if it is not dirty • Solution: modified bit in page table entry
  • 21. Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 12.21 Operating Systems, by Dhananjay Dhamdhere 21 Practical Page Table Organizations • A process with a large address space requires a large page table, which occupies too much memory • Solutions: – Inverted page table • Describes contents of each page frame – Size governed by size of memory – Independent of number and sizes of processes – Contains pairs of the form (program id, page #) • Con: information about a page must be searched – Multilevel page table • Page table of process is paged
  • 22. Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 12.22 Operating Systems, by Dhananjay Dhamdhere 22 Inverted Page Tables Use of hash table Speeds up search
  • 23. Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 12.23 Operating Systems, by Dhananjay Dhamdhere 23 Multilevel Page Tables • If size of a table entry is 2e bytes, number of page table entries in one PT page is 2nb/2e • Logical address (pi , bi) is regrouped into three fields: – PT page with the number pi 1 contains entry for pi – pi 2 is entry number for pi in PT page – bi
  • 24. Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 12.24 Operating Systems, by Dhananjay Dhamdhere 24 I/O Operations in a Paged Environment • Process makes system call for I/O operations – Parameters include: number of bytes to transfer, logical address of the data area • Call activates I/O handler in kernel – I/O subsystem does not contain an MMU, so I/O handler replaces logical address of data area with physical address, using information from process page table – I/O fix (bit in misc info field) ensures pages of data area are not paged out – Scatter/gather feature can deposit parts of I/O operation’s data in noncontiguous memory areas – Alternatively, data area pages put in contiguous areas
  • 25. Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 12.25 Operating Systems, by Dhananjay Dhamdhere 25 Example: I/O Operations in Virtual Memory
  • 26. Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 12.26 Operating Systems, by Dhananjay Dhamdhere 26 The Virtual Memory Manager
  • 27. Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 12.27 Operating Systems, by Dhananjay Dhamdhere 27 Example: Page Replacement
  • 28. Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 12.28 Operating Systems, by Dhananjay Dhamdhere 28 Overview of Operation of the Virtual Memory Manager • Virtual memory manager makes two important decisions during its operation: – Upon a page fault, decides which page to replace – Periodically decides how many page frames should be allocated to a process
  • 29. Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 12.29 Operating Systems, by Dhananjay Dhamdhere 29 Page Replacement Policies • A page replacement policy should replace a page not likely to be referenced in the immediate future • Examples: – Optimal page replacement policy • Minimizes total number of page faults; infeasible in practice – First-in first-out (FIFO) page replacement policy – Least recently used (LRU) page replacement policy • Basis: locality of reference • Page reference strings – Trace of pages accessed by a process during its operation – We associate a reference time string with each
  • 30. Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 12.30 Operating Systems, by Dhananjay Dhamdhere 30 Example: Page Reference String • A computer supports instructions that are 4 bytes in length – Uses a page size of 1KB – Symbols A and B are in pages 2 and 5
  • 31. Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 12.31 Operating Systems, by Dhananjay Dhamdhere 31
  • 32. Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 12.32 Operating Systems, by Dhananjay Dhamdhere 32 Page Replacement Policies (continued) • To achieve desirable page fault characteristics, faults shouldn’t increase when memory allocation is increased – Policy must have stack (or inclusion) property
  • 33. Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 12.33 Operating Systems, by Dhananjay Dhamdhere 33 FIFO page replacement policy does not exhibit stack property.
  • 34. Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 12.34 Operating Systems, by Dhananjay Dhamdhere 34 Page Replacement Policies (continued) • Virtual memory manager cannot use FIFO policy – Increasing allocation to a process may increase page fault frequency of process • Would make it impossible to control thrashing
  • 35. Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 12.35 Operating Systems, by Dhananjay Dhamdhere 35 Practical Page Replacement Policies • Virtual memory manager has two threads – Free frames manager implements page replacement policy – Page I/O manager performs page-in/out operations
  • 36. Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 12.36 Operating Systems, by Dhananjay Dhamdhere 36 Practical Page Replacement Policies (continued) • LRU replacement is not feasible – Computers do not provide sufficient bits in the ref info field to store the time of last reference • Most computers provide a single reference bit – Not recently used (NRU) policies use this bit • Simplest NRU policy: Replace an unreferenced page and reset all reference bits if all pages have been referenced • Clock algorithms provide better discrimination between pages by resetting reference bits periodically – One-handed clock algorithm – Two-handed clock algorithm » Resetting pointer (RP) and examining pointer (EP)
  • 37. Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 12.37 Operating Systems, by Dhananjay Dhamdhere 37 Example: Two-Handed Clock Algorithm • Both pointers are advanced simultaneously • Algorithm properties defined by pointer distance: – If pointers are close together, only recently used pages will survive in memory – If pointers are far apart, only pages that have not been used in a long time would be removed
  • 38. Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 12.38 Operating Systems, by Dhananjay Dhamdhere 38 Controlling Memory Allocation to a Process • Process Pi is allocated alloci number of page frames • Fixed memory allocation – Fixes alloc statically; uses local page replacement • Variable memory allocation – Uses local and/or global page replacement – If local replacement is used, handler periodically determines correct alloc value for a process • May use working set model • Sets alloc to size of the working set
  • 39. Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 12.39 Operating Systems, by Dhananjay Dhamdhere 39 Implementation of a Working Set Memory Allocator • Swap out a process if alloc page frames cannot be allocated • Expensive to determine WSi(t,∆) and alloci at every time instant t – Solution: Determine working sets periodically • Sets determined at end of an interval are used to decide values of alloc for use during the next interval
  • 40. Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 12.40 Operating Systems, by Dhananjay Dhamdhere 40 Shared Pages • Static sharing results from static binding performed by a linker/loader before execution of program • Dynamic binding conserves memory by binding same copy of a program/data to several processes – Program or data shared retains its identity – Two conditions should be satisfied: • Shared program should be coded as reentrant – Can be invoked by many processes at the same time • Program should be bound to identical logical addresses in every process that shared it
  • 41. Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 12.41 Operating Systems, by Dhananjay Dhamdhere 41 Shared pages should have same page numbers in all processes
  • 42. Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 12.42 Operating Systems, by Dhananjay Dhamdhere 42 Copy-on-Write • Feature used to conserve memory when data in shared pages could be modified – Copy-on-write flag in page table entries Memory allocation decisions are performed statically A private copy of page k is made when A modifies it
  • 43. Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 12.43 Operating Systems, by Dhananjay Dhamdhere 43 Memory-Mapped Files • Memory mapping of a file by a process binds file to a part of the logical address space of the process – Binding is performed when process makes a memory map system call – Analogous to dynamic binding of programs and data
  • 44. Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 12.44 Operating Systems, by Dhananjay Dhamdhere 44 Memory-Mapped Files (continued)
  • 45. Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 12.45 Operating Systems, by Dhananjay Dhamdhere 45 Case Studies of Virtual Memory Using Paging • Unix Virtual Memory • Linux Virtual Memory • Virtual Memory in Solaris • Virtual Memory in Windows
  • 46. Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 12.46 Operating Systems, by Dhananjay Dhamdhere 46 Unix Virtual Memory • Paging hardware differs in architectures • Pages can be: resident, unaccessed, swapped-out • Allocation of as little swap space as possible • Copy-on-write for fork • Lack reference bit in some HW architectures; compensated using valid bit in interesting manner • Process can fix some pages in memory • Pageout daemon uses a clock algorithm – Swaps out a process if all required pages cannot be in memory – A swap-in priority is used to avoid starvation
  • 47. Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 12.47 Operating Systems, by Dhananjay Dhamdhere 47 Linux Virtual Memory • Page size of 4 KB • On 64-bit architectures, uses three-level page table • States for page frames: free, active, inactive dirty, inactive laundered, inactive clean • Page replacement based on a clock algorithm – Uses two lists called active list and inactive list • Buddy system allocator for allocating page frames • Several virtual memory regions for a process: – Zero-filled, file-backed, private memory
  • 48. Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 12.48 Operating Systems, by Dhananjay Dhamdhere 48 Virtual Memory in Solaris • Supports normal pages and superpages – Superpages: • Automatically for processes with large address spaces • Can be requested using memcntl system call • Not used for memory-mapped files • Solaris 6 introduced priority paging to avoid interference between file processing and virtual memory • Page scanner tries to keep a sufficient number of page frames on cyclic page cache (since Solaris 8) – lotsfree parameter indicates how many page frames should be free – Uses two-handed clock algorithm on a global basis
  • 49. Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 12.49 Operating Systems, by Dhananjay Dhamdhere 49 Virtual Memory in Windows • Supports both 32-bit and 64-bit logical addresses • Page size is 4 KB • Process address space is either 2 GB or 3 GB • Two-, three- or four-level page tables and various page table entry formats – On the X-86 architecture: • Page frame can be in one of eight states, including: valid, free, zeroed, standby, modified, and bad • A process must first reserve virtual address space and then commit it for specific entities
  • 50. Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 12.50 Operating Systems, by Dhananjay Dhamdhere 50 Virtual Memory in Windows (continued) • A section object represents a section of memory that can be shared – A process maps a view to access part of a section – Copy-on-write feature used for sharing pages – Prototype PTE is set-up for shared pages • TLBs are managed by HW (32-bit) or SW (64-bit) • Exploits reference locality: loads a few pages before and after a page-faulted page into memory • Uses notion of working sets (for memory allocation) – Clock algorithm • Page lists: free, zero-initiated, modified, standby
  • 51. Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 12.51 Operating Systems, by Dhananjay Dhamdhere 51 Virtual Memory Using Segmentation • A segment is a logical entity in a program, such as a function, a data structure, or an object – Or, a module that consists of some or all of these – Convenient unit for sharing and protection
  • 52. Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 12.52 Operating Systems, by Dhananjay Dhamdhere 52 Example: Effective Address Calculation in Segmentation • Logical address (si, bi) can be specified as ids – (alpha, beta) • alpha: name of a segment • beta: id associated with a byte contained in alpha
  • 53. Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 12.53 Operating Systems, by Dhananjay Dhamdhere 53 Management of Memory • Similarities to paging: – Segment fault indicates segment is not in memory – Segment-in operation is performed to load segment • Segment-out operations may be needed first – Can use working set of segment for allocation • Segments can be replaced on NRU basis • Differences to paging: – Can lead to external fragmentation • Tackled through compaction or through memory reuse techniques (first fit, best fit, etc.) – Segments can dynamically grow or shrink in size
  • 54. Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 12.54 Operating Systems, by Dhananjay Dhamdhere 54 Sharing and Protection • Two important issues in protection and sharing of segments are: – Static and dynamic sharing of segments – Detecting use of invalid addresses • Protection exception if bi exceeds size of si
  • 55. Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 12.55 • External fragmentation exists in a virtual memory using segmentation – Solution: segmentation with paging Operating Systems, by Dhananjay Dhamdhere 55 Segmentation with Paging
  • 56. Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 12.56 Operating Systems, by Dhananjay Dhamdhere 56 Summary • Basic actions in virtual memory using paging: address translation and demand loading of pages – Implemented jointly by • Memory Management Unit (MMU): Hardware • Virtual memory manager: Software • Memory is divided into page frames • Virtual memory manager maintains a page table – Inverted and multilevel page tables use less memory but are less efficient – A fast TLB is used to speed up address translation
  • 57. Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 12.57 Operating Systems, by Dhananjay Dhamdhere 57 Summary (continued) • Which page should VM manager remove from memory to make space for a new page? – Page replacement algorithms exploit locality of reference • LRU has stack property, but is expensive • NRU algorithms are used in practice – E.g., clock algorithms • How much memory should manager allocate? – Use working set model to avoid thrashing • Copy-on-write can be used for shared pages • Memory mapping of files speeds up access to data