實驗2存儲器的分配與回收_第1頁
實驗2存儲器的分配與回收_第2頁
實驗2存儲器的分配與回收_第3頁
實驗2存儲器的分配與回收_第4頁
實驗2存儲器的分配與回收_第5頁
已閱讀5頁,還剩11頁未讀, 繼續(xù)免費閱讀

付費下載

下載本文檔

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

文檔簡介

試驗報告

學(xué)院(系)名稱:計算機及通信工程學(xué)院

姓名張君卓學(xué)號20195612專業(yè)計算機科學(xué)及技術(shù)

試驗項

班級2019級1班試驗二:存儲器的安排及回收算法實現(xiàn)

課程代

課程名稱操作系統(tǒng)0668036

2019年11月13日第

5、6節(jié)

軟件試驗室7-216

2019年12月1日第試驗地

試驗時間軟件試驗室7-219

3、4節(jié)點

軟件試驗室7-215

2019年12月5日第

7、8節(jié)

批改看法成果

老師簽字:

試驗內(nèi)容:

1.模擬操作系統(tǒng)的主存安排,運用可變分區(qū)的存儲管理算法設(shè)計主存安排和

回收程序,并不實際啟動裝入作業(yè)。

2.采納最先適應(yīng)法、最佳適應(yīng)法、最壞適應(yīng)法安排主存空間。

3.當一個新作業(yè)要求裝入主存時,必需查空閑區(qū)表,從中找出一個足夠大的

空閑區(qū)。若找到的空閑區(qū)大于作業(yè)須要量,這是應(yīng)把它分成二部分,一部

分為占用區(qū),加一部分又成為一個空閑區(qū)。

4.當一個作業(yè)撤離時,歸還的區(qū)域假如及其他空閑區(qū)相鄰,則應(yīng)合并成一個

較大的空閑區(qū),登在空閑區(qū)表中。

5.運行所設(shè)計的程序,輸出有關(guān)數(shù)據(jù)結(jié)構(gòu)表項的改變和內(nèi)存的當前狀態(tài)。

試驗要求:

1.具體描述試驗設(shè)計思想、程序結(jié)構(gòu)及各模塊設(shè)計思路;

2.具體描述程序所用數(shù)據(jù)結(jié)構(gòu)及算法;

3.明確給出測試用例和試驗結(jié)果;

4.為增加程序可讀性,在程序中進行適當注釋說明;

5.細致進行試驗總結(jié),包括:設(shè)計中遇到的問題、解決方法及收獲等;

6.試驗報告撰寫要求結(jié)構(gòu)清楚、描述精確邏輯性強;

7.試驗過程中,同學(xué)之間可以進行探討相互提高,但肯定禁止抄襲。

【試驗過程記錄(源程序、測試用例、測試結(jié)果及心得體會等)】

ttinclude<iostream>

#include<malloc.h>

ttinclude<stdlib.h>

usingnamespacestd;

typcdefstructFreeLink{〃定義自由鏈

structFreeLink*prior;

charname;

intstart;

intsize;

boolflag;

structFreeLink*next;

}*ptr,*head;

headtop;

ptrp;

voidprint。{〃將內(nèi)存安排狀況打印到屏幕上

P=top;

cout<<〃************************內(nèi)存安排狀況表

wxxG

V、xTsZj*zTszyszjszrszrszrSzr*zjsZr>?endl;

cout<<〃區(qū)號\t\t〃<<〃起始位置\t〃<<〃區(qū)間長度\t〃?!▍^(qū)間狀態(tài)

\t,z<<endl;

do{

cout<<p-〉name<<〃\t\t〃<<p->start<<〃\t'\t〃<〈p->size〈〈〃\t\t〃;

if(p->flag=二false){cout<<〃空閑〃<<endl;}

OP=P;

p=p->next;

while(p!=NULL);

if(op->size>fl->size){

fl->start=op->st£rt;

op->start=fl->start+fl->size;

op->size=op->size-fl->size;

fl->next=op;

fl->prior=op->prior;

op->prior->next=f1;

op->prior=f1;

gotoflagl;

if(op->size==fl->size){

op->flag=fl->flag;

op->name=fl->name;

free(fl);

gotoflagl;

cout<<“內(nèi)存過小,安排失敗!"〈Vendl;gotoflag2;

flagl:cout<<〃安排勝利!"<<endl;

flag2:;

voiddec(ptr&p){〃最壞適應(yīng)法的內(nèi)存安排函數(shù)

intmax;

ptrop;

FreeLink*fl=(FreeLink*)malloc(sizeof(FreeLink));

cout?〃請輸入要安排內(nèi)存的進程名〃Gendl;

cin>>fl->name;

cout?〃請輸入要安排內(nèi)存的大小〃。endl;

cin>>fl->size;

max=fl->size;

fl->flag=true;

do{

if(p->flag==false&&p->size>=max){

max=p->size;

?P=P;

p=p->next;

while(p!=NULL);

if(op->size>fl->size){

fl->start=op->start;

op->start=fl->start+fl->size;

op->size=op->size-fl->size;

fl-〉next=op;

fl->prior=op->prior;

op->prior->next=fl;

op->prior=fl;

gotoflag3;

if(op->size==fl->size){

op->flag=fl->flag;

op->name=fl->name;

free(fl);

gotoflag3;

cout<〈〃內(nèi)存過小,安排失?。 ?lt;<endl;gotoflag4;

flag3:cout<<〃安排勝利!〃<<endl;

flag4:;

voidsplice(ptr&p){//若被操作的內(nèi)存有相鄰空閑區(qū)則將空閑區(qū)拼接合

intx;

if(p->prior->flag==false&&p->next->flag==false)x=l;

if((p->prior->flag==false&&p->next->flag==true)||(p->prior->fl

ag==false&&p->next==NL'LL))x=2;

if((p->prior->flag==true&&p->next->flag==false)||(p->prior==NU

LL&&p->next->flag==false))x=3;

if((p->prior->flag==true&.&-p->next->flag==true)||(p->prior==NUL

L&&p->next->flag==true)||(p->prior->flag==true&&p->next==NULL))x

二4;

switch(x){

case1:p->next->prior=p->prior;

p->prior->next=p->next;

p->prior->size=p->prior->size+p->size+p->next->size;

p->prior->next=p->next->next;

if(p->next->next!=NULL)p->next->next->prior=p->next->prior;

free(p->next);

free(p);

break;

case2:if(p->next==NULL){

p->prior->next=p->next;

}else{

p->next->prior=p->prior;

p->prior->next=p->next;

p->prior->size=p->prior->size+p->size;

free(p);

break;

case3:if(p->prior==NULL){

top=p->noxt;

p->next->prior=NULL;

p->next->start=p->start;

p->next->size=p->next->size+p->size;

}else{

p->next->prior=p->prior;

p->prior->next=p->next;

p->next->start=p->start;

p->next->size=p->next->size+p->size;

free(p);

break;

case4:p->name=,@,;

p->flag=false;

break;

voidallocate(ptr&p){〃最先適應(yīng)法的內(nèi)存安排函數(shù)

FreeLink*fl=(FreeLink*)malloc(sizeof(FreeLink));

cout*〃請輸入要安排內(nèi)存的進程名〃<<endl;

cin>>fl->name;

cout?〃請輸入要安排內(nèi)存的大小〃〈Vendl;

cin>>fl->size;

fl->flag=true;

do{

if(p->flag==false&&p->size>fl->size){

fl->start=p->start;

p->start=fl->start+fl->size;

p->size=p->size-fl->size;

fl->next=p;

fl->prior=p->prior;

p->prior->next=fl;

p->prior=fl;

gotoa;

if(p->flag==false&&p->size==fl->size){

p->flag=fl->flag;

p->name=fl->n£me;

free(fl);

gotoa;

p=p->next;

while(p!=NULL);

cout<<”內(nèi)存過小,安排失敗!〃<<endl;gotob;

a:cout<<〃安排勝利!〃<<endl;

b:;

voidrecover(ptr&p){〃內(nèi)存回收函數(shù)

charn二'';

cout?〃請輸入要回收的內(nèi)存對應(yīng)的進程名〃;

cin>>n;

do{

if(p->flag==true&&p->name==n){

splice(p);

gotoc;

p=p->next;

while(p!=NULL);

cout<<〃內(nèi)存并未安排給對應(yīng)進程,回收失敗!〃<<endl;gotod;

c:cout<<“內(nèi)存回收勝利!〃<<endl;

d:;

intffa(){〃最先適應(yīng)法

charchoice='';

print();

ptrpcb=(FreeLink*)malloc(sizeof(FreeLink));

pcb->next=top;

pcb->prior=top->prior;

top->prior=pcb;

pcb->start=top->start;

cout。〃請輸入要為系統(tǒng)安排的內(nèi)存塊名〃*endl;

cin>>pcb->name;

cout<〈〃請輸入要安排內(nèi)存的大小〃<<endl;gotof;

e:cout?〃超過內(nèi)存最大容量請重新輸入要安排內(nèi)存的大小〃<<endl;

f:cin>>pcb->size;

if(pcb->size>256)gotoe;

top->size=top->size-pcb->size;

top=pcb;

top->flag=true;

top->next->start+=top->size;

print();

while(true){

do{

p=top->next;

cout<<〃請從下列選項中進行選擇〃0endl;

cout<<,,l.安排內(nèi)存〃<<endl;

cout<<z,2.回收內(nèi)存〃<<endl;

cout?,z3.結(jié)束操作〃<<endl;

cout?〃請輸入你的選擇〃;

cin>>choice;

while(choice!='1'&&choice!=,2'&&choice!=,3');

switch(choice){

case'1':allocate(p);print();break;

case'2':recover(p);print();break;

caseJ:clear();return0;break;

intbfa(){〃最佳適應(yīng)法

charchoice='’;

print();

ptrpcb=(FreeLink*)malloc(sizeof(I?reeLink));

pcb->next=top;

pcb->prior=top->prior;

top->prior=pcb;

pcb->start=top->start;

cout?〃請輸入要為系統(tǒng)安排的內(nèi)存塊名〃*endl;

cin>>pcb->name;

cout<<〃請輸入要安排內(nèi)存的大小"<<endl;gotoh;

g:cout?〃超過內(nèi)存最大容量請重新輸入要安排內(nèi)存的大小〃《endl;

h:cin>>pcb->size;

if(pcb->size>256)gotog;

top->size=top->size-pcb->size;

top=pcb;

top->flag=true;

top->next->start+=top->size;

print();

while(true){

do{

p=top->next;

cout<〈〃請從下列選項中進行選擇〃<<endl;

cout<<,,l.安排內(nèi)存"<<endl;

cout?z,2.回收內(nèi)存“<<endl;

cout<<z,3.結(jié)束操作“<<endl;

cout<<〃請輸入你的選擇〃;

cin>>choice;

while(choice!=,V&&choice!=,2'&&choice!=,3');

switch(choice){

case'r:asc(p);print();break;

case'2':recover(p);print();break;

case'3':clear();return0;break;

intwfa(){〃最壞適應(yīng)法

charchoice='';

print();

ptrpcb=(FreeLink*)malloc(sizeof(FreeLink));

pcb->next=top;

pcb->prior=top->prior;

top->prior=pcb;

pcb->start=top->start;

cout?〃請輸入要為系統(tǒng)安排的內(nèi)存塊名〃*endl;

cin>>pcb->name;

cout<<”請輸入要安排內(nèi)存的大小〃<<endl;gotoj;

i:cout?〃超過內(nèi)存最大容量請重新輸入要安排內(nèi)存的大小〃<<endl;

j:cin>>pcb->size;

if(pcb->size>256)gotoi;

top->size=top->size-pcb->size;

top=pcb;

top->flag=true;

top->next->start+=top->size;

print();

while(true){

do{

p=top->next;

cout<<〃請從下列選項中進行選擇〃0endl;

cout<<,,l.安排內(nèi)存〃<<endl;

cout<<z,2.回收內(nèi)存〃<<endl;

cout?,z3.結(jié)束操作〃<<endl;

cout?〃請輸入你的選擇〃;

cin>>choice;

while(choice!=,1'&&choice!=,2'&&choice!=,3,);

switch(choice){

case'1':dec(p);print();break;

case'2':recover(p);print();break;

caseJ:clear();return0;break;

intmain。{〃主函數(shù)

charch

溫馨提示

  • 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)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論