Sie sind auf Seite 1von 5

Question 2 ----> Multiprogramming

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

We will also add some methods to this class

getFreePage method will give a free page(frame) of the physical memory


So this method will return the page(frame) no or -1 if no page is available.
int getFreePage() {
Disable interrupts;
if (freePages.size() != 0) {
return freePages.removeFirst();
}
restore interrupts;
else return -1;
}

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

The file descriptor list for this process


OpenFile[] fileDescriptorTable
KThread processThread // the UserProcess object is mapped to this KThread
static KThread firstProcess = null // this is the KThread of first userprocess, both of these
are used in executing and exiting the userprocess
The default file descriptors for stdout and stdin
final int stdout = 0
final int stdin = 1

Maximum length of a string


final int maxStringSize = 256

We will also modify the default constructor of the userprocess


UserProcess() {
//Initialize the file descriptor table
fileDescriptorTable = new OpenFile[maxFiles];
fileDescriptorTable[stdout] = consoleForReading;
fileDescriptorTable[stdin] = consoleForWriting;
myChildren = new ArrayList<UserProcess>;
disable interrupts;
pid = nextPid;
nextPid++;
restore interrupts;
exitStatus = 0;
exitedProperly = false;
}

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).

int readVirtualMemory(int vaddr, byte[] data, int offset,


int length) {
if (length == 0)
return 0;

assertTruecheck(offset >= 0 && length >= 0 && offset+length <= data.length);


//asserttrue check will just check that the arguments passed are correct
byte[] memory = processor.getMemory; // this method in processor class will return
the physical memory array
vpn = Processor.pageFromAddress(vaddr); // this will return the virtual page no of
this virtual address(vaddr) passed
// Invalid memory access
if (pageTable[vpn] == null)
return 0;
paddr = vaddressToPaddress(vaddr); // this will translate virtual address and give us
the physical address
assertTruecheck(paddr >= 0);

amount = Math.min(length, pageSize);


arraycopy(memory, paddr, data, offset, amount);
amount += readVirtualMemory(vaddr+amount, data, amount+offset, length-amount);
return amount;
}

int writeVirtualMemory(int vaddr, byte[] data, int offset,


int length) {
if (length == 0)
return 0;

assertTruecheck(offset >= 0 && length >= 0 && offset+length <= data.length);

byte[] memory = processor.getMemory;

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);

amount += writeVirtualMemory(vaddr+amount, data, amount+offset, length-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);

}
}

//load arguments into memory


args_vpn = Processor.pageFromAddress((numPages-1)*pageSize);
ppn = UserKernel.getFreePage();
if (ppn < 0) {
return false;
}
// now fill the pagle table with this translation entry
pageTable[args_vpn] = new TranslationEntry(
args_vpn, ppn,
true, false, false, false);

// loads stack into memory


for (int i = 0; i < stackPages; i++) {
stack_vpn = Processor.pageFromAddress(initialSP)-i-1;
ppn = UserKernel.getFreePage();
if (ppn < 0) {
return false;
}
// now fill the pagle table with this translation entry
pageTable[stack_vpn] = new TranslationEntry(stack_vpn, ppn,true, false,
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;
}

for (int i=0;i<pageTable.length;i++)


{
if (pageTable[i] != null) {
UserKernel.freePage(pageTable[i].ppn);
}
}

Das könnte Ihnen auch gefallen