Find physical address from logical address given page table

5.6k Views Asked by At

Following is a page table - enter image description here

Assume that a page is of size 16000 bytes. How do I calculate the physical address for say the logical address 1000. Here is what I have worked out yet. Logical memory = 8 pages Logical memory size = 8 x 16000 bytes

Physical memory = 8 frames physical memory size = 8 x 16000 bytes

Now given a logical address of 1000 it will map to the first page which is in frame 3 so considering frame0, frame1, frame2 all of 16000 x 3 bytes. 1000 will be at location 16000 x 3 + 1000 so the physical address will be = 49000 byte

Is this a correct approach?

1

There are 1 best solutions below

0
On

Is this a correct approach?

Yes. To clarify:

Given a logical address; split it into pieces like:

offset_in_page = logical_address % page_size;
page_table_index = logical_address / page_size;

Then get the physical address of the page from the page table:

physical_address_of_page = page_table[page_table_index].physical_address = page_table[page_table_index].frame * page_size;

Then add the offset within the page to get the final physical address:

physical_address = physical_address_of_page + offset_in_page;

Notes:

  • a CPU (or MMU) would do various checks using other information in the page table entry (e.g. check if the page is present, check if you're writing to a "read-only" page, etc). When doing the conversion manually you'd have to do these checks too (e.g. when converting a logical address into a physical address the correct answer can be "there is no physical address because the page isn't present").

  • modulo and division (and multiplication) are expensive. In real hardware the page size will always be a power of 2 so that the modulo and division can be replaced with masks and shifts. The page size will never be 16000 bytes (but may be 16384 bytes or 0x4000 bytes or "2 to the power of 14" bytes, so that the CPU can do offset_in_page = logical_address & 0x3FFF; and page_table_index = logical_address >> 14;). For similar reasons, page table entries are typically constructed by using OR to merge the physical address of a page with other flags (present/not preset, writable/read-only, ...) and AND will be used to extract the physical address from a page table entry (like physical_address_of_page = page_table[page_table_index] & 0xFFFFC000;) and there won't be any "frame number" involved in any calculations.

  • for real systems (and realistic theoretical examples) it's much easier to use hexadecimal for addresses (to make it easier to do the masks and shifts in your head = e.g. 0x1234567 & 0x03FFFF = 0x0034567 is easy). For this reason (and similar reasons, like determining location in caches, physical address routing and decoding in buses, etc) logical and physical addresses should never be use decimal.

  • for real systems, there's almost always multiple levels of page tables. In this case approach is mostly the same - you split the logical address into more pieces (e.g. maybe offset_in_page and page_table_index and page_directory_index) and do more table lookups (e.g. maybe page_table = page_directory[page_directory_index].physical_address; then physical_address_of_page = page_table[page_table_index].physical_address;).