转自:在分页式管理方式下采用位示图来表示主存分配情况,实现主存空间的分配和回收_呱呱乐编程-CSDN博客
方便学习,如有侵权,联系本人,立即删除!
#include <iostream>
#include <stdio.h>
#include <String.h>
#include<stdlib.h>
using namespace std;
struct BitMap //用于存储位示图
{
int map[8][8]; //8*8=64,位示图
int free; //剩余的空闲块数
}bitmap;
typedef struct process //用于存储作业
{
int num; //作业序号
int size; //作业大小
int *pagetable; //页表
struct process *pre; //上一作业
struct process *next; //下一作业
}process;
void SetProcess(process *head) //创立作业,生成页表
{
int setnum;
int k=0,finish=0;
cout<<endl<<"请输入作业序号(数字且不为0):";
cin>>setnum;
process *temp1 = new process();
if(setnum == 0)
{
cout<<endl<<"错误!不能创建0号作业,返回。";
delete temp1;
return;
}
//判断此作业是否已经存在
for(temp1=head; temp1 != NULL; temp1 = temp1->next){
if(temp1->num==setnum){
cout << "已有该作业,不能再创建!" <<endl;
return ;
}
}
delete temp1;
process *temp = new process();
temp->num = setnum;
cout<<endl<<"请输入作业大小(<=64):";
cin>>temp->size;
if(temp->size > bitmap.free) //判断现有空间是否足够分配
{
cout<<endl<<"当前内存没有足够的空间分配,返回。";
delete temp;
return;
}
temp->next = head->next; //头插法插入新作业
if(head->next != NULL)
head->next->pre = temp;
temp->pre = head;
head->next = temp;
temp->pagetable = new int [temp->size]; //创立页表数组
for(int i=0; i<8 && finish == 0; i++)
{
for(int j=0; j<8 && finish == 0; j++)
{
if(bitmap.map[i][j] == 0) //修改页表数据
{
bitmap.map[i][j] = 1;
bitmap.free --;
temp->pagetable[k] = 8*i + j;
k++;
}
if(k == temp->size) //用于结束循环
finish = 1;
}
}
cout<<endl<<"已装入作业,页表如下:"<<endl<<"页号\t"<<"块号\t"<<endl; //输出页表
k = temp->size;
for(int i=0; i<k; i++)
{
cout<<i+1<<'\t'<<temp->pagetable[i]<<endl;
}
}
void RecProcess(process *head) //回收作业,退还内存
{
int recnum;
int found=0;
process *temp = new process();
cout<<endl<<"请输入回收作业序号(数字且不为0):";
cin>>recnum;
if(recnum == 0)
{
cout<<endl<<"错误!不能删除0号作业,返回。";
return;
}
for(temp=head; temp != NULL; temp = temp->next) //找到进程
{
if(temp->num == recnum)
{
found = 1;
int k = temp->size;
for(int l=0; l<k; l++) //删除数据
{
int i = temp->pagetable[l] / 8;
int j = temp->pagetable[l] % 8;
bitmap.map[i][j] = 0;
bitmap.free ++;
}
if(temp->pre != NULL) //不允许删除头结点
{
if(temp->next != NULL) //不是尾结点
{
temp->pre->next = temp->next;
temp->next->pre = temp->pre;
delete temp;
}
else //是尾结点
{
temp->pre->next = NULL;
delete temp;
}
}
cout<<endl<<"已找到并回收序号为"<<recnum<<"的作业。";
cout<<endl<<"该作业大小为"<<k<<"。"<<endl;
break;
}
}
if(found == 0) //未找到进程
{
cout<<endl<<"未找到该作业,返回。";
return;
}
cout<<endl<<"完成回收,返回。";
return;
}
void ShowBitMap() //展示位示图
{
cout<<endl<<"当前位示图状态如下:"<<endl;
cout<<'\t'<<"0\t"<<"1\t"<<"2\t"<<"3\t"<<"4\t"<<"5\t"<<"6\t"<<"7\t";
cout<<endl<<"****";
for(int i=0; i<8; i++)
{
cout<<endl<<i<<'\t';
for(int j=0; j<8; j++)
{
cout<<bitmap.map[i][j]<<'\t';
}
}
}
void ShowPageTable(process *head) //展示页表
{
int shownum;
int found=0;
process *temp = new process();
cout<<endl<<"请输入查看作业序号(数字且不为0):";
cin>>shownum;
if(shownum == 0)
{
cout<<endl<<"错误!不能查看0号作业,返回。";
return;
}
for(temp=head; temp != NULL; temp = temp->next) //找到进程
{
if(temp->num == shownum)
{
found = 1;
cout<<endl<<"序号为"<<shownum<<"的作业页表如下:"<<endl<<"页号\t"<<"块号"<<endl; //输出页表
int k = temp->size;
for(int i=0; i<k; i++)
{
cout<<i+1<<'\t'<<temp->pagetable[i]<<'\t'<<endl;
}
cout<<endl;
cout<<endl<<"已找到并展示序号为"<<shownum<<"的作业页表。"<<"该作业大小为"<<k<<"。"<<endl;
continue;
}
}
if(!found) //未找到进程
{
cout<<endl<<"未找到该作业,返回。";
return;
}
}
int main()
{
for(int i=0; i<8; i++){
for(int j=0; j<8; j++){
bitmap.map[i][j] = 0;
}
}
bitmap.map[0][0] = bitmap.map[0][1] = bitmap.map[0][4] = bitmap.map[0][5] = bitmap.map[0][6] = bitmap.map[1][1] = bitmap.map[1][3] = bitmap.map[1][5] = bitmap.map[3][0] = bitmap.map[3][7] = 1;
bitmap.free = 64;
process *head = new process();
head->next = NULL;
head->pre = NULL;
head->num = 0;
head->size = 6;
head->pagetable = new int [head->size]; //创立页表数组
head->pagetable[0] = 0;
head->pagetable[1] = 1;
head->pagetable[2] = 9;
head->pagetable[3] = 11;
head->pagetable[4] = 13;
head->pagetable[5] = 24;
process *proce1 = new process();
head->next = proce1;
proce1->pre = head;
proce1->next = NULL;
proce1->num = 1;
proce1->size = 4;
proce1->pagetable = new int [proce1->size]; //创立页表数组
proce1->pagetable[0] = 4;
proce1->pagetable[1] = 5;
proce1->pagetable[2] = 6;
proce1->pagetable[3] = 31;
char choice;
do
{
cout<<endl<<"-----------操作列表-----------";
cout<<endl<<"------------------------------";
cout<<endl<<"0.退出程序;";
cout<<endl<<"1.创建新作业,并请求内存空间;";
cout<<endl<<"2.回收作业,并返回内存;";
cout<<endl<<"3.展示位示图;";
cout<<endl<<"4.展示进程页表;";
cout<<endl<<"请输入你选择功能对应的序号:";
cin>>choice;
switch(choice)
{
case '0': return 0;
case '1': SetProcess(head); break;
case '2': RecProcess(head); break;
case '3': ShowBitMap(); break;
case '4': ShowPageTable(head); break;
case '5': system("close"); break;
default:
cout<<endl<<"输入错误!"<<endl;
}
}while(choice != '0');
return 0;
}