39,叶慧敏 实验四 主存空间的分配和回收模拟
一、 實(shí)驗(yàn)?zāi)康?/strong>
為了合理地分配和使用這些存儲空間,當(dāng)用戶提出申請主存儲器空間時,存儲管理必須根據(jù)申請者的要求,按一定的策略分析主存空間和使用情況,找出足夠 的空閑區(qū)域給申請者。當(dāng)作業(yè)撤離歸還主存資源時,則存儲管理要收回占用的主存空間。主存的分配和回收的實(shí)現(xiàn)是與主存儲器的管理方式有關(guān)的,通過本實(shí)驗(yàn)幫助 我們理解在不同的存儲管理方式下應(yīng)怎樣實(shí)現(xiàn)主存空間的分配和回收。
用高級語言完成一個主存空間的分配和回收模擬程序,以加深對內(nèi)存分配方式及其算法的理解。
?
二、實(shí)驗(yàn)內(nèi)容和要求
2.1? 模擬包括3部分:
1)實(shí)現(xiàn)特定的內(nèi)存分配算法
2)實(shí)現(xiàn)內(nèi)存回收模擬
3)每種內(nèi)存分配策略對應(yīng)的碎片數(shù)統(tǒng)計(jì)
?
2.2? 固定分區(qū)存儲管理
假設(shè)內(nèi)存容量為120KB,并且分別劃分成8,16,32,64KB大小的塊各一塊。
一個進(jìn)程所需要的內(nèi)存為0到100個KB。同時假設(shè)一個進(jìn)程在運(yùn)行過程中所需內(nèi)存的大小不變。
模擬五個進(jìn)程到達(dá)請求分配與運(yùn)行完回收情況,輸出主存分配表.
?
2.3? 動態(tài)分區(qū)分配存儲管理
采用連續(xù)分配方式之動態(tài)分區(qū)分配存儲管理,使用首次適應(yīng)算法、下次適應(yīng)算法、最佳適應(yīng)算法和最壞適應(yīng)算法4種算法完成設(shè)計(jì)(任選兩種算法)。
(1)在程序運(yùn)行過程,由用戶指定申請與釋放。
(2)設(shè)計(jì)一個已占用分區(qū)表,以保存某時刻主存空間占用情況。
(3)設(shè)計(jì)一個空閑分區(qū)表,以保存某時刻主存空間剩余情況。
(4)用兩個表的變化情況,反應(yīng)各進(jìn)程所需內(nèi)存的申請與釋放情況。
?
三、實(shí)驗(yàn)方法、步驟及結(jié)果測試
3.1 源程序名:1230.c
????? 可執(zhí)行程序名:1230.exe
3.2 原理分析及流程圖
3.3 主要程序段及其解釋:
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 const int CANUSE = 1; 5 const int CANTUSE = 0; 6 //#define MSIZE 128; 7 const int MSIZE = 128; 8 9 10 //內(nèi)存分區(qū) 11 struct MZone 12 { 13 //空閑區(qū)起始地址 14 int begin_addr; 15 //一個連續(xù)空閑區(qū)的長度 16 int length; 17 //狀態(tài) 18 int state; 19 //內(nèi)存中任務(wù)名 20 char task_name[32]; 21 //指向下一個空閑分區(qū) 22 struct MZone *next; 23 }; 24 25 //內(nèi)存頭指針 26 struct MZone *Mhead = NULL; 27 28 //showmemory函數(shù),顯示當(dāng)前內(nèi)存分配情況 29 void showmemory() 30 { 31 struct MZone *Mpoint = Mhead; 32 33 printf("內(nèi)存的使用情況\n"); 34 printf("beginaddr\tlength\tstate\ttask\n"); 35 36 while( NULL!=Mpoint) 37 { 38 printf("%dk\t\t",Mpoint->begin_addr); 39 printf("%dk\t",Mpoint->length); 40 Mpoint->state?printf("CANUSE\t"):printf("CANTUSE\t"); 41 printf("%s\n",Mpoint->task_name); 42 Mpoint = Mpoint->next; 43 } 44 45 system("pause"); 46 47 } 48 49 //memoallocate函數(shù),用于分配內(nèi)存 50 void memoallocate(void) 51 { 52 struct MZone *Mnew = (struct MZone*)malloc(sizeof(struct MZone)); 53 printf("輸入要分配內(nèi)存大小(kb):\n"); 54 scanf("%d",&Mnew->length); 55 printf("輸入任務(wù)名:\n"); 56 scanf("%s",&Mnew->task_name); 57 Minsert(Mnew)?printf("分配內(nèi)存成功\n"):printf("沒有符合大小的空閑分區(qū),內(nèi)存分配失敗。\n"); 58 system("pause"); 59 free(Mnew); 60 } 61 62 //Minsert函數(shù),功能插入任務(wù)到空閑分區(qū) 63 int Minsert(struct MZone* Mnew) 64 { 65 66 struct MZone *Zinsert = Mhead; 67 //flag用于指示是Zinsert到了NULL,既沒有內(nèi)存可以分配 68 int flag = 1; 69 70 while( Zinsert->length<Mnew->length || !Zinsert->state) 71 { 72 if( NULL!=Zinsert->next ) 73 { 74 Zinsert = Zinsert->next; 75 } 76 else 77 { 78 Zinsert = Zinsert->next; 79 break; 80 } 81 82 } 83 84 if( NULL==Zinsert ) 85 { 86 return 0; 87 } 88 89 if( MSIZE == Zinsert->begin_addr+Mnew->length ) 90 { 91 Zinsert->state = CANTUSE; 92 strcpy(Zinsert->task_name , Mnew->task_name); 93 Zinsert->next = NULL; 94 return 1; 95 } 96 else 97 { 98 struct MZone *Ztail = (struct MZone *)malloc(sizeof(struct MZone)); 99 Zinsert->state = CANTUSE; 100 strcpy(Zinsert->task_name , Mnew->task_name); 101 Zinsert->length = Mnew->length; 102 Zinsert->next = Ztail; 103 104 memset( Ztail, 0, sizeof(char)*32 ); 105 Ztail->begin_addr = Zinsert->begin_addr + Mnew->length; 106 Ztail->state = CANUSE; 107 Ztail->length = MSIZE - Ztail->begin_addr; 108 Ztail->next = NULL; 109 110 return 1; 111 } 112 } 113 114 //memoreturn函數(shù),用于回收內(nèi)存 115 void memoreturn(void) 116 { 117 char tname[32]; 118 printf("輸入要收回的任務(wù)名\n"); 119 scanf("%s",tname); 120 Mreturn(tname); 121 system("pause"); 122 } 123 124 //Mreturn函數(shù),功能回收內(nèi)存 125 int Mreturn(char taskname[]) 126 { 127 struct MZone *front = NULL; 128 struct MZone *position = Mhead; 129 struct MZone *tail = Mhead->next; 130 131 while( 0!=strcmp(position->task_name,taskname) ) 132 { 133 front = position; 134 if( NULL!=position->next ) 135 { 136 position = position->next; 137 } 138 else 139 { 140 position = NULL; 141 break; 142 } 143 tail = position->next; 144 } 145 146 if( NULL==position ) 147 { 148 printf("內(nèi)存中沒有此任務(wù)!"); 149 } 150 else 151 { 152 //不能用CANTUSE 153 if( NULL!=tail&&NULL!=front ) 154 { 155 156 if( front->state&&tail->state ) 157 { 158 front->length = front->length + position->length + tail->length; 159 front->next = tail->next; 160 free(position); 161 free(tail); 162 } 163 else if( front->state&&!tail->state ) 164 { 165 front->length = front->length + position->length; 166 front->next = position->next; 167 free(position); 168 } 169 else if( !front->state&&tail->state ) 170 { 171 position->length = position->length + tail->length; 172 memset( position->task_name, 0, sizeof(char)*32 ); 173 position->next = tail->next; 174 position->state = CANUSE; 175 free(tail); 176 } 177 else if( !front->state&&!tail->state ) 178 { 179 memset( position->task_name, 0, sizeof(char)*32 ); 180 position->state = CANUSE; 181 } 182 } 183 else if( NULL!=tail&&NULL==front ) 184 { 185 if( !tail->state ) 186 { 187 memset( position->task_name, 0, sizeof(char)*32 ); 188 position->state = CANUSE; 189 } 190 else 191 { 192 position->length = position->length + tail->length; 193 position->next = NULL; 194 free(tail); 195 } 196 } 197 else if( NULL==tail&&NULL!=front ) 198 { 199 if(front->state) 200 { 201 front->length = front->length + position->length; 202 front->next = NULL; 203 free(position); 204 } 205 else 206 { 207 memset( position->task_name, 0, sizeof(char)*32 ); 208 position->state = CANUSE; 209 } 210 } 211 else if( NULL==tail&&NULL==front ) 212 { 213 memset( position->task_name, 0, sizeof(char)*32 ); 214 position->state = CANUSE; 215 } 216 printf("內(nèi)存回收成功!\n"); 217 } 218 } 219 220 int main(void) 221 { 222 int func_ = 0; 223 224 //初始化Mhead 225 Mhead = (struct MZone*)malloc(sizeof(struct MZone)); 226 Mhead->begin_addr = 0; 227 Mhead->length = MSIZE; 228 Mhead->state = CANUSE; 229 memset(Mhead->task_name, 0, sizeof(char)*32 ); 230 Mhead->next = NULL; 231 232 while( 1 ) 233 { 234 printf("******************首次適應(yīng)算法實(shí)現(xiàn)主存分配和回收系統(tǒng)(內(nèi)存)***************\n"); 235 printf("1:查看內(nèi)存分配情況\n"); 236 printf("2:申請分配內(nèi)存\n"); 237 printf("3:申請回收內(nèi)存\n"); 238 printf("4:退出程序\n"); 239 printf("~~~~~~~~~~~~~~~~~"); 240 scanf("%d",&func_); 241 switch( func_ ) 242 { 243 case 1 :showmemory();break; 244 case 2 :memoallocate();break; 245 case 3 :memoreturn();break; 246 case 4 :return 1; 247 } 248 system("cls"); 249 } 250 }四、 實(shí)驗(yàn)總結(jié)
?
轉(zhuǎn)載于:https://www.cnblogs.com/1994-ann/p/5089979.html
總結(jié)
以上是生活随笔為你收集整理的39,叶慧敏 实验四 主存空间的分配和回收模拟的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 带你飞过PMP—备考上篇•乖乖看书就对了
- 下一篇: 在Prezi中输入简体中文的完美解决方案