天天看點

[作業系統]實驗四 主存空間的配置設定和回收

實驗四主存空間的配置設定和回收

1.    目的和要求

1.1.           實驗目的

用進階語言完成一個主存空間的配置設定和回收程式,以加深對動态分區配置設定方式及其算法的了解。

1.2.           實驗要求

采用連續配置設定方式之動态分區配置設定存儲管理,使用首次适應算法、循環首次适應算法、最佳适應算法和最壞适應算法4種算法完成設計。

(1)**設計一個作業申請隊列以及作業完成後的釋放順序,實作主存的配置設定和回收。采用分區說明表進行。

(2)或在程式運作過程,由使用者指定申請與釋放。

(3)設計一個空閑區說明表,以儲存某時刻主存空間占用情況。

把空閑區說明表的變化情況以及各作業的申請、釋放情況顯示。

2.    實驗内容

根據指定的實驗課題,完成設計、編碼和調試工作,完成實驗報告。

3.    實驗環境

可以選用Visual C++作為開發環境。也可以選用Windows下的VB,CB或其他可視化環境,利用各種控件較為友善。自主選擇實驗環境。

4.    參考資料結構:

#include<stdio.h>

#include<conio.h>

#include<string.h>

#define MAX 24

struct partition{

      char pn[10];

      int begin;

      int size;

      int end;   ////////

      char status;  //////////

      };

typedef struct partition PART;

5、源代碼

1 #include <stdio.h>
  2 #include<conio.h>
  3 #include<string.h>
  4 #define MAX 100
  5 struct partition{
  6     char pn[10];
  7     int begin;
  8     int size;
  9     int end;   
 10     char status;  
 11     };
 12 typedef struct partition PART;
 13 PART p[MAX];
 14 int n;
 15 
 16 void init()
 17 {
 18     p[0].begin = 0;
 19     p[0].end = 150;
 20     strcpy(p[0].pn, "SYSTEM");
 21     p[0].size = 100;
 22     p[0].status = 'u';
 23 
 24     p[1].begin = 150;
 25     p[1].end = 1024;
 26     strcpy(p[1].pn, "-----");
 27     p[1].size = p[1].end - p[1].begin;
 28     p[1].status = 'f';
 29 
 30     n = 2;
 31 }
 32 
 33 void print()
 34 {
 35     int x = 1;
 36 
 37     printf("空閑區表Free:\n");
 38     printf("\tNo.\tproname\tbegin\tsize\tstatus\n");
 39     for(int i = 0; i < n; i++)
 40     {
 41         if(p[i].status=='f')
 42             printf("\tNo.%d\t%s\t%4d\t%4d\t%4c\n", x++, p[i].pn, p[i].begin, p[i].size, p[i].status);
 43     }
 44     printf("\n\n=========================================================\n");
 45 
 46     printf("已配置設定分區表Used:\n");
 47     printf("\tNo.\tproname\tbegin\tsize\tstatus\n");
 48     for(i = 0, x = 1; i < n; i++)
 49     {
 50         if(p[i].status=='u')
 51             printf("\tNo.%d\t%s\t%4d\t%4d\t%4c\n", x++, p[i].pn, p[i].begin, p[i].size, p[i].status);
 52     }
 53 
 54     printf("\n\n=========================================================\n");
 55 
 56     printf("記憶體使用情況:\nprintf sorted by address:\n");
 57     printf("\tNo.\tproname\tbegin\tsize\tstatus\n");
 58     printf("\t--------------------------------------\n");
 59     for(i = 0, x = 1; i < n; i++)
 60     {
 61             printf("\tNo.%d\t%s\t%4d\t%4d\t%4c\n", x++, p[i].pn, p[i].begin, p[i].size, p[i].status);
 62     }
 63 }
 64 
 65 void input()
 66 {
 67     int x = 1;
 68     while(x)
 69     {
 70         printf("\n\n請輸入程序名稱:");
 71         scanf("%s", &p[n].pn);
 72 
 73         for(int i = 0; i < n; i++)
 74         {    
 75             x = 0;
 76             if(strcmp(p[n].pn, p[i].pn) == 0)
 77             {
 78                 x = 1;
 79                 printf("程序名稱已存在,請重新輸入!");                
 80                 break;
 81             }
 82         }
 83     }
 84     x = 1;
 85     while(x)
 86     {
 87         printf("\n請輸入程序需要的空間大小:");
 88         scanf("%d", &p[n].size);
 89 
 90         for(int i = 0; i < n; i++)
 91         {
 92             
 93             if(p[i].size >=p[n].size)
 94             {
 95                 x = 0;    
 96                 break;
 97             }
 98         }
 99         if(x)
100             printf("找不到适合的空間,請重新輸入!");
101     }
102 }
103 
104 void fenqu()
105 {
106     PART  temp[MAX];
107     for(int i = 0;i < n; i++)
108     {
109         if(p[i].status=='f')
110         {
111             if(p[i].size >= p[n].size)
112             {    
113                 temp[0]=p[i];
114                 p[i]=p[n];
115                 p[n]=temp[0];
116                 
117                 p[i].end=p[n].begin+p[i].size;
118                 p[i].status='u';
119                 p[i].begin=p[n].begin;
120                 p[n].begin=p[i].end;
121                 p[n].end=temp[0].end;
122                 p[n].status='f';
123                 p[n].size=p[n].size-p[i].size;
124                 n++;
125                 break;
126             }
127 
128         }
129     }
130 }
131 
132 
133 void caculate(int i)
134 {    
135     int x=0;
136     p[i].end = p[i].begin+p[i].size;
137     p[i-1].end=p[i-1].begin+p[i-1].size;
138     if(p[i+1].status=='f' && p[i].end==p[i+1].begin)
139     {    x=1;
140         p[i+1].begin=p[i].begin;
141         p[i+1].size=p[i].size+p[i+1].size;
142         for(int j=i;j<n;j++)
143         {
144             p[j]=p[j+1];
145         }
146             n=n-1;
147     }
148     if(p[i-1].status=='f' && p[i-1].end==p[i].begin)
149     {    x=1;
150         p[i].begin=p[i-1].begin;
151         p[i].size=p[i-1].size+p[i].size;
152         strcpy(p[i].pn, "-----");
153         p[i].status = 'f';
154         for(int k=i;k<n;k++)
155         {
156             p[k-1]=p[k];
157         }
158         n=n-1;
159     }
160     if(x==0)
161     {
162         strcpy(p[i].pn, "-----");
163         p[i].status = 'f';
164     }
165 }
166 
167 void recycle()
168 {
169     char name[50];  
170     int x = 1;
171     while(x)
172     {
173         printf("\n請輸入程序名稱:");
174         scanf("%s", &name);
175         for(int i = 0; i < n; i++)
176         {    
177             if(strcmp(name, p[i].pn) == 0)
178             {
179                 x = 0;
180                  caculate(i);                
181                 break;
182             }
183         }
184         if(x==1)
185         {
186             printf("沒找到請重新輸入\n");
187         }
188         
189     }
190 }
191 int main(void)
192 {
193     int choose1,choose2;    
194     printf("初始化:設定記憶體總容量為 1024k\n系統從低位址部分開始占用 150k\n\n");
195 
196     init();
197     print();
198     
199     while(1)
200     {
201         printf("請選擇:1.配置設定記憶體        2.回收記憶體        3.結束\n");
202         scanf("%d",&choose1);
203         if(choose1==1)
204         {
205             input();
206             fenqu();
207             print();
208         }
209         if(choose1==2)
210         {
211             recycle();
212             print();
213         }
214         if(choose1==3)
215             break;        
216     }        
217     
218     return 0;
219 }      

運作結果:

[作業系統]實驗四 主存空間的配置設定和回收

6、實驗總結:

  今次實驗讓我學會了首次适應算法、循環首次适應算法、和最壞适應算法3種算法,讓我明白了學習程式設計必須要思路清晰。