(1)本次实验设计了三种方法实现内存的分配-最先适应法、最佳适应法、最坏适应法
(2)能够输入给定的内存大小、进程个数,每个进程所需的内存空间大小。
(3)能够进行分配操作,并能显示完成内存分配后内存空间的使用情况。
(4)能够显示进程在内存的存储位置、大小等。
废话不多说,代码如下:
#include<iostream>
#include <fstream>
#include <iomanip>
#include <stdlib.h>
using namespace std;
typedef struct {
int pSize;
int id;
}sortResult;
#define MAXNUMBER 100
static int PNum; //内存中空闲分区的个数
static int FreeProcess[MAXNUMBER]; //空闲分区对应的内存
static int choice;//算法选择
static int SNum; //进程个数
static char SName[MAXNUMBER];//进程名字
static int SNeed[MAXNUMBER]; //进程大小
static char SWhere[MAXNUMBER][MAXNUMBER];//各个进程的分区位置
static int LeftFreeProcess[MAXNUMBER];//分区的剩余大小
static int LeftSNeed[MAXNUMBER];//进程剩余的大小
void input();//初始输入
void SelectSolution();
void initial();//初始化
void display();//展示
void firstAdapt();//最先适应法
void bestAdapt();//最佳适应法
void worstAdapt();//最坏适应法
int main() {
input();
SelectSolution();
return 0;
}
//输入分区数和大小、资源数和大小
void input() {
int i;
ifstream inData;
inData.open("dataInput.txt");
//读取数据
inData >> PNum;//分区个数
for (i = 0; i < PNum; i++) {//对应的内存
inData >> FreeProcess[i];
}
inData >> SNum;//进程个数
for (i = 0; i < SNum; i++) {//初始化名称
SName[i] = i + 65;
}
for (i = 0; i < SNum; i++) {
inData >> SNeed[i];//进程大小
}
cout << "进程名称: " << "\t\t";
for (i = 0; i < SNum; i++) {
cout << SName[i] << "\t";
}
cout << endl;
cout << "进程大小: " << "\t\t";
for (i = 0; i < SNum; i++) {
cout << SNeed[i] << "\t";
}
cout << endl;
cout << "分区名称: " << "\t\t";
for (i = 0; i < PNum; i++) {
cout << "P" << i + 1 << "\t";
}
cout << endl << "内存大小: " << "\t\t";
for (i = 0; i < PNum; i++) {
cout << FreeProcess[i] << "\t";
}
}
void SelectSolution() {
cout <<endl<< "请选择你需要的算法"<<endl;
cin >> choice;
if (choice == 1) {
firstAdapt();
SelectSolution();
}
else if (choice == 2) {
bestAdapt();
SelectSolution();
}
else if (choice == 3) {
worstAdapt();
SelectSolution();
}
else {
cout << "输入格式错误,请重新输入";
SelectSolution();
}
}
void initial() {
int i, j;
for (int i = 0; i < SNum; i++) {
for (int j = 0; j< PNum; j++) {
SWhere[i][j] = NULL;//初始化进程的分区位置
LeftFreeProcess[j] = FreeProcess[j];//初始化剩余内存分区的大小
}
}
for (i = 0; i < SNum; i++) {
LeftSNeed[i] = SNeed[i];//初始化进程的剩余的大小
}
}
void firstAdapt() {
cout << "最先适应法"<<endl;
initial();
int i, j;
for (i = 0; i < SNum; i++) {
for (j = 0; j < PNum; j++) {
if (LeftSNeed[i]<=LeftFreeProcess[j]&& LeftFreeProcess[j]!=0) {
int startPosition=0;//起始位置
for (int p = 0; p < j; p++) {
startPosition += FreeProcess[p];
}
if (LeftFreeProcess[j] < FreeProcess[j]) {
startPosition += (FreeProcess[j] - LeftFreeProcess[j]);
}
cout << SName[i] << "的起始位置是:" << startPosition<<endl;
LeftFreeProcess[j] -= LeftSNeed[i];
LeftSNeed[i] = 0;
SWhere[i][j] = SName[i];//指定进程的位置
break;
}
}
}
display();
}
void bestAdapt() {
cout << "最佳适应法"<<endl;
initial();
int i, j,s,t;
sortResult bestsort[MAXNUMBER];//用来存取每一个内存区的大小
sortResult temp;//用来作为交换中介
for (i = 0; i < PNum; i++) {
bestsort[i].pSize = LeftFreeProcess[i];
bestsort[i].id = i;
}
for (i = 0; i < SNum; i++) {
//每一次寻找都是找最接近该进程大小的内存,因此每次循环都得将内存空间进行排序
//冒泡排序
for (j = 0; j < PNum-1; j++) {
for (t = j; t < PNum; t++) {
if (bestsort[j].pSize >= bestsort[t].pSize) {
temp = bestsort[j];
bestsort[j] = bestsort[t];
bestsort[t] = temp;
}
}
}
for (s = 0; s < PNum; s++) {
if (LeftSNeed[i] <= bestsort[s].pSize) {
int startPosition = 0;
for (int p = 0; p < bestsort[s].id; p++) {
startPosition += FreeProcess[p];
}
if (LeftFreeProcess[bestsort[s].id] < FreeProcess[bestsort[s].id]) {
startPosition += (FreeProcess[bestsort[s].id] - LeftFreeProcess[bestsort[s].id]);
}
cout << SName[i] << "的起始位置是:" << startPosition << endl;
bestsort[s].pSize -= LeftSNeed[i];
LeftSNeed[i] = 0;
SWhere[i][bestsort[s].id] = SName[i];
break;
}
}
LeftFreeProcess[bestsort[s].id] = bestsort[s].pSize;
}
display();
}
void worstAdapt() {
cout << "最坏适应法"<<endl;
initial();
int i, j, s, t;
sortResult worstsort[MAXNUMBER];//用来存取每一个内存区的大小
sortResult temp;//用来作为交换中介
for (i = 0; i < PNum; i++) {
worstsort[i].pSize = LeftFreeProcess[i];
worstsort[i].id = i;
}
for (i = 0; i < SNum; i++) {
//每一次寻找都是找最接近该进程大小的内存,因此每次循环都得将内存空间进行排序
//冒泡排序
for (j = 0; j < PNum - 1; j++) {
for (t = j; t < PNum; t++) {
if (worstsort[j].pSize <= worstsort[t].pSize) {
temp = worstsort[j];
worstsort[j] = worstsort[t];
worstsort[t] = temp;
}
}
}
for (s = 0; s < PNum; s++) {
if (LeftSNeed[i] <= worstsort[s].pSize) {
int startPosition = 0;
for (int p = 0; p < worstsort[s].id; p++) {
startPosition += FreeProcess[p];
}
if (LeftFreeProcess[worstsort[s].id] < FreeProcess[worstsort[s].id]) {
startPosition += (FreeProcess[worstsort[s].id] - LeftFreeProcess[worstsort[s].id]);
}
cout << SName[i] << "的起始位置是:" << startPosition << endl;
worstsort[s].pSize -= LeftSNeed[i];
LeftSNeed[i] = 0;
SWhere[i][worstsort[s].id] = SName[i];
break;
}
}
if (s < PNum) {
LeftFreeProcess[worstsort[s].id] = worstsort[s].pSize;
}
}
display();
}
//显示分区结果
void display() {
int i;
cout << endl;
cout << "进程名称: " << "\t\t";
for (i = 0; i < SNum; i++) {
cout << SName[i] << "\t";
}
cout << endl;
cout << "进程大小: " << "\t\t";
for (i = 0; i < SNum; i++) {
cout << SNeed[i] << "\t";
}
cout << endl;
cout << "分区名称: " << "\t\t";
for (i = 0; i < PNum; i++) {
cout << "P" << i + 1 << "\t";
}
cout << endl << "内存大小: " << "\t\t";
for (i = 0; i < PNum; i++) {
cout << FreeProcess[i] << "\t";
}
cout << endl << "分区剩余内存大小: " << "\t";
for (i = 0; i < PNum; i++) {
cout << LeftFreeProcess[i] << "\t";
}
cout << endl << "分区结果:" << endl;
/*for (i = 0; i < PNum; i++) {
for (int j = 0; j < SNum; j++) {
if (SWhere[j][i] != NULL) {
cout << SWhere[j][i] << ": P" << i + 1 << endl;
}
}
}*/
for (i = 0; i < SNum; i++) {
bool find = false;
for (int j = 0; j < PNum; j++) {
if (SWhere[i][j] != NULL) {
find = true;
cout << SWhere[i][j] << ":P" << j + 1 << endl;
}
}
if (!find) {
cout << SName[i] << ":没有合适的内存P?";
}
}
}
效果如下:
看完记得点个赞噢!