标签:
1.1. 实验目的
用高级语言完成一个主存空间的分配和回收程序,以加深对动态分区分配方式及其算法的理解。
1.2. 实验要求
采用连续分配方式之动态分区分配存储管理,使用首次适应算法、循环首次适应算法、最佳适应算法和最坏适应算法4种算法完成设计。
(1)**设计一个作业申请队列以及作业完成后的释放顺序,实现主存的分配和回收。采用分区说明表进行。
(2)或在程序运行过程,由用户指定申请与释放。
(3)设计一个空闲区说明表,以保存某时刻主存空间占用情况。
把空闲区说明表的变化情况以及各作业的申请、释放情况显示。
根据指定的实验课题,完成设计、编码和调试工作,完成实验报告。
可以选用Visual C++作为开发环境。也可以选用Windows下的VB,CB或其他可视化环境,利用各种控件较为方便。自主选择实验环境。
#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;
1 #include<stdio.h>
2 #include<conio.h>
3 #include<string.h>
4 #define MAX 24
5 #define Memory 512
6 struct partition{
7
8 char pn[10];
9 int begin;
10 int size;
11 int end;
12 char status;
13 };
14 typedef struct partition PART;
15 PART Free[MAX],User[MAX],addresses[MAX];
16 int sumFree,sumUsed,sumaddresses;
17
18 void addFree(int i,int j)
19 {
20 strcpy(addresses[i].pn,Free[j].pn);
21 addresses[i].begin=Free[j].begin;
22 addresses[i].size=Free[j].size;
23 addresses[i].status=Free[j].status;
24 }
25 void addUsed(int i,int j)
26 {
27 strcpy(addresses[i].pn,Used[j].pn);
28 addresses[i].begin=Used[j].begin;
29 addresses[i].size=Used[j].size;
30
31 addresses[i].status=Used[j].status;
32 }
33 void init()
34 {
35 sumFree=0,sumUsed=0,sumaddresses=0;
36 strcpy(Used[1].pn,"SYSTEM");
37 Used[1].begin=0;
38 Used[1].size=100;
39 Used[1].status=‘u‘;
40 sumUsed++;
41
42 sumaddresses++;
43 addUsed(sumaddresses,sumUsed);
44 printf("初始化,设内存总容量为512k\n");
45 printf("系统从低地址部分开始使用,占用100k\n\n");
46
47 strcpy(Free[1].pn,"----");
48 Free[1].begin=100;
49 Free[1].size=Memory-Free[1].begin;
50 Free[1].status=‘f‘;
51 sumFree++;
52
53 sumaddresses++;
54 addFree(sumaddresses,sumFree);
55 }
56
57 void PT()
58 {
59 int i;
60 printf("空闲区表Free\n");
61 printf("\t\t\tNo.\trproname\tbegin\tsize\tstatus\n");
62 for(i=1;i<=sumFree;i++)
63 printf("\t\t\tNo.%d\t%s\t%d\t%d\t%c\n",i,Free[i].pn,Free[i].begin,Free[i].size,Free[i].status);
64
65
66 printf("已分配分区表Used\n");
67 printf("\t\t\tNo.\trproname\tbegin\tsize\tstatus\n");
68 for(i=1;i<=sumUsed;i++)
69 printf("\t\t\tNo.%d\t%s\t%d\t%d\t%c\n",i,Used[i].pn,Used[i].begin,Used[i].size,Used[i].status);
70
71 printf("内存使用情况,按起始址增长的排:\n");
72 printf("\t\t\tNo.\trproname\tbegin\tsize\tstatus\n");
73 for(i=1;i<=sumaddresses;i++)
74 printf("\t\t\tNo.%d\t%s\t%d\t%d\t%c\n",i,addresses[i].pn,addresses[i].begin,addresses[i].size,addresses[i].status);
75
76 }
77 int main()
78 {
79 init();
80 PT();
81 return 0;
82 }
83
84
85
86
87
88
89 #include "string.h"
90 #include "iostream"
91 using namespace std;
92 #define FALSE 0
93 #define TRUE 1
94 #define W 10
95 #define R 20
96 int M=100;//进程的最大数为100
97 int N=100;//资源的最大数为100
98 int ALL_RESOURCE[W];//各种资源的数目总和
99 int MAX[W][R]; //M个进程对N类资源最大资源需求量
100 int AVAILABLE[R]; //系统可用资源数
101 int ALLOCATION[W][R]; //M个进程已经得到N类资源的资源量
102 int NEED[W][R]; //M个进程还需要N类资源的资源量
103 int Request[R]; //请求资源个数
104
105
106 void showdata() //函数showdata,输出资源分配情况
107 {
108 int i,j;
109 cout<<"各种资源的总数量(all):"<<endl;
110 cout<<" ";
111 for (j=0;j<N;j++)cout<<" 资源"<<j<<": "<<ALL_RESOURCE[j];
112 cout<<endl<<endl;
113 cout<<"系统目前各种资源可用的数为(available):"<<endl;
114 cout<<" ";
115 for (j=0;j<N;j++)cout<<" 资源"<<j<<": "<<AVAILABLE[j];
116 cout<<endl<<endl;
117 cout<<" 各进程还需要的资源量(need):"<<endl<<endl;
118 cout<<" 资源0"<<" 资源1"<<" 资源2"<<endl;
119 for (i=0;i<M;i++)
120 for (i=0;i<M;i++)
121 {
122 cout<<"进程p"<<i<<": ";
123 for (j=0;j<N;j++)cout<<NEED[i][j]<<" ";;
124 cout<<endl;
125 }
126 cout<<endl;
127 cout<<" 各进程已经得到的资源量(allocation): "<<endl<<endl;
128 cout<<" 资源0"<<" 资源1"<<" 资源2"<<endl;
129 for (i=0;i<M;i++)
130 {
131 cout<<"进程p"<<i<<": ";
132 for (j=0;j<N;j++)cout<<ALLOCATION[i][j]<<" ";
133 cout<<endl;
134 }
135 cout<<endl;
136 }
137 void changdata(int k) //函数changdata,改变可用资源和已经拿到资源和还需要的资源的值
138 {
139 int j;
140 for (j=0;j<N;j++)
141 {
142 AVAILABLE[j]=AVAILABLE[j]-Request[j];
143 ALLOCATION[k][j]=ALLOCATION[k][j]+Request[j];
144 NEED[k][j]=NEED[k][j]-Request[j];}}
145 void rstordata(int k) //函数rstordata,恢复可用资源和已经拿到资源和还需要的资源的值
146 {int j;
147 for (j=0;j<N;j++)
148 { AVAILABLE[j]=AVAILABLE[j]+Request[j];
149 ALLOCATION[k][j]=ALLOCATION[k][j]-Request[j];
150 NEED[k][j]=NEED[k][j]+Request[j];}}
151 int chkerr(int s) //函数chkerr,检查是否安全
152 { int WORK,FINISH[W];
153 int i,j,k=0;
154 for(i=0;i<M;i++)FINISH[i]=FALSE;
155 for(j=0;j<N;j++)
156 {
157 WORK=AVAILABLE[j];
158 i=s;
159 do
160 {
161 if(FINISH[i]==FALSE&&NEED[i][j]<=WORK)
162 {
163 WORK=WORK+ALLOCATION[i][j];
164 FINISH[i]=TRUE;
165 i=0;
166 }
167 else
168 { i++;
169 }
170 }while(i<M);
171 for(i=0;i<M;i++)
172 if(FINISH[i]==FALSE)
173 {
174 cout<<endl;
175 cout<<" 系统不安全!!! 本次资源申请不成功!!!"<<endl;
176 cout<<endl;
177 return 1;
178 }
179 }
180 cout<<endl;
181 cout<<" 经安全性检查,系统安全,本次分配成功。"<<endl;
182 cout<<endl;
183 return 0;
184 }
185 void bank() //银行家算法
186 {
187 int i=0,j=0;
188 char flag=‘Y‘;
189
190 while(flag==‘Y‘||flag==‘y‘)
191 {
192 i=-1;
193 while(i<0||i>=M)
194 {
195 cout<<" 请输入需申请资源的进程号(从P0到P"<<M-1<<",否则重输入!):";
196 cout<<"p";cin>>i;
197 if(i<0||i>=M)cout<<" 输入的进程号不存在,重新输入!"<<endl;
198 }
199 cout<<" 请输入进程P"<<i<<"申请的资源数:"<<endl;
200 for (j=0;j<N;j++)
201 {
202 cout<<" 资源"<<j<<": ";
203 cin>>Request[j];
204 if(Request[j]>NEED[i][j]) //若请求的资源数大于进程还需要i类资源的资源量j
205 {
206 cout<<" 进程P"<<i<<"申请的资源数大于进程P"<<i<<"还需要"<<j<<"类资源的资源量!";
207 cout<<"申请不合理,出错!请重新选择!"<<endl<<endl;
208 flag=‘N‘;
209 break;
210 }
211 else
212 {
213 if(Request[j]>AVAILABLE[j]) //若请求的资源数大于可用资源数
214 {
215 cout<<" 进程P"<<i<<"申请的资源数大于系统可用"<<j<<"类资源的资源量!";
216 cout<<"申请不合理,出错!请重新选择!"<<endl<<endl;
217 flag=‘N‘;
218 break;
219 }
220 }
221 }
222 if(flag==‘Y‘||flag==‘y‘)
223 {
224 changdata(i); //调用changdata(i)函数,改变资源数
225 if(chkerr(i)) //若系统安全
226 {
227 rstordata(i); //调用rstordata(i)函数,恢复资源数
228 showdata(); //输出资源分配情况
229 }
230 else //若系统不安全
231 showdata(); //输出资源分配情况
232 }
233 else //若flag=N||flag=n
234 showdata();
235 cout<<endl;
236 cout<<" 是否继续银行家算法演示,按‘Y‘或‘y‘键继续,按‘N‘或‘n‘键退出演示: ";
237 cin>>flag;
238 }
239 }
240 void main() //主函数
241 {
242 int i=0,j=0,p;
243 cout<<"请输入总进程数:"<<endl;
244 cin>>M;
245 cout<<"请输入总资源种类:"<<endl;
246 cin>>N;
247 cout<<"请输入总资源数:"<<endl;
248 for(i=0;i<N;i++)
249 cin>>ALL_RESOURCE[i];
250 cout<<"依次输入各进程所需要的最大资源数量:"<<endl;
251 for (i=0;i<M;i++)
252 {
253 for (j=0;j<N;j++)
254 {
255 do
256 {
257 cin>>MAX[i][j];
258 if (MAX[i][j]>ALL_RESOURCE[j])
259 cout<<endl<<"占有资源超过了声明的该资源总数,请重新输入"<<endl;
260 }while (MAX[i][j]>ALL_RESOURCE[j]);
261 }
262 }
263 cout<<"依次输入各进程已经占据的资源数量(allocation):"<<endl;
264 for (i=0;i<M;i++)
265 {
266 for (j=0;j<N;j++)
267 {
268 do
269 {
270 cin>>ALLOCATION[i][j];
271 if (ALLOCATION[i][j]>MAX[i][j])
272 cout<<endl<<"占有资源超过了声明的最大资源,请重新输入"<<endl;
273 }while (ALLOCATION[i][j]>MAX[i][j]);
274 }
275 }
276 //初始化资源数量
277 for (j=0;j<N;j++)
278 { p=ALL_RESOURCE[j];
279 for (i=0;i<M;i++)
280 {
281 p=p-ALLOCATION[i][j];//减去已经被占据的资源
282 AVAILABLE[j]=p;
283 if(AVAILABLE[j]<0)
284 AVAILABLE[j]=0;
285 }
286 }
287 for (i=0;i<M;i++)
288 for(j=0;j<N;j++)
289 NEED[i][j]=MAX[i][j]-ALLOCATION[i][j];
290 showdata();
291 bank();
292 }
实验结果与预期相符


标签:
原文地址:http://www.cnblogs.com/wqma/p/5628265.html