Sections Thu Nov 17 to Fri Nov 18
Solutions
1) Virtual Memory Review
Q1: In a base and bound implementation, let's say a process has base = 4000 and bound = 200. For each of the following memory accesses, are they valid? And if so, what physical address would they really access?
- accessing virtual address 0
- accessing virtual address 50
- accessing virtual address 300
A1: The first two are valid, but the third is not - becuase it's >= bound. The first one would really access physical address 4000 + 0 = 4000, and the second one would really access physical address 4000 + 50 = 4050.
Q2: Let's say the OS decides to move that process's reserved physical memory somewhere else; specifically, the OS copies it from base 4000 to base 2000. They also give the process more memory, updating its bound to bound 500. How would the outcome of the memory accesses above change? (Cool note: the process itself has no idea its physical memory was moved, and all it needs to do to access the additional memory we've given it is to refer to those larger virtual addresses. Pretty neat!).
A2: Now, all 3 are valid because they are all < bound. The first one would now really access physical address 2000 + 0 = 2000, the second one would really access physical address 2000 + 50 = 2050, and the third one would really access physical address 2000 + 300 = 2300.
Q3: In a multiple segments implementation, let's say a process has 2 segments, segment 1 with base 1000 and bound 500, and segment 2 with base 2000 and bound 300. For each of the following memory accesses, are they valid? And if so, what physical address would they really access?
- accessing segment 1, offset 400
- accessing segment 2, offset 400
- accessing segment 2, offset 50
A3: the first and last ones are valid, but the second one is invalid because it is >= to segment 2's bound of 300. The first one would really access physical address 1000 + 400 = 1400, and the third one would really access physical address 2000 + 50 = 2050.
2) Implementing Multiple Segments Translation
Q4: implement the translate method and test it by running the provided test program, which attempts to access 3 addresses, the first two of which are valid, and the third of which is invalid and should cause a crash.
A4: a solution could look something like this:
char *VirtualMemory::translate(const VirtualPointer& p) {
if (p.offset >= segment_map[p.segment].bound) {
cerr << "memory violation - accessing invalid offset " << p.offset << " in segment " << p.segment << endl;
raise(SIGSEGV);
return nullptr;
} else {
return segment_map[p.segment].base + p.offset;
}
}
Checkoff Questions
-
[Q1] If a process has 2 segments, segment 1 with base 1000 and bound 500, and segment 2 with base 2000 and bound 300, is accessing segment 1 at offset 50 valid? If so, what physical address would it really be accessing?
- Yep - it would really access physical address 1000 + 50 = 1050.
-
[Q2] Explain one benefit of the multiple segments approach vs. the regular base and bound approach (where there is 1 base and bound for the entire process).
- One benefit is that each segment can be managed independently; one could be swapped to disk while others stay in memory, one could be enlarged while others stay the same size, one could be marked read-only while the others are read-write.
-
[Q3] In the
translatemethod, once you confirm that a virtual address is valid, how do you go about translating it to a physical address?- First, we look in the map at the entry for this segment, and check if the offset is less than the segment's bound. If it isn't, then we trigger a segmentation fault. Otherwise, we add the segment's base to the virtual address's offset and return that result.