This guide is about difference between logical and physical address in Operating Systems. Two different types of addresses are used for memory in computer operating systems. Physical address is a actual address of memory like RAM and virtual address is a just a logical address mapping between cache and RAM.
In operating systems, whenever we talk about addresses or address space of a code or its portion we mean the place in memory where that part of code resides. Lets’ talk about a real life anomaly to understand the addressing in operating systems. In real life we have houses which are assigned specific addresses, so if we want to go to the place of a specific person we recall for the address of his/her house. Same is the case with addresses in operating systems. We store our data in memory at different locations and assign them with different addresses so that we can access them again in future using the same address at which they are stored. In operating systems we uses 32-bit address space in 32 bit architecture and in hexadecimal numbers of the form 0xFFFFFFFF, starting from 0x00000000 to 0xFFFFFFFF.
After having a catch up discussion on addresses and why they are needed, we will now differentiate between logical and physical address.
Introduction to Logical Address
Whenever we talk about logical addresses, we mean the address CPU assign to each process. As we have already discussed the actual address at which a process is located in memory is not same as that of the address which the process thinks it is placed at. This mapping of addresses is basically needed for memory sharing along the processes without letting the processes know that they are sharing same memory space with each other. Lets talk about logical addresses now.
Whenever the CPU run a process it assign a specific memory to that process. The division of the memory segment in different portions is explained briefly in the image shown below, where the text is at lower address and stack is at higher address.
Figure 1: Memory space of a process
This allocated memory space is located at the virtual address generated by the CPU for the process (virtual or logical address) indicating the memory space of the process at which the code, heap and stack section of process will reside. This logical address is no doubt different from the actual address of the memory space. The virtual address is also known as the reference or pointer to the actual or physical address in the main memory.
Most of the operating systems have a base address defined in their program, at the time of the generation of a logical address the CPU generates an address and the program add the base address to the address generated by the CPU to get the logical address i.e.
Logical address = Base address + CPU generated address
And a logical address space is referred as the set of all the logical addresses generated by the CPU.
Introduction to Physical Address
Physical address is the address at which a process and its content is placed in the main memory or hard disk. Whenever we run a process or store some data in the primary or secondary storage devices of a computer we always store it for a purpose of accessing it in the future whenever we want to. For instance, I save a word file in the D drive of my PC and I want to access it so how can I access it in the future? Of course by going to the D drive and opening the word file. But what If I save a process in some storage cell or memory cell in the main memory and want to access it in the future. As far as we have studied about addresses in this tutorial we know that whenever a process is created CPU generates its virtual address but there is again a mess. A virtual address is not the same as that of the address at which the process is actually placed in the memory.
However, the memory cell is stored in the main memory accessed by a physical address ( unknown to the user ) and all we know is the logical address of the process. So when we need to access the process how we are going to access the physical address? This seems impossible so far but of course it is not because we have been accessing the created processes from the birth of computers up till now.
Therefore, we need to discuss this issue in detail. At the time of process creation, the CPU not only generates the virtual address but it maps that ( previously generated ) virtual address on the physical address at which it is actually stored using some hardware support which is discussed in the Mapping of address section below. Hence, when we try to access the already saved process in the memory, the CPU returns the virtual address to the hardware and the hardware maps the virtual / logical address on the physical address and give indirect access to the process’s memory space.
Mapping of address
Let’s now discuss how the mapping is performed by the hardware between logical and physical address. In the hardware of the CPU and Memory Management Unit ( MMU ) is installed which helps in the mapping of the address. The figure below explains it well.
Figure 2: Mapping of logical address on physical address
This memory management unit performs all the required mapping for the physical address on the logical address. The very basic mapping performed by MMU is by a scheme known as base and bound approach. This is anyhow the simplest approach one can use to perform mapping. As the mapping of the addresses become more sophisticated we need to add more and more hardware to the memory management unit. Lets’ discuss the base and bound register.
Base and bound approach
In the base and bound approach, each MMU unit have two registers named as base register and bound register. The base register contains in it the starting address of the memory section of the particular process ( which is running ). For example, when the hardware needs to fetch some instructions, it first needs to add the value of the base register to the logical address to get the physical address. Thus the base register is related to accessing the memory and on the other hand the bound register either stores the ending address of the memory section of a particular process or it may also contain the total size of the memory section ( including code, stack and heap ). It is used only for protection so that the process to not exceed the memory region allocated to it and it may not jumps into the territory of the memory region of another process. Below is given the general comparison of logical and physical address.
Comparison Char for Logical and Physical Address
The comparison chart of the logical and physical address is given in the table below,
Field of comparison | Physical Address | Logical Address |
---|---|---|
Definition | Physical memory address | Virtual address |
Space | Actual memory address at which memory space is located | It is reference to the physical address/ actual address and is referred as logical address space |
Visibility | It is visible to the developer only and not to the programmer | It is visible to the user only |
Access | It can’t be accessed by the user in any scenario | User can access it on their ease |
Generation | It is generated by the CPU | It is generated by the MMU |