资源简介
本代码是操作系统大作业,是对操作系统内存管理的仿真,主要模拟操作系统为各个进程分配和回收内存的机制
代码片段和文件信息
#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;
scanf(“%d“ &msize);
MemoryManager(msize);
char line[100];
while (gets(line))
{
char oper;
int pid size;
sscanf(line “%c“ &oper)
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 3351 2018-10-27 13:30 compact-output.txt
文件 3266 2018-10-29 21:15 compact.c
文件 3191 2018-10-29 21:19 compact.txt
文件 3109 2018-10-29 19:26 firstfit-output.txt
文件 2915 2018-10-29 21:18 firstfit.c
文件 2993 2018-10-29 21:18 firstfit.txt
文件 2238 2018-10-29 21:15 holes.h
文件 41181 2018-10-27 13:30 hw5.pdf
文件 161 2018-10-27 13:30 input.txt
文件 164 2018-10-27 13:30 Makefile
文件 1729 2018-10-29 20:52 process.h
文件 140 2018-10-29 21:28 readme.txt
评论
共有 条评论