Memory Virtualization

  1. Introduction

    • OS virtualizes its physical memory
    • OS provides an illusion memory space per each process
  2. Benefit

    1. Ease to use in programming
    2. Memory efficiency in items of time and space
    3. Isolate the processes and OS, protect from errant accesses of other processes
  3. Multiprogramming and Time Sharing

    • Load multiple processes in memory

      1. execute one process for a while
      2. switch processes between them in the memory
      • increase utilization and efficiency
    • But it causes an important protection problem as the errant memory accesses from other processes

Address Space

  • OS creates an abstraction of physical memory

    • The address space contains all about a running process
  • Structure:

    1. Code: Instructions
    2. Data: Global or static local variables
    3. Heap: Dynamically allocate memory (malloc)
    4. Stack:
      • Store return address or values (parameters)
      • Contain local variables arguments to routines

Virtual Address

  • Every address in a running program is virtual (OS translates the virtual address to physical address)
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    #include <stdio.h>
    #include <stdlib.h>

    int main(int argc, char *argv[]){
    printf("location of code : %p\n", (void *) main);
    printf("location of heap : %p\n", (void *) malloc(1));
    int x = 3;
    printf("location of stack : %p\n", (void *) &x);
    return x;
    }

    /*
    Outputs: Code > Heap >> Stack
    location of code : 0x102937ed8
    location of heap : 0x132606810
    location of stack : 0x16d4cb42c
    */

Address Transition

  • Relocation Address Space

    • Every process has it own address space (start at address 0).
    • The OS wants to place the process somewhere else in physical memory, not at address 0.
    1. Single Process

    2. Multiple Processes

      • Base register: store the location of the process in physical memory, so that it can easily address the memory by adding the base register.
      • Bounds register: remember the maximum length of address space.
      • It can prevent the process access the location out of the boundary.

Dynamic Relocation

  • When a program starts running, the OS decides where a process should be loaded in physical memory.

    • Set the base register a value

    • Every virtual address must not be negative or greater than bound

    • Two ways of bounds register

Example

  1. Base

  2. Base + Bounds

Problems

  • The OS must take action to implement base-and-bounds approach
  • Three critical junctures:

    1. When a process starts running: Finding space for address space in physical memory.

      • free list: a list of the range of the physical memory are not in use.
    2. When a process is terminated: Reclaiming the memory for use.

      • The OS must put the memory back to the free list.
    3. When context switch occurs: Saving and storing the base-and-bounds pair.

      • The OS must save and restore the base-and-bounds pair.
  • Inefficiency

    1. Big chunk of free space
    2. The free space takes up physical memory
    3. Hard to run when an address space does not fit into the physical memory (e.g. 4KB space)