Monday, July 1, 2013

Virtual Memory

Virtual Memory     

   


A cache stores a subset of the address space of RAM. An address space is the set of valid addresses. 
Each address in cache, there is a corresponding address in RAM. This subset of addresses  changes over time, based on the behaviour of your program.
Cache is used to keep the most commonly used sections of RAM in the cache, where it can be accessed quickly. 
This is necessary because CPU speeds increase much faster than speed of memory access.
One way to extend the amount of memory accessible by a program is to use disk. Thus, we can use 10 Megs of disk space. At any time, only 1 Mega resides in RAM.


  • The real problem with disk is that it's really, really slow to access. 
  • If registers can be accessed in 1 nanosecond, and cache in 5 ns and RAM in about 100 ns, then disk is accessed in fractions of seconds. It can be a million times slower to access disk than a register.
  • The advantage of disk is it's easy to get lots of disk space for a small cost.


Uses of Virtual Memory

  • Virtual memory is an old concept. Before computers had cache, they had virtual memory. Initially, virtual memory meant the idea of using disk to extend RAM. Programs wouldn't have to care whether the memory was "real/RAM" memory  or disk. 
  • Virtual memory was used as a means of memory protection. Every program uses a range of addressed called the address space.
  • The assumption of operating systems developers is that any user program can not be trusted. 
  • User programs will try to destroy themselves, other user programs, and the operating system itself. That seems like such a negative view, however, it's how operating systems are designed. 
  • Programs can be accidentally malicious 
  • Virtual memory can help there too. It can help prevent programs from interfering with other programs.


How Virtual Memory Works

  • When a computer is running, many programs are simulataneously sharing the CPU. 
  • Each running program, plus the data structures needed to manage it, is called a process.
  • Each process is allocated an address space. 
  • This is a set of valid addresses that can be used. This address space can be changed dynamically. 
  • If a process tries to access an address that is not part of its address space, an error occurs, and the operating system takes over, usually killing the process.

How doesAddresses are generated  in one of three ways:
  • A load instruction
  • A store instruction
  • Fetching an instruction

  • Load/store create data addresses, while fetching an instruction creates instruction addresses. Of course, RAM doesn't distinguish between the two kinds of addresses. 
  • Each address generated by a program is considered virtual. It must be translated to a real physical address. Thus, address tranlation is occuring all the time. As you might imagine, this must be handled in hardware, if it's to be done efficiently.
  • You might think translating each address from virtual to physical is a crazy idea, because of how slow it is. However, you get memory protection from address translation, so it's worth the hardware needed to get memory protection.


No comments:

Post a Comment