Mapping Virtual Address to Physical Address

Mapping Virtual Add to Physical Address img

Mapping Virtual Address to Physical Address in OS

The physical memory is basically a large array of words (storage spaces), with each having its own address. The CPU allocates logical memory address to any given physical address where actual data is stored. Since, in real case scenario related data may be stored at different contiguous locations. Logical address generated by CPU may help in viewing the data stored as contiguous.

CPU also, performs the job of fetching the memory / instructions based on the program counter. It may load or store data in those physical address based array of words.

Address Binding

Address binding is essentially the process of binding (mapping) different address spaces to another.

Different Types of Addresses

There are two different types of addresses in the system –

  • Logical or Virtual
  • Physical

The physical address is where the actual data is stored, and the logical address generated by CPU is how the system will see the addresses.

Physical memory (RAM) is divided into pages contiguous sequences of memory typically in the 4KB-16KB range. Physical addresses are provided by the hardware.

  • One physical address space per machine
  • Valid addresses are usually between 0 and some machine-specific
  • Not all addresses have to belong to the machine’s main memory.
  • Other hardware devices can be mapped into the address space.

Virtual (or logical) addresses are provided by the OS kernel:

  • One virtual address space per process
  • Addresses may start at zero, but not necessarily
  • Space may consist of several segments

Memory Management Unit (MMU)

The user only thinks that he is accessing the data from the logical address. The translation from the logical to the physical address is done by special equipment in the CPU that is called the Memory Management Unit (MMU), in some systems it is also called as address translator unit.

MMU Additional does –

  • Checks for protection violations
  • Raises exceptions when necessary (e.g., write operation on readonly memory region).
mapping virtual add to physical add

The address translation can be done in the three ways or at three different stages –

Compile Time

In some cases it is pre-known at the compile time itself about the final address (physical) of the memory for a running process.

This helps the system overall as –

  • It is faster to load an existing executable format process

Note – There are chances for the program itself to crash as the defined address space (physical) may already be preoccupied by another process. In this case crash may happen and the system itself or with user intervention has to re-compile the whole program.

Load Time

The above crashing issue can be solved here, or in cases where it is not known that where the process memory will finally be residing. Here the relocatable address will be generated.

Loader does –

  • Job of the loader is to translate the above relocatable address to an absolute address
  • Adds : Base address to all, logical addresses, in turn to generate absolute address.

Note – Reloading of the whole process is necessary if in case the base address itself has changed in the mean time,

Execution Time

We all know that at the time of execution of a process we may need dynamic memory to save intermediate results or remove the results, i.e. allocation and de-allocation is inadverent at execution time.

Adding addresses as follows –

  • CPU generated logical address
    • 245
  • MMU generated relocation register, which is also called as base register
    • 8000
  • So finally physical memory location will be
    • 8000 + 245 = 8245

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription