1. 程式人生 > >nachos 記憶體管理和多程序實現

nachos 記憶體管理和多程序實現

1.首先利用點陣圖來管理記憶體
mBitmap = new BitMap(NumPhysPages);


2.建立使用者空間時,首先從點陣圖中找到一個空閒頁,如果沒有空間為,置為0,並標識valid = FALSE
  
 pageTable = new TranslationEntry[numPages];
    for (i = 0; i < numPages; i++) {
pageTable[i].virtualPage = i;// for now, virtual page # = phys page #
int ppn = machine->mBitmap->Find();//查詢一個空閒塊
if(ppn != -1) //如果有空閒塊
{
pageTable[i].physicalPage = ppn;
pageTable[i].valid = TRUE;
}
else //如果沒有空閒塊,無法將該頁放入記憶體
{
pageTable[i].physicalPage = 0;
pageTable[i].valid = FALSE;
}
pageTable[i].use = FALSE;
pageTable[i].dirty = FALSE;
pageTable[i].readOnly = FALSE;  // if the code segment was entirely on 
// a separate page, we could set its 
// pages to be read-only
    }



3.修改讀入記憶體的程式碼,每次只讀一個位元組
 
   if (noffH.code.size > 0) {
        DEBUG('a', "Initializing code segment, at 0x%x, size %d\n", 
noffH.code.virtualAddr, noffH.code.size);
for(int i = 0;i<noffH.code.size;i++)
{
vAddr = noffH.code.virtualAddr + i;
vpn = vAddr/PageSize;//虛擬頁號
offset = vAddr%PageSize;//偏移量
if(pageTable[vpn].valid != FALSE)
{ 
ppn = pageTable[vpn].physicalPage;      //物理塊號
pAddr = ppn*PageSize + offset;      //實體地址
executable->ReadAt(&(machine->mainMemeory[pAddr],1,noffH.code.inFileAddr+i));
}
}

    }
    if (noffH.initData.size > 0) {

for(int i = 0;i<noffH.initData.size;i++)
{
vAddr = noffH.initData.virtualAddr + i;
vpn = vAddr/PageSize;//虛擬頁號
offset = vAddr%PageSize;//偏移量
if(pageTable[vpn].valid != FALSE)
{ 
ppn = pageTable[vpn].physicalPage;//物理塊號
pAddr = ppn*PageSize + offset;//實體地址
executable->ReadAt(&(machine->mainMemeory[pAddr],1,noffH.initData.inFileAddr+i));
}
}
    }