欢迎来到天天文库
浏览记录
ID:53800815
大小:125.00 KB
页数:13页
时间:2020-04-07
《计算机操作系统内存分配实验源代码.doc》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、.#include#include#defineOK1//完成#defineERROR0//出错typedefintStatus;typedefstructfree_table//定义一个空闲区说明表结构{intnum;//分区序号longaddress;//起始地址longlength;//分区大小intstate;//分区状态}ElemType;typedefstructNode//线性表的双向链表存储结构{ElemTypedata;structNode*prior;
2、//前趋指针structNode*next;//后继指针}Node,*LinkList;LinkListfirst;//头结点LinkListend;//尾结点intflag;//记录要删除的分区序号StatusInitblock()//开创带头结点的内存空间链表{first=(LinkList)malloc(sizeof(Node));end=(LinkList)malloc(sizeof(Node));first->prior=NULL;first->next=end;end->prior=first;e
3、nd->next=NULL;end->data.num=1;end->data.address=40;end->data.length=600;end->data.state=0;returnOK;}voidsort()//分区序号重新排序{Node*p=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;}}}}//显
4、示主存分配情况voidshow(){intflag=0;//用来记录分区序号Node*p=first;p->data.num=0;p->data.address=0;p->data.length=40;p->data.state=1;sort();printf("tt》主存空间分配情况《");printf("**********************************************************");printf("分区序号t起始地址t分区大小t分区状态
5、n");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("**********************************************************");}//首次适应算法StatusFirst_fi
6、t(intrequest){//为申请作业开辟新空间且初始化Node*p=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;
7、}elseif((p->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->da
8、ta.length-=request;p->data.num+=1;returnOK;break;}p=p->next;}returnERROR;}//最佳适应算法StatusBest_fit(intrequest){intch;//记录最小剩余空间Node*p=first;Node*q=NULL;//记录最佳插入位置LinkListtemp=(LinkList)malloc(sizeof(Node
此文档下载收益归作者所有