一个简单的内存管理程序
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#define PROCESS_NAME_LEN 32 /*进程名长度*/
#define MIN_SLICE 10 /*最小碎片的大小*/
#define DEFAULT_MEM_SIZE 1024 /*内存大小*/
#define DEFAULT_MEM_START 0 /*起始位置*/
/* 内存分配算法 */
#define MA_FF 1
#define MA_BF 2
#define MA_WF 3
int mem_size=DEFAULT_MEM_SIZE; /*内存大小*/
int ma_algorithm = MA_FF; /*当前分配算法*/
static int pid = 0; /*初始pid*/
int flag = 0; /*设置内存大小标志*/
/*描述每一个空闲块的数据结构*/
struct free_block_type{
int size;
int start_addr;
struct free_block_type *next;
};
/*指向内存中空闲块链表的首指针*/
struct free_block_type *free_block;
/*每个进程分配到的内存块的描述*/
struct allocated_block{
int pid;
int size;
int start_addr;
char process_name[PROCESS_NAME_LEN];
struct allocated_block *next;
};
/*进程分配内存块链表的首指针*/
struct allocated_block *allocated_block_head = NULL;
/*初始化空闲块,默认为一块,可以指定大小及起始地址*/
struct free_block_type* init_free_block(int mem_size){
struct free_block_type *fb;
struct allocated_block *p=allocated_block_head;
fb=(struct free_block_type *)malloc(sizeof(struct free_block_type));
if(fb==NULL){
printf("No memn");
return NULL;
}
fb->size = mem_size;
while(p)
p=p->next;
if(p==allocated_block_head)
fb->start_addr = mem_size;
else
fb->start_addr=mem_size-(p->size+p->start_addr);
// fb->start_addr = DEFAULT_MEM_START;
fb->next = NULL;
return fb;
}
display_menu();
int set_mem_size();
set_algorithm();
rearrange(int algorithm);
rearrange_FF();
rearrange_BF();
rearrange_WF();
new_process();
kill_process();
do_exit();
struct allocated_block *find_process(int pid);
int allocate_mem(struct allocated_block *ab);
int free_mem(struct allocated_block *ab);
int dispose(struct allocated_block *free_ab);
display_mem_usage();
display_menu(){
printf("n");
printf("1 - Set memory size (default=%d)n", DEFAULT_MEM_SIZE);
printf("2 - Select memory allocation algorithmn");
printf("3 - New process n");
printf("4 - Terminate a process n");
printf("5 - Display memory usage n");
printf("0 - Exitn");
}
/*设置内存的大小*/
int set_mem_size(){
int size;
if(flag!=0){ //防止重复设置
printf("Cannot set memory size againn");
return 0;
}
printf("Total memory size =");
scanf("%d", &size);
if(size>0) {
mem_size = size;
free_block->size = mem_size;
free_block->start_addr=mem_size;
flag=1;
return 1;
}
else
{
printf("the size is erro!n");
return 0;
}
}
/* 设置当前的分配算法 */
set_algorithm(){
int algorithm;
printf("t1 - First Fitn");
printf("t2 - Best Fit n");
printf("t3 - Worst Fit n");
scanf("%d", &algorithm);
if(algorithm>=1 && algorithm <=3)
ma_algorithm=algorithm;
//按指定算法重新排列空闲区链表
rearrange(ma_algorithm);
}
/*按指定的算法整理内存空闲块链表*/
rearrange(int algorithm){
switch(algorithm){
case MA_FF: rearrange_FF(); break;
case MA_BF: rearrange_BF(); break;
case MA_WF: rearrange_WF(); break;
}
}
/*按FF算法重新整理内存空闲块链表*/
rearrange_FF(){
int size;
int start_addr;
struct free_block_type *p, *q; //*free_block
p=q=free_block;
while(p)
{
while(q)
{
if(p->size>q->size)
{
size=p->size;
start_addr=p->start_addr;
p->size=q->size;
p->start_addr=q->start_addr;
q->size=size;
q->start_addr=start_addr;
}
q=q->next;
}
p=p->next;
}
//请自行补充
}
/*按BF算法重新整理内存空闲块链表*/
rearrange_BF(){
int size;
int start_addr;
struct free_block_type *p, *q; //*free_block
p=q=free_block;
while(p)
{
while(q)
{
if(p->size>q->size)
{
size=p->size;
start_addr=p->start_addr;
p->size=q->size;
p->start_addr=q->start_addr;
q->size=size;
q->start_addr=start_addr;
}
q=q->next;
}
p=p->next;
}
//请自行补充
}
/*按WF算法重新整理内存空闲块链表*/
rearrange_WF(){
int size;
int start_addr;
struct free_block_type *p, *q; //*free_block
p=q=free_block;
while(p)
{
while(q)
{
if(p->size<q->size)
{
size=p->size;
start_addr=p->start_addr;
p->size=q->size;
p->start_addr=q->start_addr;
q->size=size;
q->start_addr=start_addr;
}
q=q->next;
}
p=p->next;
}
//请自行补充
}
/*创建新的进程,主要是获取内存的申请数量*/
new_process(){
struct allocated_block *ab;
int size; int ret;
int j=0;
ab=(struct allocated_block *)malloc(sizeof(struct allocated_block));
if(!ab) exit(-5);
ab->next = NULL;
pid++;
sprintf(ab->process_name, "PROCESS-%02d", pid);
ab->pid = pid;
printf("Memory for %s:", ab->process_name);
scanf("%d", &size);
if(size>0) ab->size=size;
ret = allocate_mem(ab); /* 从空闲区分配内存,ret==1表示分配ok*/
/*如果此时allocated_block_head尚未赋值,则赋值*/
if((ret==1) &&(allocated_block_head == NULL)){
allocated_block_head=ab;
return 1; }
/*分配成功,将该已分配块的描述插入已分配链表*/
else if (ret==1) {
ab->next=allocated_block_head;
allocated_block_head=ab;
return 2; }
else if(ret==-1){ /*分配不成功*/
printf("Allocation failn");
free(ab);
return -1;
}
//ab->start_addr=++j;
return 3;
}
/*分配内存模块*/
int allocate_mem(struct allocated_block *ab){
struct free_block_type *fbt, *pre;
int request_size=ab->size;
struct allocated_block *p,*q;
p=q=allocated_block_head;
fbt = pre = free_block;
while(q)
{
q=q->next;
}
while(fbt)
{
pre=fbt->next;
if(fbt->size-request_size<10&&fbt->size-request_size>0)
{
if(fbt==free_block)
{
ab->size=request_size;
if(q==allocated_block_head)
ab->start_addr=0;
else
ab->start_addr=p->start_addr+p->size;
free_block=free_block->next;
free(fbt);
return 1;
}
else
{
ab->size=request_size;
if(q==allocated_block_head)
ab->start_addr=0;
else
ab->start_addr=p->start_addr+p->size;
pre->next=fbt->next;
free(fbt);
return 1;
break;
}
}
else if(fbt->size-request_size>10)
{
ab->size=request_size;
if(q==allocated_block_head)
ab->start_addr=0;
else
ab->start_addr=p->start_addr+p->size;
fbt->size-=request_size;
switch(ma_algorithm)
{
case 1:
{
rearrange_FF();
break;
}
case 2:
{
rearrange_BF();
break;
}
case 3:
{
rearrange_WF();
break;
}
}
return 1;
}
fbt=fbt->next;
}
if(!fbt)
{
// printf("没有合适的空间!n");
return -1;
}
return 0;
}
/*删除进程,归还分配的存储空间,并删除描述该进程内存分配的节点*/
struct allocated_block *find_process(int pid)
{
struct allocated_block *p=allocated_block_head;
// p=allocated_block_head;struct allocated_block *allocated_block_head = NULL
while(p)
//判断条件必须是p是否为空不能是p->next,因为如果只有一个节点时p->next=NULL,此时不能返回要删除的节点
{
if(p->pid==pid)
{
return p;
}
p=p->next;
}
return NULL;
}
kill_process(){
struct allocated_block *ab;
int pid;
printf("Kill Process, pid=");
scanf("%d", &pid);
ab=find_process(pid);
if(ab!=NULL){
free_mem(ab); /*释放ab所表示的分配区*/
dispose(ab); /*释放ab数据结构节点*/
}
}
/*将ab所表示的已分配区归还,并进行可能的合并*/
void sort()
{
int size;
int start_addr;
struct free_block_type *p, *q; //*free_block
p=q=free_block;
while(p)
{
while(q)
{
if(p->start_addr >q->start_addr)
{
size=p->size;
start_addr=p->start_addr;
p->size=q->size;
p->start_addr=q->start_addr;
q->size=size;
q->start_addr=start_addr;
}
q=q->next;
}
p=p->next;
}
}
int free_mem(struct allocated_block *ab){
int algorithm = ma_algorithm;
struct free_block_type *fbt, *pre;//*work;
fbt=(struct free_block_type*) malloc(sizeof(struct free_block_type));
if(!fbt) return -1;
else
{
pre=free_block;
fbt->size=ab->size;
fbt->start_addr=ab->start_addr;
while(pre->next)
pre=pre->next;
fbt->next=NULL;
pre->next=fbt;
switch(algorithm)
{
case 1:
{
sort();
rearrange_FF();
break;
}
case 2:
{
sort();
rearrange_BF();
break;
}
case 3:
{
sort();
rearrange_WF();
break;
}
}
}
return 1;
}
/*释放ab数据结构节点*/
int dispose(struct allocated_block *free_ab){
struct allocated_block *pre, *ab;
if(free_ab == allocated_block_head) { /*如果要释放第一个节点*/
allocated_block_head = allocated_block_head->next;
free(free_ab);
return 1;
}
else
{
pre = allocated_block_head;
ab = allocated_block_head->next;
while(ab!=free_ab){ pre = ab; ab = ab->next; }
pre->next = ab->next;
free(ab);
return 2;
}
}
/* 显示当前内存的使用情况,包括空闲区的情况和已经分配的情况 */
display_mem_usage(){
struct free_block_type *fbt=free_block;
struct allocated_block *ab=allocated_block_head;
if(fbt==NULL) return(-1);
printf("----------------------------------------------------------n");
/* 显示空闲区 */
printf("Free Memory:n");
printf("%20s %20sn", " start_addr", " size");
while(fbt!=NULL){
printf("%20d %20dn", fbt->start_addr, fbt->size);
fbt=fbt->next;
}
/* 显示已分配区 */
printf("nUsed Memory:n");
printf("%10s %20s %10s %10sn", "PID", "ProcessName", "start_addr", "
size");
while(ab!=NULL){
printf("%10d %20s %10d %10dn", ab->pid, ab->process_name,
ab->start_addr, ab->size);
ab=ab->next;
}
printf("----------------------------------------------------------n");
return 0;
}
do_exit()
{
printf("操作完毕,退出系统!n");
}
main(){
char choice; pid=0;
free_block = init_free_block(mem_size); //初始化空闲区
while(1) {
display_menu(); //显示菜单
fflush(stdin);
choice=getchar(); //获取用户输入
switch(choice)
{
case '1': {
set_mem_size();
break;
}//设置内存大小
case '2':
{
set_algorithm();flag=1;
break;
} //设置算法
case '3':
{
new_process();
flag=1;
break;
} //创建新进程
case '4':
{
kill_process();
flag=1;
break;
} //删除进程
case '5':
{
display_mem_usage();
flag=1;
break;
} //显示内存使用
case '0':
{
do_exit();
exit(0);
}//释放链表并退出
default: break;
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
没看出来时简单的。