I was reading about how Virtual Memory and Memory management works in an Operating System. I understood how each process has its own contiguous logical address space. This address space need not be contiguous in physical memory. For this purpose, paging is used. The page tables are used to do the mapping from logical address to a physical address. The logical address is divided in two parts, first part gives the logical page number, which using the page table is translated to the physical page number, and the second part is offset into that page. Thus the contents from memory are retrieved.
Virtual memory is an extension of this, where all the pages need not be in main memory, and can be brought there through page faults.
Using my understanding I solved the 4th question on this page: http://www.ics.uci.edu/~bic/courses/JaverOS/ch8-ex.pdf
I got my answers wrong, and I have no clue what the right answers are.
This is the way I did it:
From the diagram in the question, Page table for Process P1 will look like this according to me:
0-4
1-5
2-6
3-7
So when process P1 makes a reference to 0 i.e is 0000 in 4 bit binary, we divide
it as 00|00.
Thus logical page no = 00 and offset = 00.
From the page table, we can see 0 is mapped to 4th physical frame.
Offset is also 00 here. So I get the 0th entry(offset) in the 4th frame.
The content at this memory location(i.e Frame No. 4, offset 0) is 0.
Why is this wrong?
Can anyone help?
I’ll start with virtual address 8, it should illustrate things better.
8 = 2×4 + 0. The physical address is stored at offset 0 of page 2 of the process. We look up the second page. For P1, page 2’s address is stored at PA 4 + 2, and it is 28. We look up the contents of PA 28, and get value 0. For P2, page 2’s address is stored at PA 12 + 2, it is 24, the contents of PA 24 are 5.
Now with virtual address 15, which illustrates the exceptional cases.
15 = 3×4 + 3. The physical address is stored at offset 3 of the process’s page 3. For P1, page 3’s address is stored at PA 4 + 3, which contains -2. The sign bit indicates the page is outside physical memory, which means there will be a hard page fault (a situation the OS handles exceptionally, but not an error). For P2, page 3’s address is stored at PA 12 + 3, which some special mechanism (a poison value, an external frame table?) tells us is invalid. This is an error, and gets reported as a segmentation fault.
One last example with VA 7 and P1 to explain offsets:
7 = 1×4 + 3. The physical address is stored at offset 3 of P1’s page 1. Page 1 is at PA 8, offset 3 of page 1 is at PA 8+3.