Memory – Calculating Physical Address Given Only an Offset

memory

Learning about virtual memory with an program assignment. Currently this is what I have:

Main memory has 60 bytes and virtual memory another 60 bytes. The size of a frame/page is 10 bytes. So there are 6 frames for main and 6 for virtual memory.

I have a process that uses 40 bytes, so it needs 4 pages. By assignment specifications, when I load a process its four pages must be allocated randomly, so suppose this is how it looks now:

enter image description here

I've managed to do most things, like paging something, restoring a page with LFU and LRU, loading more processes etc. But there is one particular point that confuses me:

Given an offset for your process, you must return the physical
address. You must verify that the offset is within the size of your
process.

As far as I am concerned, you can calculate a physical address if you are given an offset AND a page number. But I am only given an offset number.

My guess is that you can get the page number using this same offset number too. For instance, if the offset is 36, then the page number would be 3 (because each page is 10 bytes large) with an offset of 6 bytes. Therefore the physical address would be 56? But then I have a bigger question:

What if I am given the number 25? It would be page 2 with offset 5. But such page is in virtual memory. Do I have to MOVE this page to main memory first?

Best Answer

You have a basic misunderstanding of how virtual memory works.

The virtual memory space of a process is a linear list of addresses, 0 to N. The addresses within the virtual memory space are segmented into pages, and those pages may reside either in RAM or on disk (although they must be moved to RAM to execute).

So your diagram needs three blocks:

virtual memory diagram

To "calculate a physical address" given a virtual address (which is the closest interpretation I have of "offset"), you use the page table to find out where a page resides. The result is a tuple that identifies where the pages resides (RAM or disk), and where in that space it resides (this could be an absolute address, as long as you consider swap to be a contiguous memory space).