ICode9

精准搜索请尝试: 精确搜索
首页 > 系统相关> 文章详细

操作系统-内存管理-最佳适应算法

2020-12-01 17:58:11  阅读:201  来源: 互联网

标签:index 操作系统 space mid temp1 free next 算法 内存


#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;
}

标签:index,操作系统,space,mid,temp1,free,next,算法,内存
来源: https://blog.csdn.net/qq_43149127/article/details/110442333

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有