logo资料库

内存分配算法,包括first-fit,best-fit等.doc

第1页 / 共16页
第2页 / 共16页
第3页 / 共16页
第4页 / 共16页
第5页 / 共16页
第6页 / 共16页
第7页 / 共16页
第8页 / 共16页
资料共16页,剩余部分请下载后查看
//⑴ 自定义内存管理策略对应的数据结构; //⑵ 随机产生一组申请和收回进程及要求分配和收回内存的大小,实现内存分配和收回算 法(可以采用多种分配算法),计算内存利用率; //⑶ 显示内存分区管理的分配和收回过程。 #include #include #include #include #define Size 4 #define num 10 #define SUCCESS 1 #define ERROR -1 typedef int byte; typedef struct { byte subareaSize;//分区大小 int startLoc;//起始地址 int index;// 分区号 }SubareaTable;//分区表 typedef struct node{// 结点 SubareaTable subarea;// 分区 struct node *next; int status;//状态位 0(空闲)1(使用) }*Node,*LinkList; typedef struct { byte processSize; int subareaIndex;//保存分区号 int status;//进程状态,0(新建)1(执行)-1(终止)-2(未绪。申请但没有分配内存)2(就绪,已分 配内存) }Process;//进程 int subareaSize[num]={8,12,16,32,24,16,64,128,40,64};//分区大小 Process *pro=NULL;//保持进程信息 int ProcessNum=0;//进程数目 int applyProcessNum=0;//每次申请进程数目 int maxApplyNum=0;//最大可申请数目 int *applyIndex=NULL;//申请进程队列 int totalApplyNum=0;//申请总数 int *assignPointer=NULL;//已分配内存的进程队列 int assignFlag=0;//分配索引,表示已申请队列已分配的进程数 int exeIndex;//执行的进程号 Node *subareaNode=new Node[3];//分区回收时,进程所在分区及其前,后分区信息 LinkList createLinkList(int n );//建立空闲分区链
nestFit(LinkList &head,Process pro,Node flag);//循环适应算法 Node firstFit(LinkList &head,Process pro);//首次适应算法 Node Node bestFit(LinkList &head,Process pro);//最佳适应算法 Node worstFit(LinkList &head,Process pro);//最坏适应算法 Node assign(LinkList &head,int orderIndex,int index,Node flagNode);//一次分区分配 int assignMemory(LinkList &head);//内存分配 void insertNode(LinkList &head,Node q,int index);//插入节点 Node deleteNode(LinkList &head,int index);//删除节点 int display(LinkList &head);//打印分区分配情况 int lowAttemper(int *excursionPointer);//低级调度 int findSubarea(LinkList &head,int index);//回收内存 int menu();//菜单 int creatProcess();//创建进程 Process* randomCreatPro(int n);//随机产生进程 LinkList createLinkList(int n){//建立空闲分区链 cout<<" LinkList head; Node p; head=(LinkList)malloc(sizeof(node)); if(head==NULL){ cout<<"头结点分配错误"<next=NULL;//链表尾巴设置为 NULL LinkList q=head; int start=0; for(int i=1;i<=n;i++){ p=(Node)malloc(sizeof(node)); if(p==NULL){ cout<<"节点分配错误"<next=q->next; q->next=p; q=p; p->subarea.index=i; p->subarea.subareaSize=subareaSize[i-1];//分区表赋值大小 p->subarea.startLoc=start; p->status=0; start+=subareaSize[i-1]; } cout<<"分区创建成功!!!"<
return head; } Node firstFit(LinkList &head,Process pro){//首次适应算法 Node p=head->next;//遍历结点,返回结点,从第一个结点开始遍历 if(p==NULL){ cout<<"空闲链表不存在"<status==0&&p->subarea.subareaSize>=pro.processSize){ break; } p=p->next; } while(p!=NULL); if(p==NULL){//没找到合适的结点 return head; } return p; } } Node nestFit(LinkList &head,Process pro,Node flag){//循环适应算法 Node p=flag->next;//遍历结点 while(p!=NULL){ if(p->status==0&&p->subarea.subareaSize>=pro.processSize){ break; } p=p->next; } if(p==NULL){//遍历到链表结尾 p=head;//从头开始遍历 while(p!=flag){//标记结点 p=p->next; if(p->status==0&&p->subarea.subareaSize>=pro.processSize){ break; }
} if(p==flag){//正常跳出循环,没有合适的结点可分配 return head; }else{ return p;// 在 flag 结点前找到一合适的结点分配 } }else{ return p;// 在 flag 结点后找到一合适的结点分配 } } Node bestFit(LinkList &head,Process pro){//最佳适应算法 Node p=head->next;//遍历结点,返回结点,从第一个结点开始遍历 Node q;//返回最佳空闲结点 int leave;//剩余空间 int count=0;//计数器 if(p==NULL){ cout<<"空闲链表不存在"<status==0&&p->subarea.subareaSize>=pro.processSize){ count++; if(count==1){//第一个可以分配的空闲分区 leave=p->subarea.subareaSize-pro.processSize; q=p; }else if(count>1){ if(p->subarea.subareaSize-pro.processSizesubarea.subareaSize-pro.processSize; q=p; } } } p=p->next; }while(p!=NULL); return q; }
} Node worstFit(LinkList &head,Process pro){//最坏适应算法 Node p=head->next;//遍历结点,返回结点,从第一个结点开始遍历 Node q;//返回最大空闲结点 int count=0;//计数器 if(p==NULL){ cout<<"空闲链表不存在"<status==0){ count++; if(count==1){//第一个空闲区 q=p; } else{//非第一个空闲区 面最大结点 if(p->subarea.subareaSize>q->subarea.subareaSize){// 当 前 结 点 大 于 前 q=p; } } } p=p->next; }while(p!=NULL); if(q->subarea.subareaSize>=pro.processSize){ return q; }else{ cout<<"进程大小大于最大分区,无法分配"<
} void insertNode(LinkList &head,Node q,int index) { Node p;//遍历结点 int j=1; if(head==NULL){ cout<<"链表为空"; return; } p=head->next; for(;p;p=p->next){ j++; if(j>=index) break; } q->next=p->next; p->next=q;//插入完成 j=q->subarea.index;//j 保持 q 的分区号 q=q->next;//开始修改分区号 j=q->subarea.index; while(q!=NULL){ q->subarea.index=j+1; q=q->next; j++; } } Node deleteNode(LinkList &head,int index){ Node q;//保存要删掉的节点 Node p=head;//遍历的节点 int count=0; while(p&&countnext; count++; } q=p->next; p->next=q->next; return q;
} int findSubarea(LinkList &head,int index){ Node p=head->next;//遍历结点 if(p==NULL){ cout<<"空闲链表不存在"<next; if(j>=index-1)break; } for(int i=0;i<3;i++){ subareaNode[i]=p; p=p->next; } return SUCCESS; } } int display(LinkList &head){//打印分区分配情况 -------------分区信息--------------"<next; cout.fill(' '); while(p!=NULL){ cout.width(3); cout<subarea.index<<"分区的大小:"<subarea.subareaSize<<"KB " <<" 分 区 开 始 位 置 :"<subarea.startLoc<<" 是 否 空 闲:"<status<next; } return SUCCESS;
} } int displayProcess(){ 进程状况:"<processSize<<" if(pro->status==0){ cout<<"创建"; } else if(pro->status==1){//进程状态 cout<<"执行"; }else if(pro->status==-1){ cout<<"终止"; }else if(pro->status==2){ cout<<"就绪"; }else if(pro->status==-2){ cout<<"未绪"; } cout<>applyProcessNum; while(applyProcessNum>maxApplyNum){//申请数目大于最大可申请数目 cout<<"申请进程数目大于可申请进程数目,不合法"<>applyProcessNum; }
分享到:
收藏