设计一个可变式分区分配的存储管理方案。并模拟实现分区的分配和回收过程。
对分区的管理法可以是下面三种算法之一:
首次适应算法
循环首次适应算法
最佳适应算法
对于测试样例 :
首地址 大小
123 3
125 2
213 4
作业名 大小
1 3
2 3
3 1
从123开始查找,其大小满足作业1的需求,因此直接分配,开始作业2,再从头开始,123已经不能再分配,查找125,125由于被占用了1个地址单元,可以利用的只有1个地址单元,继续寻找,213满足,分配,开始作业3,此时125刚好满足,只不过地址是从126开始的。
代码:
#include <iostream> #include <cstring> #define Max_num 10 #define Max_job_num 10 using namespace std; struct partition { int address; int size; bool state; partition& operator = (partition& B) { this->address = B.address; this->size = B.size; this->state = B.state; return *this; } }; typedef struct { string name; int size; } job; typedef struct { partition data[100]; } Singlypartition; typedef struct { job data[100]; } Singlyjob; Singlypartition p; Singlyjob j; int part_num,job_num; int address[10]; int address1[10]; void init() { for(int i = 0; i<100; i++) { p.data[i].size = 0; j.data[i].size = 0; } cout<<"input the number of partition(less than 10) : "; cin>>part_num; if(part_num > 10) { cout<<"out of boundary"; return; } for(int i = 0; i<part_num; i++) { cout<<"starting address: "; cin>>p.data[i].address; cout<<"the NO."<<i+1<<" size: "; cin>>p.data[i].size; p.data[i].state = true; } cout<<"input the number of job(less than 10) : "; cin>>job_num; if(job_num > 10) { cout<<"out of boundary"; return; } for(int i = 0; i<job_num; i++) { cout<<"job's name: "; cin>>j.data[i].name; cout<<"size: "; cin>>j.data[i].size; } } void sort() { for(int i = 0; i<part_num - 1; i++) { for(int k = i; k<part_num; k++) { if(p.data[i].size > p.data[k].size) swap(p.data[i],p.data[k]); } } } bool find(int ad,int js) { for(int i = 0;i<10;i++) { if(address[i] - ad >= js && address1[i] < ad && address[i] != -1) return true; } return false; } bool jud(int ad,int js) { for(int i = 0;i<10;i++) { if(ad + js > address1[i] && ad < address1[i] && address1[i] != -1) return true; } return false; } void FF() { int index = 0; while(1) { int judge = 0; if(index == job_num) return; int i; for(i = 0; i<part_num; i++) { if(p.data[i].state == true && p.data[i].size >= j.data[index].size && !jud(p.data[i].address,j.data[index].size) && !find(p.data[i].address,j.data[index].size)) { address1[index] = p.data[i].address; p.data[i].state = false; cout<<"partition address "<<"partition size " <<"job name "<<"job size "<<"partition state"<<endl; cout<<" "<<p.data[i].address<<" "<<p.data[i].size <<" "<<j.data[index].name <<" "<<j.data[index].size<<" " <<"have been allocated"<<endl; address[index] = p.data[i].address + j.data[index].size; i = part_num; judge = 1; } else continue; } if(judge == 0) cout<<"can not allocate: "<<j.data[index].name<<endl; index++; } } void NF() { int index = 0; int signal = 0; int judge = 0; int i = 0; int k = 0; int index1 = 0; while(index != job_num) { i = signal; index1 = 0; while(p.data[i].size != 0 && index1 <= index) { if(p.data[i].state == true && p.data[i].size >= j.data[index].size && !jud(p.data[i].address,j.data[index].size) && !find(p.data[i].address,j.data[index].size)) { address1[index] = p.data[i].address; p.data[i].state = false; cout<<"partition address "<<"partition size " <<"job name "<<"job size "<<"partition state"<<endl; cout<<" "<<p.data[i].address<<" "<<p.data[i].size <<" "<<j.data[index].name <<" "<<j.data[index].size<<" " <<"have been allocated"<<endl; address[index] = p.data[i].address + j.data[index].size; i++; signal = i; judge = 1; break; } else { swap(p.data[i],p.data[k+part_num]); i++; k++; signal = i; index1++; } } if(judge == 0) cout<<"can not allocate: "<<j.data[index].name<<endl; index++; judge = 0; } } void BF() { sort(); FF(); } int main() { int k; cout<<" 1.FF"<<endl<<" 2.NF"<<endl<<" 3.BF"<<endl<<" 4.quit"<<endl; cout<<"please choose: "; cin>>k; memset(address,-1,sizeof(address)); memset(address1,-1,sizeof(address1)); switch(k) { case 1: init(); FF(); break; case 2: init(); NF(); break; case 3: init(); BF(); break; case 4: break; default: break; } return 0; }