专业品牌设计网站建设荣耀借壳上市公司
web/
2025/10/5 17:45:02/
文章来源:
专业品牌设计网站建设,荣耀借壳上市公司,怎么做网贷网站,上海小程序搭建BF算法、男朋友算法#xff0c;哈哈 要实现动态分区分配#xff0c;需要考虑三个方面的问题。分别是数据结构、分区分配算法、分区的分配与回收操作。
首数据结构
这里我们使用的是空闲分区链#xff0c;采用双向链表表示空闲分区。 具体实现如下#xff1a;
typedef …BF算法、男朋友算法哈哈 要实现动态分区分配需要考虑三个方面的问题。分别是数据结构、分区分配算法、分区的分配与回收操作。
首数据结构
这里我们使用的是空闲分区链采用双向链表表示空闲分区。 具体实现如下
typedef struct LNode{int order; //表示内存块的顺序int start; //表示内存的初始地址int end; //表示内存的结束地址int size; //表示内存块的大小int state; //表示内存块的状态1表示被占用0表示空闲int process; //存储占用该内存块的进程的序号struct LNode *next; //指向下一个内存块struct LNode *pre; //指向上一个内存块
}LNode;
分配算法
采用基于顺序搜索的动态分区分配算法中的最佳适应best fit BF算法。 每次为作业分配内存时总是把能满足要求又是最小的空闲分区分配给作业避免“大材小用”。
内存的分配与回收
分配内存 从空闲分区链中找到所需大小的分区。设请求的分区大小为u.size表中每个空闲分区的大小可表示为m.size若m.size-u.size0时进行内存分配操作若大于0则申请一新节点插入到双向链表中若等于0则只需修改符合要求的结点的信息就行了。回收内存四种情况F要回收的内存区、F1F的前一分区、F2:F的后一分区 F与F1地址衔接且F1空闲将F与F1合并合并后结点首地址为F1首地址末地址为F末地址结点数减一。F与F2地址衔接且F2空闲将F与F2合并合并后结点首地址为F首地址末地址为F2末地址地址结点数减一。F与F1和F2的地址衔接且F1F2空闲合并后结点首地址为F1首地址末地址为F2末地址结点数减二。其他情况将结点的state标志和process标志均设置为0。
程序解释
int buf[N]{100,500,200,700,300}; //内存块大小用来初始化空闲分区链表
int add[N]{20,150,700,950,1700,}; //内存块的初始地址用来初始化空闲分区链表
int dis[N]{301,400,310,105,190}; //进程所需内存下标记为进程编号List list_init(); //用来初始化空闲分区链表的函数返回空闲分区链表的头部
void print(List head); //顺序输出链表的信息
List allot_memory(List head,ing i);//为编号为i的进程分配内存
List free_memory(List head,int i);//释放编号为i的进程所占用的内存
全部代码
#includestdio.h
#includestdlib.h#define N 5int buf[N]{100,500,200,700,300};
int add[N]{20,150,700,950,1700,};
int dis[N]{301,400,310,105,190};
typedef struct LNode *List;typedef struct LNode{int order;int start;int end;int size;int state; int process;struct LNode *next;struct LNode *pre;
}LNode;List list_init(){List head,p,m;int i;for(i0;iN;i){m(List)malloc(sizeof(struct LNode));if(!m){printf(error\n);exit(0);}m-orderi1;m-startadd[i];m-endm-startbuf[i]-1;m-sizebuf[i];m-nextNULL;m-preNULL;m-state0;p-process0;if(i0)headpm;else{p-nextm;m-prep;pp-next;}}return head;
}void print(List head){List phead;while(p){printf(第%d块内存---始地址%-5d---末地址%-5d---大小%-5d---状态,p-order,p-start,p-end,p-size);if(p-state1)printf(被%d号进程占用中\n,p-process);else if(p-state0){printf(空闲中\n);} pp-next;}printf(\n);
}List free_memory(List head,int i){List p,m,temp;phead;while(p){if(p-processi1){tempp;if(p-next){mp-next;if(p-end1m-start){if(!m-state){p-sizem-size;p-endm-size;p-nextm-next;p-state0;p-process0;if(m-next){m-next-prep;}pm-next;free(m);while(p){p-order--;pp-next;}}else{p-state0;p-process0;}}else{p-state0;p-process0;}}ptemp;if(p-pre){mp-pre;if(p-startm-end1){if(!m-state){m-sizep-size;m-endp-size;m-nextp-next;if(p-next){p-next-prem;}free(p);pm-next;while(p){p-order--;pp-next;}}else{p-state0;p-process0;}}else{p-state0;p-process0;}}return head;}pp-next;}
}List allot_memory(List head,int i){int memory_sizedis[i];List phead;List m;int min-1;int order-1;while(p){if(p-process-1i){printf(内存中已有%d号进程了\n,i1);return head;}pp-next;}phead;while(p){if(p-sizememory_sizep-state0){if(min0){minp-size-memory_size;orderp-order;}else{if(minp-size-memory_size){minp-size-memory_size;orderp-order;}}}pp-next;}if(order-1){printf(%d号进程分配内存失败\n,i1);return head;}else{phead;while(p){if(p-orderorder){if(p-sizememory_size){p-state1;p-processi1;return head;}else{m(List)malloc(sizeof(struct LNode));m-orderp-order;m-startp-start;m-endm-startmemory_size-1;m-sizememory_size;m-state1;m-nextp;m-processi1;m-prep-pre;p-pre-nextm;p-prem;p-startm-end1;p-size-memory_size;while(p){p-order;pp-next;}return head;}}pp-next;}}
}int main(){List p,m;int choice1,choice2;int i;plist_init();print(p);pallot_memory(p,3);print(p);pallot_memory(p,3);pfree_memory(p,3);print(p);pallot_memory(p,0);print(p);pallot_memory(p,4);print(p);pfree_memory(p,4);print(p);pallot_memory(p,4);print(p);pfree_memory(p,0);print(p);pfree_memory(p,4);print(p);return 0;
}
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/web/87488.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!