




版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領
文檔簡介
1、精選優(yōu)質文檔-傾情為你奉上學號E 專業(yè)計算機科學與技術 姓名施飛宇實驗日期2018/11/05 教師簽字 成績實驗報告【實驗名稱】 實驗四 主存空間的分配與回收(一)【實驗目的】理解在連續(xù)分區(qū)動態(tài)的存儲管理方式下,如何實現(xiàn)主存空間的分配與回收?!緦嶒炘怼?. 首次適應算法實現(xiàn)主存空間的分配與回收;2. 循環(huán)首次適應算法實現(xiàn)主存空間的分配與回收;【實驗內(nèi)容】1. 采用可變式分區(qū)管理,使用首次適應算法實現(xiàn)主存空間的分配與回收數(shù)據(jù)結構:typedef struct pcbchar pnameN;/進程名|分區(qū)號 int begin;/起始地址 int end;/結束地址 int length;/進
2、程長度|分區(qū)大小char state;/進程狀態(tài)|分區(qū)狀態(tài) struct pcb *next;/鏈表指針PCB;分區(qū)和進程同樣的數(shù)據(jù)結構,其內(nèi)容用鏈表進行存儲。state=y表示已分配內(nèi)存,state=n表示未進行內(nèi)存分配。算法流程圖:實驗代碼:/*2018/11/15施飛宇 篤行南樓a202*/#define N 20#include <iostream>#include <stdlib.h>#include <stdio.h>using namespace std;typedef struct pcbchar pnameN;/進程名|分區(qū)號 int be
3、gin;/起始地址 int end;/結束地址 int length;/長度|分區(qū)大小char state;/進程狀態(tài)|分區(qū)狀態(tài) struct pcb *next;/鏈表指針PCB;PCB headinput;/進程鏈表PCB headarea;/分區(qū)鏈表char number='a'void input_process();/建立進程函數(shù)void create_area();/創(chuàng)建分區(qū)大小void assign(PCB *Q,PCB *p);/分配內(nèi)存void colect(PCB *Q,PCB *p);/回收內(nèi)存void runprocess();/運行進程函數(shù)void p
4、rintp(PCB *p);/輸出進程函數(shù)void printa(PCB *Q);/輸出分區(qū)函數(shù)void input_process() cout<<"*創(chuàng)建進程*"<<endl;PCB *p1,*p2;cout<<"輸入進程數(shù)目"<<endl;int num;cin>>num;int i=0;p1=&headinput;p2=p1;for(i=0;i<num;i+)cout<<"輸入第"<<i+1<<"進程名,進程
5、大小"<<endl; scanf("%s",p1->pname);cin>>p1->length;p1->begin=p1->end=0;p1->state='n'/n代表未被分配內(nèi)存p1->next=new PCB;p2=p1;p1=p1->next;delete p1;p1=NULL;p2->next=NULL;void create_area()/創(chuàng)建分區(qū)大小PCB *Q1;cout<<"*創(chuàng)建分區(qū)*"<<endl;Q1=&am
6、p;headarea;cin>>Q1->pname;Q1->begin=0;Q1->length=20;Q1->end=Q1->begin+Q1->length;Q1->state='n'Q1->next=new PCB;Q1=Q1->next;cin>>Q1->pname;Q1->begin=20;Q1->length=30;Q1->end=Q1->begin+Q1->length;Q1->state='n'Q1->next=new P
7、CB; Q1=Q1->next;cin>>Q1->pname;Q1->begin=50;Q1->length=40;Q1->end=Q1->begin+Q1->length;Q1->state='n'Q1->next=new PCB; Q1=Q1->next;cin>>Q1->pname;Q1->begin=90;Q1->length=50;Q1->end=Q1->begin+Q1->length;Q1->state='n'Q1->
8、next=new PCB; Q1=Q1->next; cin>>Q1->pname;Q1->begin=140;Q1->length=60;Q1->end=Q1->begin+Q1->length;Q1->state='n'Q1->next=NULL; Q1=Q1->next;void assign(PCB *Q,PCB *p)/分配內(nèi)存 PCB *Q1=Q; PCB *Q2; while(Q1) if(Q1->length>=p->length&&Q1->state
9、!='y') p->begin=Q1->begin; p->end=p->begin+p->length; p->state='y' Q1->state='y' if(Q1->length!=p->length) Q2=new PCB; Q2->next=Q1->next; Q1->next=Q2; Q2->begin=p->end; Q2->end=Q1->end; Q1->end=Q2->begin; Q2->state='
10、;n' Q2->length=Q2->end-Q2->begin; Q1->length=p->length; Q2->pname0=char(+number); Q2->pname1='0' return; else Q1=Q1->next; cout<<p->pname<<"未被成功分配內(nèi)存"<<endl;void colect(PCB *Q,PCB *p)/回收內(nèi)存 PCB *Q1,*Q2,*Q3; Q1=Q;Q2=Q; while(Q1) if(Q1-&
11、gt;begin=p->begin&&Q1->state='y') Q1->state='n' Q1->end=p->end; Q1->length=Q1->end-Q1->begin; if(Q1->next->state='n'&&Q1->next->pname0!='P') Q3=Q1->next; Q1->next=Q3->next; Q1->end=Q3->end; Q1->leng
12、th=Q1->end-Q1->begin; delete Q3; if(Q1->pname0!='P'&&Q2->state='n') Q2->end=Q1->end; Q2->length=Q2->end-Q2->begin; Q2->next=Q1->next; delete Q1; return; Q2=Q1; Q1=Q1->next; void printp(PCB *p)/輸出進程函數(shù) cout<<"進程名 "<<&quo
13、t;起始地址 "<<"結束地址 "<<"進程大小"<<" "<<"分配狀態(tài)(y|n)"<<endl; while(p) cout<<p->pname<<" "<<p->begin<<"k "<<p->end<<"k "<<p->length<<"k "
14、;<<p->state<<endl; p=p->next; void printa(PCB *Q)/輸出分區(qū)函數(shù) cout<<"分區(qū)名 "<<"起始地址 "<<"結束地址 "<<"分區(qū)大小"<<" "<<"分配狀態(tài)(y|n)"<<endl; while(Q) cout<<Q->pname<<" "<&l
15、t;Q->begin<<"k "<<Q->end<<"k "<<Q->length<<"k "<<Q->state<<endl; Q=Q->next; int main() input_process();printp(&headinput); create_area();printa(&headarea); PCB *p1,*Q1; p1=&headinput; Q1=&headarea;
16、 cout<<"*內(nèi)存分配*"<<endl; while(p1) assign(Q1,p1); printa(&headarea); p1=p1->next; p1=&headinput; cout<<"*內(nèi)存回收*"<<endl; while(p1) Q1=&headarea; colect(Q1,p1); printa(&headarea); p1=p1->next; return 0;測試數(shù)據(jù):進程名 起始地址 結束地址 進程大小 分配狀態(tài)(y|n)A 0k
17、 0k 2k nB 0k 0k 3k nC 0k 0k 4k nD 0k 0k 13k nE 0k 0k 23k nF 0k 0k 45k n分區(qū)名 起始地址 結束地址 分區(qū)大小 分配狀態(tài)(y|n)P0 0k 20k 20k nP1 20k 50k 30k nP2 50k 90k 40k nP3 90k 140k 50k nP4 140k 200k 60k n運行結果:2. 采用可變式分區(qū)管理,使用循環(huán)首次適應算法實現(xiàn)主存空間的分配與回收數(shù)據(jù)結構:typedef struct pcbchar pnameN;/進程名|分區(qū)號 int begin;/起始地址 int end;/結束地址 int l
18、ength;/進程長度|分區(qū)大小char state;/進程狀態(tài)|分區(qū)狀態(tài) struct pcb *next;/鏈表指針PCB;分區(qū)和進程同樣的數(shù)據(jù)結構,其內(nèi)容用鏈表進行存儲。state=y表示已分配內(nèi)存,state=n表示未進行內(nèi)存分配。算法流程圖:程序代碼:/*2018/11/15施飛宇 篤行南樓a202*/#define N 20#include <iostream>#include <stdlib.h>#include <stdio.h>using namespace std;typedef struct pcbchar pnameN;/進程名|分區(qū)
19、號 int begin;/起始地址 int end;/結束地址 int length;/長度|分區(qū)大小char state;/進程狀態(tài)|分區(qū)狀態(tài) struct pcb *next;/鏈表指針PCB;PCB headinput;/進程鏈表PCB headarea;/分區(qū)鏈表char number='a'void input_process();/建立進程函數(shù)void create_area();/創(chuàng)建分區(qū)大小PCB * assign(PCB *Q,PCB *p);/分配內(nèi)存void colect(PCB *Q,PCB *p);/回收內(nèi)存void runprocess();/運行進
20、程函數(shù)void printp(PCB *p);/輸出進程函數(shù)void printa(PCB *Q);/輸出分區(qū)函數(shù)void input_process() cout<<"*創(chuàng)建進程*"<<endl;PCB *p1,*p2;cout<<"輸入進程數(shù)目"<<endl;int num;cin>>num;int i=0;p1=&headinput;p2=p1;for(i=0;i<num;i+)cout<<"輸入第"<<i+1<<&qu
21、ot;進程名,進程大小"<<endl; scanf("%s",p1->pname);cin>>p1->length;p1->begin=p1->end=0;p1->state='n'/n代表未被分配內(nèi)存p1->next=new PCB;p2=p1;p1=p1->next;delete p1;p1=NULL;p2->next=NULL;void create_area()/創(chuàng)建分區(qū)大小PCB *Q1;cout<<"*創(chuàng)建分區(qū)*"<<en
22、dl;Q1=&headarea;cin>>Q1->pname;Q1->begin=0;Q1->length=20;Q1->end=Q1->begin+Q1->length;Q1->state='n'Q1->next=new PCB;Q1=Q1->next;cin>>Q1->pname;Q1->begin=20;Q1->length=30;Q1->end=Q1->begin+Q1->length;Q1->state='n'Q1->n
23、ext=new PCB; Q1=Q1->next;cin>>Q1->pname;Q1->begin=50;Q1->length=40;Q1->end=Q1->begin+Q1->length;Q1->state='n'Q1->next=new PCB; Q1=Q1->next;cin>>Q1->pname;Q1->begin=90;Q1->length=50;Q1->end=Q1->begin+Q1->length;Q1->state='n
24、9;Q1->next=new PCB; Q1=Q1->next; cin>>Q1->pname;Q1->begin=140;Q1->length=60;Q1->end=Q1->begin+Q1->length;Q1->state='n'Q1->next=NULL; Q1=Q1->next;PCB* assign(PCB *Q,PCB *p)/分配內(nèi)存 PCB *Q1=Q; PCB *Q2; while(Q1) if(Q1->length>=p->length&&Q1-
25、>state!='y') p->begin=Q1->begin; p->end=p->begin+p->length; p->state='y' Q1->state='y' if(Q1->length!=p->length) Q2=new PCB; Q2->next=Q1->next; Q1->next=Q2; Q2->begin=p->end; Q2->end=Q1->end; Q1->end=Q2->begin; Q2->s
26、tate='n' Q2->length=Q2->end-Q2->begin; Q1->length=p->length; Q2->pname0=char(+number); Q2->pname1='0' return Q1; else Q1=Q1->next; cout<<p->pname<<"未被成功分配內(nèi)存"<<endl;void colect(PCB *Q,PCB *p)/回收內(nèi)存 PCB *Q1,*Q2,*Q3; Q1=Q;Q2=Q; while
27、(Q1) if(Q1->begin=p->begin&&Q1->state='y') Q1->state='n' Q1->end=p->end; Q1->length=Q1->end-Q1->begin; if(Q1->next->state='n'&&Q1->next->pname0!='P') Q3=Q1->next; Q1->next=Q3->next; Q1->end=Q3->end;
28、 Q1->length=Q1->end-Q1->begin; delete Q3; if(Q1->pname0!='P'&&Q2->state='n') Q2->end=Q1->end; Q2->length=Q2->end-Q2->begin; Q2->next=Q1->next; delete Q1; return; Q2=Q1; Q1=Q1->next; void printp(PCB *p)/輸出進程函數(shù) cout<<"進程名 "
29、<<"起始地址 "<<"結束地址 "<<"進程大小"<<" "<<"分配狀態(tài)(y|n)"<<endl; while(p) cout<<p->pname<<" "<<p->begin<<"k "<<p->end<<"k "<<p->length<<"k "<<p->state<<endl; p=p->next; void printa(PCB *Q)/輸出分區(qū)函數(shù) cout<<"分區(qū)名 "<<"起始地址 "<<"結束地址 "<<"分區(qū)大小"<<&
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 工廠叉車安全培訓
- 手術患者轉交接護理
- 同音詞辨析課件
- 骨盆骨折的護理及宣教
- 醫(yī)學裝備管理制度
- 計算機基礎知識培訓課件
- 媽媽家庭理發(fā)操作指南
- 眼部護理營銷策劃
- 小學科學實驗室安全培訓
- 中學改擴建工程初步設計
- 主題:現(xiàn)代戰(zhàn)爭與軍事科技課件
- 上海華東師范大學附屬東昌中學南校重點中學初一語文自主招生試卷模擬試題(5套帶答案)
- 化工企業(yè)適用-法律法規(guī)文件清單
- 貴州省黔東南苗族侗族自治州2022-2023學年七年級下學期期末生物試題(含答案)
- 工業(yè)催化原理及應用
- 新修訂《土地管理法》考試題庫及答案
- 小老虎過生日
- 2023-2024學年廣西壯族自治區(qū)南寧市小學語文六年級期末深度自測試卷詳細參考答案解析
- 國開2023春《語言學概論》形考任務1-3+大作業(yè)參考答案
- 國開《學前兒童語言教育活動指導》形考1-4試題及答案
- ???023綜合安防工程師認證試題答案HCA
評論
0/150
提交評論