6.1 Memory allocation
The default Nachos implementation assumes uni-programming, namely, only one user
process runs and occupies the physical memory at a time (note this is different from
having multiple kernel threads). When a process is executed, its program and data
are loaded to the main memory. Furthermore, the entire address space fits in
memory. Therefore, virtual memory and physical memory are in fact identical in this
case.
1 protected boolean UserProcess.loadSections() {
2 ...
3
4 // load sections
5 for (int s = 0; s < coff.getNumSections(); s++) {
6 CoffSection section = coff.getSection(s);
7
8 Lib.debug(dbgProcess, "\tinitializing " + section.getName()
9 + " section (" + section.getLength() + " pages)");
10
11 for (int i = 0; i < section.getLength(); i++) {
12 int vpn = section.getFirstVPN() + i;
13
14 // for now, just assume virtual addresses=physical addresses
15 section.loadPage(i, vpn);
16 }
17 }
18
19 return true;
20 }
In Line 15, section.loadPage loads a page in the section into a physical
memory page indexed by vpn.
Implementation notes: Nachos user programs do not make use of malloc()
or free() and thus effectively have no dynamic memory allocation needs (or
equivalently, no heap). In the current implementation, a fixed number of pages
is used for the process’ stack, e.g., 8 pages. Therefore, the complete memory
needs of a process is known when it is created. This eases the task of static
memory allocation when no on-demand paging is used.