赞
踩
运行环境:
Dev c++
算法思想:
本实验是模拟分段存储管理,系统需要建立两张分区表,分别是已分配和未分配分区表,首先根据装入作业的大小判断是否小于空闲分区的总容量,若满足,则对该作业继续进行分段,每输入一个分段大小就在空闲分区中找到第一个没有使用且足够大的分区,若找到将该分区标记为该作业名和对应的分段号,修改已分配和未分配分区表,并打印内存分配信息。对已分配分区的回收,首先输入要回收的作业名,找到该作业的每一段所在的分区,修改已分配分区表,未分配分区表(根据是否存在上下邻),打印回收成功后内存分配信息。
关键数据结构定义:
//内存结构体
typedef struct memory_node
{
int size; //内存大小
int address; //内存始址
} memoryNode;
memoryNode memory;
//分区结构体
typedef struct link_node
{
int id;//分区号
int size; //分区长度
int address; //分区始址
char flag[20]; //分区状态,空闲或者占用作业名
struct link_node* next;
} node;
//段表
typedef struct segment_node
{
int a[10][10];
struct segment_node *next;
} segmentNode;
程序框架:
//函数名 :initMemory 参数:无
node* initMemory()
{
//函数功能:初始化内存空间
}
//函数名:operation 参数:node* head
int operation(node *head)
{
//函数功能:打印操作菜单,选择需要进行的操作,输入1进行内存 分配,输入2进 行内存去配,输入0退出
}
//函数名:allocate 参数:node* head
void allocate(node *head)
{
//函数名:输入作业名和大小,默认采用最先分配
}
//函数名:firstAllocation 参数:node* head,int size,charc[10]
void firstAllocation(node* head,int size,char c[10])
{
//函数功能:对作业进行分段,并采用最先分配,分段结束后打印 段表
}
//函数名:reorder 参数:node* head
void reorder(node* head)
{
//函数功能:对分区和未分区的存储区域进行编号
}
//函数名:recycle 参数:node* head
void recyle(node* head) //回收算法
{
//函数功能:对归还分区按情况进行处理,,有上邻有下邻,有上邻 无下邻,无上邻有下邻,无上邻无下邻
}
//函数名:print参数:node* head
void print(node* head)
{
//函数功能:打印主存分配表
}
int main()
{
node* head;
head=initMemory();
while(1)
{
int c;
c=operation(head);
if(c==1)
{
break;
}
}
return 0;
}
测试用例:
/*
256
40
1
jobA
50
2
20
30
1
jobB
100
3
30
35
35
2
jobB
2
jobA
0
*/
- #include<stdio.h>
- #include<malloc.h>
- #include<string.h>
- int g_size;
- //内存结构体
- typedef struct memory_node
- {
- int size; //内存大小
- int address; //内存始址
- } memoryNode;
-
- memoryNode memory;
-
- //分区结构体
- typedef struct link_node
- {
- int id;//分区号
- int size; //分区长度
- int address; //分区始址
- char flag[20]; //分区状态,空闲或者占用作业名
- struct link_node* next;
- } node;
-
- //段表
- typedef struct segment_node
- {
- int sum;
- char jobname[20];
- int a[10][10];
- struct segment_node *next;
- } segmentNode;
-
- node* initMemory();
- node* operation(node *head);
- node* allocate(node *head);
- node* firstAllocation(node* head,int size,char c[10]);
- node* reorder(node* head);
- node* recyle(node* head);
- void print(node* head);
-
- node *distribute = NULL;
- segmentNode *duanbiao =NULL;
-
- //函数名 :initMemory 参数:无
- node* initMemory()
- {
- //函数功能:初始化内存空间
- printf("请输入内存大小:");
- scanf("%d",&memory.size);g_size = memory.size;
- printf("请输入起始地址:");
- scanf("%d",&memory.address);
- node* p = (node *) malloc ( sizeof(node) * memory.size ); //分配可以放得下265个node的内存空间
- p->next = NULL;
- p->id = 1;
- p->size = memory.size;
- p->address = memory.address;
- p->flag[0] = '\0';
- return p;
- }
-
- //函数名:operation 参数:node* head
- node* operation(node *head)
- {
- //函数功能:打印操作菜单,选择需要进行的操作,输入1进行内存分配,输入2进行内存去配,输入0退出
- int choice;
- printf("*********可变分区管理**********\n");
- printf(" * 1.内存分配 *\n");
- printf(" * 2.内存去配 *\n");
- printf(" * 0.退出 *\n");
- printf(" 请输入选项[ ]\b\b");
- scanf("%d",&choice);
- if(choice == 1)
- {
- printf("1.内存分配\n");
- reorder(head);
- print(head);
- head=allocate(head);
- }
- else if(choice == 2)
- {
- printf("2.内存去配\n");
- reorder(head);
- print(head);
- head=recyle(head);
- }
- else
- {
- return NULL;
- }
- return head;
- }
- //函数名:allocate 参数:node* head
- node* allocate(node *head)
- {
- //函数名:输入作业名和大小,默认采用最先分配
- char name[20];
- int jobsize;
- printf("请输入作业名:");
- scanf("%s",&name);
- printf("请输入%s需要分配的主存大小:",name);
- scanf("%d",&jobsize);
- if(jobsize <= g_size)
- head = firstAllocation(head,jobsize,name);
- else
- printf("分配失败!");
- return head;
-
- }
-
- //函数名:firstAllocation 参数:node* head,int size,charc[10]
- node* firstAllocation(node* head,int size,char c[10])
- {
- //函数功能:对作业进行分段,并采用最先分配,分段结束后打印 段表
- int count = 0,blockNum,jobblocksize,state;
- node *q,*pre;
-
- segmentNode* D = (segmentNode *) malloc ( sizeof(segmentNode) * 1 );
- D->next = NULL;
- strcpy(D->jobname,c);
- if(duanbiao == NULL)
- duanbiao = D;
- else
- {
- D->next = duanbiao;
- duanbiao = D;
- }
-
- q = head;
- printf("请输入要分成几段:");
- scanf("%d",&blockNum);D->sum = blockNum;
- while(count != blockNum)
- {
- printf("剩余%dKB的内存,请输入第%d段的大小:",size,count+1);
- scanf("%d",&jobblocksize);
- node* p = (node *) malloc ( sizeof(node) * 1 );
- p->next = NULL;
- p->size = jobblocksize;
- strcpy(p->flag,c);
- // p->flag[strlen(p->flag)+1] = p->flag[strlen(p->flag)];
- // p->flag[strlen(p->flag)]= '0' + count;
-
- state = count;
- while(1)
- {
- if(state/10 == 0)
- {
- p->flag[strlen(p->flag)+1] = p->flag[strlen(p->flag)];
- p->flag[strlen(p->flag)]= '0' + state%10;
- break;
- }
- else
- {
- p->flag[strlen(p->flag)+1] = p->flag[strlen(p->flag)];
- p->flag[strlen(p->flag)]= '0' + state/10;
- state=state%10;
- }
- }
-
- while(q!=NULL)
- {
- if(q->size >= p->size)
- break;
- pre = q;
- q = q->next;
- }
- if(q!=NULL)
- {
- p->address = q->address;
- if(q->size == p->size)
- {
- if(q==head)
- head=NULL;
- else
- pre->next = q->next;
- }
- else
- {
- q->size = q->size -p->size;
- q->address = q->address + p->size;
- }
- if(distribute == NULL)
- {
- distribute = p;
- }
- else
- {
- p->next = distribute;
- distribute = p;
- }
- }
- else
- {
- printf("分配失败!");
- return head;
- }
- D->a[count][0] = jobblocksize;
- D->a[count][1] = p->address + p->size;
- count++;
- size = size - jobblocksize;
- reorder(head);
- print(head);
- }
- printf("分配成功!\n");
- printf("************打印%s段表************\n",c);
- printf("段号\t段长\t基址\n");
- for(int i=0;i<blockNum;i++)
- printf("%d\t%d\t%d\n",i,D->a[i][0],D->a[i][1]);
- return head;
- }
-
- //函数名:reorder 参数:node* head
- node* reorder(node* head)
- {
- //函数功能:对分区和未分区的存储区域进行编号
- int count;
- node *p;
- count = 1;
- p = distribute;
- while(p!=NULL)
- {
- p->id = count;
- count++;
- p = p->next;
- }
-
- p = head;
- while(p!=NULL)
- {
- p->id = count;
- count++;
- p = p->next;
- }
- return head;
- }
-
- //函数名:recycle 参数:node* head
- node* recyle(node *head) //回收算法
- {
- //函数功能:对归还分区按情况进行处理,有上邻有下邻,有上邻无下邻,无上邻有下邻,无上邻无下邻
- char name[20];
- int state;
- segmentNode *q,*preq;
- node *qq,*preqq,*qqq,*preqqq;//distribute
- q = duanbiao;
- printf("请输入您想回收的作业名:");
- scanf("%s",&name);
- while(q!=NULL&&strcmp(q->jobname,name)!=0)
- {
- preq = q;
- q = q->next;
- }
- if(q==duanbiao)
- {
- duanbiao=q->next;
- }
- else
- {
- preq->next=q->next;
- }
- if(q==NULL)
- printf("没有该作业!");
- else
- {
- for(int i=0;i<q->sum;i++)
- {
- qq = distribute;//已分配链表
- while(qq!=NULL)//在已分配表中找要回收的作业
- {
- if(qq->address == q->a[i][1]-q->a[i][0])
- {
- if(qq==distribute)
- {
- distribute = qq->next;
- }
- else
- {
- preqq->next = qq->next;
- }
- break;
- }
- preqq = qq;
- qq = qq->next;
- }
-
- preqqq = NULL;
- qqq = head;//未分配链表
- while(qqq!=NULL)//将要回收的作业的空闲分区合并
- {
- if(qq->address+qq->size<head->address)//头没有临界区
- {
- qq->next = head;
- head = qq;
- printf("回收%s的段%s成功!\n",name,qq->flag);
- qq->flag[0] = '\0';
- break;
- }
- else if(qq->address+qq->size == head->address)//头有下
- {
- head->address = qq->address;
- head->size = head->size + qq->size;
- printf("回收%s的段%s成功!\n",name,qq->flag);
- break;
- }
- else if(preqqq!=NULL&&qq->address == preqqq->address+preqqq->size&&qq->address+qq->size==qqq->address)//有上有下
- {
- preqqq->size = preqqq->size+qqq->size+qq->size;
- preqqq->next = qqq->next;
- printf("回收%s的段%s成功!\n",name,qq->flag);
- free(qq);
- break;
- }
- else if(preqqq!=NULL&&qq->address == preqqq->address+preqqq->size&&qq->address+qq->size<qqq->address)//有上没有下
- {
- preqqq->size =preqqq->size + qq->size;
- printf("回收%s的段%s成功!\n",name,qq->flag);
- free(qq);
- break;
- }
- else if(preqqq!=NULL&&qq->address > preqqq->address+preqqq->size&&qq->address+qq->size==qqq->address)//没有上有下
- {
- qqq->address = qq->address;
- qqq->size = qqq->size+qq->size;
- printf("回收%s的段%s成功!\n",name,qq->flag);
- free(qq);
- break;
- }
- else if(preqqq!=NULL&&qq->address > preqqq->address+preqqq->size&&qq->address+qq->size<qqq->address)//没有上没有下
- {
- qq->next = qqq;
- preqqq->next = qq;
- printf("回收%s的段%s成功!\n",name,qq->flag);
- qq->flag[0] = '\0';
- break;
- }
- else if(preqqq!=NULL&&qqq->next == NULL&&qqq->address+qqq->size==qq->address)//尾部(有上)
- {
- qqq->size = qqq->size+qq->size;
- printf("回收%s的段%s成功!\n",name,qq->flag);
- break;
- }
- else if(preqqq!=NULL&&qqq->next == NULL&&qqq->address+qqq->size<qq->address)// 尾部(没有上)
- {
- qqq->next = qq;
- qq->next =NULL;
- printf("回收%s的段%s成功!\n",name,qq->flag);
- qq->flag[0]='\0';
- break;
- }
- preqqq = qqq;
- qqq = qqq->next;
- }
-
- }
- reorder(head);
- print(head);
- free(q);
- }
- return head;
- }
- //函数名:print参数:node* head
- void print(node* head)
- {
- //函数功能:打印主存分配表
- node *p;
- printf("******************主存分配情况******************\n");
- p = distribute;
- printf("已分配:\n");
- printf("分配号 大小(KB) 起始(KB) 状态\n");
- while(p!=NULL&&p->flag[0]!='\0')
- {
- printf("%d\t%d\t\t%d\t\t%s\n",p->id,p->size,p->address,p->flag);
- p = p->next;
- }
- printf("\n\n\n");
- p = head;
- printf("未分配:\n");
- printf("分配号 大小(KB) 起始(KB) 状态\n");
- while(p!=NULL&&p->flag[0] == '\0')
- {
- printf("%d\t%d\t\t%d\t\t空闲\n",p->id,p->size,p->address,p->flag);
- p = p->next;
- }
- }
-
- int main()
- {
- node* head;
- head=initMemory();
- while(1)
- {
- head=operation(head);
- if(head==NULL)
- {
- break;
- }
- }
- return 0;
- }
①对单链表还是不熟,没有头结点的单链表head没有next;
②对链表的遍历条件没有设计好,在该题中preqqq!=NULL才能进行遍历;
③对于指针,如果修改了指针则要返回该指针并赋值给它本身,或者在传递指针时,传的是指针的指针,否则对指针的处理是无效的。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。