實(shí)驗(yàn)二 主存儲(chǔ)器空間的分配和回收_第1頁
實(shí)驗(yàn)二 主存儲(chǔ)器空間的分配和回收_第2頁
實(shí)驗(yàn)二 主存儲(chǔ)器空間的分配和回收_第3頁
實(shí)驗(yàn)二 主存儲(chǔ)器空間的分配和回收_第4頁
實(shí)驗(yàn)二 主存儲(chǔ)器空間的分配和回收_第5頁
已閱讀5頁,還剩2頁未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

1、實(shí)驗(yàn)二 主存儲(chǔ)器空間的分配和回收1、 實(shí)驗(yàn)內(nèi)容主存儲(chǔ)器空間的分配和回收。2、 實(shí)驗(yàn)?zāi)康挠?jì)算機(jī)系統(tǒng)不僅要有足夠容量、存儲(chǔ)速度高、穩(wěn)定可靠的主存儲(chǔ)器,而且要能合理的分配和使用者且存儲(chǔ)空間。主存的分配和回收的實(shí)現(xiàn)是與主存儲(chǔ)器的管理方式有關(guān)的。本實(shí)驗(yàn)有助于了解在不同的存儲(chǔ)管理方式下,應(yīng)怎樣實(shí)現(xiàn)主存空間的分配和回收。3、 實(shí)驗(yàn)題目在可變分區(qū)管理方式下,采用最先適應(yīng)算法實(shí)現(xiàn)主存空間的分配和回收。4、 數(shù)據(jù)結(jié)構(gòu)struct Block /空閑鏈結(jié)構(gòu)體 string name; /作業(yè)名 int address; /分區(qū)首地址 int size; /分區(qū)大小 int state; /分區(qū)轉(zhuǎn)態(tài) struct B

2、lock *next; /前向指針 struct Block *front; /后向指針 ; 構(gòu)造一個(gè)空閑鏈struct Used /已分配分區(qū)結(jié)構(gòu)體 Block *usedArea; Used *next; ;分配分區(qū)結(jié)構(gòu)體void Allocate(string reqName,int reqSize) /分配函數(shù) Block *p=freeHead->front ; Used *r1,*r2;while(p!=NULL) if(reqSize<p->size) /如果請(qǐng)求的分區(qū)的大小小于一個(gè)空閑分區(qū)的大小 Used *temp=new Used; temp->us

3、edArea =p; Block *q=new Block; *q=*p; temp->usedArea ->name =reqName; temp->usedArea ->size =reqSize; temp->usedArea ->front =q; temp->usedArea ->state =1; q->size =q->size -reqSize; q->address =q->address + reqSize; q ->next->front=q; if(q ->front!=NULL)

4、 q ->front->next=q; r1=usedHead; r2=usedHead->next; while(r2!=NULL&&r2->usedArea->address<temp->usedArea->address) r1=r2;r2=r2->next; r1->next=temp; temp->next=r2; break; else if(reqSize=p->size)/如果請(qǐng)求的分區(qū)的大小等于一個(gè)空閑分區(qū)的大小 Used *temp=new Used; temp->usedArea

5、 =p; temp->usedArea ->name =reqName; temp->usedArea ->state =1; p->next->front =p->front ; if(p->front!=NULL) p->front ->next =p->next ; r1=usedHead; r2=usedHead->next; while(r2!=NULL&&r2->usedArea->address<temp->usedArea->address) r1=r2;r2=

6、r2->next; r1->next=temp; temp->next=r2; break; p=p->front;構(gòu)建一個(gè)鏈表,實(shí)現(xiàn)內(nèi)存分配。5、 源程序#include<iostream> #include<string> using namespace std; struct Block /空閑鏈結(jié)構(gòu)體 string name; /作業(yè)名 int address; /分區(qū)首地址 int size; /分區(qū)大小 int state; /分區(qū)轉(zhuǎn)態(tài) struct Block *next; /前向指針 struct Block *front; /后

7、向指針 ; struct Used /已分配分區(qū)結(jié)構(gòu)體 Block *usedArea; Used *next; ; Block *freeHead; / 帶表頭附加節(jié)點(diǎn)的空閑鏈頭指針 Used *usedHead; /帶表頭附加結(jié)點(diǎn)的已分配分區(qū)頭指針 bool InitValue() /初始化函數(shù) cout<<"本程序設(shè)立的操作功能:1-申請(qǐng)資源 2-釋放資源 3-打印信息"<<endl; freeHead=new Block; freeHead->size=0; freeHead->next=NULL; freeHead->st

8、ate=1; usedHead=new Used; Block *p=new Block; p->address=0; usedHead->usedArea=p; usedHead->next=NULL; Block *temp=new Block; cout<<"請(qǐng)先輸入主存大小(k):"<<endl; cin>>temp->size; temp->address=0; temp->state =0; temp->next=freeHead; temp->front=NULL; freeH

9、ead->front=temp; return true; void Display(Block *p,Used *q) /打印信息的函數(shù) cout<<"已分配分區(qū)信息(作業(yè),始址,大小):"<<endl; while(q!=NULL) cout<<q->usedArea->name<<','<<q->usedArea->address<<','<<q->usedArea->size<<endl; q=q-

10、>next; cout<<"空閑鏈分區(qū)信息(始址,大小):"<<endl; while(p!=NULL) cout<<p->address<<',' cout<<p->size<<endl; p=p->front; void Allocate(string reqName,int reqSize) /分配函數(shù) Block *p=freeHead->front ; Used *r1,*r2; while(p!=NULL) if(reqSize<p->

11、;size) /如果請(qǐng)求的分區(qū)的大小小于一個(gè)空閑分區(qū)的大小 Used *temp=new Used; temp->usedArea =p; Block *q=new Block; *q=*p; temp->usedArea ->name =reqName; temp->usedArea ->size =reqSize; temp->usedArea ->front =q; temp->usedArea ->state =1; q->size =q->size -reqSize; q->address =q->addr

12、ess + reqSize; q ->next->front=q; if(q ->front!=NULL) q ->front->next=q; r1=usedHead; r2=usedHead->next; while(r2!=NULL&&r2->usedArea->address<temp->usedArea->address) r1=r2;r2=r2->next; r1->next=temp; temp->next=r2; break; else if(reqSize=p->size

13、)/如果請(qǐng)求的分區(qū)的大小等于一個(gè)空閑分區(qū)的大小 Used *temp=new Used; temp->usedArea =p; temp->usedArea ->name =reqName; temp->usedArea ->state =1; p->next->front =p->front ; if(p->front!=NULL) p->front ->next =p->next ; r1=usedHead; r2=usedHead->next; while(r2!=NULL&&r2->us

14、edArea->address<temp->usedArea->address) r1=r2;r2=r2->next; r1->next=temp; temp->next=r2; break; p=p->front; if(p=NULL) cout<<"資源不足 請(qǐng)等待"<<endl; void Recycle(string freeName) /回收分區(qū)的函數(shù) Used *p=usedHead->next,*r=usedHead; Block *q; while(p!=NULL) if(p-&g

15、t;usedArea->name=freeName)/找到同名的作業(yè)后,再分四種情況討論 q=p->usedArea; int flag1=1,flag2=1; Block *p1=freeHead->front; Block *pfront,*pnext; while(p1->address<q->address) if(p1->address+p1->size=q->address)flag1=0;pnext=p1; p1=p1->front; if(q->address+q->size=p1->address)

16、 flag2=0;pfront=p1; if(flag1=0) if(flag2=0) pnext->front =pfront->front; pnext->size=pnext->size + q->size + pfront->size; if(pfront->front!=NULL) pfront ->front ->next=pnext; r->next =p->next; else pnext ->size +=q->size;r->next =p->next ; else if(flag2

17、=0) pfront ->address -=q->size; pfront ->size +=q->size ; r->next =p->next ; else Block *temp=freeHead; while(temp->address <=q->address )temp=temp->front; q->front =temp; q->next =temp->next ; q->next->front =q; temp->next =q; q->state =0; r->next =p->next ; break; r=p; p=p->next ; int main() InitValue(); int operate;/操作符1-申請(qǐng)資源 2-釋放資源 3-打印信息 string name; int size; while(1) cout<<"請(qǐng)選擇操作功能"<<endl; cin>>operate; if(operate=1) cout<<&

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論