Facebook Pixel

Address Spaces: Logical vs Physical

When you write a C++ or Java program, you deal with variables, objects, and functions. Under the hood, the CPU needs to know exactly where these items are stored in memory.

However, the addresses your program sees are almost never the actual physical locations in your computer's RAM. Operating systems use a clever abstraction to separate the program's view of memory from the hardware's reality.

What is a Logical Address?

A Logical Address (often called a Virtual Address) is an address generated by the CPU while a program is running. It is the perspective of the process.

When a program is compiled and executed, it usually acts as if it owns the entire memory of the computer, starting perfectly from address zero. The set of all logical addresses generated by a program is called its Logical Address Space.

What is a Physical Address?

A Physical Address is the actual, physical location of data on the silicon RAM chips installed in your motherboard.

Your program might think a variable is stored at address 1050, but the operating system might have actually placed it in the physical RAM chip at address 541050. The set of all physical addresses corresponding to the logical addresses is called the Physical Address Space.

Address Binding: When do they connect?

The process of mapping a program's instructions and data to physical memory addresses is called Address Binding. It can happen at three different stages:

StageHow It WorksDrawback
Compile TimeIf you know exactly where the program will sit in RAM, the compiler generates absolute code (e.g., "Start exactly at address 500").If address 500 is taken later, the program simply crashes. You have to recompile it.
Load TimeThe compiler leaves the addresses relative (e.g., "Start at X"). The OS loader assigns physical addresses when the program launches.Once loaded, the program cannot be moved in memory. If you need to swap it to disk and back, it must go back to the exact same spot.
Execution TimeThe program can be moved around in memory while it is actively running. Addresses are translated on the fly, every single time an instruction executes.Requires special hardware support (the MMU). (Note: Almost all modern OSs use this).

The Memory Management Unit (MMU)

Because modern operating systems use Execution Time binding, they need to translate logical addresses to physical addresses millions of times per second. Doing this in software would be incredibly slow.

Instead, computers have a dedicated hardware chip called the Memory Management Unit (MMU) that sits between the CPU and the RAM.

text
How the MMU works (Dynamic Relocation):

1. The OS assigns the process a 'Base Register' (e.g., 14000).
2. The CPU generates a Logical Address (e.g., 346).
3. The MMU intercepts the address and adds the Base Register to it.
   Physical Address = 346 + 14000 = 14346
4. The RAM returns the data at 14346.

The user program NEVER sees the real physical addresses. It only knows about address 346. The MMU silently does the heavy lifting in the background.

Why Keep Them Separate?

Separating logical memory from physical memory provides three massive benefits:

  • Security and Isolation: Because a process only sees logical addresses, the OS can ensure it never accidentally (or maliciously) accesses the physical memory belonging to another process or the OS itself.
  • Relocation: The OS can pause a process, move it to a different location in physical RAM to clean up fragmentation, update the Base Register, and resume the process. The process will have no idea it was moved.
  • Virtual Memory: It allows the OS to pretend it has more RAM than it actually does. The Logical Address Space can be much larger than the Physical Address Space, with the OS swapping chunks to the hard drive as needed.

Summary

The separation of logical and physical address spaces is one of the most important concepts in operating systems. It frees programmers from worrying about physical hardware limitations and allows the OS to safely and dynamically manage memory behind the scenes via the hardware MMU.

MMU Address Translation

Question 1 of 1

Test your understanding of how the MMU calculates physical addresses.

A process generates a logical address of 450. The operating system has loaded this process into memory with a Base (Relocation) Register value of 25000. What is the actual physical address accessed in RAM?
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.