实验四 主存空间的分配和回收模拟

                               实验四 主存空间的分配和回收模拟

                                                        物联网工程                  郑伯瑜                        201306104134

一、实验目的

为了合理地分配和使用这些存储空间,当用户提出申请主存储器空间时,存储管理必须根据申请者的要求,按一定的策略分析主存空间和使用情况,找出足够的空闲区域给申请者。当作业撤离归还主存资源时,则存储管理要收回占用的主存空间。主存的分配和回收的实现是与主存储器的管理方式有关的,通过本实验帮助我们理解在不同的存储管理方式下应怎样实现主存空间的分配和回收。

用高级语言完成一个主存空间的分配和回收模拟程序,以加深对内存分配方式及其算法的理解。

二、实验内容

2.1 模拟包括3部分:

1) 实现特定的内存分配算法

2) 实现内存回收模拟

3) 每种内存分配策略对应的碎片数统计

2.2 固定分区存储管理

假设内存容量为120KB,并且分别划分成23,24,25,26KB大小的块各一块。

一个进程所需要的内存为0100KB。同时假设一个进程在运行过程中所需内存的大小不变。

模拟五个进程到达请求分配与运行完回收情况,输出主存分配表.

2.3 动态分区分配存储管理

采用连续分配方式之动态分区分配存储管理,使用首次适应算法、下次适应算法、最佳适应算法和最坏适应算法4种算法完成设计(任选两种算法)。

(1)在程序运行过程,由用户指定申请与释放。

(2)设计一个已占用分区表,以保存某时刻主存空间占用情况。

(3)设计一个空闲分区表,以保存某时刻主存空间剩余情况。

(4)用两个表的变化情况,反应各进程所需内存的申请与释放情况。

三、实验环境

根据指定的实验课题,完成设计、编码和调试工作,完成实验报告。

可以选用Turbo C作为开发环境。也可以选用Windows下的VBCB或其他可视化环境,利用各种控件较为方便。自主选择实验环境。

四、实验源代码

  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 #include <string.h> 
  4 const int CANUSE = 1;
  5 const int CANTUSE = 0;
  6 const int MSIZE = 128;
  7  
  8  
  9 //内存分区 
 10 struct MZone
 11 {
 12     int begin_addr;  //空闲区起始地址
 13     int length;      //一个连续空闲区的长度
 14     int state;       //状态 
 15     char task_name[32];    //内存中任务名
 16     struct MZone *next;     //指向下一个空闲分区
 17 };
 18  
 19 //内存头指针 
 20 struct MZone *Mhead = NULL;
 21  
 22 //showmemory函数,显示当前内存分配情况 
 23 void showmemory()
 24 {
 25      struct MZone *Mpoint = Mhead;
 26       
 27      printf("内存的使用情况
");
 28      printf("beginaddr	length	state	task
"); 
 29       
 30      while( NULL!=Mpoint)
 31      {
 32          printf("%dk		",Mpoint->begin_addr);
 33          printf("%dk	",Mpoint->length);
 34          Mpoint->state?printf("CANUSE	"):printf("CANTUSE	");
 35          printf("%s
",Mpoint->task_name);
 36          Mpoint = Mpoint->next;
 37      } 
 38      
 39      system("pause");
 40       
 41 }
 42  
 43 //memoallocate函数,用于分配内纯 
 44 void memoallocate(void)
 45 {
 46      struct MZone *Mnew = (struct MZone*)malloc(sizeof(struct MZone));
 47      printf("输入要分配内存大小(kb):
");
 48      scanf("%d",&Mnew->length);
 49      printf("输入任务名:
");
 50      scanf("%s",&Mnew->task_name);
 51      Minsert(Mnew)?printf("分配内存成功
"):printf("没有符合大小的空闲分区,内存分配失败。
"); 
 52      system("pause");
 53      free(Mnew);
 54 }
 55  
 56 //Minsert函数,功能插入任务到空闲分区 
 57 int Minsert(struct MZone* Mnew)
 58 {
 59  
 60      struct MZone *Zinsert = Mhead;
 61      //flag用于指示是Zinsert到了NULL,既没有内存可以分配 
 62      int flag = 1;   
 63       
 64      while( Zinsert->length<Mnew->length || !Zinsert->state)
 65      {
 66              if( NULL!=Zinsert->next )
 67              {
 68                 Zinsert = Zinsert->next;
 69              }
 70              else
 71              {   
 72                  Zinsert = Zinsert->next;
 73                  break;
 74              }
 75              
 76      }
 77       
 78      if( NULL==Zinsert ) 
 79      {
 80          return 0;
 81      }
 82      
 83      if( MSIZE == Zinsert->begin_addr+Mnew->length )
 84      {
 85           Zinsert->state = CANTUSE;
 86           strcpy(Zinsert->task_name , Mnew->task_name);
 87           Zinsert->next = NULL;
 88           return 1;
 89      }
 90      else
 91      {
 92          struct MZone *Ztail = (struct MZone *)malloc(sizeof(struct MZone));
 93          Zinsert->state = CANTUSE;
 94          strcpy(Zinsert->task_name , Mnew->task_name);
 95          Zinsert->length = Mnew->length;
 96          Zinsert->next = Ztail;
 97           
 98          memset( Ztail, 0, sizeof(char)*32 );
 99          Ztail->begin_addr = Zinsert->begin_addr + Mnew->length;
100          Ztail->state = CANUSE;
101          Ztail->length = MSIZE - Ztail->begin_addr;
102          Ztail->next = NULL;
103           
104          return 1;
105      }
106 }
107  
108 //memoreturn函数,用于回收内存
109 void memoreturn(void)
110 {
111      char tname[32];
112      printf("输入要收回的任务名
");
113      scanf("%s",tname);
114      Mreturn(tname); 
115      system("pause"); 
116 } 
117  
118 //Mreturn函数,功能回收内存
119 int Mreturn(char taskname[])
120 {
121     struct MZone *front = NULL;
122     struct MZone *position = Mhead;
123     struct MZone *tail = Mhead->next;
124      
125     while( 0!=strcmp(position->task_name,taskname) ) 
126     {
127            front = position;
128            if( NULL!=position->next )
129            {
130                position = position->next;
131            }
132            else
133            {
134                position = NULL;
135                break;
136            }
137            tail = position->next;
138     }
139      
140     if( NULL==position )
141     {
142         printf("内存中没有此任务!");   
143     }
144     else
145     {
146       //不能用CANTUSE 
147       if( NULL!=tail&&NULL!=front )
148        {
149      
150             if( front->state&&tail->state )
151             {
152                 front->length = front->length + position->length + tail->length;
153                 front->next = tail->next;
154                 free(position);
155                 free(tail);
156             }
157             else if( front->state&&!tail->state )
158             {
159                 front->length = front->length + position->length;
160                 front->next = position->next;
161                 free(position);
162             }
163             else if( !front->state&&tail->state )
164             {
165                 position->length = position->length + tail->length;
166                 memset( position->task_name, 0, sizeof(char)*32 );
167                 position->next = tail->next;
168                 position->state = CANUSE;
169                 free(tail);
170             }
171             else if( !front->state&&!tail->state )
172             {
173                 memset( position->task_name, 0, sizeof(char)*32 );
174                 position->state = CANUSE;   
175             }
176        }
177        else if( NULL!=tail&&NULL==front )
178        {
179          if( !tail->state )
180           {
181              memset( position->task_name, 0, sizeof(char)*32 );
182              position->state = CANUSE;
183           }
184           else
185           {
186              position->length = position->length + tail->length;
187              position->next = NULL;
188              free(tail);
189           }
190        } 
191        else if( NULL==tail&&NULL!=front )
192        {
193          if(front->state)
194           { 
195               front->length = front->length + position->length;
196               front->next = NULL;
197               free(position);
198           }
199           else
200           {
201               memset( position->task_name, 0, sizeof(char)*32 );
202               position->state = CANUSE;
203           }
204        }
205        else if( NULL==tail&&NULL==front )
206        {
207             memset( position->task_name, 0, sizeof(char)*32 );
208             position->state = CANUSE; 
209        }
210     printf("内存回收成功!
");
211    }
212 }
213   
214 int main(void)
215 {
216      int func_ = 0;
217       
218      //初始化Mhead 
219      Mhead = (struct MZone*)malloc(sizeof(struct MZone));
220      Mhead->begin_addr = 0;
221      Mhead->length = MSIZE;
222      Mhead->state = CANUSE;
223      memset(Mhead->task_name, 0, sizeof(char)*32 );
224      Mhead->next = NULL;
225       
226      while( 1 )
227      {
228            printf("******************首次适应算法实现主存分配和回收系统(内存MSIZE)***************");
229            printf("|1:查看内存分配情况
");
230            printf("|2:申请分配内存
");
231            printf("|3:申请回收内存
");
232            printf("|4:退出程序
");
233            printf("********************************************************************************");
234            scanf("%d",&func_);
235            switch( func_ )
236            {
237                    case 1 :showmemory();break;
238                    case 2 :memoallocate();break;
239                    case 3 :memoreturn();break; 
240                    case 4 :return 1;
241            }
242            system("cls");
243      }       
244 }

五、实验截图

查看内存空间

申请内存空间

查看内存分配

申请内存回收

查看回收后内存

再次申请空间

五、实验总结

      通过这次实验,我大概了解主存空间的分配和回收模拟。在编程过程中加深了对课程内容的理解,虽然编程中有遇到一些错误,例如括号的匹配问题,无法换行等问题,都能很快得到解决。其余方面,没有多大的问题。在这次实验中,自己对主存空间的分配和回收模拟有了初步的认识,希望在后面的学习中能有机会继续深入了解。

原文地址:https://www.cnblogs.com/boyuzheng/p/5089019.html