Sub topic 1
Address Spaces and Address Binding
This chapter covers how a program's addresses become real memory locations, and how the operating system places programs in memory. It ends with paging and the translation lookaside buffer (TLB), which the rest of the tutorial builds on.
Prerequisites
You should be comfortable with binary and hexadecimal numbers, and with powers of two. You should know the basic roles of the CPU, main memory, and cache. Reading short C code is helpful.
Address Spaces and Address Binding
A logical address (also called a virtual address) is the address a program uses. A physical address is the location in RAM the hardware actually accesses. Address binding is the step that maps logical addresses to physical ones. It can happen at compile time, load time, or execution time.
Each process gets its own address space, the set of logical addresses it may use. Two processes can both use address 0x1000 without conflict, because the system maps each one to different physical memory.
Contiguous Allocation and Placement Strategies
In contiguous allocation, each process gets one block of physical memory. The hardware checks each access against a base register and a limit register.
When a free hole must hold a new process, the OS uses one of three placement strategies:
- First-fit: take the first hole large enough.
- Best-fit: take the smallest hole large enough.
- Worst-fit: take the largest hole.
Internal and External Fragmentation
Internal fragmentation is wasted space inside an allocated block. A 10 KB request in a 12 KB block wastes 2 KB.
External fragmentation is wasted space between blocks. Total free memory may be enough for a request, but no single hole is large enough. Compaction can fix this by moving processes together, at a cost in time.
Paging and Address Translation
Paging divides logical memory into fixed-size pages and physical memory into same-sized frames. A page table maps each page number to a frame number. The offset within the page stays the same.
With 4 KB pages, the low 12 bits of an address are the offset. The remaining bits are the page number.
#include <stdint.h>
#include <stdio.h>
int main(void) {
uint32_t va = 0x12345678;
uint32_t page = va >> 12;
uint32_t offset = va & 0xFFF;
printf("page=0x%x offset=0x%x\n", (unsigned)page, (unsigned)offset);
return 0;
}This prints page=0x12345 offset=0x678.
The TLB and Effective Access Time
A translation lookaside buffer (TLB) is a small, fast cache of recent page-table entries. A TLB hit avoids the extra memory access that a page-table lookup needs.
Effective access time (EAT) is the average time per memory access. With hit ratio h, TLB time ttlb, and memory time tmem, a single-level page table gives:
EAT = h × (ttlb + tmem) + (1 − h) × (ttlb + 2 × tmem)
With h = 0.9, ttlb = 10 ns, and tmem = 100 ns, EAT is 120 ns.
Exercise
A single-level page table is used with h = 0.8, ttlb = 20 ns, and tmem = 100 ns. What is the EAT?
Show answer
EAT = 0.8 × (20 + 100) + 0.2 × (20 + 200) = 96 + 44 = 140 ns.