实验三 存储管理分区分配算法
一、 实验目的
通过对操作系统内存管理算法的模拟实现,了解可变分区的动态分配和回收算法实现,掌握最佳适应法和首次适应法放置策略,加深对内存管理的理解。
二、实验内容
1、在可变分区管理模式下采用首次适应法和最佳适应法实现主存的分配和回收。 • 建立分区描述区(用空闲分区表或空闲分区链描述内存所有空闲区)
• 建立自由主存队列(针对不同的放置策略建立相应队列结构) • 编写分区分配算法2、具体步骤:
从未分配表中找到一个足以容纳该作业的可用空白区(未分配区);如果这个空白区比所要求的大,则将它分成两部分:一部部分成为已经分配的分区,剩余部分仍为空白区;修改数据结构的有关信息,并回送一个所分配分区的序号或该分区的始址。
• 编写分区回收算法。(检查回收的分区是否与空白区相邻接,如有则加以合并,使之成为一个连续的空白区)
3、 运行结果(带下划线的表示输入)
(1) 最佳适应法
> Index * adr * end size
> 1 0 32766 32767
> input the way (best or first):best
> Assign or Accept: as input
> APPLACATION: 30000
> SUCCESS!!! ADDRESS=2767
>
> Index * adr * end * size
> 1 0 2766 2767
> Assign or Accept: ac
> Input adr and size:3000 2767
>
>
> Index * adr * end * size
> 1 0 2766 2767
> 2 3000 5766 2767
> Assign or Accept : ac
> Input adr and size: 8000 4000
>
>
> Index * adr * end * size
> 1 0 2766 2767
> 2 3000 5766 2767
> 3 8000 11999 4000
> Assign or Accept :ac
> Input adr and size: 2767 10
>
>
> Index * adr * end * size
> 1 3000 5766 2767
> 2 0 2776 2777
> 3 8000 11999 4000
> Assign or Accept: as
> input APPLACATION: 3000
> SUCCESS!!! ADDRESS=9000 Index *
> adr * end * size
> 1 8000 8999 1000
> 2 3000 5766 2767
> 3 0 2776 2777
> Assign or Accept: as
> input APPLACATION: 3000
> Too large application!
(2)首次适应法
```cpp
>
>Index * adr * end * size
> 1 0 32766 32767
> input the way (best or first):first
> Assign or Accept: as
> input APPLACATION: 30000
> SUCCESS!!! ADDRESS=2767
>
>
> Index * adr * end * size
> 1 0 2766 2767
> Assign or Accept: ac
> Input adr and size:3000 2767
>
> Index * adr * end * size
> 1 0 2766 2767
> 2 3000 5766 2767
> Assign or Accept : ac
> Input adr and size: 8000 4000
> Index * adr * end * size
> 1 0 2766 2767
> 2 3000 5766 2767
> 3 8000 11999 4000
> Assign or Accept :ac
> Input adr and size: 2767 10
>
> Index * adr * end * size
> 1 0 2776 2777
> 2 3000 5766 2767
> 3 8000 11999 4000
> Assign or Accept: as
> input APPLACATION: 3000
> SUCCESS!!! ADDRESS=9000
>
>
> Index * adr * end * size
> 1 0 2776 2777
> 2 3000 5766 2767
> 3 8000 8999 1000
> Assign or Accept: as
> input APPLACATION: 3000
> Too large application!
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define OK 1
#define ERROR 0
#define Status int
#define best 0
#define first 1
//内存空白区,队列结点类型
typedef struct blank
{
int adr;
int end;
int size;
struct blank* next;
}BLANK, *PBLANK;
//内存,队列类型
typedef struct memory
{
int count;//空闲区的个数
struct blank* head;//
}MEMORY, *PMEMORY;
//首次适应法,按照地址从小到大排序
//最佳适应法,按照内存从小到大排序
void InitMemory(PMEMORY memory);
void BestSort(PMEMORY memory);
void FirstSort(PMEMORY memory);
Status Accept(PMEMORY memory, int adr, int size);
Status Assign(PMEMORY memory, int APPLICATION);
void DisPlay(PMEMORY memory);
//初始化空闲分区表
void InitMemory(PMEMORY memory)
{
memory->count = 1;
do
{
memory->head = (PBLANK)malloc(sizeof(BLANK));
} while (!memory->head);
memory->head->adr = 0;
memory->head->end = 32766;
memory->head->size = 32767;
memory->head->next = NULL;
return;
}
//最佳适应法
//按照内存从小到大排序
void BestSort(PMEMORY memory)
{
//对链表中的数据进行选择排序
PBLANK i = NULL, j = NULL;
for (i = memory->head; i != NULL; i = i->next)
{
for (j = i->next; j != NULL; j = j->next)
{
if (i->size > j->size)
{
int t = i->adr;
i->adr = j->adr;
j->adr = t;
t = i->end;
i->end = j->end;
j->end = t;
t = i->size;
i->size = j->size;
j->size = t;
}
}
}
return;
}
//首次适应法
//按照地址从小到大排序
void FirstSort(PMEMORY memory)
{
//对链表中的数据进行选择排序
PBLANK i = NULL, j = NULL;
for (i = memory->head; i != NULL; i = i->next)
{
for (j = i->next; j != NULL; j = j->next)
{
if (i->adr > j->adr)
{
int t = i->adr;
i->adr = j->adr;
j->adr = t;
t = i->end;
i->end = j->end;
j->end = t;
t = i->size;
i->size = j->size;
j->size = t;
}
}
}
return;
}
//回收算法
Status Accept(PMEMORY memory, int adr, int size)
{
PBLANK pMove = memory->head;
while (pMove)
{
if (adr == pMove->end + 1)
{
pMove->end += size;
pMove->size += size;
return OK;
}
pMove = pMove->next;
}
PBLANK blank = NULL;
do
{
blank = (PBLANK)malloc(sizeof(BLANK));
} while (!blank);
blank->adr = adr;
blank->end = adr + size - 1;
blank->size = size;
blank->next = NULL;
//插入到链表中
PBLANK t = memory->head;
if (t == NULL)
{
memory->head = blank;
}
else
{
while (t->next)
t = t->next;
t->next = blank;
}
memory->count++;
return OK;
}
//分配算法
Status Assign(PMEMORY memory, int APPLICATION)
{
if (memory->count == 0)
return ERROR;
PBLANK pMove = memory->head;
PBLANK prePMove = pMove;
while (pMove)
{
//够分配
if (APPLICATION < pMove->size)
{
pMove->end -= APPLICATION;
pMove->size -= APPLICATION;
printf("SUCCESS!!! ADDRESS=%d\n",pMove->end + 1);
return OK;
}
else if (APPLICATION == pMove->size)//刚好分配完这个分区
{
printf("SUCCESS!!! ADDRESS=%d\n", pMove->end - APPLICATION + 1);
prePMove->next = pMove->next;
free(pMove);
if (memory->count == 1)
memory->head = NULL;
memory->count--;
return OK;
}
prePMove = pMove;
pMove = pMove->next;
}
return ERROR;
}
void DisPlay(PMEMORY memory)
{
printf("Index\t*\tadr\t*\tend\t*\tsize\n");
PBLANK pMove = memory->head;
int i = 0;
while (pMove)
{
printf("%d\t \t%d\t \t%d\t \t%d\n", i + 1, pMove->adr, pMove->end, pMove->size);
pMove = pMove->next;
i++;
}
return;
}
void Process(PMEMORY memory,char * hit)
{
int APPLICATION, adr, size;
char choose[10] = { 0 };
while (1)
{
DisPlay(memory);
printf("Assign or Accept:");
scanf("%s", choose);
getchar();
if (!strcmp(choose, "as"))
{
printf("input APPLACATION:");
scanf("%d", &APPLICATION);
getchar();
if (!Assign(memory, APPLICATION))
printf("Too large application!\n");
}
else if (!strcmp(choose, "ac"))
{
printf("Input adr and size:");
scanf("%d%d", &adr, &size);
getchar();
Accept(memory, adr, size);
}
else
{
printf("ERROR\n");
}
if (!strcmp(hit, "best"))
BestSort(memory);
else if (!strcmp(hit, "first"))
FirstSort(memory);
}
}
int main()
{
PMEMORY memory = NULL;
do
{
memory = (PMEMORY)malloc(sizeof(MEMORY));
} while (!memory);
InitMemory(memory);
char hit[10] = { 0 };
printf("input the way (best or first):");
scanf("%s", hit);
getchar();
if (!strcmp(hit,"best") && !strcmp(hit,"first"))
{
printf("ERROR\n");
exit(-1);
}
else
Process(memory, hit);
return 0;
}