What is the difference between logical and physical address wrt Operating System?
The addresses identify a location in the memory. In the operating system, when we talk about memory we discuss a location where the actual code resides in the system. Like we have the address of our house so that anyone can reach out to us. In the same way, we store the data in the memory at different locations with addresses so that we can access the data again whenever required in the future. There are two types of addresses that are used for memory in the operating system i.e. the physical address and logical address about which we will learn in this blog. So, let's get started.
The physical address refers to a location in the memory. It allows access to data in the main memory. A physical address is not directly accessible to the user program hence, a logical address needs to be mapped to it to make the address accessible. This mapping is done by the MMU. Memory Management Unit(MMU) is a hardware component responsible for translating a logical address to a physical address.
A logical address or virtual address is an address that is generated by the CPU during program execution. A logical address doesn't exist physically. The logical address is used as a reference to access the physical address. A logical address usually ranges from zero to maximum (max). The user program that generates the logical address assumes that the process runs on locations between 0 to the max. This logical address(generated by CPU) combines with the base address generated by the MMU to form the physical address.
The diagram below explains how the mapping between logical and physical addresses is done.
- The CPU generates the logical address(here, 324).
- The MMU will generate the base address (here, 2000) which is stored in the Relocation Register.
- The value of Relocation Register(here, 2000) is added to the logical address to get the physical address. i.e. 2000+324= 2324(Physical Address).
Difference between the physical and logical address
- The fundamental difference between a physical address and the logical address is that logical address is generated by the CPU while the program is running whereas the physical address is a location in memory.
- The logical address is generated by the CPU whereas physical address is computed by the MMU.
- The logical address does not exist physically in the memory hence it is sometimes known as virtual address whereas the physical address is a location in the memory unit.
- The logical address is used as a reference to access the physical address. The physical address cannot be accessed directly.
- Users can view the logical address of a program. But, they cannot view the physical address of a program.
- The set of all the logical addresses generated in reference to a program by the CPU is called Logical Address Space whereas the set of all the physical addresses mapped to the logical address is called Physical Address Space.
This was about the differences between logical and physical addresses. Hope you learned something new today.
Keep Learning :)