




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
1、實驗二 主存儲器空間的分配和回收1、 實驗內(nèi)容主存儲器空間的分配和回收。2、 實驗?zāi)康挠嬎銠C系統(tǒng)不僅要有足夠容量、存儲速度高、穩(wěn)定可靠的主存儲器,而且要能合理的分配和使用者且存儲空間。主存的分配和回收的實現(xiàn)是與主存儲器的管理方式有關(guān)的。本實驗有助于了解在不同的存儲管理方式下,應(yīng)怎樣實現(xiàn)主存空間的分配和回收。3、 實驗題目在可變分區(qū)管理方式下,采用最先適應(yīng)算法實現(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)造一個空閑鏈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ū)的大小小于一個空閑分區(qū)的大小 Used *temp=new Used; temp->us
3、edArea =p; Block *q=new Block; 1 / 7 *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!
4、=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)/如果請求的分區(qū)的大小等于一個空閑分區(qū)的大小 Used *temp=new Used; temp->us
5、edArea =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=
6、r2;r2=r2->next; r1->next=temp; temp->next=r2; break; p=p->front;構(gòu)建一個鏈表,實現(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 *fro
7、nt; /后向指針 ; struct Used /已分配分區(qū)結(jié)構(gòu)體 Block *usedArea; Used *next; ; Block *freeHead; / 帶表頭附加節(jié)點的空閑鏈頭指針 Used *usedHead; /帶表頭附加結(jié)點的已分配分區(qū)頭指針 bool InitValue() /初始化函數(shù) cout<<"本程序設(shè)立的操作功能:1-申請資源 2-釋放資源 3-打印信息"<<endl; freeHead=new Block; freeHead->size=0; freeHead->next=NULL; freeHead-
8、>state=1; usedHead=new Used; Block *p=new Block; p->address=0; usedHead->usedArea=p; usedHead->next=NULL; Block *temp=new Block; cout<<"請先輸入主存大小(k):"<<endl; cin>>temp->size; temp->address=0; temp->state =0; temp->next=freeHead; temp->front=NULL;
9、 freeHead->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
10、; q=q->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<
11、;p->size) /如果請求的分區(qū)的大小小于一個空閑分區(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-&g
12、t;address + 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-&g
13、t;size)/如果請求的分區(qū)的大小等于一個空閑分區(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-
14、>usedArea->address<temp->usedArea->address) r1=r2;r2=r2->next; r1->next=temp; temp->next=r2; break; p=p->front; if(p=NULL) cout<<"資源不足 請等待"<<endl; void Recycle(string freeName) /回收分區(qū)的函數(shù) Used *p=usedHead->next,*r=usedHead; Block *q; while(p!=NULL) i
15、f(p->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->ad
16、dress) 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(
17、flag2 =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-申請資源 2-釋放資源 3-打印信息 string name; int size; while(1) cout<<"請選擇操作功能"<<endl; cin>>operate; if(operate=1) cout<<&
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 肛瘺患者護(hù)理課件
- 肌肉骨骼疾病職業(yè)病課件
- 高一a10聯(lián)盟數(shù)學(xué)試卷
- 2025至2030柴油車排氣管行業(yè)市場深度研究與戰(zhàn)略咨詢分析報告
- 甘肅省學(xué)業(yè)水平數(shù)學(xué)試卷
- 再生橡膠市場質(zhì)量需求分析考核試卷
- 費縣五年級期末數(shù)學(xué)試卷
- 高2期末數(shù)學(xué)試卷
- 廣東高一上冊數(shù)學(xué)試卷
- 事故案例分析與借鑒應(yīng)急法律法規(guī)遵守考核試卷
- 成人高等教育本科生學(xué)士學(xué)位英語水平考試大綱(非英語專業(yè))
- 四川省綿陽市2024-2025學(xué)年高一數(shù)學(xué)下學(xué)期期末教學(xué)質(zhì)量測試試題
- 2025高考物理步步高同步練習(xí)必修3練透 帶電粒子在電場中的運動
- 2024人形機器人產(chǎn)業(yè)半年研究報告
- 某化纖毛紡廠總配變電所及高壓配電系統(tǒng)設(shè)計
- 北京市海淀區(qū)2023-2024學(xué)年七年級下學(xué)期期末數(shù)學(xué)練習(xí)試題(解析版)
- DB32-T 4790-2024建筑施工特種作業(yè)人員安全操作技能考核標(biāo)準(zhǔn)
- 人教版英語九年級全一冊《教材解讀分析課件》完整版課件
- 北京市房山區(qū)2023-2024學(xué)年七年級下學(xué)期期末數(shù)學(xué)試題(解析版)
- 小學(xué)教育集團(tuán)三年發(fā)展規(guī)劃(2024年-2027年)
- 問題解決型護(hù)理品管圈QCC成果匯報之提高兒科護(hù)士橈動脈采血的穿刺成功率
評論
0/150
提交評論