资源简介
memory_management.7z
代码片段和文件信息
#include
#include “process.h“
#include “holes.h“
#define MAX_PROCESS 100
int holes_size;
hole_node *holes;
process_array *pa;
int memorysize;
int allocate_byte;
int free_byte;
void MemoryManager(int bytes)
{ // initialize memory with these many bytes.
memorysize = bytes;
free_byte = bytes;
allocate_byte = 0;
holes = init_hole(0bytes);
pa = init_process(MAX_PROCESS);
holes_size = 1;
}
int allocate(int bytes int pid)
{ // allocate these many bytes to the process with this id
// assume that each pid is unique to a process
// return 1 if successful
// return -1 if unsuccessful; print an error indicating
// whether there wasn’t sufficient memory or whether
// there you ran into external fragmentation
if (bytes <= 0)
{
printf(“Process size should be positive.\n“);
return -1;
}
if (bytes > memorysize - allocate_byte) {
printf(“Not enough memory space for process %d.\n“ pid);
return -1;
}
/*search first hole which size >= bytes*/
hole_node *s = search_hole(holes bytes);
if (s)
{//if have a hole to add process
process p;
p.start = s->data.start;
p.size = bytes;
p.ID = pid;
if (-1 == add_process(pa p)) {
printf(“Process with same pid(%d) already exists.\n“ p.ID);
return -1;
}
/*change hole after add process*/
allocate_byte += bytes;
change_hole(s bytes);
}
else
{
printf(“Memory defragmentation.\n“);
/*creating one hole at the high region of memory*/
destory_hole(holes);
holes = init_hole(allocate_bytememorysize-allocate_byte);
holes_size = 1;
/*compact processes to the start of main memory*/
resort_process(pa);
allocate(bytes pid);
}
}
int deallocate(int pid)
{ //deallocate memory allocated to this process
// return 1 if successful -1 otherwise with an error message
process p = del_process(pa pid);
if (p.ID != -1)
{// delete successfully
hole h;
h.start = p.start;
h.size = p.size;
allocate_byte -= p.size;
holes_size += add_hole(holes h);
return 1;
}
else
{
printf(“Process with pid(%d) does not exist.\n“ pid);
return -1;
}
}
void printMemoryState()
{ // print out current state of memory
printf(“Memory size = %d bytes allocated bytes = %d free = %d\n“ memorysize allocate_byte memorysize - allocate_byte);
printf(“There‘re currently %d holes and %d processes:\n“ holes_size pa->size);
printf(“Hole list:\n“);
int j = 1;
for (hole_node *i = holes->next; i; i = i->next ++j)
{
printf(“hole %d: start location=%d size=%d\n“ j i->data.start i->data.size);
}
printf(“\nProcess list:\n“);
for (int i = 0; i < pa->size; ++i)
{
printf(“process %d: id=%d start location=%d size=%d\n“ i + 1 pa->p[i].ID pa->p[i].start pa->p[i].size);
}
}
int main()
{
int msize;
FILE *fin = fopen(“input.txt“ “r“);
if (!fin)
{
printf(“file not found/n“);
return -1;
}
fscanf(fin “%d“ &msize);
MemoryManager(msi
- 上一篇:字符串解密大作业
- 下一篇:五子棋源码加详细注释
相关资源
- PID_AutoTune_v0.rar
- vspd7.2.308.zip
- 价值2k的H漫画小说系统
- Pythonamp;课堂amp;笔记(高淇amp;400;集第
- ddos压力测试工具99657
- UML建模大全
- 开源1A锂电池充电板TP4056原理图+PCB
- m1卡 ic卡可选择扇区初始化加密软件
- TSCC.exe
- FTP课程设计(服务端+客户端)
- 计算机图形学 边填充算法实现代码
- 电力系统潮流计算程序集合
- oracle数据迁移项目实施方案
- Web Api 通过文件流 文件到本地
- Visio图标-最新最全的网络通信图标库
- Spire API文档
- OpenGL参考手册
- Python中Numpy库最新教程
- SPD博士V5.3.exe
- 直流无刷电机方波驱动 stm32 例程代码
- layui后台管理模板
- 仿知乎界面小程序源代码
- 云平台-阿里云详细介绍
- photoshop经典1000例
- scratch垃圾分类源码(最终版本).sb
- IAR ARM 7.8破解
- TI CCS V5.4 安装步骤及破解文件
- 松下plc FP-XH的驱动
- 局域网硬件信息收集工具
- 加快Windows XP操作系统开机速度
评论
共有 条评论