Skip to main content

Memory Management: Logical and Physical Addresses Explained

Memory management is a fundamental function of operating systems, ensuring that memory is used efficiently and safely by multiple processes. It plays a key role in providing isolation between processes, translating logical addresses to physical addresses, and managing memory allocation. But how does all this work? Let's dive deeper into the concepts of logical and physical addresses and the Memory Management Unit (MMU).

What is Memory Management?​

Memory management is a critical function of an operating system that enables efficient and safe use of memory by multiple processes. It ensures isolation between processes, translates logical addresses to physical addresses, and manages memory allocation.

Why is Memory Management Important?​

  • Isolation: It prevents processes from interfering with each other's memory space, ensuring that each process operates within its own boundaries.

  • Efficient Use of Memory: Optimizes the use of available memory, ensuring that multiple applications can run simultaneously without issues.

  • Protection: Protects the memory space of one process from being accessed or modified by another, enhancing system security and stability.

To understand how memory management achieves these goals, we first need to look at logical and physical addresses.

Memory Management
Memory Management

Logical Address​

What is a Logical Address?​

A logical address, also known as a virtual address, is the address generated by the CPU while a program is running. It’s the address used by the program and its developer. The logical address space allows a process to have its own private view of memory, starting from 0 KB, which helps developers focus on writing code without worrying about actual memory locations.

Example of Logical Address​

Imagine you're a developer working on two separate applications:

CPP
// Application 1
int main() {
int data[100]; // Logical addresses: 0 KB to 400 KB (assuming each int is 4 KB)
data[0] = 10; // Logical address: 0 KB
data[1] = 20; // Logical address: 4 KB
return 0;
}
CPP
int main() {
// Application 2
string data[2];
data[0] = "shubh"; // Logical address: 0 KB
data[1] = "patel"; // Logical address: 5 KB
return 0;
}

In both applications, data[0] starts at logical address 0 KB. This might make it seem like the two applications could interfere with each other. However, these logical addresses are just an abstraction—they don’t represent the actual physical locations in memory.

Why Logical Addresses?​

  • Developer Convenience: Developers can use simple, consistent address spaces without worrying about the actual hardware memory layout.

  • Process Isolation: Each process has its own logical address space, ensuring that the operations of one process do not affect another.

Physical Address​

What is a Physical Address?​

A physical address is the actual location in the computer’s main memory (RAM). When a program runs, its instructions and data are stored in RAM, and each part of the process has a physical address. These addresses are unique and ensure that different processes do not overlap in memory.

How Are Physical Addresses Managed?​

  • Unique Physical Address Space: Each running process is assigned a unique range of physical addresses, ensuring no overlap with other processes.

  • Non-overlapping: This non-overlapping nature is what ensures memory protection and process isolation.

Example: Logical to Physical Address Translation​

  • Application 1 might start at a physical address of 200 KB.

  • Application 2 might start at a physical address of 500 KB.

Even though both applications use logical address 0 KB for their first data element, they map to different physical locations.

Memory Management Unit (MMU)​

What is the MMU?​

The Memory Management Unit (MMU) is a hardware component responsible for translating logical addresses to physical addresses. It acts as a bridge between the CPU and the RAM, ensuring that the memory operations are performed correctly and efficiently.

Memory Management Unit
Memory Management Unit

How Does the MMU Work?​

  1. Process Scheduling: When a process is scheduled to run, it is assigned to the CPU.

  2. Logical Address Generation: The CPU generates a logical address during execution, like when accessing an array or a variable.

  3. Address Translation: The MMU translates the logical address into a physical address using a mapping mechanism.

    • Base Register: Each process has a base register that stores the starting physical address. For example, P1’s base might be 200 KB.

    • Limit Register: It also has a limit register defining the total size of the process, like P1’s limit being 10 KB.

    • Bounds Check: The MMU checks if the logical address is within the specified limit (e.g., 0 - 10 KB). If it’s out of bounds, it triggers an error.

    • Relocation Register: This adds the base address to the logical address to find the physical address. For example, if accessing 5 KB logically, the physical address becomes 200 + 5 = 205 KB.

  4. Access Data: The physical address is then used to access the actual data stored in RAM. Note

When a context switch happens (switching the CPU from one process to another), the Base and Limit values from the Process Control Block (PCB) are loaded into the Relocation and Limit registers.