实验7
- 格式:doc
- 大小:359.50 KB
- 文档页数:8
盐城师范学院信息科学与技术学院
实验报告
课程名称 操作系统实验
班级 092 学号 09263108 姓名 李培培
实验地点 5-113 实验日期 6月31日 实验学时 4课时
实验名称
实验模拟可变式分区管理
实验类型
验证性 综合性 设计性
实验目的:
通过实验使学生了解可变式分区管理使用的主要数据结构,分配、回收的主要技术,了解最优
分配,最坏分配和最先分配的算法。
实验环境:(包括软件平台和硬件平台)
Pc机
Tevation OS Lab
实验内容及步骤 (含源程序):
#include
#include
#include
unsigned char memory[1024];
struct node
{
char name[5];
int start,length;
struct node *next;
};
struct node *p, *f, *pp,*ff;
/* 还要为它们确立一个初始的状态。这两个链表最好带有一个头结点。*/
struct node *free(char *c, struct node *p,struct node *f);
void print();
int fenpei(char *c, int room, struct node *p,struct node *f);
void main()
{
int t=1,xz;
char name[5];
unsigned int size;
p = (struct node *)malloc (sizeof(struct node));
p->next = NULL;
f = (struct node *) malloc (sizeof(struct node));
f->next = (struct node *)malloc (sizeof(struct node));
f->next->start = 1;
f->next->length = 1024;
f->next->next = NULL;
printf("可变式内存管理模拟\n ");
while (t==1)
{
printf("1:运行进程;2:终止进程;3:退出 请选择: ");
scanf("%d",&xz);
switch (xz)
{
case 1:
{
printf("请输入请求进程的进程名(<=5个字符):");
scanf("%s",name);
printf("请输入请求进程所需空间(起始可用内存1024):");
scanf("%d",&size);
fenpei(name,size,p,f);
print(); /*打印分配表空闲表*/
}
break;
case 2:
{
printf("请输入终止进程的进程名:");scanf("%s,%d",name);
ff=free(name,p,f );
print();/*打印分配表空闲表*/
}
break;
case 3:
t=0;
break;
}
}
}
/*打印分配表空闲表函数*/
void print()
{ /*打印分配表*/
printf("分配表\n");
pp=p->next;
while (pp!=NULL)
{
printf("%5s,%d,%d\n",pp->name,pp->start, pp->length);pp=pp->next;}
/*打印空闲表 */
printf("空闲表\n");
ff=f->next;
while (ff!=NULL)
{
printf(" %d,%d\n",ff->start, ff->length);ff=ff->next;
}
}
/*为进程分配空间函数*/
int fenpei(char *c, int room, struct node *p,struct node *f)
{
struct node *p1, *f1, *f2; /*f2是f1的跟随指针*/
f1 = f->next;
f2 = f;
while (f1!=NULL) /*查找F表,看是否有满足条件的一个结点,如果没有
则提示出错了*/
{
if (f1->length >=room)
break;
f1 = f1->next; f2 = f2->next;
}
if (f1 == NULL)
{
printf("没有足够的空间,不能实施分配!!!\n");
return (-1);
}
/*定位到P表的末尾,生成一个新的结点,联接到后面。*/
p1 = p;
while(p1->next != NULL)
p1 = p1->next;
p1->next = (struct node *)malloc(sizeof(struct node));
p1 = p1->next ;
p1->length = room/*-1*/;
strcpy(p1->name, c);
p1->start = f1->start;
p1->next = NULL; /*修改F表中被分配的节点*/
f1->length = f1->length - room;
f1->start = f1->start + room;
f2->next = f1->next;
if (f1->length !=0) /*如果修改后的节点大小为0,则舍弃之。
*/
{
f2 = f;
while((f2->next!=NULL)&&(f2->next->length<= f1->length))
f2 = f2 ->next;
f1->next = f2->next;
f2->next = f1; }
return (p1->start) ;
}
/*回收进程所占空间函数*/
struct node *free(char *c, struct node *p,struct node *f)
{
struct node *p1, *p2, *f1, *f2, *f3;
p1 = p->next ;
p2 = p; /*p2是p1是跟随指针*/
while(p1!= NULL)
/*按外界提供的程序名在P表中查找其结点,若没找到,则返回NULL*/
{
if (!(strcmp(p1->name,c))) break;
p1 = p1->next;
p2 = p2->next;
}
if (p1==NULL)
{
printf("没找到该进程!!!\n");
return (NULL);
}
p2->next = p1->next ; /*将找到的结点取出*/
f1 = (struct node *)malloc(sizeof(struct node));/*据此生成一个新的空闲结
点*/
f1->start = p1->start ;
f1->length = p1->length;
/*在F表中依次观察各个结点,看是否能与此新的空闲结点合并*/
f2 = f->next;
f3 = f;
while (f2!= NULL)
{
if (f1->start + f1 ->length == f2->start)
{
f1->length += f2->length;
f3->next = f2->next;
f2 = f2->next;
}
else if (f2->start + f2->length == f1->start )
{
f1->start = f2 ->start;
f1 ->length += f2->length;
f3 ->next = f2 ->next ;
f2 = f2->next;
}
else
{
f2=f2->next ;
f3 = f3->next;
}
}
f2 = f; /*再寻找一个合适的地方插入此空闲结点*/
while ((f2->next!=NULL)&&(f2->next->length<=f1->length))
f2 = f2->next;
f1->next = f2->next;
f2->next = f1;
return (f1);
}
实验结果及分析:
复习了单向链表操作编程,编写全部程序。运行了分配或回收一次,必须可以显示空闲内存状
态。注意回收时,空闲块的合并操作。
成绩 优秀 教师签名 王创伟 日期 7.3