© Oxford University Press 2014. All rights reserved.
Virtual Memory
© Oxford University Press 2014. All rights reserved.
Objectives
• Need of Virtual Memory
• Virtual addresses and virtual address space
• Implementation of VM system through demand loading
• Demand paging
• Page replacement algorithms
• Thrashing and its solutions
© Oxford University Press 2014. All rights reserved.
Virtual Memory
• Virtual memory may be realized with paging or segmentation as
it requires a non-contiguous memory allocation method.
• All the basics related to these concepts apply here also in VM
system.
• The logical address here is known as virtual address and the
logical address space is called as virtual address space.
© Oxford University Press 2014. All rights reserved.
Why Virtual Memory
•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
•Consider ability to execute 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
•
© Oxford University Press 2014. All rights reserved.
Virtual Memory
Virtual memory – separation of user logical memory
from physical 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
•More programs running concurrently
•Less I/O needed to load or swap processes
© Oxford University Press 2014. All rights reserved.
Virtual Memory
• Virtual memory can be implemented via:
Demand paging
Demand segmentation
• The address translation using paging and segmentation are
applied in the same way as discussed in the last chapter.
• The only difference of applying paging and segmentation in
relation to virtual memory is that there is no need to load the
whole process in the form of pages or segments in the main
memory.
• VM system requires only those pages or segments of a process
in the memory which are needed at a certain time of execution.
© Oxford University Press 2014. All rights reserved.
Demand loading
© Oxford University Press 2014. All rights reserved.
Demand paging
Demand paging is to load only those pages in the memory that are
needed at an instant of time of execution.
•Could bring entire process into memory at load time Or bring a page into memory
only when it is needed
•Less I/O needed, no unnecessary I/O
•Less memory needed
•Faster response
•More users
•Similar to paging system with swapping
•Page is needed  reference/logical address to it
•invalid reference  abort
•not-in-memory  bring to memory
•Lazy swapper – never swaps a page into memory unless page will be needed
•Swapper that deals with pages is a pager
© Oxford University Press 2014. All rights reserved.
Demand paging
© Oxford University Press 2014. All rights reserved.
Valid-Invalid Bit
• With each page table entry a valid–invalid bit
is associated
(v  in-memory – memory resident, i  not-
in-memory)
• Initially valid–invalid bit is set to i on all
entries
• Example of a page table snapshot:
During MMU address translation, if valid–
invalid bit in page table entry is i  page fault
© Oxford University Press 2013. All rights reserved.
Page Table When Some Pages Are Not in Main Memory
© Oxford University Press 2014. All rights reserved.
Page and Frame Replacement Algorithms
• Frame-allocation algorithm determines
– How many frames to give each process
– Which frames to replace
• Page-replacement algorithm
– 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
– Results depend on number of frames available
• In all our examples, the reference string of referenced page numbers is
7,0,1,2,0,3,0,4,2,3,0,3,0,3,2,1,2,0,1,7,0,1
© Oxford University Press 2014. All rights reserved.
Page replacement algorithms
•A page fault occurs when a page is not found in the memory, and
needs to be loaded from the disk.
•If a page fault occurs and all memory frames have been already
allocated, then replacement of a page in memory is required on
the request of a new page. This is referred to as demand-paging.
•The choice of which page to replace is specified by a page
replacement algorithms. The commonly used page replacement
algorithms are FIFO, LRU, optimal page replacement algorithms
etc.
© Oxford University Press 2014. All rights reserved.
Page replacement algorithms
The strategy to choose a best page to be replaced in the memory is
called a page replacement algorithm.
FIFO page replacement algorithm
A page to be replaced is chosen such that it is the oldest one among
the all pages in the memory.
Optimal page replacement algorithm
A page will be replaced that will not be referenced for the longest
time.
© Oxford University Press 2014. All rights reserved.
First-In-First-Out (FIFO) Algorithm
• Reference string: 7,0,1,2,0,3,0,4,2,3,0,3,0,3,2,1,2,0,1,7,0,1
• 3 frames (3 pages can be in memory at a time per process)
• Can vary by reference string: consider 1,2,3,4,1,2,5,1,2,3,4,5
– Adding more frames can cause more page faults!
• Belady’s Anomaly
15 page faults
© Oxford University Press 2014. All rights reserved.
Belody Anamoly
• Generally, on increasing the number of frames to a process’ virtual
memory, its execution becomes faster as less number of page
faults occur. Sometimes the reverse happens, i.e. more number of
page faults occur when more frames are allocated to a process.
This most unexpected result is termed as Belady’s Anomaly.
• Bélády’s anomaly is the name given to the phenomenon where
increasing the number of page frames results in an increase in the
number of page faults for a given memory access pattern.
© Oxford University Press 2014. All rights reserved.
Optimal Page Replacement Algorithm
• Replace page that will not be used for longest period of time (Page
that is referred in last in reference string is replaced first)
• How do you know this?
– Can’t read the future
• Used for measuring how well your algorithm performs
© Oxford University Press 2014. All rights reserved.
Page replacement algorithms
Least Recently Used (LRU) page replacement algorithm
Replace a page which will not be referenced for a longest time in the
future.
•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
•
© Oxford University Press 2014. All rights reserved.
Least Recently Used (LRU) Algorithm
•12 faults – better than FIFO but worse than OPT
•Generally good algorithm and frequently used
© Oxford University Press 2014. All rights reserved.
Global vs. Local Allocation
• 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
© Oxford University Press 2014. All rights reserved.
Thrashing
The high paging activity is known as thrashing. A process is said to be
thrashing if most of the time is being consumed in paging rather than
its actual execution.
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
© Oxford University Press 2014. All rights reserved.
Thrashing

Virtual Memory sjkdhikejv vsdkjnksnv vkjhfvk

  • 1.
    © Oxford UniversityPress 2014. All rights reserved. Virtual Memory
  • 2.
    © Oxford UniversityPress 2014. All rights reserved. Objectives • Need of Virtual Memory • Virtual addresses and virtual address space • Implementation of VM system through demand loading • Demand paging • Page replacement algorithms • Thrashing and its solutions
  • 3.
    © Oxford UniversityPress 2014. All rights reserved. Virtual Memory • Virtual memory may be realized with paging or segmentation as it requires a non-contiguous memory allocation method. • All the basics related to these concepts apply here also in VM system. • The logical address here is known as virtual address and the logical address space is called as virtual address space.
  • 4.
    © Oxford UniversityPress 2014. All rights reserved. Why Virtual Memory •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 •Consider ability to execute 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 •
  • 5.
    © Oxford UniversityPress 2014. All rights reserved. Virtual Memory Virtual memory – separation of user logical memory from physical 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 •More programs running concurrently •Less I/O needed to load or swap processes
  • 6.
    © Oxford UniversityPress 2014. All rights reserved. Virtual Memory • Virtual memory can be implemented via: Demand paging Demand segmentation • The address translation using paging and segmentation are applied in the same way as discussed in the last chapter. • The only difference of applying paging and segmentation in relation to virtual memory is that there is no need to load the whole process in the form of pages or segments in the main memory. • VM system requires only those pages or segments of a process in the memory which are needed at a certain time of execution.
  • 7.
    © Oxford UniversityPress 2014. All rights reserved. Demand loading
  • 8.
    © Oxford UniversityPress 2014. All rights reserved. Demand paging Demand paging is to load only those pages in the memory that are needed at an instant of time of execution. •Could bring entire process into memory at load time Or bring a page into memory only when it is needed •Less I/O needed, no unnecessary I/O •Less memory needed •Faster response •More users •Similar to paging system with swapping •Page is needed  reference/logical address to it •invalid reference  abort •not-in-memory  bring to memory •Lazy swapper – never swaps a page into memory unless page will be needed •Swapper that deals with pages is a pager
  • 9.
    © Oxford UniversityPress 2014. All rights reserved. Demand paging
  • 10.
    © Oxford UniversityPress 2014. All rights reserved. Valid-Invalid Bit • With each page table entry a valid–invalid bit is associated (v  in-memory – memory resident, i  not- in-memory) • Initially valid–invalid bit is set to i on all entries • Example of a page table snapshot: During MMU address translation, if valid– invalid bit in page table entry is i  page fault
  • 11.
    © Oxford UniversityPress 2013. All rights reserved. Page Table When Some Pages Are Not in Main Memory
  • 12.
    © Oxford UniversityPress 2014. All rights reserved. Page and Frame Replacement Algorithms • Frame-allocation algorithm determines – How many frames to give each process – Which frames to replace • Page-replacement algorithm – 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 – Results depend on number of frames available • In all our examples, the reference string of referenced page numbers is 7,0,1,2,0,3,0,4,2,3,0,3,0,3,2,1,2,0,1,7,0,1
  • 13.
    © Oxford UniversityPress 2014. All rights reserved. Page replacement algorithms •A page fault occurs when a page is not found in the memory, and needs to be loaded from the disk. •If a page fault occurs and all memory frames have been already allocated, then replacement of a page in memory is required on the request of a new page. This is referred to as demand-paging. •The choice of which page to replace is specified by a page replacement algorithms. The commonly used page replacement algorithms are FIFO, LRU, optimal page replacement algorithms etc.
  • 14.
    © Oxford UniversityPress 2014. All rights reserved. Page replacement algorithms The strategy to choose a best page to be replaced in the memory is called a page replacement algorithm. FIFO page replacement algorithm A page to be replaced is chosen such that it is the oldest one among the all pages in the memory. Optimal page replacement algorithm A page will be replaced that will not be referenced for the longest time.
  • 15.
    © Oxford UniversityPress 2014. All rights reserved. First-In-First-Out (FIFO) Algorithm • Reference string: 7,0,1,2,0,3,0,4,2,3,0,3,0,3,2,1,2,0,1,7,0,1 • 3 frames (3 pages can be in memory at a time per process) • Can vary by reference string: consider 1,2,3,4,1,2,5,1,2,3,4,5 – Adding more frames can cause more page faults! • Belady’s Anomaly 15 page faults
  • 16.
    © Oxford UniversityPress 2014. All rights reserved. Belody Anamoly • Generally, on increasing the number of frames to a process’ virtual memory, its execution becomes faster as less number of page faults occur. Sometimes the reverse happens, i.e. more number of page faults occur when more frames are allocated to a process. This most unexpected result is termed as Belady’s Anomaly. • Bélády’s anomaly is the name given to the phenomenon where increasing the number of page frames results in an increase in the number of page faults for a given memory access pattern.
  • 17.
    © Oxford UniversityPress 2014. All rights reserved. Optimal Page Replacement Algorithm • Replace page that will not be used for longest period of time (Page that is referred in last in reference string is replaced first) • How do you know this? – Can’t read the future • Used for measuring how well your algorithm performs
  • 18.
    © Oxford UniversityPress 2014. All rights reserved. Page replacement algorithms Least Recently Used (LRU) page replacement algorithm Replace a page which will not be referenced for a longest time in the future. •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 •
  • 19.
    © Oxford UniversityPress 2014. All rights reserved. Least Recently Used (LRU) Algorithm •12 faults – better than FIFO but worse than OPT •Generally good algorithm and frequently used
  • 20.
    © Oxford UniversityPress 2014. All rights reserved. Global vs. Local Allocation • 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
  • 21.
    © Oxford UniversityPress 2014. All rights reserved. Thrashing The high paging activity is known as thrashing. A process is said to be thrashing if most of the time is being consumed in paging rather than its actual execution. 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
  • 22.
    © Oxford UniversityPress 2014. All rights reserved. Thrashing