CS110: Principles of Computer Systems

Spring 2021
Instructors Roz Cyrus and Jerry Cain

PDF

Virtual Memory

  • Main memory (or rather, RAM, which is short for random access memory) is organized as an array of contiguous bytes.  It's physical hardware where the operating system and all active user processes store relevant code and data in order to run more efficiently.
    • Each byte has its own address, and addresses range from 0 (the zero address) through some number M - 1, when M is typically a large power of 2.
    • Older machines rely on physical addressing, which means addresses generated and dereferenced by the OS and user processes are trivially mapped to the data residing at that same address.
    • Physical addressing is the standard
      addressing scheme when operating
      systems only permit one process to
      exist at any one time.
      • Stated another way: a process more or
        less owns all of memory while it's
        running and, in principle, can read to or
        write from any address.  That's physical
        addressing!

Virtual Memory

  • Modern devices—laptops, desktops, smart phones, etc.—allow multiple processes to run at the same time.  If two or more processes operate as if they own all of memory, then physical addressing is simply not possible.
    • The simplest solution is to allow all processes to operate as if they own all of memory anyway and require the CPU and the OS to jointly treat a process's addresses as virtual, and to convert every virtual address to a physical one—a computation that's 100% invisible to the process—before accessing RAM.
    • The CPU and the OS use specialized hardware called a memory management unit (MMU) to manage translations between virtual addresses (the addresses computed by the process) and physical ones (as generated by the MMU).

Virtual Memory

  • We'll define an address space as an ordered sequence integer addresses, starting at 0.  Main memory defines a physical address space, but the address space that a process thinks it's accessing is virtual.
  • The size of a physical address space is dictated by the hardware.  If you have 16GB of RAM, physical addresses range from 0 through 2^34 - 1.
  • The size of a virtual address space is dictated by the size of a pointer.  The myth machines, along with virtually all modern desktops and laptops, are 64-bit, which means that pointers are 64 bits wide.  That means processes can distinguish between 2^64 different addresses and their virtual address spaces are 2^64 bytes.
  • The MMU converts each (virtual address, pid) pair to a physical address in main memory, and the operating system ensures that no two virtual addresses ever map to the same physical address.
  • If all process addresses are virtual,
    the OS can map those addresses to
    any physical storage device.

Virtual Memory

  • One approach is to assume an infinite amount of physical
    memory, and map each virtual address space to an
    equally large, contiguous portion of physical memory.
    • Virtual memory addresses would be 64-bits, but
      physical addresses would be even wider.
    • The OS could maintain a data structure—a map or
      a dictionary—that maps pids to some multiple of
      ​2^64.  In the context of the diagram on the right:
      • pid 2001 maps to 0x0000
      • pid 2002 maps to 0x0001
      • pid 2003 maps to 0x0002, etc
    • The OS and the MMU would jointly convert virtual
      addresses to physical ones by prepending that multiple
      to the left, e.g. 0x7FFFFFFF80004000 in process 2004's space would be reliably and deterministically converted to 0x00037FFFFFFF80004000.
    • This translation process is delightfully simple, but it's hopelessly naive.
      • We don't have an infinite amount of physical memory.  In fact, no computer I know of has anywhere near 2^64 = 18.4 exabytes.
      • The vast majority of any single virtual address space is unused.  The accumulation of bytes needed for all segments is typically on the order of MBs.

Virtual Memory

  • The OS shouldn't try to map virtual addresses that will never be used. Instead, it should only map virtual addresses that are part of segments.
  • We could relax the requirement that segments within any single process be clustered in physical memory.
  • Doing so would allow the OS more flexibility in how physical memory is used to back full segments in virtual memory.
  • Small segments in new processes might be mapped to open pockets within physical memory that couldn't be used previously.

Virtual Memory

  • The huge win with this approach is that only virtual addresses that contribute to a process's runtime get mapped.  The physical memory requirements are much smaller and more realistic.
     
  • This approach is better, but far from perfect.
    • Address translations are more complicated and time consuming.
    • The heap and stack segments often grow over time and might need to be moved, and moved often.
    • If variably sized regions of physical memory are allocated and deallocated, over time, physical memory may become fragmented and interfere with the OS's ability to map large virtual segments to physical memory.

Lecture 09 Pre: Virtual Memory and Processes

By Jerry Cain

Lecture 09 Pre: Virtual Memory and Processes

  • 1,124