计算机操作系统 内存动态分配和回收模拟实现

计算机操作系统 内存动态分配和回收模拟实现


#include <iomanip>
#include <iostream>
using namespace std;

//空闲分区20个,已分配分区20个
struct empty//空闲分区表
{
int address_of_start; //起始地址
int size_of_empty; //空闲大小
int next;
int yesorno; //是否空闲,1 空闲,有效
} Empty[20];

struct not_empty//已分配分区表
{
int pid;
int address_of_start;
int size_of_notempty;
int next;
int yesorno; //是否分配,1 分配 有效
} Not_Empty[20];
int CountOfEmpty=1,CountOfNotEmpty=0; //从 0 开始计数
int Min_Size = 2; //不可分割的最小分区
int Max_Size = 300; //最大空间300 X 300 = 90000
int Num_Of_Row = 30; //显示时,横排个数
int Num_Of_Area = 20; //空闲分区 和 分配分区 的最大分区数

int enough(int Size_Of_NotEmpty) //是否有足够的空闲空间
{
int i;
for (i=0;i<CountOfEmpty;i++) //首次适应
{
if (Empty[i].size_of_empty >= Size_Of_NotEmpty + Min_Size)
return i;
}
return -1;
}

int where(int num ,int a[]) //找到起始地址为num的区域的下标,a0 = -1,则在Not_Empty中,下标为a1;a1 = -1 在Empty中,下标为a0
{
int i=0;
while (i < Num_Of_Area)
{
if (Empty[i].address_of_start == num && Empty[i].yesorno == 1)
{
a[0] = i;
a[1] = -1;
return 0;
}
if (Not_Empty[i].address_of_start == num && Not_Empty[i].yesorno == 1)
{
a[0] = -1;
a[1] = i;
return 0;
}
i += 1;
}
return -1;
}

int fenpei()
{
int temp,i;
CountOfNotEmpty += 1;
Not_Empty[CountOfNotEmpty].yesorno = 0;
for (i=0;i<CountOfNotEmpty;i++)
if (Not_Empty[i].yesorno == 0)
{
cout<<"输入进程ID (0 ~ 99),占用空间大小(0 ~ "<<Max_Size<<")\n";
cin>>Not_Empty[i].pid>>Not_Empty[i].size_of_notempty;
if (Not_Empty[i].pid <0 || Not_Empty[i].pid > 99)
{
CountOfNotEmpty -= 1;
cout<<"进程号 0 ~ 99\n";
return -1;
}
for (int ii=0;ii<CountOfNotEmpty;ii++)
{
if (Not_Empty[i].pid == Not_Empty[ii].pid && i!= ii)
{
CountOfNotEmpty -= 1;
cout<<"进程号必须唯一\n";
return -1;
}

你可能喜欢

  • 内存分配
  • 可变分区存储管理
  • 操作系统内存管理
  • 首次适应算法
  • 动态分区分配方式的模拟
  • windows操作系统
  • 操作系统存储管理实验
  • 分配报告

计算机操作系统 内存动态分配和回收模拟实现相关文档

最新文档

返回顶部