资源描述:
《中职编程语言基础C语言07 资源拓展 C语言课程设计-存储管理分区分配算法.doc》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、C语言课程设计_存储管理分区分配算法/***pcb.c***/#include"stdio.h"#include"stdlib.h"#include"string.h"#defineMAX32767typedefstructnode /*设置分区描述器*/{ intaddress,size; structnode*next;}RECT;/*函数原型*/RECT*assignment(RECT*head,intapplication);voidacceptment1(RECT*head,RECT*back1);voidacceptment2(RECT*head,REC
2、T*back1);intbackcheck(RECT*head,RECT*back1);voidprint(RECT*head);/*变量声明*/RECT*head,*back,*assign1,*p;intapplication1,maxblocknum;charway;/*主函数*/main(){ charchoose[10]; intcheck; head=malloc(sizeof(RECT));/*建立可利用区表的初始状态*/ p=malloc(sizeof(RECT)); head->size=MAX; head->address=0; head
3、->next=p; maxblocknum=1; p->size=MAX; p->address=0; p->next=NULL; print(head); /*输出可利用表初始状态*/ printf("Entertheway(bestorfirst(b/f)");/*选择适应策略*/ scanf("%c",&way); do{ printf("Entertheassignoraccept(as/ac)"); scanf("%s",choose);/*选择分配或回收*/ if(strcmp(choose,"as")==0)/*a
4、s为分配*/ { printf("Inputapplication:"); scanf("%d",&application1);/*输入申请空间大小*/ assign1=assignment(head,application1);/*调用分配函数*/ if(assign1->address==-1)/*分配不成功*/ printf("Toolargeapplication!,assignfails!!"); else printf("Success!!ADDRESS=%5d
5、",assign1->address);/*分配成功*/ print(head);/*输出*/ } else if(strcmp(choose,"ac")==0)/*回收*/ { back=malloc(sizeof(RECT)); printf("InputAdressandSize!!"); scanf("%d%d",&back->address,&back->size);/*输入回收地址和大小*/ check=backcheck(head,back);/*检查*/
6、 if(check==1) { if(tolower(way)=='f')/*首先适应算法*/ acceptment1(head,back);/*首先适应*/ else acceptment2(head,back);/*最佳适应*/ print(head); } } }while(!strcmp(choose,"as")
7、
8、!strcmp(choose,"ac"));}/*分配函数*/RECT*assignment(RECT
9、*head,intapplication){ RECT*after,*before,*assign; assign=malloc(sizeof(RECT));/*分配申请空间*/ assign->size=application; assign->next=NULL; if(application>head->size
10、
11、application<=0) assign->address=-1;/*申请无效*/ else { before=head; after=head->next; whi