资源简介
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
- 上一篇:字符串解密大作业
- 下一篇:五子棋源码加详细注释
相关资源
- 基于Multisim10的电子摇号器设计与仿真
- hathitrust.zip
- 说明.txt
- 电力电子课程设计_secret.doc
- C.txt
- officescan.rar
- portal10.5.ecp
- Qimage.rar
- samp2_3.rar
- DropDataControl.rar
- 20.rar
- monihid.c
- sound.properties
- iso.txt
- MaskWindow.zip
- VS2010中文旗舰版(破解版).txt
- probability-sheldon-ross-solution-manual.pdf
- sc.zip
- cn4020026_2422865.zip
- cklover1_10516440.zip
- CorelProductsKeyGen20180618全家桶注册机.
- 编译原理.rar
- 题库脚本.xls
- waidps-master.zip
- QQ消息轰炸机Delphi版,听说效果很好
- elk百度云地址.txt
- GetAlibaba_Setup.rar
- Editor-1.5.4.rar
- y8l7mn.xls
- AdobeAcrobat破解方法.txt
评论
共有 条评论