Sub topic 1
Address Spaces and Address Binding
Every memory management technique in this tutorial depends on one idea. The addresses a program uses are not the addresses the hardware uses. This chapter defines the kinds of address, shows when the translation between them is fixed (binding), and builds the base/limit hardware that later chapters extend into paging and segmentation.
Prerequisites
This chapter assumes you can:
- Convert between binary, hexadecimal and decimal, and work with powers of two (2^10 = 1 KB, 2^20 = 1 MB, 2^30 = 1 GB).
- Describe the roles of the CPU, main memory, cache and disk.
- Explain what a process is and what a context switch does.
- Read short C or Python programs.
Three kinds of address
A program does not know where in RAM it will run. The system therefore separates the addresses the program generates from the addresses the memory chips see.
- A logical address is generated by the CPU while a program runs. It is the address the program "sees".
- A physical address is the address loaded into the memory address register. It selects an actual location in main memory.
- A virtual address is a logical address in a system that uses virtual memory. The process may have a larger address space than physical memory, and only part of it is resident at any time.
In many textbooks, and in most GATE questions, "logical" and "virtual" are used as synonyms. The distinction matters only when the question is about virtual memory (demand paging, page faults). The default reading is that a logical address is what the CPU emits and a physical address is what reaches RAM.
An address space is the set of all addresses a process can generate. The logical address space is the set of logical addresses. The physical address space is the set of physical addresses that correspond to them.
Their sizes follow directly from the address width:
| Quantity | Formula | Example |
|---|---|---|
| Logical address space size | 2^(logical address bits) bytes (byte-addressable) | 32 bits gives 4 GB |
| Physical address space size | 2^(physical address bits) bytes | 36 bits gives 64 GB |
| Number of address bits | log2(size in bytes) | 256 MB needs 28 bits |
The two sizes are independent. A machine may have a 32-bit logical address and only 1 GB of RAM (30 physical address bits). Whether the physical size is smaller or larger than the logical size is a design choice.
The memory-management unit
The hardware that converts logical addresses to physical ones at run time is the memory-management unit (MMU). The program never sees physical addresses. Every load, store and instruction fetch passes through the MMU.
The simplest MMU uses two registers:
- The relocation register (base register) holds the physical address where the process starts.
- The limit register holds the size of the process's logical address space.
For each logical address L, the hardware does the following:
- If
L >= limit, raise a trap (addressing error) to the operating system. - Otherwise, the physical address is
base + L.
The check runs before the addition. Valid logical addresses run from 0 to limit - 1. The operating system loads these two registers during a context switch, so each process gets its own pair. Only privileged instructions can change them, which prevents a user process from escaping its region.
def translate(logical, base, limit):
"""Relocation-register translation with a limit check."""
if logical < 0 or logical >= limit:
raise MemoryError(f"trap: logical address {logical} outside 0..{limit - 1}")
return base + logical
print(translate(346, 14000, 3000)) # 14346
try:
translate(3000, 14000, 3000) # first invalid address
except MemoryError as e:
print(e) # trap: logical address 3000 outside 0..2999Address binding
Address binding is the mapping of one address space onto another. A programmer writes names such as variables and function labels. The system must turn each name into a numeric address. The step at which a final physical address is committed is the binding time, and there are three possibilities.
Compile-time binding
If the compiler knows where the process will reside in memory, it generates absolute code with physical addresses already inside the instructions. If the starting location later changes, the program must be recompiled. MS-DOS .COM programs worked this way. The logical and physical addresses are identical.
Load-time binding
If the location is unknown at compile time, the compiler generates relocatable code, which uses addresses relative to the start of the program. The loader adds the actual starting address to each reference when it places the program in memory. If the starting address changes, the program must be reloaded, but not recompiled. Once running, the process cannot move. Here too the logical and physical addresses are the same after loading.
Execution-time binding
If a process can move from one memory region to another while it runs, binding is delayed until execution. This needs hardware support such as the base/limit registers above, or the page tables of later chapters. This is the scheme modern operating systems use. It is the only one in which logical and physical addresses differ. Because the logical addresses never change while the physical ones do, execution-time binding is what makes swapping, compaction and virtual memory possible.
| Binding time | Who binds | Code form | Can the process move after start? | Logical = physical? |
|---|---|---|---|---|
| Compile time | Compiler | Absolute | No | Yes |
| Load time | Loader | Relocatable | No (must reload) | Yes |
| Execution time | MMU hardware | Position-independent via translation | Yes | No |
Related ideas: loading and linking
Three techniques appear alongside binding in exam questions. They concern when code is brought in or connected, not how addresses are translated.
- Dynamic loading: a routine is not loaded into memory until it is first called. Unused routines (such as rare error handlers) never occupy memory. The program must be designed this way, but the operating system provides library support.
- Static linking: system libraries are combined into the executable by the linker. Every program carries its own copy.
- Dynamic linking: linking is postponed until run time. The executable contains a small stub. On first call, the stub finds the library routine, replaces itself with its address, and the routine runs. Several processes can share one copy of a library. This needs help from the operating system, because it must check whether the routine is already in another process's address space.
Do not confuse the two groups. Binding decides when addresses become physical. Loading and linking decide when code is brought in and when external references are resolved.
Swapping
Execution-time binding allows swapping. A process can be moved temporarily from main memory to a backing store (a fast disk) and later brought back, possibly to a different physical location. The total memory demanded by all processes can then exceed physical memory.
Two points matter for calculations:
- The cost is dominated by transfer time. Swapping a 100 MB process at 50 MB/s takes 2 seconds in each direction.
- A process with pending I/O cannot simply be swapped out, because the I/O device may write into the old physical location. The usual fixes are to never swap such processes or to do I/O only into operating system buffers.
Common errors
- Equating address bits with memory size in bytes. 32 bits give 2^32 bytes, which is 4 GB, not 32 GB.
- Off-by-one in the limit check. With limit 3000, address 3000 is invalid. The last legal address is 2999.
- Adding base before checking. The comparison uses the logical address against the limit, not the physical address.
- Assuming logical and physical addresses differ in every scheme. They differ only with execution-time binding.
- Treating the physical address space as fixed by the program. It is set by the hardware (RAM size and address width), not by the compiler.
Worked example
A system has a 24-bit logical address and a 20-bit physical address. A process has the relocation register set to 0x40000 (262144) and the limit register to 0x08000 (32768).
- The logical address space holds 2^24 bytes, which is 16 MB. The physical address space holds 2^20 bytes, which is 1 MB.
- The process may legally use logical addresses 0 to 32767, because the limit is 32768. This is less than the 16 MB the hardware could address.
- Logical address 0x01234 (4660) is below the limit, so the physical address is 0x40000 + 0x01234 = 0x41234.
- Logical address 0x08000 equals the limit, so it causes a trap.
- Check the result fits in physical memory: the highest valid physical address is 0x40000 + 0x7FFF = 0x47FFF, which is below 2^20 = 0x100000. The mapping is valid.
The next chapter uses this hardware to give each process one contiguous region and examines how the operating system chooses where to put it.
Exercise
A process runs with relocation register = 20000 and limit register = 5000. What physical address does logical address 4999 map to, and what happens for logical address 5000?
Show answer
Logical address 4999 is below the limit, so it is valid and maps to 20000 + 4999 = 24999. Logical address 5000 equals the limit, so the hardware traps to the operating system with an addressing error. The valid logical range is 0 to 4999.
Exercise
A system has byte-addressable memory with 2^28 bytes of physical memory and a 32-bit logical address. How many bits does a physical address need, and what is the maximum size of a process's logical address space? Which binding time must be used if the logical and physical addresses differ?
Show answer
A physical address needs 28 bits. The logical address space can be at most 2^32 bytes, which is 4 GB. Because the logical and physical addresses differ, the binding must be done at execution time, using MMU hardware.