操作系统——页式内存管理算法实现

本程序演示的是操作系统页式内存管理方法。

基本流程为:

假设分配内存总大小255,创建进程之前检查内存空间是否有剩余足够的空间装入该进程,

如果有,则分配,否则,提示不能分配,内存不足。

每个进程看做一个对象,包括进程名,所占内存大小,分配完空间后,具体占用哪块空间

要做记录。

内存存储情况采用位示图法,分配为1,空闲为0。

空闲块被送入空闲队列(此过程可以省略)

进程撤销时,要把内存相应标志置为0,把释放的块号送入空闲队列。

最多创建10个进程,用一个进程集合来存储。

下面是详细代码:C++语言描述。希望各位高手多多指点。

  1 // Menu.cpp : Defines the entry point for the console application.
2 //
3
4 #include "stdafx.h"
5 #include <iostream.h>
6 #include <string.h>
7
8 #define MemorySize 255
9
10 //函数声明
11
12 void DisplayMemory(int[255]); //显示内存
13
14 //定义进程类
15 class Process{
16
17 private:
18 char name[20];
19 int size;
20 public:
21 int* status;
22 Process(char *n=" ", int s=0){
23 strcpy(name,n);
24 size = s;
25 }
26 ~Process(){}
27 char* GetName(){
28 return name;
29 }
30 int GetSize(){
31 return size;
32 }
33 void Display(){
34 int i = 0;
35 cout<<"\n\t";
36 while(i<size){
37 cout<<name<<"["<<i<<"]:"<<status[i];
38 i++;
39 if(i % 20 == 0){
40 cout<<"\n\t";
41 }
42 }
43 }
44 };
45
46 //定义类 QNode
47 class QNode{
48 private:
49 int data;
50 QNode * next;
51 public:
52 QNode(){
53 next = NULL;
54 }
55
56 //声明本类为LinkQueue的友元类
57 //这样可以在LinkQueue类内使用QNode里的私有成员(数据和指向下一个节点的指针)
58 friend class LinkQueue;
59 };//QNode类定义结束
60
61 //定义类 LinkQueue
62 class LinkQueue {
63 private:
64 QNode * front;
65 QNode * rear;
66 int len;
67 public:
68 //构造函数
69 LinkQueue(){
70 front = new QNode();
71 rear = front;
72 len = 0;
73 }
74 //析构函数
75 ~LinkQueue(){
76 while(front->next){
77 DeQueue();
78 }
79 }
80 //入队函数
81 void Enqueue(int v){
82 QNode *p = new QNode();
83
84 p->data = v;
85 p->next = NULL;
86 rear->next = p;
87 rear = p;
88 len++;
89 }
90 //是否为空,声明在出队函数之前
91 bool Empty(){
92 return front->next == NULL;
93 }
94 //获取当前队列长度
95 int Length(){
96 return len;
97 }
98 //出队函数
99 int DeQueue(){
100 if(Empty()){
101 cout<<"\nThe LinkQueue has been empty!"<<endl;
102 return 0;
103 }
104 QNode *p = front->next;
105 int v = p->data;
106 front->next = p->next;
107 if(rear == p)
108 rear = front;
109 delete p;
110 len--;
111 return v;
112 }
113 void Display(){
114 cout<<"The empty block as followings:"<<endl;
115 QNode *p = front->next;
116 int i = 0;
117 while(p){
118 cout<<"Empty["<<i<<"]:"<<p->data<<endl;
119 p = p->next;
120 i++;
121 }
122 }
123
124 };//LinkQueue定义结束
125
126
127 //循环队列 可用来存储进程对象,这是此程序待改进之处,
128 //本程序在撤销进程后,把其后面的进程号向前移动
129 class Queue {
130 private:
131 int base[10];
132 int front;
133 int rear;
134 int length;
135 public:
136 Queue(){
137 for(int i=0; i<10; i++){
138 base[i] = 0;
139 }
140 front = 0;
141 rear = 0;
142 length = 0;
143 }
144 int EnQueue(int v){
145 if(!Full()){
146 base[rear] = v;
147 length = (rear - front + 10)%10 + 1;
148 rear = (rear + 1) % 10;
149 return 1;
150 }
151 else
152 cout<<"Already FULL! EnQueue error!"<<endl;
153 return 0;
154 }
155 int DeQueue(){
156 if(!Empty()){
157 int v = base[front];
158 front = (front + 1) % 10;
159 length--;
160 return v;
161 }
162 else{
163 cout<<"Emtpy!"<<endl;
164 return -1;
165 }
166 }
167 bool Full(){
168 return length == 10;
169 }
170 bool Empty(){
171 return length == 0;
172 }
173 };
174
175 int main(int argc, char* argv[])
176 {
177 int P_MaxNum = 10;
178 int k = 1;
179 Process** PTC = new Process*[P_MaxNum]; // ProcessTableCollection
180 int Memory[255]; //定义内存最大容量255
181 for(int i=0; i<255; i++){ //格式化内存数据 清零表示空闲可用 置1时表示占用
182 Memory[i] = 0;
183 }
184
185 LinkQueue* lq = new LinkQueue();//空闲页面表,用链队列模拟,存放空闲页面
186 Queue * xhq = new Queue();//循环队列,存储当前所有进程,最大10个
187 //将空闲页面号存入空闲页面表
188 for(i=0; i<MemorySize; i++){
189 if(Memory[i] == 0){
190 lq->Enqueue(i);
191 }
192 }
193 bool flag = true;
194 while(flag){
195 cout<<"\t****************************************************"<<endl;
196 cout<<"\t* The Demo showes the Page Memory Management Way *"<<endl;
197 cout<<"\t* (Author:Dong Zhi 2011-11-25) *"<<endl;
198 cout<<"\t* The Main Menu: *"<<endl; //主菜单
199 cout<<"\t* *"<<endl;
200 cout<<"\t* 1.Create a process *"<<endl; //创建进程
201 cout<<"\t* 2.Withdraw a process *"<<endl; //撤销进程
202 cout<<"\t* 3.Explore the Memory status *"<<endl; //查看内存使用清单(初始内存为255)
203 cout<<"\t* 4.Quit the Programe *"<<endl; //退出程序
204 cout<<"\t****************************************************"<<endl;
205 cout<<"\tPlease input a num(1/2/3/4):"; //输入选择
206 int a = 0;
207 char name[10];
208 int i;
209 int size;
210 int m = 0;
211 cin>>a;
212 switch(a){
213 case 1:
214 i = 0; //统一迭代变量
215 size = 0;//进程所需页面数
216 //int* ProcessArray;//整型数组,存放当前进程页表
217
218 cout<<"\tlq->length:"<<lq->Length();
219 //lq->Display();//空余情况不必显示
220 cout<<"\n\tWe have been ready! Please input the process name:";
221 cin>>name;
222 cout<<"\n\tPlease input the space you(your process) need: ";
223 cin>>size;
224 //判断所需内存空间是否允许分配(查看剩余内存空间大小length并和size比较)
225 if(size < lq->Length()){
226 cout<<"\n\tCongratulations! We have enough space for you!"<<endl<<endl;
227 }else if(size == lq->Length()){
228 cout<<"\n\tCongratulations! We just left the space you need!"<<endl<<endl;
229 }else{
230 cout<<"\n\tSorry! The space you need is bigger than wo have now! Please withdraw a process!"<<endl<<endl;
231 break;
232 }
233
234
235 PTC[k] = new Process(name,size);
236 if(k > P_MaxNum){
237 cout<<"\n\tThe number of process have been the max value,please choose 3 to withdraw a Process!"<<endl;
238 break;
239 }
240 PTC[k]->status = new int[size];
241 //格式化页表
242 for(i=0; i<size; i++){
243 PTC[k]->status[i] = 0;
244 }
245 cout<<"\tThe Process Page Table has been ready!(Empty) Please press any key to coutinue!"<<endl<<endl;
246 //由上面安全判断后,可以为其分配内存了,
247 //将空闲队列按所需大小出队,并将内存标号写到页表,然后将相应内存标志置为占用(1),即修改空闲页面表,进程页表和页面存储表
248 for(i=0; i<size; i++){
249 int deQ = lq->DeQueue();
250 PTC[k]->status[i] = deQ;
251 for(int j=0; j<MemorySize; j++){
252 if(j == deQ){
253 Memory[deQ] = 1;
254 }
255 }
256 }
257 //lq->Display();剩余情况不必显示
258 cout<<"Distributed over! Press any key to have a look at the Process Table Status!"<<endl;
259 getchar();
260 //再次显示当前内存分配情况
261 //DisplayMemory(Memory);
262
263 //显示当前进程页表
264 cout<<"The followings have showed the ProcessTable Status:"<<endl;
265 for(i=0; i<size; i++){
266 cout<<PTC[k]->status[i]<<" ";
267 }
268 cout<<endl<<endl;
269 k++;
270 break;
271 case 2:
272 char c[10];
273 if((k-1)<1){
274 cout<<"\tNo process has been created!"<<endl;
275 break;
276 }
277 for(i=1; i<k; i++){
278 cout<<"\t"<<"PTC["<<i<<"]:"<<PTC[i]->GetName()<<endl;
279 }
280
281 loop:cout<<"\tplease input the name of the process)(input 0 return):";
282 cin>>c;
283 if(strcmp(c,"0") == 0){break;}
284
285
286 for(i=1; i<k; i++){
287 if(strcmp(PTC[i]->GetName(),c) == 0){
288 m = i;
289 }
290 }
291 //cout<<m;
292 if(m == 0){
293 cout<<"\tThe process doesn't exist!"<<endl;
294 goto loop;
295 }
296
297 for(i=0; i<PTC[m]->GetSize(); i++){
298 int n = PTC[m]->status[i];
299 Memory[n] = 0;
300 lq->Enqueue(n);
301 }
302 for(i=m;i<=k-1;i++){
303 PTC[i] = PTC[i+1];
304 }
305 PTC[k-1] = NULL;
306 cout<<"\n\tOK,The free space is :"<<lq->Length()<<endl;
307 k--;
308 break;
309 case 3:
310 DisplayMemory(Memory);
311 break;
312 case 4:
313 flag = false;
314 break;
315 default:
316 cout<<endl<<"\tPlease choose again! Press any key to continue!"<<endl;
317 }
318 }
319
320 return 0;
321 }
322
323 //定义显示内存情况
324 void DisplayMemory(int Memory[255]){
325 int i = 0;
326 cout<<"\n\tMemory Status As Followings:";
327 cout<<endl<<"\t";
328 while(i<255){
329 cout<<Memory[i]<<" ";
330 i++;
331 if(i % 20 == 0){
332 cout<<endl<<"\t";
333 }
334 }
335 cout<<endl<<endl;
336 }

转载于:https://www.cnblogs.com/dongzhi0000/archive/2011/12/01/2271067.html

通过《操作系统》课程实训,达到以下目的:(1)巩固和加深对操作系统(OS)原理的理解,初步掌握操作系统组成模块和应用接口的使用方法,提高进行工程设计和系统分析的能力;(2)通过相关课题的设计,锻炼学生解决复杂工程问题的能力;(3)通过选做相关的课题,提高学生查阅资料、相互交流沟通的能力,锻炼学生使用信息化工具的能力; 请求管理是一种常用的虚拟存储管理技术。本设计通过请求存储管理中页面置换算法模拟设计,了解虚拟存储技术的特点,掌握请求管理的页面置换算法。 (1)从置换算法中任选 2 种(OPT、 FIFO、LRU、Clock);(2)建立表;(3) 设计的输入数据要能体现算法的思想(4) 模拟缺中断过程;(5)求出各置换算法中的缺次数和置换次数以及依次被换出的号;(6)利用Java Swing进行图形化界面设计。 在此次实训过程中,我先是完成了FIFO、LRU、OPT、Clock四个算法实现,之后结合Java的Swing图形化界面,将算法融入到图形化界面中,并且可以进行序列长度和运行时间的初始化,紧接着,可以将序列和物理块进行随机生成序列,最后,在算法执行中,可以将缺中断过程显示在文本区域内,并且在文本区域内可以显示缺次数、置换次数、被换号的实时统计。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值