您的位置:首页 > 其它

【大三操作系统实验】 动态分区时的适应算法

2010-12-30 13:04 537 查看
(1)最先适应算法要求可用表或自由链按起始地址递增的次序排序。一旦找到大于或等于所要求内存长度的分区,则结束探索。

(2)最佳适应算法要求从小到大的次序组成空闲区可用表或自由链。

(3)最坏适应算法要求空闲区按其大小递减的顺序组成空闲区可用表或自由链。













Code:

#include<stdio.h>

#include<iostream.h>

#include<string.h>

#include<windows.h>

typedef struct LinkList_Empty

{
int length;

int start_add;
struct LinkList_Empty *next;

}LNode_Empty,*List_Empty;

typedef struct LinkList_Own

{
int length;
int start_add;

char Process[10];

struct LinkList_Own *next;

}LNode_Own,*List_Own;

typedef struct Proc

{
int length;
char Process[10];

struct Proc *next;
}*Process,LNode_Proc;

List_Empty Empty_head= new LNode_Empty; //头指针

List_Own Own_head= new LNode_Own;

Process Proc_head= new LNode_Proc;

int length; //内存分配个数


void Show_Empty(List_Empty Empty_head); //空闲区显示

void Show_Own(List_Own Own_head); //占有区显示

void Init_Empty(List_Empty Empty_head); //内存空闲区初始化

void Init_Proc(Process Proc_head); //作业进程初始化

void FF(List_Empty &Empty_head,List_Own &Own_head,Process &Proc_head);

void BF(List_Empty &Empty_head,List_Own &Own_head,Process &Proc_head);

void WF(List_Empty &Empty_head,List_Own &Own_head,Process &Proc_head);

void Recycle(List_Empty &Empty_head,List_Own &Own_head);

void Sort_Up(List_Empty &Empty_head);

void Sort_Down(List_Empty &Empty_head);

void main()
{
Empty_head->next=NULL;
Own_head->next=NULL;
Proc_head->next=NULL;
int i;

while(1)
{
cout<<"1-FF最先适应算法"<<endl;

cout<<"2-BF最佳适应算法"<<endl;

cout<<"3-WF最坏适应算法"<<endl<<"请选择 ";

cin>>i;
switch(i)
{
case 1:
Init_Empty(Empty_head); //空闲区初始


cout<<"内存分配空闲区"<<endl;


Show_Empty(Empty_head); //显示空闲区


Init_Proc(Proc_head); //作业请求表


cout<<endl<<"空闲区链表:"<<endl;


FF(Empty_head,Own_head,Proc_head); //FF


//Show_Empty(Empty_head);


cout<<endl<<"作业进程占有区链表:"<<endl;


Show_Own(Own_head);

cout<<endl<<"分区内存回收"<<endl;


Recycle(Empty_head,Own_head); //分区内存回收


Show_Empty(Empty_head);
break;
case 2:

Init_Empty(Empty_head);

cout<<"内存分配空闲区"<<endl;


Show_Empty(Empty_head);

Sort_Up(Empty_head); //搜索前先排序

cout<<"排序后空闲区"<<endl;

Show_Empty(Empty_head);

Init_Proc(Proc_head);

cout<<endl<<"空闲区链表:"<<endl;


BF(Empty_head,Own_head,Proc_head); //BF


//Show_Empty(Empty_head);


cout<<endl<<"作业进程占有区链表:"<<endl;


Show_Own(Own_head);

cout<<endl<<"分区内存回收"<<endl;


Recycle(Empty_head,Own_head);
Sort_Up(Empty_head);
Show_Empty(Empty_head);
break;

case 3:
Init_Empty(Empty_head);

cout<<"内存分配空闲区"<<endl;


Show_Empty(Empty_head);

Sort_Down(Empty_head); //搜索前先排序

cout<<"排序后空闲区"<<endl;

Show_Empty(Empty_head);

Init_Proc(Proc_head);

cout<<endl<<"空闲区链表:"<<endl;


WF(Empty_head,Own_head,Proc_head); //WF


//Show_Empty(Empty_head);


cout<<endl<<"作业进程占有区链表:"<<endl;


Show_Own(Own_head);

cout<<endl<<"分区内存回收"<<endl;


Recycle(Empty_head,Own_head);
Sort_Down(Empty_head);
Show_Empty(Empty_head);
break;
default:

cout<<"重新输入!"<<endl;

}
cout<<endl<<endl;
}


}

void Init_Empty(List_Empty Empty_head) //内存空闲区初始化

{
List_Empty Empty_p,Empty_q; //当前节点和上一节点

Empty_p=Empty_q=Empty_head;


cout<<"内存分配初始化:"<<endl;

cout<<"请输入内存分配空闲区个数"<<endl;

cin>>length;
int length_1=length;

int num=0;
printf("起始地址 分区长度/n");


while(length_1--)

{
//建立链表

Empty_p = new LNode_Empty;

Empty_p->next=NULL;
Empty_q->next=Empty_p;
Empty_q=Empty_p;
scanf("%d%d",&Empty_p->start_add,&Empty_p->length);

}
}




void Init_Proc(Process Proc_head)

{
Process p,q; //当前节点和上一节点

p=q=Proc_head;

int length_2;

cout<<"进程作业初始化:"<<endl;

cout<<"请输入进程作业个数"<<endl;

cin>>length_2;
printf("作业进程号 请求长度/n");


while(length_2--)

{
p = new LNode_Proc;

p->next=NULL;
q->next=p;
q=p;
scanf("%s%d",p->Process,&p->length);

}

p=Proc_head->next;
cout<<"作业进程占有区"<<endl;

while(p)

{
cout<<"["<<p->Process<<"]"<<"["<<p->length<<"]"<<"-";

p=p->next;
}
cout<<endl;
}



void Show_Empty(List_Empty Empty_head) //显示

{
List_Empty p=Empty_head->next;
Empty_head->start_add=p->start_add;
cout<<"["<<Empty_head->start_add<<"]"<<"-";

while(p)
{

cout<<"["<<p->length<<"]";//<<"["<<p->next->start_add<<"]";

if(p->next==NULL)

cout<<"[NULL]";

else cout<<"["<<p->next->start_add<<"]"<<"-";

p=p->next;
}
cout<<endl;
}

void Show_Own(List_Own Own_head) //显示

{
if(Own_head->next==NULL)

{
cout<<"[NULL]"<<endl;

}
else
{
List_Own p=Own_head->next;
Own_head->start_add=p->start_add;
cout<<"["<<Own_head->start_add<<"]"<<"-";

while(p)

{

cout<<"["<<p->length<<"]"<<"["<<p->Process<<"]";

if(p->next==NULL)

cout<<"[NULL]";

else cout<<"["<<p->next->start_add<<"]"<<"-";

p=p->next;
}
cout<<endl;
}
}

void FF(List_Empty &Empty_head,List_Own &Own_head,Process &Proc_head)

{
List_Empty Empty_p=Empty_head->next; //第一节点

List_Own Own_q=Own_head;
Process Proc_p=Proc_head->next;
List_Empty Empty_q=Empty_head; //首节点

while(Proc_p)

{
while(Empty_p) //从头到尾搜索

{
if(Empty_p->length > Proc_p->length)

{
//建立链表

List_Own Own_p =new LNode_Own;

Own_p->next=NULL;
Own_q->next=Own_p;
Own_q=Own_p;
//保存作业到占有链表

Own_p->start_add=Empty_p->start_add;
Own_p->length=Proc_p->length;
strcpy(Own_p->Process,Proc_p->Process);
//起始地址+=作业请求长度

Empty_p->start_add+=Proc_p->length;
//分区长度-=作业请求长度

Empty_p->length-=Proc_p->length;
break;

}
else if(Empty_p->length == Proc_p->length)

{
//建立链表

List_Own Own_p =new LNode_Own;

Own_p->next=NULL;
Own_q->next=Own_p;
Own_q=Own_p;
//保存作业到占有链表

Own_p->start_add=Empty_p->start_add;

Own_p->length=Proc_p->length;
strcpy(Own_p->Process,Proc_p->Process);


//销毁当前节点

Empty_q->next=Empty_p->next;
delete Empty_p; //Empty_p是野指针无指向

break;

}
else
{ //都不满足,链表下移

Empty_q=Empty_p; //保存当前节点

Empty_p=Empty_p->next; //移到下一节点

}
}
Show_Empty(Empty_head);
cout<<endl;
Empty_p=Empty_head->next;//重新开始

Proc_p=Proc_p->next;
}

}

void Recycle(List_Empty &Empty_head,List_Own &Own_head) //回收

{
List_Empty Empty_p=Empty_head->next; //第一节点

List_Empty Empty_q=Empty_head; //头节点

List_Own Own_p=Own_head->next;
List_Own Own_q=Own_head;
int flag=-1;
while(Own_p)

{
while(Empty_p)

{
if(Empty_p->start_add >= Own_p->start_add)

{
if(Empty_p->start_add ==Own_p->start_add + Own_p->length) //下相邻区

{
Empty_p->start_add=Own_p->start_add; //首地址为释放区地址

Empty_p->length += Own_p->length;
Own_p->length = Empty_p->length;

flag=0;
}
if(Own_p->start_add == Empty_q->start_add + Empty_q->length) //上相邻区

{
//首地址为上相邻区首地址 Empty_q->start_add

//分区长度=释放区长度+分区长度

Empty_q->length+=Own_p->length;
if(flag==0) flag=1;

}

if(flag==-1)

{ //没有相邻区

Empty_p = new LNode_Empty;

Empty_p->next=Empty_q->next;
Empty_q->next=Empty_p;
Empty_p->start_add=Own_p->start_add;

Empty_p->length=Own_p->length;
}
//销毁当前占有节点和空闲节点

Own_q->next=Own_p->next;
delete Own_p;

Own_p=Own_q;
if(flag==1) //有上下相邻区才销毁当前一个节点,再建立链表

{
Empty_q->next=Empty_p->next;
delete Empty_p;


}
break;

}
else
{
Empty_q=Empty_p; //保存当前节点

Empty_p=Empty_p->next; //移到下一节点

if(Empty_p==NULL) //到最后一个节点

{
Empty_p = new LNode_Empty;

Empty_p->next=NULL;
Empty_q->next=Empty_p;
Empty_q=Empty_p;
Empty_p->start_add=Own_p->start_add;
Empty_p->length = Own_p->length;

Empty_p=Empty_p->next; //移到最后一个节点

}
}

}
Show_Empty(Empty_head);
flag=-1; //还原

Empty_p=Empty_head->next;//重新开始

Own_p=Own_p->next;
}
}

void BF(List_Empty &Empty_head,List_Own &Own_head,Process &Proc_head)

{
//Sort_Up(Empty_head); //搜索前先排序

List_Empty Empty_p=Empty_head->next;
List_Own Own_q=Own_head;
Process Proc_p=Proc_head->next;
List_Empty Empty_q=Empty_head;
cout<<endl;
while(Proc_p)

{
while(Empty_p) //从头到尾搜索

{
if(Empty_p->length > Proc_p->length)

{
List_Own Own_p =new LNode_Own;

Own_p->next=NULL;
Own_q->next=Own_p;
Own_q=Own_p; //建立链表

//保存作业到占有链表

Own_p->start_add=Empty_p->start_add;

Own_p->length=Proc_p->length;
strcpy(Own_p->Process,Proc_p->Process);

//起始地址+=作业请求长度

Empty_p->start_add+=Proc_p->length;

//分区长度-=作业请求长度

Empty_p->length-=Proc_p->length;
break;

}
else if(Empty_p->length == Proc_p->length)

{
List_Own Own_p =new LNode_Own;

Own_p->next=NULL;
Own_q->next=Own_p;
Own_q=Own_p; //建立链表

//保存作业到占有链表

Own_p->start_add=Empty_p->start_add;

Own_p->length=Proc_p->length;
strcpy(Own_p->Process,Proc_p->Process);


//销毁当前节点

Empty_q->next=Empty_p->next;
delete Empty_p;

break;

}
else
{ //都不满足,链表下移

Empty_q=Empty_p; //保存当前节点

Empty_p=Empty_p->next; //移到下一节点

}
}
Sort_Up(Empty_head);
Show_Empty(Empty_head);
cout<<endl;
Empty_p=Empty_head->next;//重新开始

Proc_p=Proc_p->next;
}
}

void Sort_Up(List_Empty &Empty_head)

{
List_Empty Empty_p=Empty_head; //首节点

List_Empty Empty_q=Empty_p->next->next; //第二个节点

int i_add,j_length;

while(Empty_p->next)

{
while(Empty_q)

{
if(Empty_p->next->length >= Empty_q->length)

{
i_add=Empty_p->start_add;
j_length=Empty_p->length;

Empty_p->start_add=Empty_q->start_add;
Empty_p->length=Empty_q->length;

Empty_q->start_add=i_add;
Empty_q->length =j_length;
}
Empty_q=Empty_q->next;
}
Empty_p=Empty_p->next;
Empty_q=Empty_p->next;

}

}

void Sort_Down(List_Empty &Empty_head)

{

List_Empty Empty_p=Empty_head; //首节点

List_Empty Empty_q=Empty_p->next->next; //第二个节点

int i_add,j_length;

while(Empty_p->next)

{
while(Empty_q)

{
//if(Empty_p == Empty_head) break;

if(Empty_p->next->length <= Empty_q->length)

{
i_add=Empty_p->start_add;
j_length=Empty_p->length;

Empty_p->start_add=Empty_q->start_add;

Empty_p->length=Empty_q->length;

Empty_q->start_add=i_add;
Empty_q->length =j_length;
}
Empty_q=Empty_q->next;
}
Empty_p=Empty_p->next;
Empty_q=Empty_p->next;

}
}

void WF(List_Empty &Empty_head,List_Own &Own_head,Process &Proc_head)

{
List_Empty Empty_p=Empty_head->next;
List_Own Own_q=Own_head;
Process Proc_p=Proc_head->next;
List_Empty Empty_q=Empty_head;
cout<<endl;
while(Proc_p)

{
while(Empty_p) //从头到尾搜索

{
if(Empty_p->length > Proc_p->length)

{
List_Own Own_p =new LNode_Own;

Own_p->next=NULL;
Own_q->next=Own_p;
Own_q=Own_p; //建立链表

//保存作业到占有链表

Own_p->start_add=Empty_p->start_add;

Own_p->length=Proc_p->length;
strcpy(Own_p->Process,Proc_p->Process);

//起始地址+=作业请求长度

Empty_p->start_add+=Proc_p->length;

//分区长度-=作业请求长度

Empty_p->length-=Proc_p->length;
break;

}
else if(Empty_p->length == Proc_p->length)

{
List_Own Own_p =new LNode_Own;

Own_p->next=NULL;
Own_q->next=Own_p;
Own_q=Own_p; //建立链表

//保存作业到占有链表

Own_p->start_add=Empty_p->start_add;

Own_p->length=Proc_p->length;
strcpy(Own_p->Process,Proc_p->Process);


//销毁当前节点

Empty_q->next=Empty_p->next;
delete Empty_p;

break;

}
else
{ //都不满足,链表下移

Empty_q=Empty_p; //保存当前节点

Empty_p=Empty_p->next; //移到下一节点

}
}
Sort_Down(Empty_head);
Show_Empty(Empty_head);
cout<<endl;
Empty_p=Empty_head->next;//重新开始

Proc_p=Proc_p->next;
}
}

/*
{
List_Empty Empty_p=Empty_head->next; //第一个节点

List_Empty Empty_q=Empty_p->next; //第二个节点

int i_add,j_length;
while(Empty_p)
{
while(Empty_q)

{
if(Empty_p->length <= Empty_q->length)

{
i_add=Empty_p->start_add;

j_length=Empty_p->length;


Empty_p->start_add=Empty_q->start_add;

Empty_p->length=Empty_q->length;


Empty_q->start_add=i_add;

Empty_q->length =j_length;

}
Empty_q=Empty_q->next;

}
Empty_p=Empty_p->next;
if(Empty_p->next!=NULL)

Empty_q=Empty_p->next;

}
}
*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: