首页 > 代码库 > 模拟操作系统动态内存管理(c实现)
模拟操作系统动态内存管理(c实现)
操作系统对内存申请与释放(对连续的区域合并)的模拟
1 #include <stdio.h> 2 #include <stdlib.h> 3 #define N 10 //预设的内碎片的最小值 4 #define M_set 1024//预设的内存大小 5 6 struct mem_block 7 { 8 int num; 9 int addr; 10 int length; 11 mem_block *next; 12 }; 13 typedef mem_block* point; 14 15 int numcal_M=1;//内存id 16 int numcal_P=1;//进程id 17 18 void create(point &pHead); 19 void init_get_size(point pHead); 20 void insert_P(point pHead,int addr,int siz); 21 void sort_size(point &pHead);//small to big 22 void sort_addr(point &pHead); 23 void apply(point M_pHead,point P_pHead); 24 void chick_conn(point pHead); 25 void insert_M(point pHead,int addr,int siz); 26 void free(point P_pHead,point M_pHead); 27 void print(point pHead); 28 void destory(point pHead); 29 int main() 30 { 31 printf(" *********************************************\n"); 32 printf(" ** 内存管理系统 (最佳适应算法) **\n"); 33 printf(" *********************************************\n"); 34 point mem; 35 create(mem); 36 init_get_size(mem); 37 38 point prog; 39 create(prog); 40 41 int op=-1; 42 while(1) 43 { 44 printf("\n\n请输入要进行的操作:\n1.申请内存 2.释放内存 3.显示当前内存情况 4.显示被申请走的内存块 0.退出操作\n"); 45 scanf("%d",&op); 46 switch(op) 47 { 48 case 1: 49 apply(mem,prog); 50 break; 51 case 2: 52 free(prog,mem); 53 break; 54 case 3: 55 sort_addr(prog); 56 printf("内存号 起始地址 内存大小\n"); 57 print(mem); 58 break; 59 case 4: 60 printf("进程号 起始地址 内存大小\n"); 61 print(prog); 62 break; 63 case 0: 64 destory(mem); 65 destory(prog); 66 exit(0); 67 default: 68 printf("非法操作\n"); 69 break; 70 } 71 } 72 return 0; 73 } 74 void create(point &pHead) 75 { 76 pHead = (point)malloc(sizeof(mem_block)); 77 pHead ->num=0; 78 pHead->addr=0; 79 pHead->length = 0; 80 pHead->next=NULL; 81 return; 82 } 83 84 void insert_P(point pHead,int addr,int siz) 85 { 86 point pPro=(point)malloc(sizeof(mem_block)); 87 pPro->length=siz; 88 pPro->addr=addr; 89 pPro->next=NULL; 90 pPro->num=numcal_P; 91 numcal_P++; 92 93 point pCur = pHead; 94 while(pCur->next!=NULL) 95 pCur=pCur->next; 96 pCur->next=pPro; 97 return; 98 } 99 void sort_size(point &pHead)//small to big 100 { 101 point pCur_i=pHead,pCur_j; 102 int num,addr,siz; 103 while(pCur_i->next!=NULL) 104 { 105 pCur_i=pCur_i->next; 106 pCur_j=pCur_i; 107 while(pCur_j->next!=NULL) 108 { 109 pCur_j=pCur_j->next; 110 if(pCur_j->length < pCur_i->length) 111 { 112 num=pCur_i->num; 113 addr=pCur_i->addr; 114 siz=pCur_i->length; 115 116 pCur_i->num=pCur_j->num; 117 pCur_i->addr=pCur_j->addr; 118 pCur_i->length=pCur_j->length; 119 120 pCur_j->num=num; 121 pCur_j->addr=addr; 122 pCur_j->length=siz; 123 } 124 } 125 } 126 } 127 void sort_addr(point &pHead) 128 { 129 point pCur_i=pHead,pCur_j; 130 int num,addr,siz; 131 while(pCur_i->next!=NULL) 132 { 133 pCur_i=pCur_i->next; 134 pCur_j=pCur_i; 135 while(pCur_j->next!=NULL) 136 { 137 pCur_j=pCur_j->next; 138 if(pCur_j->addr < pCur_i->addr) 139 { 140 num=pCur_i->num; 141 addr=pCur_i->addr; 142 siz=pCur_i->length; 143 144 pCur_i->num=pCur_j->num; 145 pCur_i->addr=pCur_j->addr; 146 pCur_i->length=pCur_j->length; 147 148 pCur_j->num=num; 149 pCur_j->addr=addr; 150 pCur_j->length=siz; 151 } 152 } 153 } 154 } 155 void apply(point M_pHead,point P_pHead) 156 { 157 sort_size(M_pHead); 158 int siz; 159 printf("请输入要申请内存的大小\n"); 160 scanf("%d",&siz); 161 point pCur=M_pHead; 162 int flag=1; 163 while(pCur->next!=NULL) 164 { 165 166 if(pCur->next->length-siz<=N&&pCur->next->length>=siz) 167 { 168 insert_P(P_pHead,pCur->next->addr,pCur->next->length); 169 pCur->next=pCur->next->next; 170 171 flag=0; 172 break; 173 } 174 else if(pCur->next->length-siz>N) 175 { 176 177 insert_P(P_pHead,pCur->next->addr,siz); 178 pCur->next->length-=siz; 179 pCur->next->addr+=siz; 180 flag=0; 181 break; 182 } 183 pCur=pCur->next; 184 } 185 if(flag) printf("哪有那么大的空间呀,插不进去\n"); 186 else printf("分配成功\n"); 187 return; 188 189 } 190 191 void init_get_size(point pHead) 192 { 193 point pPro=(point)malloc(sizeof(mem_block)); 194 195 pPro->length=M_set; 196 pPro->addr=0; 197 pPro->next=NULL; 198 pPro->num=numcal_M; 199 200 numcal_M++; 201 pHead->next=pPro; 202 return; 203 } 204 void chick_conn(point pHead) 205 { 206 point i,j; 207 for(i=pHead;i->next!=NULL;i=i->next) 208 for(j=pHead;j->next!=NULL;j=j->next) 209 { 210 if( (j->next->addr) == (i->next->addr) + (i->next->length) ) 211 { 212 i->next->length+=j->next->length; 213 j->next=j->next->next; 214 break; 215 } 216 } 217 return; 218 } 219 220 void insert_M(point pHead,int addr,int siz) 221 { 222 223 point pCur=pHead; 224 int flag=0; 225 while(pCur->next!=NULL) 226 { 227 if(pCur->next->addr+pCur->next->length==addr) 228 { 229 flag=1; 230 pCur->next->length+=siz; 231 break; 232 } 233 pCur=pCur->next; 234 } 235 if(!flag) 236 { 237 point pPro=(point)malloc(sizeof(mem_block)); 238 pPro->length=siz; 239 pPro->addr=addr; 240 pPro->next=NULL; 241 pPro->num=numcal_M; 242 numcal_M++; 243 244 pCur->next=pPro; 245 246 } 247 248 chick_conn(pHead); 249 printf("释放成功\n"); 250 return; 251 } 252 void free(point P_pHead,point M_pHead) 253 { 254 int prog_num =0; 255 printf("请输入要释放的内存块号\n"); 256 scanf("%d",&prog_num); 257 point pCur = P_pHead; 258 int flag=1; 259 while(pCur->next!=NULL) 260 { 261 if(pCur->next->num==prog_num) 262 { 263 flag=0; 264 insert_M(M_pHead,pCur->next->addr,pCur->next->length); 265 pCur->next=pCur->next->next; 266 break; 267 } 268 pCur=pCur->next; 269 } 270 if(flag) 271 printf("亲,这个内存号不存在呀,要不你先按4,看看情况?\n"); 272 } 273 void print(point pHead) 274 { 275 int flg=0; 276 point pCur=pHead; 277 while(pCur->next!=NULL) 278 { 279 pCur=pCur->next; 280 printf("%3d%12d%15d\n",pCur->num,pCur->addr,pCur->length); 281 flg=1; 282 } 283 if(!flg) 284 printf("\n\n 空链表,没有数据 \n"); 285 printf("\n"); 286 return; 287 } 288 void destory(point pHead) 289 { 290 point pNext=pHead->next; 291 while(pHead->next!=NULL) 292 { 293 free(pHead); 294 pHead=pNext; 295 pNext=pHead->next; 296 } 297 free(pHead); 298 pHead = NULL; 299 printf("撤销成功\n"); 300 }
模拟操作系统动态内存管理(c实现)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。