Beruflich Dokumente
Kultur Dokumente
In the UserKernel class we will add a static attribute which will be a global link list maintaining
all the free pages in physical memory.
Static LinkedList<Integer> freePages
freeApage method will just free the page with physical page no ppn and add it to the freePages
link list
void freeApage(int ppn) {
Disable interrupts;
freePages.add(ppn);
Enable interrupts;
}
getNofreePages method will return the no of free pages available at that time
int getNofreePages() {
disable interrupts;
return freePages.size();
enable interrupts;
}
Now the attributes (and some variables) to be added to UserProcess class are the following:
UserProcess myParent // parent process
ArrayList<UserProcess> myChildren // array of child processes
int pid // process id
static int nextPid = 1 // used for assigning pid's of children
int exitStatus
boolean exitedProperly // boolean to check if program exits properly or not
The maximum number of open files for a process, this value is fixed
final int maxFiles = 16
we will define a method vaddressToPaddress(vaddr) which uses a virtual address and gives its
physical address or -1 if the vaddr is not in the page table
int vaddressToPaddress(vaddr) {
vpn = Processor.pageFromAddress(vaddr);
offset = Processor.offsetFromAddress(vaddr);
if(pageTable[vpn] == null) return -1;
int ppn = pageTable[vpn].ppn;
return (ppn*pageSize)+offset;
}
Now we will modify readVirtualMemory and writeVirtualMemory methods, these methods will
return the no of bytes read or written respectively(even if there's an error).
vpn = Processor.pageFromAddress(vaddr);
if (pageTable[vpn] == null || pageTable[vpn].readOnly)
return 0;
paddr = vaddressToPaddress(vaddr);
assertTruecheck(paddr >= 0);
amount = Math.min(length, pageSize);
arraycopy(data, offset, memory, paddr, amount);
return amount;
}
loadSections method allocates memory for this process, and loads the COFF sections into
memory. If this returns successfully, the process will definitely
run (this is the last step in process initialization that can fail).
returns true if the sections were successfully loaded.
boolean loadSections() {
check if this coff can be loaded or not , if not close the coff and return false
if (numPages >processor().getNumPhysPages()) {
coff.close();
return false;
}
// load the sections into memory
for (int s=0; s<coff.getNumSections; s++) {
CoffSection section = coff.getSection(s);
for (int i=0; i<section.Length; i++) {
int vpn = section.getFirstVPN()+i;
int ppn = getFreePage();
if (ppn < 0) {
return false;
}
section.loadPage(i, ppn);
// now fill the pagle table with this translation entry
pageTable[vpn] = new TranslationEntry(vpn, ppn,
true, section.isReadOnly(), false, false);
}
}
return true;
}
Now we will implement unloadSections which helps in deallocating alloted memory and closing
all the file descriptors
void unloadSections() {
for (int i = 0; i < fileDescriptorTable.length; i++) {
f = fileDescriptorTable[i];
if (f != null)
f.close();
f = null;
}