Operating System - Logical and Physical Address



In computers, an address is used to identify a location in the computer memory. In operating systems, there are two types of addresses, namely, logical address and physical address. A logical address is the virtual address that is generated by the CPU to access memory locations. On the other hand, a physical address is one that represents a location in the computer memory.

Physical Address in Operating System

The physical address of a computer program is one that represents a location in the memory unit of the computer. The physical address is the real address that is used to access data stored in the RAM (Random Access Memory) of the computer system. A group of several physical addresses is referred to as a physical address space.

The physical address is generated by the Memory Management Unit (MMU) of the computer system after translating the logical address generated by the CPU. The physical address is used by the memory controller to access the actual data stored in the RAM.

The image below shows how the logical address and physical address are used in a computer system −

Logical and Physical Address

Logical Address in Operating System

The logical address is a virtual address created by the CPU of the computer system. The logical address of a program is generated when the program is running. A group of several logical address is referred to a logical address space. The logical address is basically used as a reference to access the physical memory locations.

Why Logical Address is Needed?

The idea behind implementing logical address in operating systems is:

  • The part of the program that is currently in use will be stored in physical memory (RAM), and the rest of the program can be stored in secondary storage (like hard disk).
  • The logical address is used to access the parts of the program that are currently stored in secondary storage.
  • So the logical address allows the program to use more memory than what is physically available in the RAM. This technique is known as virtual memory.
  • Also, it provides an abstraction layer between the program and the physical memory.

The logical address is translated into physical address by the Memory Management Unit (MMU) of the computer system. This translation is done using a page table that maps logical addresses to physical addresses.

Address Translation Using MMU

The Memory Management Unit (MMU) is a special hardware component used for translating virtual addresses generated by the CPU into physical addresses used to access RAM. The MMU uses page tables ( along with translation lookaside buffers) to perform this mapping. A page table is a data structure that stores the mapping between virtual pages and physical frames in memory. The translation lookaside buffer (TLB) is a small cache that stores recently used page table entries.

When a logical address is generated, the MMU extracts the page number and offset from it. It then check the page table to find frame number for the corresponding page number. The frame number is combined with the offset to create the final physical address using the following formula −

$$\mathrm{\text{Physical Address} = (\text{Frame Number} \times \text{Page Size}) + \text{Offset}}$$

Where,

  • Frame number is the number of the frame in physical memory where the page is stored.
  • Page size is the size of each page in memory.
  • Offset is the distance from the start of the page to the specific location within the page.

Example

Suppose a virtual address like 0x100000 is generated by a process. Assume that the page size is 4 KB (4096 bytes). The MMU will extract the page number and offset from the virtual address as follows:

$$\mathrm{\text{Page Number} = \frac{0x100000}{4096} = 256}$$

$$\mathrm{\text{Offset} = 0x100000 \mod 4096 = 0}$$

Now, the MMU will look up the page table to find the corresponding frame number for page number 256. Let's assume that the frame number is 512. The MMU will then calculate the physical address as follows −

$$\mathrm{\text{Physical Address} = (512 \times 4096) + 0 = 0x200000}$$

So, the virtual address 0x100000 is translated to the physical address 0x200000 using the MMU.

Note: The translation explained here is based on a simple paging mechanism. The formula and methods may vary based on the specific memory management techniques like segmentation, inverted page tables, etc.

Logical Address vs Physical Address

The following table highlights all the major differences between logical and physical address in operating system −

Logical Address Physical Address
This address is generated by the CPU. This address is a location in the memory unit.
The address space consists of the set of all logical addresses. This address is a set of all physical addresses that are mapped to the corresponding logical addresses.
These addresses are generated by CPU with reference to a specific program. It is computed using Memory Management Unit (MMU).
The user has the ability to view the logical address of a program. The user can't view the physical address of program directly.
The user can use the logical address in order to access the physical address. The user can indirectly access the physical address.

Conclusion

A physical address is a memory address that is represented in the form of a binary number on the address bus circuitry in order to enable the data bus to access a particular storage cell of main memory. A logical address, on the other hand, is a memory address that is generated by the CPU and is translated into a physical address by the MMU in order to access a specific location in the main memory.

The most significant difference between a logical address and a physical address is that the logical address is a virtual address generated by the CPU that is used to access the physical address, while a physical address is a location in the computer's memory unit.

Advertisements