欢迎来到天天文库
浏览记录
ID:39563624
大小:121.00 KB
页数:13页
时间:2019-07-06
《计算机操作系统内存分配实验源代码》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、#include#include#defineOK1//完成#defineERROR0//出错typedefintStatus;typedefstructfree_table//定义一个空闲区说明表结构{intnum;//分区序号longaddress;//起始地址longlength;//分区大小intstate;//分区状态}ElemType;typedefstructNode//线性表的双向链表存储结构{ElemTypedata;structNodeprior;//前趋指针structNodenext;//后继指针}No
2、de,LinkList;LinkListfirst;//头结点LinkListend;//尾结点intflag;//记录要删除的分区序号StatusInitblock()//开创带头结点的内存空间链表{first=(LinkList)malloc(sizeof(Node));end=(LinkList)malloc(sizeof(Node));first->prior=NULL;first->next=end;end->prior=first;end->next=NULL;end->data.num=1;end->data.address=40;end->data
3、.length=600;end->data.state=0;returnOK;}voidsort()//分区序号重新排序{Nodep=first->next,q;q=p->next;for(;p!=NULL;p=p->next){for(q=p->next;q;q=q->next){if(p->data.num>=q->data.num){q->data.num+=1;}}}}//显示主存分配情况voidshow(){intflag=0;//用来记录分区序号Nodep=first;p->data.num=0;p->data.address=0;p->data.le
4、ngth=40;p->data.state=1;sort();printf("tt》主存空间分配情况《");printf("");printf("分区序号t起始地址t分区大小t分区状态");while(p){printf("%dtt%dtt%d",p->data.num,p->data.address,p->data.length);if(p->data.state==0)printf("tt空闲");elseprintf("tt已分配");p=p->next;}printf("");}//
5、首次适应算法StatusFirst_fit(intrequest){//为申请作业开辟新空间且初始化Nodep=first->next;LinkListtemp=(LinkList)malloc(sizeof(Node));temp->data.length=request;temp->data.state=1;p->data.num=1;while(p){if((p->data.state==0)&&(p->data.length==request)){//有大小恰好合适的空闲块p->data.state=1;returnOK;break;}elseif((p-
6、>data.state==0)&&(p->data.length>request)){//有空闲块能满足需求且有剩余temp->prior=p->prior;temp->next=p;temp->data.address=p->data.address;temp->data.num=p->data.num;p->prior->next=temp;p->prior=temp;p->data.address=temp->data.address+temp->data.length;p->data.length-=request;p->data.num+=1;retur
7、nOK;break;}p=p->next;}returnERROR;}//最佳适应算法StatusBest_fit(intrequest){intch;//记录最小剩余空间Nodep=first;Nodeq=NULL;//记录最佳插入位置LinkListtemp=(LinkList)malloc(sizeof(Node));temp->data.length=request;temp->data.state=1;p->data.num=1;while(p)//初始化最小空间和最佳位置{if((p->data.state==0)&&(p->data.length>=
8、reque
此文档下载收益归作者所有