【OS课程设计一】动态分区式存储管理的存储分配和回收
来源:互联网 发布:mysql递归函数 编辑:程序博客网 时间:2024/05/17 23:12
动态分区式存储管理的存储分配和回收
一、设计目的
1、理解动态分区式存储管理的工作原理
2、掌握分区分配的一下三算法:首先适应算法、最佳适应算法和最坏适应算法
3、掌握动态分区分配的存储分配和存储回收的过程
二、设计要求
1、建立空闲分区表数据文件,该文件包括两个字段:空闲区的起始地址和长度;该文件有若干个记录,表示目前系统有多个空闲分区;
2、建立已分配分区表数据文件,该文件包括三个字段:已分配分区的起始地址、长度、作业名称;该文件有若干个记录,表示目前系统有多个作业;
3、程序启动时读两分区表数据文件,并在屏幕上显示内存的使用状态
3、接受用户的内存申请,格式:作业名、申请空间的大小
4、分别按照三种内存分配算法,选择一个空闲分区,分割,修改空闲分区表、填写已分配分区表;
5、接收作业结束信息,回收分配给作业的分区,必要时合并空闲分区,改写两个分区表;
6、在接受用户的内存申请时,当申请空间的大小大于目前任意一个空闲分区而小于若干个空闲分区的大小之和时,分配前进行碎片整理
Free_Info.h
- #ifndef _FREE_INFO_H_
- #define _FREE_INFO_H_
- class Free_Info
- {
- public:
- Free_Info(unsigned int sa,unsigned int l):start_addr(sa),len(l){}
- unsigned int get_Start_Addr() const
- {
- return start_addr;
- }
- void set_Start_Addr(unsigned int sa)
- {
- start_addr = sa;
- }
- unsigned int get_Len() const
- {
- return len;
- }
- void set_Len(unsigned int l)
- {
- len = l;
- }
- private:
- unsigned int start_addr;
- unsigned int len;
- };
- #endif
Allocated_Info.h
- #ifndef _ALLOCATED_INFO_H_
- #define _ALLOCATED_INFO_H_
- #include <string>
- using std::string;
- class Allocated_Info
- {
- public:
- Allocated_Info(string pn,unsigned int sa,unsigned int l):proc_name(pn),start_addr(sa),len(l){}
- string get_Proc_Name() const
- {
- return proc_name;
- }
- void set_Proc_Name(string pn)
- {
- proc_name = pn;
- }
- unsigned int get_Start_Addr() const
- {
- return start_addr;
- }
- void set_Start_Addr(unsigned int sa)
- {
- start_addr = sa;
- }
- unsigned int get_Len()
- {
- return len;
- }
- void set_Len(unsigned int l)
- {
- len = l;
- }
- private:
- string proc_name;
- unsigned int start_addr;
- unsigned int len;
- };
- #endif
Memory_Routine.h
- #ifndef _MEMORY_ROUTINE_H_
- #define _MEMORY_ROUTINE_H_
- #include <string>
- #include <list>
- #include "Free_Info.h"
- #include "Allocated_Info.h"
- using std::string;
- using std::list;
- class Memory_Routine
- {
- public:
- Memory_Routine();
- virtual ~Memory_Routine(){}
- virtual void memory_Request(string proc_name,unsigned int size) = 0;
- virtual void sort_Free_Info() = 0;
- void memory_Call_Back(string proc_name);
- void memory_Defragment();
- unsigned int get_Avai_Memory();
- unsigned int get_Allocated_Memory();
- void disp_Free_Info();
- void disp_Allocated_Info();
- protected:
- list<Free_Info> free_info;
- list<Allocated_Info> allocated_info;
- static unsigned int memory_size;
- void memory_Join();
- };
- #endif
Memory_Routine_By_BFA.h
- #ifndef _MEMORY_ROUTINE_BY_BFA_H_
- #define _MEMORY_ROUTINE_BY_BFA_H_
- #include "Memory_Routine.h"
- class Memory_Routine_By_BFA : public Memory_Routine
- {
- public:
- Memory_Routine_By_BFA();
- virtual void memory_Request(string proc_name,unsigned int size);
- virtual void sort_Free_Info();
- };
- #endif
Memory_Routine_By_FFA.h
- #ifndef _MEMORY_ROUTINE_BY_FFA_H_
- #define _MEMORY_ROUTINE_BY_FFA_H_
- #include "Memory_Routine.h"
- class Memory_Routine_By_FFA : public Memory_Routine
- {
- public:
- Memory_Routine_By_FFA();
- virtual void memory_Request(string proc_name,unsigned int size);
- virtual void sort_Free_Info();
- };
- #endif
Memory_Routine_By_WFA.h
- #ifndef _MEMORY_ROUTINE_BY_WFA_H_
- #define _MEMORY_ROUTINE_BY_WFA_H_
- #include "Memory_Routine.h"
- class Memory_Routine_By_WFA : public Memory_Routine
- {
- public:
- Memory_Routine_By_WFA();
- virtual void memory_Request(string proc_name,unsigned int size);
- virtual void sort_Free_Info();
- };
- #endif
Alloc_Info_Sort.h:
- #ifndef _ALLOC_INFO_SORT_H_
- #define _ALLOC_INFO_SORT_H_
- #include "Allocated_Info.h"
- class Alloc_Info_Sort
- {
- public:
- bool operator()(const Allocated_Info &ail,const Allocated_Info &air)
- {
- return ail.get_Start_Addr() < air.get_Start_Addr() ? 1 : 0;
- }
- };
- #endif
Best_Fit_Algo_Sort.h
- #ifndef _BEST_FIT_ALGO_SORT_H_
- #define _BEST_FIT_ALGO_SORT_H_
- #include "Free_Info.h"
- class Best_Fit_Algo_Sort
- {
- public:
- bool operator()(const Free_Info &fil,const Free_Info &fir)
- {
- return fil.get_Len() < fir.get_Len() ? 1 : 0;
- }
- };
- #endif
First_Fit_Algo_Sort.h
- #ifndef _FIRST_FIT_ALGO_SORT_H_
- #define _FIRST_FIT_ALGO_SORT_H_
- #include "Free_Info.h"
- class First_Fit_Algo_Sort
- {
- public:
- bool operator()(const Free_Info &fil,const Free_Info &fir)
- {
- return fil.get_Start_Addr() < fir.get_Start_Addr() ? 1 : 0;
- }
- };
- #endif
Worst_Fit_Algo_Sort.h
- #ifndef _WORST_FIT_ALGO_SORT_H_
- #define _WORST_FIT_ALGO_SORT_H_
- #include "Free_Info.h"
- class Worst_Fit_Algo_Sort
- {
- public:
- bool operator()(const Free_Info &fil,const Free_Info &fir)
- {
- return fil.get_Len() > fir.get_Len() ? 1 : 0;
- }
- };
- #endif
Memory_Routine.cpp
- #include <iostream>
- #include <algorithm>
- #include "Memory_Routine.h"
- #include "First_Fit_Algo_Sort.h"
- #include "Alloc_Info_Sort.h"
- using std::cout;
- using std::endl;
- using std::cerr;
- unsigned int Memory_Routine::memory_size = 1024;
- Memory_Routine::Memory_Routine()
- {
- free_info.push_back(Free_Info(0,1024));
- //free_info.push_back(Free_Info(4096,2048));
- //free_info.push_back(Free_Info(10000,1024));
- }
- void Memory_Routine::memory_Call_Back(string proc_name)
- {
- list<Allocated_Info>::iterator ai_it;
- for(ai_it = allocated_info.begin();ai_it != allocated_info.end();ai_it++)
- {
- if(ai_it->get_Proc_Name() == proc_name)
- {
- break;
- }
- }
- if(ai_it == allocated_info.end())
- {
- cerr<<"process name was not found"<<endl;
- return;
- }
- bool flag;
- while(true)
- {
- flag = false;
- for(ai_it = allocated_info.begin();ai_it != allocated_info.end();ai_it++)
- {
- if(ai_it->get_Proc_Name() == proc_name)
- {
- free_info.push_back(Free_Info(ai_it->get_Start_Addr(),ai_it->get_Len()));
- allocated_info.erase(ai_it);
- flag = true;
- break;
- }
- }
- if(!flag)
- {
- free_info.sort(First_Fit_Algo_Sort());
- memory_Join();
- sort_Free_Info();
- break;
- }
- }
- cout<<"memory call back success"<<endl;
- }
- void Memory_Routine::memory_Defragment()
- {
- list<Allocated_Info>::iterator ai_it = allocated_info.begin();
- unsigned int new_addr;
- allocated_info.sort(Alloc_Info_Sort());
- if(ai_it->get_Start_Addr() != 0)
- {
- ai_it->set_Start_Addr(0);
- }
- new_addr = ai_it->get_Len();
- ai_it++;
- for(;ai_it != allocated_info.end();ai_it++)
- {
- ai_it->set_Start_Addr(new_addr);
- new_addr = ai_it->get_Start_Addr() + ai_it->get_Len();
- }
- free_info.clear();
- free_info.push_back(Free_Info(new_addr,memory_size - get_Allocated_Memory()));
- }
- unsigned int Memory_Routine::get_Avai_Memory()
- {
- list<Free_Info>::iterator fi_it;
- unsigned int avai_memory = 0;
- for(fi_it = free_info.begin();fi_it != free_info.end();fi_it++)
- {
- avai_memory += fi_it->get_Len();
- }
- return avai_memory;
- }
- unsigned int Memory_Routine::get_Allocated_Memory()
- {
- list<Allocated_Info>::iterator ai_it;
- unsigned int alloc_memory = 0;
- for(ai_it = allocated_info.begin();ai_it != allocated_info.end();ai_it++)
- {
- alloc_memory += ai_it->get_Len();
- }
- return alloc_memory;
- }
- void Memory_Routine::disp_Free_Info()
- {
- list<Free_Info>::iterator fi_it;
- cout<<"------------------free_info-------------------"<<endl;
- for(fi_it = free_info.begin();fi_it != free_info.end();fi_it++)
- {
- cout<<"start address:"<<fi_it->get_Start_Addr();
- cout<<" length:"<<fi_it->get_Len()<<endl;
- }
- cout<<"available memory is:"<<get_Avai_Memory()<<endl;
- cout<<"----------------------------------------------"<<endl;
- }
- void Memory_Routine::disp_Allocated_Info()
- {
- //memory_Defragment();
- list<Allocated_Info>::iterator ai_it;
- cout<<"----------------allocated_info----------------"<<endl;
- if(allocated_info.size() == 0)
- {
- cout<<"********************EMPTY!********************"<<endl;
- }
- else
- {
- for(ai_it = allocated_info.begin();ai_it != allocated_info.end();ai_it++)
- {
- cout<<"process name:"<<ai_it->get_Proc_Name();
- cout<<" start address:"<<ai_it->get_Start_Addr();
- cout<<" length:"<<ai_it->get_Len()<<endl;
- }
- }
- cout<<"allocated memory is:"<<get_Allocated_Memory()<<endl;
- cout<<"----------------------------------------------"<<endl;
- }
- void Memory_Routine::memory_Join()
- {
- list<Free_Info>::iterator fi_it,fi_prev,fi_next;
- bool flag;
- while(true)
- {
- if(free_info.size() == 1)
- {
- return;
- }
- flag = false;
- for(fi_it = free_info.begin();fi_it != free_info.end();)
- {
- fi_prev = fi_it;
- fi_it++;
- if(fi_prev->get_Start_Addr() + fi_prev->get_Len() == fi_it->get_Start_Addr())
- {
- fi_prev->set_Len(fi_prev->get_Len() + fi_it->get_Len());
- free_info.erase(fi_it);
- flag = true;
- break;
- }
- }
- if(!flag)
- {
- break;
- }
- }
- }
Memory_Routine_By_BFA.cpp
- #include "Memory_Routine_By_BFA.h"
- #include "Best_Fit_Algo_Sort.h"
- #include <iostream>
- using std::cout;
- using std::cerr;
- using std::endl;
- Memory_Routine_By_BFA::Memory_Routine_By_BFA()
- {
- sort_Free_Info();
- }
- void Memory_Routine_By_BFA::memory_Request(string proc_name,unsigned int size)
- {
- if(static_cast<int>(size) <= 0)
- {
- cerr<<"the size must be positive number"<<endl;
- return ;
- }
- list<Free_Info>::iterator fi_it;
- for(fi_it = free_info.begin();fi_it != free_info.end();fi_it++)
- {
- if(fi_it->get_Len() >= size)
- {
- break;
- }
- }
- if(fi_it != free_info.end())
- {
- allocated_info.push_back(Allocated_Info(proc_name,fi_it->get_Start_Addr(),size));
- if(fi_it->get_Len() - size != 0)
- {
- fi_it->set_Start_Addr(fi_it->get_Start_Addr() + size);
- fi_it->set_Len(fi_it->get_Len() - size);
- }
- else
- {
- free_info.erase(fi_it);
- }
- free_info.sort(Best_Fit_Algo_Sort());
- cout<<"allocate success"<<endl;
- }
- else
- {
- if(get_Avai_Memory() >= size)
- {
- cout<<"memory defragment..."<<endl;
- memory_Defragment();
- memory_Request(proc_name,size);
- return;
- }
- else
- {
- cerr<<"memory request failed"<<endl;
- }
- }
- }
- void Memory_Routine_By_BFA::sort_Free_Info()
- {
- free_info.sort(Best_Fit_Algo_Sort());
- }
Memory_Routine_By_FFA.cpp
- #include "Memory_Routine_By_FFA.h"
- #include "First_Fit_Algo_Sort.h"
- #include <iostream>
- using std::cout;
- using std::cerr;
- using std::endl;
- Memory_Routine_By_FFA::Memory_Routine_By_FFA()
- {
- sort_Free_Info();
- }
- void Memory_Routine_By_FFA::memory_Request(string proc_name,unsigned int size)
- {
- if(static_cast<int>(size) <= 0)
- {
- cerr<<"the size must be positive number"<<endl;
- return ;
- }
- list<Free_Info>::iterator fi_it;
- for(fi_it = free_info.begin();fi_it != free_info.end();fi_it++)
- {
- if(fi_it->get_Len() >= size)
- {
- break;
- }
- }
- if(fi_it != free_info.end())
- {
- allocated_info.push_back(Allocated_Info(proc_name,fi_it->get_Start_Addr(),size));
- if(fi_it->get_Len() - size != 0)
- {
- fi_it->set_Start_Addr(fi_it->get_Start_Addr() + size);
- fi_it->set_Len(fi_it->get_Len() - size);
- }
- else
- {
- free_info.erase(fi_it);
- }
- free_info.sort(First_Fit_Algo_Sort());
- cout<<"allocate success"<<endl;
- }
- else
- {
- if(get_Avai_Memory() >= size)
- {
- cout<<"memory defragment..."<<endl;
- memory_Defragment();
- memory_Request(proc_name,size);
- return;
- }
- else
- {
- cerr<<"memory request failed"<<endl;
- }
- }
- }
- void Memory_Routine_By_FFA::sort_Free_Info()
- {
- free_info.sort(First_Fit_Algo_Sort());
- }
Memory_Routine_By_WFA.cpp
- #include "Memory_Routine_By_WFA.h"
- #include "Worst_Fit_Algo_Sort.h"
- #include <iostream>
- using std::cout;
- using std::cerr;
- using std::endl;
- Memory_Routine_By_WFA::Memory_Routine_By_WFA()
- {
- sort_Free_Info();
- }
- void Memory_Routine_By_WFA::memory_Request(string proc_name,unsigned int size)
- {
- if(static_cast<int>(size) <= 0)
- {
- cerr<<"the size must be positive number"<<endl;
- return ;
- }
- list<Free_Info>::iterator fi_it = free_info.begin();
- if(fi_it->get_Len() >= size)
- {
- allocated_info.push_back(Allocated_Info(proc_name,fi_it->get_Start_Addr(),size));
- if(fi_it->get_Len() - size != 0)
- {
- fi_it->set_Start_Addr(fi_it->get_Start_Addr() + size);
- fi_it->set_Len(fi_it->get_Len() - size);
- }
- else
- {
- free_info.erase(fi_it);
- }
- free_info.sort(Worst_Fit_Algo_Sort());
- cout<<"allocate success"<<endl;
- }
- else
- {
- if(get_Avai_Memory() >= size)
- {
- cout<<"memory defragment..."<<endl;
- memory_Defragment();
- memory_Request(proc_name,size);
- return;
- }
- else
- {
- cerr<<"memory request failed"<<endl;
- }
- }
- }
- void Memory_Routine_By_WFA::sort_Free_Info()
- {
- free_info.sort(Worst_Fit_Algo_Sort());
- }
main.cpp
- #include <iostream>
- #include <string>
- #include <limits>
- #include "Memory_Routine.h"
- #include "Memory_Routine_By_WFA.h"
- #include "Memory_Routine_By_FFA.h"
- #include "Memory_Routine_By_BFA.h"
- using std::cerr;
- using std::cout;
- using std::cin;
- using std::endl;
- using std::numeric_limits;
- using std::streamsize;
- int main()
- {
- Memory_Routine *mr;
- char c;
- cout<<"OS BIG WORK NO 3 BY MARCUSXING 2009 12 8"<<endl;
- cout<<"****************************************"<<endl;
- cout<<"please select a algorithm to allocate memory dynamically"<<endl;
- cout<<"1:First Fit Algorithm"<<endl;
- cout<<"2:Best Fit Algorithm"<<endl;
- cout<<"3:Worst Fit Algorithm"<<endl;
- cin>>c;
- switch(c)
- {
- case '1':
- mr = new Memory_Routine_By_FFA();
- break;
- case '2':
- mr = new Memory_Routine_By_BFA();
- break;
- case '3':
- mr = new Memory_Routine_By_WFA();
- break;
- default:
- cout<<"are you kidding me?"<<endl;
- return -1;
- break;
- }
- cin.ignore(numeric_limits<streamsize>::max(),'/n');
- mr->disp_Free_Info();
- mr->disp_Allocated_Info();
- cout<<"please decide what you want to do?"<<endl;
- cout<<"q:quit this program"<<endl;
- cout<<"h:display this help message"<<endl;
- cout<<"1:request memory for a process"<<endl;
- cout<<"2:call back all the memory of a process"<<endl;
- cout<<"3:display the available memory and allocated memory's infomation"<<endl;
- string proc_name;
- unsigned int size;
- bool flag = true;
- while(flag)
- {
- cout<<"please enter your choice:";
- if(!cin)
- {
- cin.ignore(numeric_limits<streamsize>::max(),'/n');
- }
- cin>>c;
- switch(c)
- {
- case 'h':
- cin.ignore(numeric_limits<streamsize>::max(),'/n');
- cout<<"please decide what you want to do?"<<endl;
- cout<<"q:quit this program"<<endl;
- cout<<"h:display this help message"<<endl;
- cout<<"1:request memory for a process"<<endl;
- cout<<"2:call back all the memory of a process"<<endl;
- cout<<"3:display the available memory and allocated memory's infomation"<<endl;
- break;
- case '1':
- cout<<"please enter the process name:";
- cin.ignore(numeric_limits<streamsize>::max(),'/n');
- cin>>proc_name;
- cin.ignore(numeric_limits<streamsize>::max(),'/n');
- cout<<"please enter the request memory size:";
- if(!(cin>>size))
- {
- cerr<<"the request size must be a positive number,request failed"<<endl;
- flag = false;
- break;
- }
- mr->memory_Request(proc_name,size);
- cin.ignore(numeric_limits<streamsize>::max(),'/n');
- break;
- case '2':
- cin.ignore(numeric_limits<streamsize>::max(),'/n');
- cout<<"please enter the process name:";
- cin>>proc_name;
- mr->memory_Call_Back(proc_name);
- cin.ignore(numeric_limits<streamsize>::max(),'/n');
- break;
- case '3':
- cin.ignore(numeric_limits<streamsize>::max(),'/n');
- mr->disp_Free_Info();
- mr->disp_Allocated_Info();
- break;
- case 'q':
- flag = false;
- break;
- default:
- cout<<"are you kidding me?"<<endl;
- flag = false;
- break;
- }
- }
- delete mr;
- return 0;
- }
- 【OS课程设计一】动态分区式存储管理的存储分配和回收
- 设计一个可变式分区分配的存储管理方案。并模拟实现分区的分配和回收过程
- 存储管理动态分区分配及回收算法
- 操作系统:存储管理动态分区分配及回收算法
- 操作系统实验_动态分区存储管理方式的主存分配回收
- 通信实验 动态分区存储管理方式的主存分配回收 查错
- 可变分区存储管理的内存分配与回收
- C语言内存的动态存储管理3-分配算法和回收
- 存储管理之动态分区分配(实验四)
- 分区存储管理——空闲分区的分配策略
- 可变存储分配和回收
- 模拟设计存储管理的分配与回收实践说明
- 存储管理-分区分配与回收算法(Java简单实现)
- 模拟可变分区存储管理的内存分配(C)
- 【OS】动态分区分配算法的比较
- 内存管理——动态分区时的分配与回收
- os课程设计:实现可变分区管理的模拟
- 动态多分区存储管理模拟系统
- 解决我在德国空间中文乱码的荒谬的过程
- red5 server 使用笔记(一)
- Tomcat6.0连接池配置
- 日记
- 寒冰老师的LINUX教程
- 【OS课程设计一】动态分区式存储管理的存储分配和回收
- 看到的帖子
- 【OS课程设计二】模拟DOS系统文件的物理结构和管理使用
- JavaScript类型转换与运算符
- 今天发现了新大陆
- 梯形法求定积分(一)设计梯形法求积分的类模板,梯形法
- 忠言逆耳
- 梯形法求定积分(二)--设计一个函数模板,独立的非成员函
- 南昌大学学生学习服务中心标志