A program uses addresses that it believes start at zero, but its actual place in memory may be anywhere. The operating system and hardware bridge the two.

Logical and Physical Addresses

An address generated by the CPU when a program runs is a logical address, also called a virtual address. The address seen by the memory unit is the physical address. The set of all logical addresses of a program is its logical address space, and the set of corresponding physical addresses is its physical address space. The run-time mapping from logical to physical addresses is done by a hardware device called the memory management unit (MMU). In the simplest scheme, a relocation (base) register holds the starting physical address of the process, and every logical address is added to it: physical = base + logical. A limit register holds the size of the process, and any logical address greater than or equal to the limit causes a trap, which protects other processes.

Definition: Logical address. An address generated by the CPU during program execution, which the memory management unit maps to a physical address in main memory.

Address Binding

Instructions and data are bound to memory addresses at compile time, if the location is known in advance; at load time, if the compiler produces relocatable code; or at execution time, if the process can move during execution. Execution-time binding, which needs the MMU, is used by most modern systems.

Swapping

Swapping moves a process temporarily out of main memory to a backing store (a fast disk) and later brings it back to continue. It lets the total size of all processes exceed physical memory, so more processes can be kept in the system. The medium-term scheduler decides which to swap. The main cost is the transfer time, which is proportional to the amount swapped. A process waiting for I/O into its memory buffer should not be swapped out. Modern systems swap individual pages rather than whole processes.

Table 4.1: Address binding times

Binding time Requirement Can process move?
Compile time Location known in advance No
Load time Relocatable code Only when reloaded
Execution time MMU hardware Yes, at any time

Example 4.1: Relocation and limit registers

Problem: A process has base register 14000 and limit register 3000. Translate logical addresses 346 and 3500.

Solution:

  1. 346 < 3000, so it is valid. Physical = 14000 + 346 = 14346.
  2. 3500 ≥ 3000, so it is outside the process: the hardware traps to the OS.

Result: 346 maps to 14346, and 3500 causes an addressing error.

#include <stdio.h>
int main(void) {
    int base = 14000, limit = 3000, la[] = {0, 346, 2999, 3500};
    for (int i = 0; i < 4; i++) {
        if (la[i] < limit)
            printf("logical %4d -> physical %d\n",
                   la[i], base + la[i]);
        else printf("logical %4d -> TRAP: addressing error\n", la[i]);
    }
    return 0;
}

Output

logical    0 -> physical 14000
logical  346 -> physical 14346
logical 2999 -> physical 16999
logical 3500 -> TRAP: addressing error

Common mistakes

  • Adding the limit instead of the base to form the physical address.
  • Thinking that a program sees physical addresses directly.
  • Swapping out a process that is waiting for I/O into its buffer.

Key takeaways

  • The CPU generates logical addresses; memory sees physical addresses.
  • The MMU maps them: physical = base + logical.
  • The limit register protects memory.
  • Swapping moves processes between memory and the backing store.

Practice questions

  1. What does the MMU do?
  2. What is the purpose of the limit register?
  3. What is swapping?

Answers to the practice questions

  1. It maps logical addresses to physical addresses at run time.
  2. To check that a logical address is within the process, giving protection.
  3. Moving a process temporarily from memory to a backing store and back.

Sources

  • Official BCA syllabus Operating Systems, course code 25BCA402DS03, Unit IV