资源简介
借用外存空闲分区管理中位示图的方法来表示主存分配情况,实现主存空间的分配和回收。
代码片段和文件信息
#include
#include
#include
#include
using namespace std;
struct bitGraph{
int a[8][8];
int freebit;
}BG;
struct Process{
char name[10];
int *pagetable;
int size;
Process *pre;
Process *nxt;
}*head;;
int Show(char *name)
{
int flag = 0;
Process *p;
for(p = head;p != NULL;p = p->nxt)
{
if(strcmp(p->name name)==0)
{
cout << endl;
cout << “-----------------“ << endl;
cout << “ 进程名 “ << endl;
cout << “-----------------“ << endl;
cout << “ 页号 块号“ << endl;
cout << “- - - - - - - - -“ << endl;
for(int i = 0; i < p->size; i ++){
cout << “ “ << i << “ “ << p->pagetable[i] << endl;
flag = 1;
}
cout << “-----------------“ << endl;
}
}
if(flag == 0)
cout << “没有该进程!“ << endl;
}
int getMalloc(){
int k = 0 flag = 0;
Process *process;
process = (Process *)malloc(sizeof(Process));
process->nxt = head->nxt;
if(head->nxt != NULL)
head->nxt->pre = process;
head->nxt = process;
process->pre = head;
cout << “请输入进程名:“;
cin >> process->name;
cout << “请输入进程所需内存大小:“;
cin >> process->size;
if(process->size > BG.freebit){
cout << “对不起,空闲空间不足以分配如此多内存给该进程。“ << endl;
return 0;
}
else{
process->pagetable = new int [process->size];
for(int i= 0; i < 8 && flag == 0; i ++)
for(int j = 0; j < 8 && flag == 0; j ++){
if(!BG.a[i][j]){
BG.a[i][j] = 1;
BG.freebit--;
process->pagetable[k] = 8 * i + j;
k ++;
if(k == process->size)
flag = 1;
}
}
}
cout << endl << “位示图:“ << endl;
for(int i = 0; i < 8; i ++)
{
for(int j = 0; j < 8; j ++)
cout << BG.a[i][j] << “ “;
cout << endl;
}
cout << endl;
cout << “当前空闲快数: “ << BG.freebit << endl;
Show(process->name);
}
int Free()
{
char name[10];
int flag = 0;
Process *p;
cout << “请输入需要回收的进程名:“;
cin >> name;
for(p = head;p != NULL; p = p->nxt)
{
if(strcmp(p->name name) == 0){
flag = 1;
for(int i = 0; i < p->size; i ++){
int m = p->pagetable[i] / 8;
int n = p->pagetable[i] % 8;
BG.a[m][n] = 0;
BG.freebit ++;
}
if(p->pre != NULL){
- 上一篇:天津理工C++实验报告齐全
- 下一篇:小区水电费管理系统
评论
共有 条评论