首页 / 算法 / 操作系统-内存管理-最佳适应算法
操作系统-内存管理-最佳适应算法
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了操作系统-内存管理-最佳适应算法,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含6762字,纯文字阅读大概需要10分钟。
内容图文
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <time.h>
using namespace std;
struct node{
//status表示当前节点的状态 0:空闲 1:占用
//free_space表示当前节点的空余空间
//*str表示当前存储字符串的首地址
//index表示当前第几块空闲区,充当地址的作用
int free_space,status,index;
char *str;
node *next;
};
node *p,*s[15];
int piece;//记录当前系统中有几块空闲区
void priority_sort(node *s[],int size);
void display(node *s[],int size);
void init_s(node *p){
cout<<"重新初始化s数组"<<endl;
node *temp = p;
piece = 0;
while(temp != NULL){
if(temp->status == 0){
s[piece++] = temp;
// cout<<"第"<<piece<<"块剩余空间"<<temp->free_space<<endl;
}
temp = temp->next;
}
/*
temp = p;
cout<<"链表"<<endl;
while(temp != NULL){
cout<<temp->index<<" "<<temp->status<<endl;
temp = temp->next;
}
cout<<"s数组"<<endl;
for(int i=0;i<piece;i++){
cout<<s[i]->index<<" "<<s[i]->free_space<<endl;
}
*/
cout<<"更新s数组后空闲区块数:"<<piece<<endl;
}
node *init(node *p){
p = (node *)malloc(sizeof(node));
node *first = p;
srand((unsigned)time(NULL));
for(int i=0;i<10;i++){
//cout<<(rand()%10)<<endl;
int temp = rand()%10+1;
first->index = i;
first->str = (char *)malloc(sizeof(char));
first->free_space = temp;
first->status = 0;
first->next = (node *)malloc(sizeof(node));
s[i] = first;
piece++;
if(i != 9)
first = first->next;
}
first->next = NULL;
cout<<"当前系统中有"<<piece<<"块空闲区"<<endl;
//display(s,piece);
priority_sort(s,piece);
cout<<"按照剩余空间升序排列后"<<endl;
display(s,piece);
return p;
}
void display(node *ss[],int size){
for(int i=0;i<size;i++)
cout<<"第"<<i<<"块空闲空间:"<<ss[i]->free_space<<" 链表起始地址:"<<s[i]->index<<endl;
}
void priority_sort(node *s[],int size){
for(int i=0;i<size;i++)
for(int j=i+1;j<size;j++)
if(s[j]->free_space < s[i]->free_space)
swap(s[i],s[j]);
}
void findMinNode(node *ss[],int size,char *str){
//printf("%d\n",strlen(temp));
int len = strlen(str);
bool flag = false;
node *temp1 = p;
/*
cout<<"插入前链表状态"<<endl;
while(temp1 != NULL){
cout<<"链表下标"<<temp1->index<<endl;
temp1 = temp1->next;
}
*/
for(int i=0;i<size;i++){
if(ss[i]->free_space == len){
ss[i]->str = str;
//cout<<"ss[i]->str "<<ss[i]->str<<endl;
ss[i]->free_space = 0;
ss[i]->status = 1;
flag = true;
break;
}
if(ss[i]->free_space > len){
node *tail;
tail = (node *)malloc(sizeof(node));
tail->str = (char *)malloc(sizeof(char));
tail->status = 0;
tail->free_space = ss[i]->free_space-len;
tail->index = ss[i]->index+1;
node *t = ss[i]->next;
int tt = tail->index+1;
while(t != NULL){
t->index = tt++;
t = t->next;
}
tail->next = ss[i]->next;
ss[i]->next = tail;
ss[i]->str = str;
//cout<<"ss[i]->str "<<ss[i]->str<<endl;
ss[i]->free_space = 0;
ss[i]->status = 1;
flag = true;
size++;
break;
}
}
/*
cout<<"插入后链表状态"<<endl;
temp1 = p;
while(temp1 != NULL){
cout<<"链表下标"<<temp1->index<<" 字符串"<<temp1->str<<endl;
temp1 = temp1->next;
}
*/
if(flag){
cout<<"成功插入字符串:"<<str;
cout<<"剩余空闲区:"<<size-1<<"块"<<endl;
}
else
cout<<"没有空闲区能装入字符串:"<<str<<endl;
init_s(p);
/*
cout<<"s数组"<<piece<<endl;
for(int i=0;i<piece;i++){
cout<<s[i]->index<<" "<<s[i]->free_space<<endl;
}
*/
priority_sort(ss,piece);
}
void del(node *pp,int index){
node *temp = p,*top = NULL,*tail = NULL,*mid;
//temp是找到要是释放的节点
while(temp != NULL){
if(temp->index == index - 1)
top = temp;
if(temp->index == index)
mid = temp;
if(temp->index == index + 1)
tail = temp;
temp = temp->next;
}
int len = strlen(mid->str);
node *temp1 = p;
/*
cout<<"释放前链表状态"<<endl;
while(temp1 != NULL){
cout<<"链表下标"<<temp1->index<<" 剩余空间"<<temp1->free_space<<endl;
temp1 = temp1->next;
}
if(top != NULL)
cout<<"top找到了"<<endl;
if(tail != NULL)
cout<<"tail找到了"<<endl;
*/
if(top != NULL && tail != NULL){
cout<<"top and tail are not NULL"<<endl;
//如果上下两块都没有被使用三块合并
if(top->status == 0 && tail->status == 0){
cout<<"!top->status && !tail->status"<<endl;
int next_index = top->index+1;
top->next = tail->next;
top->free_space = len + top->free_space + tail->free_space;
free(mid);
free(tail);
node *temp1 = top->next;
while(temp1 != NULL){
temp1->index = next_index++;
temp1 = temp1->next;
}
}
//如果下一块没有被使用和下一块合并
else if(top->status == 0){
cout<<"!top->status"<<endl;
int next_index = top->index+1;
top->next = mid->next;
top->free_space = len + top->free_space ;
free(mid);
node *temp1 = top->next;
while(temp1 != NULL){
temp1->index = next_index++;
temp1 = temp1->next;
}
}
//如果上一块没有被使用和上一块合并
else if(tail->status == 0){
cout<<"!tail->status"<<endl;
int next_index = mid->index+1;
mid->next = tail->next;
mid->free_space = len + tail->free_space ;
free(tail);
node *temp1 = mid->next;
while(temp1 != NULL){
temp1->index = next_index++;
temp1 = temp1->next;
}
}
//如果上下都被使用则插入到对位
else{
cout<<"插入队尾"<<endl;
top->next = mid->next;
node *temp1 = mid->next;
while(temp1->next !=NULL){
temp1 = temp1->next;
}
temp1->next = mid;
mid->index = temp1->index+1;
mid->next = NULL;
mid->free_space = len;
mid->status = 0;
}
}
//如果被释放的是链表中最后一块
else if(top !=NULL){
cout<<"top is not NULL"<<endl;
if(!top->status){
cout<<"!top->status"<<endl;
int next_index = top->index+1;
top->next = mid->next;
top->free_space = len + top->free_space ;
free(mid);
node *temp1 = top->next;
while(temp1 != NULL){
temp1->index = next_index++;
temp1 = temp1->next;
}
}else{
cout<<"插入队尾"<<endl;
int next_index = mid->index;
node *temp1 = mid->next;
while(temp1 != NULL){
temp1->index = next_index++;
temp1 = temp1->next;
}
temp1->next = mid;
mid->index = temp1->index+1;
mid->next = NULL;
mid->free_space = len;
}
}
//如果被释放的是链表中第一块
else if(tail != NULL){
cout<<"tail is not NULL"<<endl;
if(!tail->status){
cout<<"!tail->status"<<endl;
int next_index = mid->index+1;
mid->next = tail->next;
mid->free_space = len + tail->free_space ;
mid->status = 0;
free(tail);
node *temp1 = mid->next;
while(temp1 != NULL){
temp1->index = next_index++;
temp1 = temp1->next;
}
}else{
cout<<"插入队尾"<<endl;
node *temp1 = mid->next;
while(temp1->next != NULL){
temp1 = temp1->next;
}
temp1->next = mid;
mid->index = temp1->index+1;
mid->next = NULL;
mid->free_space = len;
}
}
cout<<"释放后链表状态"<<endl;
temp1 = p;
while(temp1 != NULL){
cout<<"链表下标"<<temp1->index<<" 剩余空间"<<temp1->free_space<<endl;
temp1 = temp1->next;
}
init_s(p);
priority_sort(s,piece);
}
int main(){
//printf("char类型内存中占字节数:%d\n",sizeof(char));
//printf("字符串str内存中占字节数:%d\n",sizeof(str));
printf("\t\t\t\t实验五 基本存储管理\n");
printf("\t\t\t\t 最佳适应算法\n");
p = init(p);
char temp[10];
while(1){
gets(temp);
if(temp[0] == '!')
break;
findMinNode(s,piece,temp);
display(s,piece);
}
while(1){
cout<<"输出你要释放那条数据"<<endl;
gets(temp);
node *t = p;
bool flag = false;
node *temp1 = p;
while(temp1 != NULL){
if(strcmp(temp1->str,temp) == 0){
flag = true;
del(p,temp1->index);
break;
}
temp1 = temp1->next;
}
display(s,piece);
if(flag)
cout<<"找到了,释放了该数据"<<endl;
else
cout<<"没找到"<<endl;
getchar();
}
return 0;
}
内容总结
以上是互联网集市为您收集整理的操作系统-内存管理-最佳适应算法全部内容,希望文章能够帮你解决操作系统-内存管理-最佳适应算法所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。