Virtualizing Memory - Address Space
Memory Virtualization
Introduction
- OS virtualizes its physical memory
- OS provides an illusion memory space per each process
Benefit
- Ease to use in programming
- Memory efficiency in items of time and space
- Isolate the processes and OS, protect from errant accesses of other processes
Multiprogramming and Time Sharing
Load multiple processes in memory
- execute one process for a while
- 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

- The address space contains all about a running process
Structure:
- Code: Instructions
- Data: Global or static local variables
- Heap: Dynamically allocate memory (
malloc) - 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
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.
Single Process

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
Base

Base + Bounds

Problems
- The OS must take action to implement base-and-bounds approach
Three critical junctures:
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.

- free list: a list of the range of the physical memory are not in use.
When a process is terminated: Reclaiming the memory for use.
- The OS must put the memory back to the free list.

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

- The OS must save and restore the base-and-bounds pair.
Inefficiency
- Big chunk of free space
- The free space takes up physical memory
- Hard to run when an address space does not fit into the physical memory (e.g. 4KB space)


