Project 4 -- Virtual Reality
Due Wednesday, June 3, 2009
- To implement virtual memory so that processes can take up
more space than physical memory can hold
- To implement demand paging
- To understand and implement a page replacement algorithm
(maybe more than one!)
So now you've got a paging system that allows multiprogramming.
The catch is that every running process must still fit
in memory in its entirety. In this last project, you'll
remove that limitation by implementing demand paging and
virtual memory. You should also consult Lab 6 in the
Nachos Project Guide
which serves as the inspiration to this project. There are
details explained there about Nachos that can help.
Part 1: Pages on demand
Alter your code to implement demand paging.
Thus, when a process is first loaded into memory, all of its page
table entries should be set as invalid. When the CPU attempts
to access a page that is not in memory, a page fault will occur.
You will then service the page fault by allocating a free frame,
loading the appropriate
page into the frame, setting the corresponding page table entry
to valid, and then restarting the instruction. Here are some
hints and details to help you on your way.
Debug your code until it works. Be sure to test your own homemade
user programs from Project 3 to make sure your demand paging works.
- For now, when a page fault happens, you can assume that you will
have a free frame available. That is, you won't need to choose
a victim frame yet. That's for Part 2. You'll implement this
assumption simply by testing user programs that you know in advance
will completely fit in memory, like halt or
my two paging tests on Blackboard.
All of the other programs in the "test" directory, like
sort and matmult, won't work until Part 2 is finished.
- There is already a "valid" field in each page table entry. You
just have to use it.
- If you check out machine.h, you'll see a list of all
the possible exceptions (traps) in Nachos. You worked with
SyscallException in Project 3. You'll work with PageFaultException now.
Do a grep for PageFaultException so you can see how and where it
is being used. In Nachos, a page fault causing a trap to the OS
will manifest itself as just another exception to deal with
in ExceptionHandler. So you're on familiar ground. Make sure
you understand what this means! (And ask me if you don't!)
- Remember, do NOT advance the PC when you're done servicing
the page fault. You want the instruction that originally caused
the page fault to be redone. It just shouldn't cause a page fault
the second time around.
PITFALL ALERT: There is the possibility that a page fault
might occur not just in a user program, but also inside
the kernel itself. Why? (Hint: think about Exec and
what we've already discussed in class regarding address translation.)
If a page fault occurs in the kernel, you don't have control over
the PC in this instance because to Linux, the Nachos kernel is
just another C++ program and you don't have control over the
real Linux PC! To solve this, whenever you issue a ReadMem
command in the kernel (this is where the page fault would happen),
do it twice. If there's a page fault on the first ReadMem,
the second one will be valid. We'll talk more about this pitfall
Part 2: The Big Swap Out
Now, implement virtual memory by allowing page faults to be serviced
even if there are no free frames available. You will need to:
- Implement a page replacement algorithm. You should implement it
as a separate file so that you can easily switch to a different one.
Please use one of the following global replacement strategies:
- EXTRA CREDIT ALERT: if you implement Second Chance or
Third Chance, it's worth 5 points of extra credit.
- Create a swap file that can be used to write out dirty pages. A
swap file is just a file stored on the simulated disk. Check out the
filesys and openfile classes in the filesys
directory. They'll have the functions you need to create, open, read,
and write to a swap file.
I suggest using a single swap file for all of your swaps. Just read
and write to the swap file in page-sized chunks. Add an int field to
each page table entry called "swapPage" that holds the page of the
swap file containing that page's data. Initialize swapPage to -1
indicating that no swap file exists for that page at the beginning.
Then, once your page replacement algorithm identifies a victim frame,
do the following:
- Check the dirty field to see if a swap out is needed. If so...
- Write the victim frame's contents to the end of the swap file.
Keep a global counter to indicate the number of pages the swap file
already has so you know where to write. For example, if the counter's
value is 5, this means the swap file already contains 5 pages
(0 through 4). Write
the page out starting at location 5 * PageSize = 5 * 128 = 640.
- Record the counter value (5 in our example) in the swapPage field
of the appropriate page table entry and increment the counter.
Remember, you also need to consult the swapPage field when swapping
in the new page to see if the new page should be coming from
the original executable file or from the swap file.
EXTRA CREDIT ALERT: Part 3: PRA comparison
Attempt this if and only if you have the rest of the project
working. For 5 more extra credit points, create two page
replacement algorithms (PRAs) and compare their performances.
If you create each algorithm in a separate file, you should
be able to switch between them easily through modification
of Makefile.common. You will need to create
interesting test programs that stress the two algorithms
and then measure their relative performance.
Hint: What do those statistics say when Nachos
terminates at the end of an execution?
Write a report that explains what you did, what experiments you ran,
what you were trying to test, what you measured, and what your
conclusions are. Under what circumstances does one algorithm
fare better than the other?
The Nachos Project Guide
is your friend. Consult it for more details about Nachos
in relation to this project.
This project is worth 50 points:
- 25 points for Part 1.
- 25 points for Part 2.
There is a maximum of 10 points of extra credit to be gained in this
What to turn in
Turn in a paper copy of your output showing your frames being
allocated on demand (as before, use DEBUG stmts).
For virtual memory, your output should clearly indicate when
a page fault occurs, when and what victim frame is chosen,
and when the swap out/swap in occurs. Again, use DEBUG
stmts. Occasionally printing
out the page table may also be a good way of showing what's going on.
Label your tests well
and include explanations so I can tell what is happening. Also,
paper copies of source code for all code you have written
Electronically, please submit your entire nachos directory (the
code directory) using our submission script;
/home/csc335pub/submit csc335 <full path to your code
Having trouble? Don't wait until the last minute! Come see me and get
Programming assignments, like homework
assignments, are individual projects. I encourage you to
others about the general nature of the project and ideas about how to
However, the technical work, the writing, and the inspiration behind
be substantially your own. If any person besides you contributes in any
the project, you must credit their work on your homework. Similarly, if
include information that you have gleaned from other published sources,
cite them as references. Looking at, and/or copying, other people's
written work is inappropriate, and will be considered cheating.