




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、實驗報告學院(系)名稱:計算機與通信工程學院姓名張君卓學號20135612專業計算機科學與技術班級2013級1班實驗項目實驗二:存儲器的分配與回收算法實現課程名稱操作系統課程代碼0668036實驗時間2015 年11月13日 第5、6節2011 年12月1 日 第3、4節2011 年12月5 日 第7、8節實驗地點軟件實驗室7-216軟件實驗室7-219軟件實驗室7-215批改意見成績教師簽字: 實驗內容:1. 模擬操作系統的主存分配,運用可變分區的存儲管理算法設計主存分配和回收程序,并不實際啟動裝入作業。2. 采用最先適應法、最佳適應法、最壞適應法分配主存空間。3. 當一個新作業要求裝入主存
2、時,必須查空閑區表,從中找出一個足夠大的空閑區。若找到的空閑區大于作業需要量,這是應把它分成二部分,一部分為占用區,加一部分又成為一個空閑區。4. 當一個作業撤離時,歸還的區域如果與其他空閑區相鄰,則應合并成一個較大的空閑區,登在空閑區表中。5. 運行所設計的程序,輸出有關數據結構表項的變化和內存的當前狀態。實驗要求:1 詳細描述實驗設計思想、程序結構及各模塊設計思路;2 詳細描述程序所用數據結構及算法;3 明確給出測試用例和實驗結果;4 為增加程序可讀性,在程序中進行適當注釋說明;5 認真進行實驗總結,包括:設計中遇到的問題、解決方法與收獲等;6 實驗報告撰寫要求結構清晰、描述準確邏輯性強;
3、7 實驗過程中,同學之間可以進行討論互相提高,但絕對禁止抄襲。【實驗過程記錄(源程序、測試用例、測試結果及心得體會等)】#include <iostream>#include <malloc.h>#include <stdlib.h>using namespace std;typedef struct FreeLink/定義自由鏈struct FreeLink *prior;char name;int start;int size;bool flag;struct FreeLink *next;* ptr,*head;head top;ptr p;void
4、print()/將內存分配情況打印到屏幕上p=top;cout<<"*內存分配情況表*"<<endl;cout<<"區號tt"<<"起始位置t"<<"區間長度t"<<"區間狀態t"<<endl;docout<<p->name<<"tt"<<p->start<<"tt"<<p->size<&l
5、t;"tt"if(p->flag=false)cout<<"空閑"<<endl;elsecout<<"已占用"<<endl;p=p->next;while(p!=NULL);void clear()/結束操作時清空“內存”以備其他操作dop=top;top=top->next;free(p);while(top!=NULL);void asc(ptr &p)/最佳適應法的內存分配函數int min;ptr op;FreeLink *fl=(FreeLink *)
6、malloc(sizeof(FreeLink);cout<<"請輸入要分配內存的進程名"<<endl;cin>>fl->name;cout<<"請輸入要分配內存的大小"<<endl;cin>>fl->size;min=256;fl->flag=true;doif(p->flag=false&&p->size<=min&&p->size>=fl->size)min=p->size;op=p;p=
7、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; goto flag1;if(op->size=fl->size) op->flag=fl-
8、>flag; op->name=fl->name; free(fl); goto flag1;cout<<"內存過小,分配失敗!"<<endl;goto flag2;flag1: cout<<"分配成功!"<<endl;flag2: ;void dec(ptr &p)/最壞適應法的內存分配函數int max;ptr op;FreeLink *fl=(FreeLink *)malloc(sizeof(FreeLink);cout<<"請輸入要分配內存的進程名&q
9、uot;<<endl;cin>>fl->name;cout<<"請輸入要分配內存的大小"<<endl;cin>>fl->size;max=fl->size;fl->flag=true;doif(p->flag=false&&p->size>=max)max=p->size;op=p;p=p->next;while(p!=NULL);if(op->size>fl->size) fl->start=op->start;
10、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; goto flag3;if(op->size=fl->size) op->flag=fl->flag; op->name=fl->name; free(fl); goto flag3;cout<<"內存過小,分配失敗!
11、"<<endl;goto flag4;flag3: cout<<"分配成功!"<<endl;flag4: ;void splice(ptr &p)/若被操作的內存有相鄰空閑區則將空閑區拼接合并int x;if(p->prior->flag=false&&p->next->flag=false)x=1;if(p->prior->flag=false&&p->next->flag=true)|(p->prior->flag=false
12、&&p->next=NULL)x=2;if(p->prior->flag=true&&p->next->flag=false)|(p->prior=NULL&&p->next->flag=false)x=3;if(p->prior->flag=true&&p->next->flag=true)|(p->prior=NULL&&p->next->flag=true)|(p->prior->flag=true&
13、&p->next=NULL)x=4; switch(x)case 1: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->p
14、rior; free(p->next); free(p); break; case 2: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;case 3:if(p->prior=NULL) top=p->next; p->next->
15、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;case 4:p->name='' p
16、->flag=false; break; void allocate(ptr &p)/最先適應法的內存分配函數FreeLink *fl=(FreeLink *)malloc(sizeof(FreeLink);cout<<"請輸入要分配內存的進程名"<<endl;cin>>fl->name;cout<<"請輸入要分配內存的大小"<<endl;cin>>fl->size;fl->flag=true;doif(p->flag=false&&am
17、p;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; goto a;if(p->flag=false&&p->size=fl->size) p->flag=fl->flag; p->
18、name=fl->name; free(fl); goto a;p=p->next;while(p!=NULL);cout<<"內存過小,分配失敗!"<<endl;goto b;a: cout<<"分配成功!"<<endl;b: ; void recover(ptr &p)/內存回收函數char n = ' 'cout<<"請輸入要回收的內存對應的進程名"cin>>n;do if(p->flag=true&&
19、;p->name=n)splice(p); goto c; p=p->next;while(p!=NULL);cout<<"內存并未分配給對應進程,回收失敗!"<<endl;goto d;c: cout<<"內存回收成功!"<<endl;d: ;int ffa()/最先適應法char choice=' 'print();ptr pcb=(FreeLink *)malloc(sizeof(FreeLink);pcb->next=top;pcb->prior=top-&g
20、t;prior; top->prior=pcb;pcb->start=top->start;cout<<"請輸入要為系統分配的內存塊名"<<endl;cin>>pcb->name;cout<<"請輸入要分配內存的大小"<<endl; goto f;e:cout<<"超過內存最大容量請重新輸入要分配內存的大小"<<endl;f:cin>>pcb->size;if(pcb->size>256) goto
21、 e;top->size=top->size-pcb->size;top=pcb;top->flag=true;top->next->start+=top->size;print();while(true)dop=top->next;cout<<"請從下列選項中進行選擇"<<endl;cout<<"1.分配內存"<<endl;cout<<"2.回收內存"<<endl;cout<<"3.結束操作&
22、quot;<<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;case '3':clear();return 0;break;int bfa()
23、/最佳適應法char choice=' 'print();ptr pcb=(FreeLink *)malloc(sizeof(FreeLink);pcb->next=top;pcb->prior=top->prior; top->prior=pcb;pcb->start=top->start;cout<<"請輸入要為系統分配的內存塊名"<<endl;cin>>pcb->name;cout<<"請輸入要分配內存的大小"<<endl; got
24、o h;g:cout<<"超過內存最大容量請重新輸入要分配內存的大小"<<endl;h:cin>>pcb->size;if(pcb->size>256) goto g;top->size=top->size-pcb->size;top=pcb;top->flag=true;top->next->start+=top->size;print();while(true)dop=top->next;cout<<"請從下列選項中進行選擇"<&l
25、t;endl;cout<<"1.分配內存"<<endl;cout<<"2.回收內存"<<endl;cout<<"3.結束操作"<<endl;cout<<"請輸入你的選擇"cin>>choice;while(choice!='1'&&choice!='2'&&choice!='3');switch(choice)case '1'
26、:asc(p);print();break;case '2':recover(p);print();break;case '3':clear();return 0;break;int wfa()/最壞適應法char choice=' 'print();ptr pcb=(FreeLink *)malloc(sizeof(FreeLink);pcb->next=top;pcb->prior=top->prior; top->prior=pcb;pcb->start=top->start;cout<<&q
27、uot;請輸入要為系統分配的內存塊名"<<endl;cin>>pcb->name;cout<<"請輸入要分配內存的大小"<<endl; goto j;i:cout<<"超過內存最大容量請重新輸入要分配內存的大小"<<endl;j:cin>>pcb->size;if(pcb->size>256) goto i;top->size=top->size-pcb->size;top=pcb;top->flag=true;t
28、op->next->start+=top->size;print();while(true)dop=top->next;cout<<"請從下列選項中進行選擇"<<endl;cout<<"1.分配內存"<<endl;cout<<"2.回收內存"<<endl;cout<<"3.結束操作"<<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;case '3':clear();return 0;break;int main()/主函數cha
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 林業扶持資金管理辦法
- 電廠運行管理標準化操作指南與實踐探索
- 誘變劑增強納米載體穿透-洞察及研究
- 小學班級德育課程化實踐研究
- 檢測公司績效管理辦法
- 園林綠化人員崗位職責
- 智能電網數據傳輸優化:面向未來的設計方案
- 安全生產月活動情況匯報
- 名家語文教學示范
- 根據安全法的規定
- 明渠均勻流計算公式
- 林規發防護林造林工程投資估算指標
- 四年級上冊 口算題 1000題
- 九上道法知識點梳理(全冊)-九年級道德與法治上冊必備知識梳理總結(部編版)
- YB/T 5202.1-2003不定形耐火材料試樣制備方法第1部分:耐火澆注料
- GB/T 700-2006碳素結構鋼
- GB/T 41419-2022數字化試衣虛擬人體用術語和定義
- GB/T 24218.1-2009紡織品非織造布試驗方法第1部分:單位面積質量的測定
- 《病毒學》(研究生)全冊配套完整課件
- 第十七章其他熔化焊接與熱切割作業課件
- 腧穴總論 2特定穴課件
評論
0/150
提交評論