【OS课程设计一】动态分区式存储管理的存储分配和回收

来源:互联网 发布:mysql递归函数 编辑:程序博客网 时间:2024/05/17 23:12

动态分区式存储管理的存储分配和回收

一、设计目的
1、理解动态分区式存储管理的工作原理
2、掌握分区分配的一下三算法:首先适应算法、最佳适应算法和最坏适应算法
3、掌握动态分区分配的存储分配和存储回收的过程
二、设计要求
1、建立空闲分区表数据文件,该文件包括两个字段:空闲区的起始地址和长度;该文件有若干个记录,表示目前系统有多个空闲分区;
2、建立已分配分区表数据文件,该文件包括三个字段:已分配分区的起始地址、长度、作业名称;该文件有若干个记录,表示目前系统有多个作业;
3、程序启动时读两分区表数据文件,并在屏幕上显示内存的使用状态
3、接受用户的内存申请,格式:作业名、申请空间的大小
4、分别按照三种内存分配算法,选择一个空闲分区,分割,修改空闲分区表、填写已分配分区表;
5、接收作业结束信息,回收分配给作业的分区,必要时合并空闲分区,改写两个分区表;
6、在接受用户的内存申请时,当申请空间的大小大于目前任意一个空闲分区而小于若干个空闲分区的大小之和时,分配前进行碎片整理
Free_Info.h
Code:
  1. #ifndef _FREE_INFO_H_   
  2. #define _FREE_INFO_H_   
  3.        
  4.     class Free_Info   
  5.     {   
  6.     public:   
  7.         Free_Info(unsigned int sa,unsigned int l):start_addr(sa),len(l){}      
  8.         unsigned int get_Start_Addr() const  
  9.         {   
  10.             return start_addr;   
  11.         }   
  12.            
  13.         void set_Start_Addr(unsigned int sa)    
  14.         {   
  15.             start_addr = sa;   
  16.         }   
  17.            
  18.         unsigned int get_Len() const  
  19.         {   
  20.             return len;   
  21.         }   
  22.            
  23.         void set_Len(unsigned int l)   
  24.         {   
  25.             len = l;   
  26.         }   
  27.            
  28.     private:   
  29.         unsigned int start_addr;   
  30.         unsigned int len;   
  31.     };   
  32.   
  33. #endif   
Allocated_Info.h
Code:
  1. #ifndef _ALLOCATED_INFO_H_   
  2. #define _ALLOCATED_INFO_H_   
  3. #include <string>   
  4.     using std::string;   
  5.     class Allocated_Info   
  6.     {   
  7.     public:   
  8.         Allocated_Info(string pn,unsigned int sa,unsigned int l):proc_name(pn),start_addr(sa),len(l){}   
  9.            
  10.         string get_Proc_Name() const  
  11.         {   
  12.             return proc_name;   
  13.         }   
  14.            
  15.         void set_Proc_Name(string pn)   
  16.         {   
  17.             proc_name = pn;   
  18.         }   
  19.            
  20.         unsigned int get_Start_Addr() const  
  21.         {   
  22.             return start_addr;   
  23.         }   
  24.            
  25.         void set_Start_Addr(unsigned int sa)   
  26.         {   
  27.             start_addr = sa;   
  28.         }   
  29.            
  30.         unsigned int get_Len()   
  31.         {   
  32.             return len;   
  33.         }   
  34.            
  35.         void set_Len(unsigned int l)   
  36.         {   
  37.             len = l;   
  38.         }   
  39.            
  40.     private:   
  41.         string proc_name;   
  42.         unsigned int start_addr;   
  43.         unsigned int len;   
  44.     };   
  45. #endif   

Memory_Routine.h

Code:
  1. #ifndef _MEMORY_ROUTINE_H_   
  2. #define _MEMORY_ROUTINE_H_   
  3. #include <string>   
  4. #include <list>   
  5. #include "Free_Info.h"   
  6. #include "Allocated_Info.h"   
  7.   
  8.     using std::string;   
  9.     using std::list;   
  10.     class Memory_Routine   
  11.     {   
  12.     public:   
  13.         Memory_Routine();   
  14.         virtual ~Memory_Routine(){}   
  15.         virtual void memory_Request(string proc_name,unsigned int size) = 0;   
  16.         virtual void sort_Free_Info() = 0;   
  17.         void memory_Call_Back(string proc_name);   
  18.         void memory_Defragment();    
  19.         unsigned int get_Avai_Memory();   
  20.         unsigned int get_Allocated_Memory();   
  21.         void disp_Free_Info();   
  22.         void disp_Allocated_Info();   
  23.            
  24.     protected:   
  25.         list<Free_Info> free_info;   
  26.         list<Allocated_Info> allocated_info;   
  27.         static unsigned int memory_size;   
  28.         void memory_Join();   
  29.     };   
  30. #endif   

Memory_Routine_By_BFA.h

Code:
  1. #ifndef _MEMORY_ROUTINE_BY_BFA_H_   
  2. #define _MEMORY_ROUTINE_BY_BFA_H_   
  3. #include "Memory_Routine.h"   
  4.   
  5.     class Memory_Routine_By_BFA : public Memory_Routine   
  6.     {   
  7.     public:   
  8.         Memory_Routine_By_BFA();   
  9.         virtual void memory_Request(string proc_name,unsigned int size);   
  10.         virtual void sort_Free_Info();   
  11.     };   
  12. #endif   

Memory_Routine_By_FFA.h

Code:
  1. #ifndef _MEMORY_ROUTINE_BY_FFA_H_   
  2. #define _MEMORY_ROUTINE_BY_FFA_H_   
  3. #include "Memory_Routine.h"   
  4.   
  5.     class Memory_Routine_By_FFA : public Memory_Routine   
  6.     {   
  7.     public:   
  8.         Memory_Routine_By_FFA();   
  9.         virtual void memory_Request(string proc_name,unsigned int size);   
  10.         virtual void sort_Free_Info();   
  11.     };   
  12. #endif   

Memory_Routine_By_WFA.h

Code:
  1. #ifndef _MEMORY_ROUTINE_BY_WFA_H_   
  2. #define _MEMORY_ROUTINE_BY_WFA_H_   
  3. #include "Memory_Routine.h"   
  4.   
  5.     class Memory_Routine_By_WFA : public Memory_Routine   
  6.     {   
  7.     public:   
  8.         Memory_Routine_By_WFA();   
  9.         virtual void memory_Request(string proc_name,unsigned int size);   
  10.         virtual void sort_Free_Info();   
  11.     };   
  12. #endif   

Alloc_Info_Sort.h:

Code:
  1. #ifndef _ALLOC_INFO_SORT_H_   
  2. #define _ALLOC_INFO_SORT_H_   
  3. #include "Allocated_Info.h"   
  4.   
  5.     class Alloc_Info_Sort   
  6.     {   
  7.     public:   
  8.         bool operator()(const Allocated_Info &ail,const Allocated_Info &air)   
  9.         {   
  10.             return ail.get_Start_Addr() < air.get_Start_Addr() ? 1 : 0;   
  11.         }   
  12.     };   
  13. #endif   

Best_Fit_Algo_Sort.h

Code:
  1. #ifndef _BEST_FIT_ALGO_SORT_H_   
  2. #define _BEST_FIT_ALGO_SORT_H_   
  3. #include "Free_Info.h"   
  4.   
  5.     class Best_Fit_Algo_Sort   
  6.     {   
  7.     public:   
  8.         bool operator()(const Free_Info &fil,const Free_Info &fir)   
  9.         {   
  10.             return fil.get_Len() < fir.get_Len() ? 1 : 0;   
  11.         }   
  12.     };   
  13. #endif   

First_Fit_Algo_Sort.h

Code:
  1. #ifndef _FIRST_FIT_ALGO_SORT_H_   
  2. #define _FIRST_FIT_ALGO_SORT_H_   
  3. #include "Free_Info.h"   
  4.   
  5.     class First_Fit_Algo_Sort   
  6.     {   
  7.     public:   
  8.         bool operator()(const Free_Info &fil,const Free_Info &fir)   
  9.         {   
  10.             return fil.get_Start_Addr() < fir.get_Start_Addr() ? 1 : 0;   
  11.         }   
  12.     };   
  13. #endif   

Worst_Fit_Algo_Sort.h

Code:
  1. #ifndef _WORST_FIT_ALGO_SORT_H_   
  2. #define _WORST_FIT_ALGO_SORT_H_   
  3. #include "Free_Info.h"   
  4.   
  5.     class Worst_Fit_Algo_Sort   
  6.     {   
  7.     public:   
  8.         bool operator()(const Free_Info &fil,const Free_Info &fir)   
  9.         {   
  10.             return fil.get_Len() > fir.get_Len() ? 1 : 0;   
  11.         }   
  12.     };   
  13. #endif   

Memory_Routine.cpp

Code:
  1. #include <iostream>   
  2. #include <algorithm>   
  3. #include "Memory_Routine.h"   
  4. #include "First_Fit_Algo_Sort.h"   
  5. #include "Alloc_Info_Sort.h"   
  6.   
  7. using std::cout;   
  8. using std::endl;   
  9. using std::cerr;   
  10.   
  11. unsigned int Memory_Routine::memory_size = 1024;   
  12.   
  13. Memory_Routine::Memory_Routine()   
  14. {   
  15.     free_info.push_back(Free_Info(0,1024));   
  16.     //free_info.push_back(Free_Info(4096,2048));   
  17.     //free_info.push_back(Free_Info(10000,1024));   
  18. }   
  19.   
  20. void Memory_Routine::memory_Call_Back(string proc_name)   
  21. {   
  22.     list<Allocated_Info>::iterator ai_it;   
  23.     for(ai_it = allocated_info.begin();ai_it != allocated_info.end();ai_it++)   
  24.     {   
  25.         if(ai_it->get_Proc_Name() == proc_name)   
  26.         {   
  27.             break;   
  28.         }   
  29.     }   
  30.        
  31.     if(ai_it == allocated_info.end())   
  32.     {   
  33.         cerr<<"process name was not found"<<endl;   
  34.         return;   
  35.     }   
  36.        
  37.     bool flag;   
  38.     while(true)   
  39.     {   
  40.         flag = false;   
  41.         for(ai_it = allocated_info.begin();ai_it != allocated_info.end();ai_it++)   
  42.         {   
  43.             if(ai_it->get_Proc_Name() == proc_name)   
  44.             {   
  45.                 free_info.push_back(Free_Info(ai_it->get_Start_Addr(),ai_it->get_Len()));   
  46.                 allocated_info.erase(ai_it);   
  47.                 flag = true;   
  48.                 break;   
  49.             }   
  50.         }   
  51.            
  52.         if(!flag)   
  53.         {   
  54.             free_info.sort(First_Fit_Algo_Sort());   
  55.             memory_Join();   
  56.             sort_Free_Info();   
  57.             break;   
  58.         }   
  59.     }   
  60.     cout<<"memory call back success"<<endl;   
  61. }   
  62.   
  63. void Memory_Routine::memory_Defragment()   
  64. {   
  65.     list<Allocated_Info>::iterator ai_it = allocated_info.begin();   
  66.     unsigned int new_addr;   
  67.     allocated_info.sort(Alloc_Info_Sort());   
  68.     if(ai_it->get_Start_Addr() != 0)   
  69.     {   
  70.         ai_it->set_Start_Addr(0);   
  71.     }   
  72.        
  73.     new_addr = ai_it->get_Len();   
  74.     ai_it++;   
  75.     for(;ai_it != allocated_info.end();ai_it++)   
  76.     {   
  77.         ai_it->set_Start_Addr(new_addr);   
  78.         new_addr = ai_it->get_Start_Addr() + ai_it->get_Len();   
  79.     }   
  80.        
  81.     free_info.clear();   
  82.     free_info.push_back(Free_Info(new_addr,memory_size - get_Allocated_Memory()));   
  83. }   
  84.   
  85. unsigned int Memory_Routine::get_Avai_Memory()   
  86. {   
  87.     list<Free_Info>::iterator fi_it;   
  88.     unsigned int avai_memory = 0;   
  89.     for(fi_it = free_info.begin();fi_it != free_info.end();fi_it++)   
  90.     {   
  91.         avai_memory += fi_it->get_Len();   
  92.     }   
  93.     return avai_memory;   
  94. }   
  95.   
  96. unsigned int Memory_Routine::get_Allocated_Memory()   
  97. {   
  98.     list<Allocated_Info>::iterator ai_it;   
  99.     unsigned int alloc_memory = 0;   
  100.     for(ai_it = allocated_info.begin();ai_it != allocated_info.end();ai_it++)   
  101.     {   
  102.         alloc_memory += ai_it->get_Len();   
  103.     }   
  104.     return alloc_memory;   
  105. }   
  106.   
  107. void Memory_Routine::disp_Free_Info()   
  108. {   
  109.     list<Free_Info>::iterator fi_it;   
  110.     cout<<"------------------free_info-------------------"<<endl;   
  111.     for(fi_it = free_info.begin();fi_it != free_info.end();fi_it++)   
  112.     {   
  113.         cout<<"start address:"<<fi_it->get_Start_Addr();   
  114.         cout<<" length:"<<fi_it->get_Len()<<endl;   
  115.     }   
  116.     cout<<"available memory is:"<<get_Avai_Memory()<<endl;   
  117.     cout<<"----------------------------------------------"<<endl;   
  118. }   
  119.   
  120. void Memory_Routine::disp_Allocated_Info()   
  121. {   
  122.     //memory_Defragment();   
  123.     list<Allocated_Info>::iterator ai_it;   
  124.     cout<<"----------------allocated_info----------------"<<endl;   
  125.     if(allocated_info.size() == 0)   
  126.     {   
  127.         cout<<"********************EMPTY!********************"<<endl;   
  128.     }   
  129.     else  
  130.     {   
  131.         for(ai_it = allocated_info.begin();ai_it != allocated_info.end();ai_it++)   
  132.         {   
  133.             cout<<"process name:"<<ai_it->get_Proc_Name();   
  134.             cout<<" start address:"<<ai_it->get_Start_Addr();   
  135.             cout<<" length:"<<ai_it->get_Len()<<endl;   
  136.         }   
  137.     }   
  138.     cout<<"allocated memory is:"<<get_Allocated_Memory()<<endl;   
  139.     cout<<"----------------------------------------------"<<endl;   
  140. }   
  141.   
  142. void Memory_Routine::memory_Join()   
  143. {   
  144.     list<Free_Info>::iterator fi_it,fi_prev,fi_next;   
  145.     bool flag;   
  146.        
  147.     while(true)   
  148.     {   
  149.         if(free_info.size() == 1)   
  150.         {   
  151.             return;   
  152.         }   
  153.            
  154.         flag = false;   
  155.         for(fi_it = free_info.begin();fi_it != free_info.end();)   
  156.         {   
  157.             fi_prev = fi_it;   
  158.             fi_it++;   
  159.             if(fi_prev->get_Start_Addr() + fi_prev->get_Len() == fi_it->get_Start_Addr())   
  160.             {   
  161.                 fi_prev->set_Len(fi_prev->get_Len() + fi_it->get_Len());   
  162.                 free_info.erase(fi_it);   
  163.                 flag = true;   
  164.                 break;   
  165.             }   
  166.         }   
  167.            
  168.         if(!flag)   
  169.         {   
  170.             break;   
  171.         }   
  172.     }   
  173. }   

Memory_Routine_By_BFA.cpp

Code:
  1. #include "Memory_Routine_By_BFA.h"   
  2. #include "Best_Fit_Algo_Sort.h"   
  3. #include <iostream>   
  4.   
  5. using std::cout;   
  6. using std::cerr;   
  7. using std::endl;   
  8.   
  9. Memory_Routine_By_BFA::Memory_Routine_By_BFA()   
  10. {   
  11.     sort_Free_Info();   
  12. }   
  13.   
  14. void Memory_Routine_By_BFA::memory_Request(string proc_name,unsigned int size)   
  15. {   
  16.     if(static_cast<int>(size) <= 0)   
  17.     {   
  18.         cerr<<"the size must be positive number"<<endl;   
  19.         return ;   
  20.     }   
  21.        
  22.     list<Free_Info>::iterator fi_it;   
  23.     for(fi_it = free_info.begin();fi_it != free_info.end();fi_it++)   
  24.     {   
  25.         if(fi_it->get_Len() >= size)   
  26.         {   
  27.             break;   
  28.         }   
  29.     }   
  30.        
  31.     if(fi_it != free_info.end())   
  32.     {   
  33.         allocated_info.push_back(Allocated_Info(proc_name,fi_it->get_Start_Addr(),size));   
  34.         if(fi_it->get_Len() - size != 0)   
  35.         {   
  36.             fi_it->set_Start_Addr(fi_it->get_Start_Addr() + size);   
  37.             fi_it->set_Len(fi_it->get_Len() - size);   
  38.         }   
  39.         else  
  40.         {   
  41.             free_info.erase(fi_it);   
  42.         }   
  43.         free_info.sort(Best_Fit_Algo_Sort());   
  44.         cout<<"allocate success"<<endl;   
  45.     }   
  46.     else  
  47.     {   
  48.         if(get_Avai_Memory() >= size)   
  49.         {   
  50.             cout<<"memory defragment..."<<endl;   
  51.             memory_Defragment();   
  52.             memory_Request(proc_name,size);   
  53.             return;   
  54.         }   
  55.         else  
  56.         {   
  57.             cerr<<"memory request failed"<<endl;   
  58.         }   
  59.     }   
  60. }   
  61.   
  62. void Memory_Routine_By_BFA::sort_Free_Info()   
  63. {   
  64.     free_info.sort(Best_Fit_Algo_Sort());   
  65. }   

Memory_Routine_By_FFA.cpp

Code:
  1. #include "Memory_Routine_By_FFA.h"   
  2. #include "First_Fit_Algo_Sort.h"   
  3. #include <iostream>   
  4.   
  5. using std::cout;   
  6. using std::cerr;   
  7. using std::endl;   
  8.   
  9. Memory_Routine_By_FFA::Memory_Routine_By_FFA()   
  10. {   
  11.     sort_Free_Info();   
  12. }   
  13.   
  14. void Memory_Routine_By_FFA::memory_Request(string proc_name,unsigned int size)   
  15. {   
  16.     if(static_cast<int>(size) <= 0)   
  17.     {   
  18.         cerr<<"the size must be positive number"<<endl;   
  19.         return ;   
  20.     }   
  21.        
  22.     list<Free_Info>::iterator fi_it;   
  23.     for(fi_it = free_info.begin();fi_it != free_info.end();fi_it++)   
  24.     {   
  25.         if(fi_it->get_Len() >= size)   
  26.         {   
  27.             break;   
  28.         }   
  29.     }   
  30.        
  31.     if(fi_it != free_info.end())   
  32.     {   
  33.         allocated_info.push_back(Allocated_Info(proc_name,fi_it->get_Start_Addr(),size));   
  34.         if(fi_it->get_Len() - size != 0)   
  35.         {   
  36.             fi_it->set_Start_Addr(fi_it->get_Start_Addr() + size);   
  37.             fi_it->set_Len(fi_it->get_Len() - size);   
  38.         }   
  39.         else  
  40.         {   
  41.             free_info.erase(fi_it);   
  42.         }   
  43.         free_info.sort(First_Fit_Algo_Sort());   
  44.         cout<<"allocate success"<<endl;   
  45.     }   
  46.     else  
  47.     {   
  48.         if(get_Avai_Memory() >= size)   
  49.         {   
  50.             cout<<"memory defragment..."<<endl;   
  51.             memory_Defragment();   
  52.             memory_Request(proc_name,size);   
  53.             return;   
  54.         }   
  55.         else  
  56.         {   
  57.             cerr<<"memory request failed"<<endl;   
  58.         }   
  59.     }   
  60. }   
  61.   
  62. void Memory_Routine_By_FFA::sort_Free_Info()   
  63. {   
  64.     free_info.sort(First_Fit_Algo_Sort());   
  65. }   

Memory_Routine_By_WFA.cpp

Code:
  1. #include "Memory_Routine_By_WFA.h"   
  2. #include "Worst_Fit_Algo_Sort.h"   
  3. #include <iostream>   
  4.   
  5. using std::cout;   
  6. using std::cerr;   
  7. using std::endl;   
  8.   
  9. Memory_Routine_By_WFA::Memory_Routine_By_WFA()   
  10. {   
  11.     sort_Free_Info();   
  12. }   
  13.   
  14.   
  15. void Memory_Routine_By_WFA::memory_Request(string proc_name,unsigned int size)   
  16. {   
  17.     if(static_cast<int>(size) <= 0)   
  18.     {   
  19.         cerr<<"the size must be positive number"<<endl;   
  20.         return ;   
  21.     }   
  22.            
  23.     list<Free_Info>::iterator fi_it = free_info.begin();   
  24.     if(fi_it->get_Len() >= size)   
  25.     {   
  26.         allocated_info.push_back(Allocated_Info(proc_name,fi_it->get_Start_Addr(),size));   
  27.         if(fi_it->get_Len() - size != 0)   
  28.         {   
  29.             fi_it->set_Start_Addr(fi_it->get_Start_Addr() + size);   
  30.             fi_it->set_Len(fi_it->get_Len() - size);   
  31.         }   
  32.         else  
  33.         {   
  34.             free_info.erase(fi_it);   
  35.         }   
  36.         free_info.sort(Worst_Fit_Algo_Sort());   
  37.         cout<<"allocate success"<<endl;   
  38.     }   
  39.     else  
  40.     {   
  41.         if(get_Avai_Memory() >= size)   
  42.         {   
  43.             cout<<"memory defragment..."<<endl;   
  44.             memory_Defragment();   
  45.             memory_Request(proc_name,size);   
  46.             return;   
  47.         }   
  48.         else  
  49.         {   
  50.             cerr<<"memory request failed"<<endl;   
  51.         }   
  52.     }   
  53. }   
  54.   
  55. void Memory_Routine_By_WFA::sort_Free_Info()   
  56. {   
  57.     free_info.sort(Worst_Fit_Algo_Sort());   
  58. }   

main.cpp

Code:
  1. #include <iostream>   
  2. #include <string>   
  3. #include <limits>    
  4. #include "Memory_Routine.h"   
  5. #include "Memory_Routine_By_WFA.h"   
  6. #include "Memory_Routine_By_FFA.h"   
  7. #include "Memory_Routine_By_BFA.h"   
  8.   
  9. using std::cerr;   
  10. using std::cout;   
  11. using std::cin;   
  12. using std::endl;   
  13. using std::numeric_limits;   
  14. using std::streamsize;   
  15.   
  16. int main()   
  17. {   
  18.     Memory_Routine *mr;   
  19.     char c;   
  20.        
  21.     cout<<"OS BIG WORK NO 3 BY MARCUSXING 2009 12 8"<<endl;   
  22.     cout<<"****************************************"<<endl;   
  23.     cout<<"please select a algorithm to allocate memory dynamically"<<endl;   
  24.     cout<<"1:First Fit Algorithm"<<endl;   
  25.     cout<<"2:Best Fit Algorithm"<<endl;   
  26.     cout<<"3:Worst Fit Algorithm"<<endl;   
  27.        
  28.     cin>>c;   
  29.     switch(c)   
  30.     {   
  31.     case '1':   
  32.         mr = new Memory_Routine_By_FFA();   
  33.         break;   
  34.     case '2':   
  35.         mr = new Memory_Routine_By_BFA();   
  36.         break;   
  37.     case '3':   
  38.         mr = new Memory_Routine_By_WFA();   
  39.         break;   
  40.     default:   
  41.         cout<<"are you kidding me?"<<endl;   
  42.         return -1;   
  43.         break;   
  44.     }   
  45.        
  46.     cin.ignore(numeric_limits<streamsize>::max(),'/n');   
  47.     mr->disp_Free_Info();   
  48.     mr->disp_Allocated_Info();   
  49.        
  50.     cout<<"please decide what you want to do?"<<endl;  
  51.     cout<<"q:quit this program"<<endl;     
  52.     cout<<"h:display this help message"<<endl;   
  53.     cout<<"1:request memory for a process"<<endl;   
  54.     cout<<"2:call back all the memory of a process"<<endl;   
  55.     cout<<"3:display the available memory and allocated memory's infomation"<<endl;   
  56.     
  57.        
  58.     string proc_name;   
  59.     unsigned int size;   
  60.     bool flag = true;   
  61.        
  62.     while(flag)   
  63.     {   
  64.         cout<<"please enter your choice:";   
  65.         if(!cin)   
  66.         {   
  67.             cin.ignore(numeric_limits<streamsize>::max(),'/n');   
  68.         }   
  69.         cin>>c;   
  70.         switch(c)   
  71.         {   
  72.         case 'h':   
  73.             cin.ignore(numeric_limits<streamsize>::max(),'/n');   
  74.             cout<<"please decide what you want to do?"<<endl;   
  75.             cout<<"q:quit this program"<<endl;        
  76.             cout<<"h:display this help message"<<endl;   
  77.             cout<<"1:request memory for a process"<<endl;   
  78.             cout<<"2:call back all the memory of a process"<<endl;   
  79.          cout<<"3:display the available memory and allocated memory's infomation"<<endl; 
  80.             break;   
  81.         case '1':   
  82.             cout<<"please enter the process name:";   
  83.             cin.ignore(numeric_limits<streamsize>::max(),'/n');   
  84.             cin>>proc_name;   
  85.             cin.ignore(numeric_limits<streamsize>::max(),'/n');   
  86.             cout<<"please enter the request memory size:";   
  87.             if(!(cin>>size))   
  88.             {   
  89.                 cerr<<"the request size must be a positive number,request failed"<<endl;   
  90.                 flag = false;   
  91.                 break;   
  92.             }   
  93.             mr->memory_Request(proc_name,size);   
  94.             cin.ignore(numeric_limits<streamsize>::max(),'/n');   
  95.             break;   
  96.         case '2':   
  97.             cin.ignore(numeric_limits<streamsize>::max(),'/n');   
  98.             cout<<"please enter the process name:";   
  99.             cin>>proc_name;   
  100.             mr->memory_Call_Back(proc_name);   
  101.             cin.ignore(numeric_limits<streamsize>::max(),'/n');   
  102.             break;   
  103.         case '3':   
  104.             cin.ignore(numeric_limits<streamsize>::max(),'/n');   
  105.             mr->disp_Free_Info();   
  106.             mr->disp_Allocated_Info();   
  107.             break;   
  108.         case 'q':   
  109.             flag = false;   
  110.             break;   
  111.         default:   
  112.             cout<<"are you kidding me?"<<endl;   
  113.             flag = false;   
  114.             break;   
  115.         }   
  116.     }   
  117.        
  118.     delete mr;   
  119.        
  120.     return 0;   
  121. }   

 

原创粉丝点击