操作系統(tǒng)實(shí)驗四_第1頁
操作系統(tǒng)實(shí)驗四_第2頁
操作系統(tǒng)實(shí)驗四_第3頁
操作系統(tǒng)實(shí)驗四_第4頁
操作系統(tǒng)實(shí)驗四_第5頁
已閱讀5頁,還剩22頁未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡介

1、精選優(yōu)質(zhì)文檔-傾情為你奉上學(xué)號E 專業(yè)計算機(jī)科學(xué)與技術(shù) 姓名施飛宇實(shí)驗日期2018/11/05 教師簽字 成績實(shí)驗報告【實(shí)驗名稱】 實(shí)驗四 主存空間的分配與回收(一)【實(shí)驗?zāi)康摹坷斫庠谶B續(xù)分區(qū)動態(tài)的存儲管理方式下,如何實(shí)現(xiàn)主存空間的分配與回收?!緦?shí)驗原理】1. 首次適應(yīng)算法實(shí)現(xiàn)主存空間的分配與回收;2. 循環(huán)首次適應(yīng)算法實(shí)現(xiàn)主存空間的分配與回收;【實(shí)驗內(nèi)容】1. 采用可變式分區(qū)管理,使用首次適應(yīng)算法實(shí)現(xiàn)主存空間的分配與回收數(shù)據(jù)結(jié)構(gòu):typedef struct pcbchar pnameN;/進(jìn)程名|分區(qū)號 int begin;/起始地址 int end;/結(jié)束地址 int length;/進(jìn)

2、程長度|分區(qū)大小char state;/進(jìn)程狀態(tài)|分區(qū)狀態(tài) struct pcb *next;/鏈表指針PCB;分區(qū)和進(jìn)程同樣的數(shù)據(jù)結(jié)構(gòu),其內(nèi)容用鏈表進(jìn)行存儲。state=y表示已分配內(nèi)存,state=n表示未進(jìn)行內(nèi)存分配。算法流程圖:實(shí)驗代碼:/*2018/11/15施飛宇 篤行南樓a202*/#define N 20#include <iostream>#include <stdlib.h>#include <stdio.h>using namespace std;typedef struct pcbchar pnameN;/進(jìn)程名|分區(qū)號 int be

3、gin;/起始地址 int end;/結(jié)束地址 int length;/長度|分區(qū)大小char state;/進(jìn)程狀態(tài)|分區(qū)狀態(tài) struct pcb *next;/鏈表指針PCB;PCB headinput;/進(jìn)程鏈表PCB headarea;/分區(qū)鏈表char number='a'void input_process();/建立進(jìn)程函數(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();/運(yùn)行進(jìn)程函數(shù)void p

4、rintp(PCB *p);/輸出進(jìn)程函數(shù)void printa(PCB *Q);/輸出分區(qū)函數(shù)void input_process() cout<<"*創(chuàng)建進(jìn)程*"<<endl;PCB *p1,*p2;cout<<"輸入進(jìn)程數(shù)目"<<endl;int num;cin>>num;int i=0;p1=&headinput;p2=p1;for(i=0;i<num;i+)cout<<"輸入第"<<i+1<<"進(jìn)程名,進(jìn)程

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)/輸出進(jìn)程函數(shù) cout<<"進(jìn)程名 "<<&quo

13、t;起始地址 "<<"結(jié)束地址 "<<"進(jìn)程大小"<<" "<<"分配狀態(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ū)名 "<<"起始地址 "<<"結(jié)束地址 "<<"分區(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ù):進(jìn)程名 起始地址 結(jié)束地址 進(jìn)程大小 分配狀態(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ū)名 起始地址 結(jié)束地址 分區(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運(yùn)行結(jié)果:2. 采用可變式分區(qū)管理,使用循環(huán)首次適應(yīng)算法實(shí)現(xiàn)主存空間的分配與回收數(shù)據(jù)結(jié)構(gòu):typedef struct pcbchar pnameN;/進(jìn)程名|分區(qū)號 int begin;/起始地址 int end;/結(jié)束地址 int l

18、ength;/進(jìn)程長度|分區(qū)大小char state;/進(jìn)程狀態(tài)|分區(qū)狀態(tài) struct pcb *next;/鏈表指針PCB;分區(qū)和進(jìn)程同樣的數(shù)據(jù)結(jié)構(gòu),其內(nèi)容用鏈表進(jìn)行存儲。state=y表示已分配內(nèi)存,state=n表示未進(jì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;/進(jìn)程名|分區(qū)

19、號 int begin;/起始地址 int end;/結(jié)束地址 int length;/長度|分區(qū)大小char state;/進(jìn)程狀態(tài)|分區(qū)狀態(tài) struct pcb *next;/鏈表指針PCB;PCB headinput;/進(jìn)程鏈表PCB headarea;/分區(qū)鏈表char number='a'void input_process();/建立進(jìn)程函數(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();/運(yùn)行進(jìn)

20、程函數(shù)void printp(PCB *p);/輸出進(jìn)程函數(shù)void printa(PCB *Q);/輸出分區(qū)函數(shù)void input_process() cout<<"*創(chuàng)建進(jìn)程*"<<endl;PCB *p1,*p2;cout<<"輸入進(jìn)程數(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;進(jìn)程名,進(jìn)程大小"<<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)/輸出進(jìn)程函數(shù) cout<<"進(jìn)程名 "

29、<<"起始地址 "<<"結(jié)束地址 "<<"進(jìn)程大小"<<" "<<"分配狀態(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ū)名 "<<"起始地址 "<<"結(jié)束地址 "<<"分區(qū)大小"<<&

溫馨提示

  • 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)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論