您的位置:首页 > 其它

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

2011-12-01 19:07 375 查看
本程序演示的是操作系统页式内存管理方法。

基本流程为:

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

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

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

要做记录。

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

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

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

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

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

// Menu.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream.h>
#include <string.h>

#define MemorySize 255

//函数声明

void DisplayMemory(int[255]);      //显示内存

//定义进程类
class Process{

private:
char name[20];
int size;
public:
int* status;
Process(char *n=" ", int s=0){
strcpy(name,n);
size = s;
}
~Process(){}
char* GetName(){
return name;
}
int GetSize(){
return size;
}
void Display(){
int i = 0;
cout<<"\n\t";
while(i<size){
cout<<name<<"["<<i<<"]:"<<status[i];
i++;
if(i % 20 == 0){
cout<<"\n\t";
}
}
}
};

//定义类 QNode
class QNode{
private:
int data;
QNode * next;
public:
QNode(){
next = NULL;
}

//声明本类为LinkQueue的友元类
//这样可以在LinkQueue类内使用QNode里的私有成员(数据和指向下一个节点的指针)
friend class LinkQueue;
};//QNode类定义结束

//定义类 LinkQueue
class LinkQueue {
private:
QNode * front;
QNode * rear;
int len;
public:
//构造函数
LinkQueue(){
front = new QNode();
rear = front;
len = 0;
}
//析构函数
~LinkQueue(){
while(front->next){
DeQueue();
}
}
//入队函数
void Enqueue(int v){
QNode *p = new QNode();

p->data = v;
p->next = NULL;
rear->next = p;
rear = p;
len++;
}
//是否为空,声明在出队函数之前
bool Empty(){
return front->next == NULL;
}
//获取当前队列长度
int Length(){
return len;
}
//出队函数
int DeQueue(){
if(Empty()){
cout<<"\nThe LinkQueue has been empty!"<<endl;
return 0;
}
QNode *p = front->next;
int v = p->data;
front->next = p->next;
if(rear == p)
rear = front;
delete p;
len--;
return v;
}
void Display(){
cout<<"The empty block as followings:"<<endl;
QNode *p = front->next;
int i = 0;
while(p){
cout<<"Empty["<<i<<"]:"<<p->data<<endl;
p = p->next;
i++;
}
}

};//LinkQueue定义结束

//循环队列  可用来存储进程对象,这是此程序待改进之处,
//本程序在撤销进程后,把其后面的进程号向前移动
class Queue {
private:
int base[10];
int front;
int rear;
int length;
public:
Queue(){
for(int i=0; i<10; i++){
base[i] = 0;
}
front = 0;
rear = 0;
length = 0;
}
int EnQueue(int v){
if(!Full()){
base[rear] = v;
length = (rear - front + 10)%10 + 1;
rear = (rear + 1) % 10;
return 1;
}
else
cout<<"Already FULL! EnQueue error!"<<endl;
return 0;
}
int DeQueue(){
if(!Empty()){
int v = base[front];
front = (front + 1) % 10;
length--;
return v;
}
else{
cout<<"Emtpy!"<<endl;
return -1;
}
}
bool Full(){
return length == 10;
}
bool Empty(){
return length == 0;
}
};

int main(int argc, char* argv[])
{
int P_MaxNum = 10;
int k = 1;
Process** PTC = new Process*[P_MaxNum]; // ProcessTableCollection
int Memory[255];           //定义内存最大容量255
for(int i=0; i<255; i++){  //格式化内存数据 清零表示空闲可用 置1时表示占用
Memory[i] = 0;
}

LinkQueue* lq = new LinkQueue();//空闲页面表,用链队列模拟,存放空闲页面
Queue * xhq = new Queue();//循环队列,存储当前所有进程,最大10个
//将空闲页面号存入空闲页面表
for(i=0; i<MemorySize; i++){
if(Memory[i] == 0){
lq->Enqueue(i);
}
}
bool flag = true;
while(flag){
cout<<"\t****************************************************"<<endl;
cout<<"\t*   The Demo showes the Page Memory Management Way *"<<endl;
cout<<"\t*                     (Author:Dong Zhi 2011-11-25) *"<<endl;
cout<<"\t*           The Main Menu:                         *"<<endl; //主菜单
cout<<"\t*                                                  *"<<endl;
cout<<"\t*         1.Create a process                       *"<<endl; //创建进程
cout<<"\t*         2.Withdraw a process                     *"<<endl; //撤销进程
cout<<"\t*         3.Explore the Memory status              *"<<endl; //查看内存使用清单(初始内存为255)
cout<<"\t*         4.Quit the Programe                      *"<<endl; //退出程序
cout<<"\t****************************************************"<<endl;
cout<<"\tPlease input a num(1/2/3/4):";                                 //输入选择
int a = 0;
char name[10];
int i;
int size;
int m = 0;
cin>>a;
switch(a){
case 1:
i = 0;   //统一迭代变量
size = 0;//进程所需页面数
//int* ProcessArray;//整型数组,存放当前进程页表

cout<<"\tlq->length:"<<lq->Length();
//lq->Display();//空余情况不必显示
cout<<"\n\tWe have been ready! Please input the process name:";
cin>>name;
cout<<"\n\tPlease input the space you(your process) need: ";
cin>>size;
//判断所需内存空间是否允许分配(查看剩余内存空间大小length并和size比较)
if(size < lq->Length()){
cout<<"\n\tCongratulations! We have enough space for you!"<<endl<<endl;
}else if(size == lq->Length()){
cout<<"\n\tCongratulations! We just left the space  you need!"<<endl<<endl;
}else{
cout<<"\n\tSorry! The space you need is bigger than wo have now! Please withdraw a process!"<<endl<<endl;
break;
}

PTC[k] =  new Process(name,size);
if(k > P_MaxNum){
cout<<"\n\tThe number of process have been the max value,please choose 3 to withdraw a Process!"<<endl;
break;
}
PTC[k]->status = new int[size];
//格式化页表
for(i=0; i<size; i++){
PTC[k]->status[i] = 0;
}
cout<<"\tThe Process Page Table has been ready!(Empty) Please press any key to coutinue!"<<endl<<endl;
//由上面安全判断后,可以为其分配内存了,
//将空闲队列按所需大小出队,并将内存标号写到页表,然后将相应内存标志置为占用(1),即修改空闲页面表,进程页表和页面存储表
for(i=0; i<size; i++){
int deQ = lq->DeQueue();
PTC[k]->status[i] = deQ;
for(int j=0; j<MemorySize; j++){
if(j == deQ){
Memory[deQ] = 1;
}
}
}
//lq->Display();剩余情况不必显示
cout<<"Distributed over! Press any key to have a look at the Process Table Status!"<<endl;
getchar();
//再次显示当前内存分配情况
//DisplayMemory(Memory);

//显示当前进程页表
cout<<"The followings have showed the ProcessTable Status:"<<endl;
for(i=0; i<size; i++){
cout<<PTC[k]->status[i]<<" ";
}
cout<<endl<<endl;
k++;
break;
case 2:
char c[10];
if((k-1)<1){
cout<<"\tNo process has been created!"<<endl;
break;
}
for(i=1; i<k; i++){
cout<<"\t"<<"PTC["<<i<<"]:"<<PTC[i]->GetName()<<endl;
}

loop:cout<<"\tplease input the name of the process)(input 0 return):";
cin>>c;
if(strcmp(c,"0") == 0){break;}

for(i=1; i<k; i++){
if(strcmp(PTC[i]->GetName(),c) == 0){
m = i;
}
}
//cout<<m;
if(m == 0){
cout<<"\tThe process doesn't exist!"<<endl;
goto loop;
}

for(i=0; i<PTC[m]->GetSize(); i++){
int n = PTC[m]->status[i];
Memory
= 0;
lq->Enqueue(n);
}
for(i=m;i<=k-1;i++){
PTC[i] = PTC[i+1];
}
PTC[k-1] = NULL;
cout<<"\n\tOK,The free space is :"<<lq->Length()<<endl;
k--;
break;
case 3:
DisplayMemory(Memory);
break;
case 4:
flag = false;
break;
default:
cout<<endl<<"\tPlease choose again! Press any key to continue!"<<endl;
}
}

return 0;
}

//定义显示内存情况
void DisplayMemory(int Memory[255]){
int i = 0;
cout<<"\n\tMemory Status As Followings:";
cout<<endl<<"\t";
while(i<255){
cout<<Memory[i]<<" ";
i++;
if(i % 20 == 0){
cout<<endl<<"\t";
}
}
cout<<endl<<endl;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: