码迷,mamicode.com
首页 > 其他好文 > 详细

内存管理模拟

时间:2014-11-13 18:28:03      阅读:258      评论:0      收藏:0      [点我收藏+]

标签:style   blog   io   color   ar   os   使用   sp   for   

内存管理模拟算法:首次适应算法、最佳适应算法、最坏适应算法

此程序是参考别人的,因此也没有什么好说的,感觉写的不错就贴上来了

代码如下:

  1 #include<stdio.h>
  2 #include<malloc.h>
  3 #include<stdlib.h>
  4 
  5 #define PROCESS_NAME_LEN 32 //进程名字长度
  6 #define MIN_SLICE 10 //最小碎片大小
  7 #define DEFAULT_MEM_SIZE 1024 // 默认的内存大小
  8 #define DEFAULT_MEM_START 0 //起始地址
  9 
 10 #define MA_FF 1                    //首次适应算法
 11 #define MA_BF 2                    //最佳适应算法
 12 #define MA_WF 3                    //最坏适应算法
 13 
 14 
 15 //空闲分区的结构体
 16 typedef struct free_block_type{
 17     int size;
 18     int start_addr;
 19     struct free_block_type *next;
 20 }free_block_type;
 21  free_block_type *free_block;
 22 
 23 
 24  //已分配分区的结构体
 25 typedef struct allocated_block{
 26     int pid;
 27     int size;
 28     int start_addr;
 29     char process_name[PROCESS_NAME_LEN];
 30     struct allocated_block *next;
 31  }allocated_block;
 32 
 33 struct allocated_block *allocated_block_head = NULL;
 34 
 35 int mem_size=DEFAULT_MEM_SIZE;
 36 int ma_algorithm = MA_FF;
 37 static int pid = 0;
 38 int flag = 0;
 39 
 40 //函数声明
 41 void display_menu();
 42 int set_mem_size();
 43 void set_algorithm();
 44 void rearrange(int algorithm);
 45 int new_process();
 46 int allocate_mem (struct allocated_block *ab);
 47 void kill_process();
 48 int free_mem (struct allocated_block *ab);
 49 int dispose (struct allocated_block *free_ab);
 50 int display_mem_usage();
 51 allocated_block * find_process(int pid);
 52 void rearrange_FF();
 53 void rearrange_BF();
 54 void rearrange_WF();
 55 
 56 //初始化空闲分区
 57 free_block_type* init_free_block(int mem_size){
 58     free_block_type *fb;
 59     fb=(free_block_type *)malloc(sizeof(free_block_type));
 60     if(fb==NULL){
 61         printf("No mem\n");
 62         return NULL;
 63         }
 64     fb->size = mem_size;
 65     fb->start_addr = DEFAULT_MEM_START;
 66     fb->next = NULL;
 67     return fb;
 68 }
 69 
 70 //显示主菜单
 71 void display_menu(){
 72     printf("\n");
 73     printf("1 - Set memory size (default=%d)\n", DEFAULT_MEM_SIZE);
 74     printf("2 - Select memory allocation algorithm\n");
 75     printf("3 - New process \n");
 76     printf("4 - Terminate a process \n");
 77     printf("5 - Display memory usage \n");
 78     printf("0 - Exit\n");
 79 }
 80 
 81 /*设置内存大小*/
 82 int set_mem_size(){
 83     int size;
 84     if(flag!=0){ /*flag标志防止内存被再次设置*/
 85         printf("Cannot set memory size again\n");
 86         return 0;
 87         }
 88     printf("Total memory size =");
 89     scanf("%d", &size);
 90     if(size>0) {
 91         mem_size = size;
 92         free_block->size = mem_size;/*设置初始大小为 1024*/
 93         }
 94     flag=1;
 95     return 1;
 96  }
 97  /*选择当前算法*/
 98 void set_algorithm(){
 99     int algorithm;
100     printf("\t1 - First Fit\n");
101     printf("\t2 - Best Fit \n");
102     printf("\t3 - Worst Fit \n");
103     printf("Please input your choice : ");
104     scanf("%d", &algorithm);
105     if(algorithm>=1 && algorithm <=3)
106               ma_algorithm=algorithm;
107 
108     rearrange(ma_algorithm);
109 }
110 
111 /*为每一个进程分配完内存以后重新按已选择的算法再次排序*/
112 void rearrange(int algorithm){
113     switch(algorithm){
114         case MA_FF: rearrange_FF(); break;
115         case MA_BF: rearrange_BF(); break;
116         case MA_WF: rearrange_WF(); break;
117         }
118 }
119 /*首次适应算法,按地址的大小由小到达排序*/
120 void rearrange_FF(){
121     free_block_type *temp,*p=NULL;
122     free_block_type *head=NULL;
123     int current_min_addr;
124 
125     if(free_block){
126         temp=free_block;
127         current_min_addr=free_block->start_addr;
128         while(temp->next!=NULL){
129             if(temp->next->start_addr<current_min_addr){
130                 current_min_addr=temp->next->start_addr;
131                 p=temp;
132                 }
133             temp=temp->next;
134         }
135         if(p!=NULL){
136             temp=p->next;
137             p->next=p->next->next;
138             temp->next=free_block;
139             free_block=temp;
140         }
141         head=free_block;
142         p=head;
143         temp=head->next;
144         while(head->next!=NULL){
145             current_min_addr=head->next->start_addr;
146             while(temp->next!=NULL){
147                 if(temp->next->start_addr<current_min_addr){
148                     current_min_addr=temp->next->start_addr;
149                     p=temp;
150                 }
151             temp=temp->next;
152             }
153             if(p->next!=head->next){
154                 temp=p->next;
155                 p->next=p->next->next;
156                 temp->next=head->next;
157                 head->next=temp;
158             }
159         head=head->next;
160         temp=head->next;
161         p=head;
162         }
163     }
164     return ;
165 }
166 
167 /*最佳适应算法,按内存块的大小由小到大排序*/
168 void rearrange_BF(){
169     free_block_type *temp,*p=NULL;
170     free_block_type *head=NULL;
171     int current_min_size=free_block->size;
172 
173     temp=free_block;
174     while(temp->next!=NULL){
175         if(temp->next->size<current_min_size){
176             current_min_size=temp->next->size;
177             p=temp;
178         }
179         temp=temp->next;
180     }
181     if(p!=NULL){
182         temp=p->next;
183         p->next=p->next->next;
184         temp->next=free_block;
185         free_block=temp;
186     }
187     head=free_block;
188     p=head;
189     temp=head->next;
190     while(head->next!=NULL){
191         current_min_size=head->next->size;
192         while(temp->next!=NULL){
193             if(temp->next->size<current_min_size){
194                 current_min_size=temp->next->size;
195                 p=temp;
196             }
197             temp=temp->next;
198         }
199     if(p->next!=head->next){
200         temp=p;
201         p->next=p->next->next;
202         temp->next=head->next;
203         head->next=temp;
204         }
205         head=head->next;
206         temp=head->next;
207         p=head;
208     }
209 
210 }
211 
212 /*最坏适应算法,按地址块的大小从大到小排序*/
213 void rearrange_WF(){
214        free_block_type *temp,*p=NULL;
215     free_block_type *head=NULL;
216     int current_max_size=free_block->size;
217     temp=free_block;
218     while(temp->next!=NULL){
219         if(temp->next->size>current_max_size){
220             current_max_size=temp->next->size;
221             p=temp;
222         }
223         temp=temp->next;
224     }
225     if(p!=NULL){
226         temp=p;
227         p->next=p->next->next;
228         temp->next=free_block;
229         free_block=temp;
230     }
231     head=free_block;
232     p=head;
233     temp=head->next;
234     while(head->next!=NULL){
235         current_max_size=head->next->size;
236         while(temp->next!=NULL){
237             if(temp->next->size>current_max_size){
238                 current_max_size=temp->next->size;
239                 p=temp;
240             }
241         temp=temp->next;
242         }
243         if(p->next!=head->next){
244             temp=p->next;
245             p->next=p->next->next;
246             temp->next=head->next;
247             head->next=temp;
248         }
249         head=head->next;
250         temp=head->next;
251         p=head;
252     }
253     return ;
254 }
255 //创建一个新的进程
256 int new_process(){
257     struct allocated_block *ab;
258     int size;
259     int ret;
260     ab=(struct allocated_block *)malloc(sizeof(struct allocated_block));
261     if(!ab) exit(-5);
262     ab->next = NULL;
263     pid++;
264     sprintf(ab->process_name, "PROCESS-%02d", pid);
265     ab->pid = pid;
266     printf("Memory for %s:", ab->process_name);
267     printf("Please input you want to allocate process‘ size : ");
268     scanf("%d", &size);
269     if(size>0) {
270 
271         ab->size=size;
272     }
273     ret = allocate_mem(ab);
274     if((ret==1) &&(allocated_block_head == NULL)){
275         allocated_block_head=ab;
276         return 1; }
277 
278     else if (ret==1) {
279         ab->next=allocated_block_head;
280         allocated_block_head=ab;
281         return 2; }
282     else if(ret==-1){
283         printf("Allocation fail\n");
284         pid--;
285         free(ab);
286         return -1;
287      }
288     return 3;
289     }
290 
291 //内存分配
292 int allocate_mem(struct allocated_block *ab){
293     free_block_type *fbt, *pre;
294     free_block_type *temp,*p,*p1;
295     allocated_block *q;
296     int request_size=ab->size;
297     int sum=0;
298     int max;
299     fbt = pre = free_block;
300     if(fbt){
301         if(ma_algorithm==MA_WF){
302             if(fbt==NULL||fbt->size<request_size)
303                     return -1;
304         }
305         else{
306             while(fbt!=NULL&&fbt->size<request_size){
307                 pre=fbt;
308                 fbt=fbt->next;
309             }
310         }
311         if(fbt==NULL||fbt->size<request_size){
312             if(free_block->next!=NULL){
313                 sum=free_block->size;
314                 temp=free_block->next;
315                 while(temp!=NULL){
316                     sum+=temp->size;
317                     if(sum>=request_size)
318                         break;
319                     temp=temp->next;
320                 }
321                 if(temp==NULL)
322                     return -1;
323                 else{
324                     pre=free_block;
325                     max=free_block->start_addr;
326                     fbt=free_block;
327                     while(temp->next!=pre){
328                         if(max<pre->start_addr){
329                             max=pre->start_addr;
330                             fbt=pre;
331                         }
332                         pre=pre->next;
333                     }
334                     pre=free_block;
335 
336                     while(pre!=temp->next)
337                     {
338                         q=allocated_block_head;
339                         p=free_block;
340 
341                             while(q!=NULL)
342                             {
343                                 if(q->start_addr>pre->start_addr)
344                                     q->start_addr=q->start_addr-pre->size;
345                                 q=q->next;
346                             }
347                             while(p!=NULL)
348                             {
349                                 if(p->start_addr>pre->start_addr)
350                                     p->start_addr=p->start_addr-pre->size;
351                                 p=p->next;
352 
353                             }
354 
355                         pre=pre->next;
356                     }
357 
358                 pre=free_block;
359                 while(pre!=temp->next){
360 
361                     p1=pre->next;
362                     if(pre==fbt)
363                         break;
364                     free(pre);
365                     pre=p1;
366 
367                 }
368                 q=allocated_block_head;
369                 free_block=fbt;
370                 free_block->start_addr=q->start_addr+q->size;
371 
372                 free_block->size=sum;
373                 free_block->next=temp->next;
374                 if(free_block->size-request_size<MIN_SLICE){
375                     ab->size=free_block->size;
376                     ab->start_addr=free_block->start_addr;
377                     pre=free_block;
378                     free_block=free_block->next;
379                     free(pre);
380                 }
381                 else{
382                     ab->start_addr=free_block->start_addr;
383                     free_block->start_addr=free_block->start_addr+request_size;
384                     free_block->size=free_block->size-request_size;
385                 }
386 
387             }
388         }
389         else
390             return -1;
391          }
392         else{
393             if(fbt->size-request_size<MIN_SLICE){
394                 ab->size=fbt->size;
395                 ab->start_addr=fbt->start_addr;
396                 if(pre->next==free_block){
397                     free_block=fbt->next;
398                 }
399                 else{
400                     pre->next=fbt->next;
401                 }
402                 free_block=fbt->next;
403                 free(fbt);
404             }
405             else{
406                 ab->start_addr=fbt->start_addr;
407                 fbt->start_addr=fbt->start_addr+request_size;
408                 fbt->size=fbt->size-request_size;
409             }
410         }
411         rearrange(ma_algorithm);
412         return 1;
413     }
414     else
415     {
416         printf("Free Memory already has been allocated over: ");
417         return -1;
418     }
419 
420 }
421 
422 //选择杀死一个进程
423 
424 void kill_process(){
425     struct allocated_block *ab;
426     int pid;
427     printf("Kill Process, pid=");
428     scanf("%d", &pid);
429     ab=find_process(pid);
430     if(ab!=NULL){
431         free_mem(ab);
432         dispose(ab);
433         }
434 }
435 //找到要杀死的进程的标号
436 allocated_block * find_process(int pid){
437     allocated_block *abb;
438     abb=allocated_block_head;
439     if(abb->pid==pid)
440     {
441         return abb;
442     }
443     abb=allocated_block_head->next;
444     while(abb->next!=NULL){
445         if(abb->pid==pid)
446             return abb;
447         abb=abb->next;
448     }
449     return abb;
450 }
451 
452 //释放杀死进程的内存块
453 int free_mem(struct allocated_block *ab){
454        int algorithm = ma_algorithm;
455        struct free_block_type *fbt, *pre;
456        fbt=(struct free_block_type*) malloc(sizeof(struct free_block_type));
457     pre=(struct free_block_type*) malloc(sizeof(struct free_block_type));
458        if(!fbt) return -1;
459 
460     fbt->start_addr=ab->start_addr;
461     fbt->size=ab->size;
462     fbt->next=free_block;
463     free_block=fbt;
464     rearrange_FF();
465     pre->next=free_block;
466     pre->size=0;
467     while(pre->next&&(pre->next->start_addr!=fbt->start_addr))
468         pre=pre->next;
469     if(pre->size!=0&&fbt->next!=NULL){
470         if(((pre->start_addr+pre->size)==fbt->start_addr)&&((fbt->start_addr+fbt->size)==fbt->next->start_addr)){
471             pre->size=pre->size+fbt->size+fbt->next->size;
472             pre->next=fbt->next->next;
473             free(fbt->next);
474             free(fbt);
475         }
476         else if((pre->start_addr+pre->size)==fbt->start_addr){
477             pre->size=pre->size+fbt->size;
478             pre->next=fbt->next;
479             free(fbt);
480         }
481         else if(fbt->start_addr+fbt->size==fbt->next->start_addr){
482             fbt->size=fbt->size+fbt->next->size;
483             fbt->next=fbt->next->next;
484             free(fbt->next);
485         }
486     }
487     else if((pre->size==0)&&fbt->next){
488         if((fbt->start_addr+fbt->size)==fbt->next->start_addr){
489             fbt->size=fbt->size+fbt->next->size;
490             fbt->next=fbt->next->next;
491             free_block=fbt;
492             free(fbt->next);
493         }
494     }
495     else if(fbt->next==NULL){
496         if((pre->start_addr+pre->size)==fbt->start_addr){
497             pre->size=pre->size+fbt->size;
498             pre->next=fbt->next;
499             free(fbt);
500         }
501     }
502     rearrange(algorithm);
503 
504     return 1;
505 }
506 
507 //销毁杀死进程的结点
508 int dispose(struct allocated_block *free_ab){
509     struct allocated_block *pre,*ab;
510 
511    if(free_ab == allocated_block_head) {
512      allocated_block_head = allocated_block_head->next;
513         free(free_ab);
514         return 1;
515         }
516     pre = allocated_block_head;
517     ab = allocated_block_head->next;
518     while(ab!=free_ab)
519     {
520      pre = ab;
521      ab = ab->next;
522     }
523     pre->next = ab->next;
524     free(ab);
525     return 2;
526  }
527 
528 //显示内存使用情况
529 
530 int display_mem_usage(){
531     struct free_block_type *fbt=free_block;
532     struct allocated_block *ab=allocated_block_head;
533     printf("----------------------------------------------------------\n");
534 
535     if(fbt==NULL)
536     {
537         printf("Free Memory already used over !\n");
538     }
539     printf("----------------------------------------------------------\n");
540 
541 
542     if(fbt)
543     {
544         printf("Free Memory:\n");
545         printf("%20s %20s\n", " start_addr", " size");
546         while(fbt!=NULL){
547             printf("%20d %20d\n", fbt->start_addr, fbt->size);
548             fbt=fbt->next;
549             }
550     }
551 
552     printf("\nUsed Memory:\n");
553     printf("%10s %20s %10s %10s\n", "PID", "ProcessName", "start_addr", " size");
554     while(ab!=NULL){
555         printf("%10d %20s %10d %10d\n", ab->pid, ab->process_name, ab->start_addr, ab->size);
556         ab=ab->next;
557         }
558     printf("----------------------------------------------------------\n");
559     return 0;
560 }
561 
562 //退出,销毁所有链表
563 void do_exit()
564 {
565     free_block_type *temp;
566     allocated_block *temp1;
567     temp=free_block->next;
568     while(temp!=NULL)
569     {
570         free_block->next=temp->next;
571         free(temp);
572         temp=free_block->next;
573     }
574     free(free_block);
575     temp1=allocated_block_head->next;
576     while(temp1!=NULL)
577     {
578         allocated_block_head->next=temp1->next;
579         free(temp1);
580         temp1=allocated_block_head->next;
581     }
582     free(allocated_block_head->next);
583 
584 }
585 //主函数
586 int main(){
587     char choice;
588     pid=0;
589     free_block=init_free_block(mem_size);
590     while(1) {
591     display_menu();
592     fflush(stdin);
593 
594    choice=getchar();
595     switch(choice){
596         case 1: set_mem_size(); break;
597         case 2: set_algorithm();flag=1; break;
598         case 3: new_process(); flag=1; break;
599         case 4: kill_process(); flag=1; break;
600         case 5: display_mem_usage(); flag=1; break;
601         case 0: do_exit();exit(0);
602         default: break;
603     }
604     }
605     return 0;
606 }

 

内存管理模拟

标签:style   blog   io   color   ar   os   使用   sp   for   

原文地址:http://www.cnblogs.com/Karma-wjc/p/4095533.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!