《实习主存储器空间的分配和回收》由会员分享,可在线阅读,更多相关《实习主存储器空间的分配和回收(12页珍藏版)》请在金锄头文库上搜索。
1、实习四实习四主存储器空间的分配和回收主存储器空间的分配和回收一一 实验题目实验题目 (1).在可变分区管理方式下采用最先适应算法实现主存分配和实现主存回收。二二 程序中使用的数据结构及符号说明程序中使用的数据结构及符号说明 (1). 应用的数据结构为链表。 符号说明: 结构体 MemoryTable 为定义作业块, startPoint 为指向申请 128K 空间 的首地址,_name20为欲装载作业名,_Length 为欲装载作业所需空间,其余符 号在代码注释中提及,此处不做介绍。三三 流程图流程图。 (1).四四 程序源码程序源码 (1)./ System_Option.cpp : Def
2、ines the entry point for the console application. /#include #include #include #include typedef struct MemoryTable/作业快 int flag;/标志位 int Memory;/起始地址 int Length;/长度 char name20;/作业名 MemoryTable * next;/指针 *memoryTable;void LoadMemory(MemoryTable * Head, char * _name, int _Length);/作业装载 void ReMoveMem
3、ory(MemoryTable * Head, char * _name);/作业卸载 void Print(MemoryTable * Head);/打印函数int main(int argc, char* argv)/主函数 int i = 0; char _name20 = 0; int _Length = 0; void * startPoint = malloc(131072);/申请 128K 内存空间MemoryTable * L = (memoryTable)malloc(sizeof(MemoryTable); MemoryTable * Head = (memoryTabl
4、e)malloc(sizeof(MemoryTable); memoryTable fore = (memoryTable)malloc(sizeof(MemoryTable); Head = L; for(i = 0; i next = p; fore = L; strcpy(p-name, “操作系统“); p-Memory = (int)startPoint; p-Length = (int)pow(2, 10) * 5; p-flag = 1; break; case 1: L-next = p; fore = L; strcpy(p-name, “作业一“); p-Memory =
5、fore-Memory + fore-Length; p-Length = (int)pow(2, 10) * 5;p-flag = 1; break; case 2: L-next = p; fore = L; strcpy(p-name, “作业三“); p-Memory = fore-Memory + fore-Length; p-Length = (int)pow(2, 10) * 4; p-flag = 1; break; case 3: L-next = p; fore = L; strcpy(p-name, “空闲区“); p-Memory = fore-Memory + for
6、e-Length; p-Length = (int)pow(2, 10) * 12; p-flag = 0; break; case 4: L-next = p; fore = L; strcpy(p-name, “作业二“); p-Memory = fore-Memory + fore-Length; p-Length = (int)pow(2, 10) * 6; p-flag = 1; break; case 5: L-next = p; fore = L; strcpy(p-name, “空闲区“); p-Memory = fore-Memory + fore-Length; p-Len
7、gth = (int)pow(2, 10) * 96; p-flag = 0; break; case 6: L-next = NULL; fore = L; break; default: ; L = p; /*装载作业*/ Print(Head); printf(“请输入你欲装载的作业名称,以及作业大小(单位为 k)n 请输入作业名 : “); scanf(“%s“, _name); printf(“请输入作业大小 : “); scanf(“%d“, LoadMemory(Head, _name, _Length); Print(Head);/*卸载作业*/ for(int n = 0;
8、n next; while(s != NULL) t = s; s = s-next; free(t); free(startPoint); /*/ return 0; void Print(MemoryTable * Head)/打印函数 memoryTable s; s = Head-next; while(s != NULL) printf(“内存起始地址%0X, 内存大小%3dK, 内存状态%2d, 内存块名: %sn“, s-Memory, s-Length / 1024, s-flag, s-name); s = s-next; void LoadMemory(MemoryTabl
9、e * Head, char * _name, int _Length)/装载作业 int surplusMemory = 0; memoryTable p = Head-next; while(p != NULL) if(p-flag = 0 surplusMemory = p-Length - _Length * 1024; p-Memory = p-Memory; p-Length = _Length * 1024; strcpy(p-name, _name); if(surplusMemory 0) memoryTable t = (memoryTable)malloc(sizeof(
10、MemoryTable); t-flag = 0; strcpy(t-name, “空闲区“); t-Memory = p-Memory + p-Memory; t-Length = surplusMemory; t-next = p-next; p-next = t; return ; p = p-next; return ; void ReMoveMemory(MemoryTable * Head, char * _name)/卸载作业 MemoryTable * p = Head-next; MemoryTable * fore = Head; MemoryTable * t; whil
11、e(p != NULL) if(strcmp(p-name, _name) = 0)/检索作业 if(fore-flag = 0 fore-Length = fore-Length + p-Length + p-next-Length; fore-next = p-next-next; free(t-next); free(t); else if(fore-flag = 0) t = p; fore-Length = fore-Length + p-Length; fore-next = p-next; free(t); else if(p-next-flag = 0) t = p; p-Le
12、ngth = p-Length + p-next-flag; p-flag = 0; strcpy(p-name, “空闲区“); p-next = p-next-next; free(t-next); else p-flag = 0; strcpy(p-name, “空闲区“); return ; fore = p; p = p-next; return ; 五五 运行截图运行截图 (1).一一 实验题目实验题目 (2)在在分页式管理方式下采用位示图来表示主存分配情况,实现主存空间的分配和 回收。二二 程序中使用的数据结构及符号说明程序中使用的数据结构及符号说明 (2). 应用的数据结构为链
13、表。 符号说明: 结构体 Table 为定义页表, 结构体 TableLink 为作业块表, startMemory 为指向申请 512B 空间的首地址,_name20为欲装载作业名,capactiy 为欲装载 作业所需空间,其余符号在代码注释中提及,此处不做介绍。三三 流程图流程图。 (2). 附(两作业流程图思路相似)四四 程序源码程序源码 (2) / System_Option_2.cpp : Defines the entry point for the console application. /#include “stdafx.h“ #include #include #inclu
14、de typedef struct Table/定义页表 int Page;/页编号 int Piece;/块编号 struct Table * next;/指针 * table;typedef struct TableLink/定义作业块表 table Element;/页表 char WorkName20;/装载作业名 TableLink * next;/指针 * tablelink;int List64 = 0;/位示图void IniList(int * List, tablelink L_head);/初始化位示图 void PrintList(int * List);/打印位示图
15、void ApplyMemory(tablelink L_head, char * _name, int _ApplyMemorny);/申请作业空间 void RemoveMemory(tablelink L_head, char * _name);/卸载作业int main(int argc, char* argv)/主函数 char _name20; int capactiy; tablelink L_head = (tablelink)malloc(sizeof(TableLink); L_head-next = NULL;void * startMemory = malloc(512);/申请 512B 内存 int FirstAddress = (int)startMemory;IniList(List,