【大三操作系统实验】 动态分区时的适应算法

(1)最先适应算法要求可用表或自由链按起始地址递增的次序排序。一旦找到大于或等于所要求内存长度的分区,则结束探索。

(2)最佳适应算法要求从小到大的次序组成空闲区可用表或自由链。

(3)最坏适应算法要求空闲区按其大小递减的顺序组成空闲区可用表或自由链。

 

 

 

Code:
  1. #include<stdio.h>   
  2. #include<iostream.h>   
  3. #include<string.h>   
  4. #include<windows.h>   
  5. typedef struct LinkList_Empty   
  6. {   
  7. int length;   
  8. int start_add;   
  9. struct LinkList_Empty *next;   
  10. }LNode_Empty,*List_Empty;   
  11.   
  12. typedef struct LinkList_Own   
  13. {   
  14. int length;   
  15. int start_add;   
  16. char Process[10];   
  17. struct LinkList_Own *next;   
  18. }LNode_Own,*List_Own;   
  19.   
  20. typedef struct Proc   
  21. {   
  22. int length;   
  23. char Process[10];   
  24. struct Proc *next;   
  25. }*Process,LNode_Proc;   
  26.   
  27. List_Empty Empty_head= new LNode_Empty;  //头指针   
  28. List_Own Own_head= new LNode_Own;   
  29. Process Proc_head= new LNode_Proc;   
  30. int length;  //内存分配个数   
  31.   
  32. void Show_Empty(List_Empty Empty_head);   //空闲区显示   
  33. void Show_Own(List_Own Own_head);         //占有区显示   
  34. void Init_Empty(List_Empty Empty_head);  //内存空闲区初始化   
  35. void Init_Proc(Process Proc_head); //作业进程初始化   
  36. void FF(List_Empty &Empty_head,List_Own &Own_head,Process &Proc_head);   
  37. void BF(List_Empty &Empty_head,List_Own &Own_head,Process &Proc_head);   
  38. void WF(List_Empty &Empty_head,List_Own &Own_head,Process &Proc_head);   
  39. void Recycle(List_Empty &Empty_head,List_Own &Own_head);   
  40. void Sort_Up(List_Empty &Empty_head);   
  41. void Sort_Down(List_Empty &Empty_head);   
  42. void main()   
  43. {   
  44.     Empty_head->next=NULL;   
  45.     Own_head->next=NULL;   
  46.     Proc_head->next=NULL;   
  47.     int i;   
  48.     while(1)   
  49.     {   
  50.         cout<<"1-FF最先适应算法"<<endl;   
  51.     cout<<"2-BF最佳适应算法"<<endl;   
  52.     cout<<"3-WF最坏适应算法"<<endl<<"请选择 ";   
  53.     cin>>i;   
  54.     switch(i)   
  55.     {   
  56.     case 1:   
  57.         Init_Empty(Empty_head);  //空闲区初始   
  58.            
  59.         cout<<"内存分配空闲区"<<endl;   
  60.            
  61.         Show_Empty(Empty_head);   //显示空闲区   
  62.   
  63.         Init_Proc(Proc_head);     //作业请求表   
  64.            
  65.         cout<<endl<<"空闲区链表:"<<endl;   
  66.   
  67.         FF(Empty_head,Own_head,Proc_head);   //FF      
  68.            
  69.         //Show_Empty(Empty_head);   
  70.            
  71.         cout<<endl<<"作业进程占有区链表:"<<endl;   
  72.            
  73.         Show_Own(Own_head);   
  74.            
  75.         cout<<endl<<"分区内存回收"<<endl;   
  76.            
  77.         Recycle(Empty_head,Own_head);  //分区内存回收   
  78.            
  79.         Show_Empty(Empty_head);   
  80.         break;   
  81.     case 2:   
  82.         Init_Empty(Empty_head);     
  83.            
  84.         cout<<"内存分配空闲区"<<endl;   
  85.            
  86.         Show_Empty(Empty_head);    
  87.   
  88.         Sort_Up(Empty_head);   //搜索前先排序   
  89.         cout<<"排序后空闲区"<<endl;   
  90.         Show_Empty(Empty_head);   
  91.   
  92.         Init_Proc(Proc_head);    
  93.   
  94.         cout<<endl<<"空闲区链表:"<<endl;   
  95.   
  96.         BF(Empty_head,Own_head,Proc_head);  //BF   
  97.            
  98.         //Show_Empty(Empty_head);   
  99.            
  100.         cout<<endl<<"作业进程占有区链表:"<<endl;   
  101.            
  102.         Show_Own(Own_head);   
  103.            
  104.         cout<<endl<<"分区内存回收"<<endl;   
  105.            
  106.         Recycle(Empty_head,Own_head);   
  107.         Sort_Up(Empty_head);   
  108.         Show_Empty(Empty_head);   
  109.         break;   
  110.     case 3:   
  111.         Init_Empty(Empty_head);     
  112.            
  113.         cout<<"内存分配空闲区"<<endl;   
  114.            
  115.         Show_Empty(Empty_head);    
  116.   
  117.         Sort_Down(Empty_head);   //搜索前先排序   
  118.         cout<<"排序后空闲区"<<endl;   
  119.         Show_Empty(Empty_head);   
  120.   
  121.         Init_Proc(Proc_head);    
  122.   
  123.         cout<<endl<<"空闲区链表:"<<endl;   
  124.   
  125.         WF(Empty_head,Own_head,Proc_head);   //WF   
  126.            
  127.         //Show_Empty(Empty_head);   
  128.            
  129.         cout<<endl<<"作业进程占有区链表:"<<endl;   
  130.            
  131.         Show_Own(Own_head);   
  132.            
  133.         cout<<endl<<"分区内存回收"<<endl;   
  134.            
  135.         Recycle(Empty_head,Own_head);   
  136.         Sort_Down(Empty_head);   
  137.         Show_Empty(Empty_head);   
  138.         break;   
  139.     default:   
  140.         cout<<"重新输入!"<<endl;   
  141.     }   
  142.     cout<<endl<<endl;   
  143.     }   
  144.   
  145.        
  146. }   
  147.   
  148. void Init_Empty(List_Empty Empty_head)   //内存空闲区初始化   
  149. {   
  150.     List_Empty Empty_p,Empty_q;  //当前节点和上一节点   
  151.     Empty_p=Empty_q=Empty_head;   
  152.        
  153.        
  154.     cout<<"内存分配初始化:"<<endl;   
  155.     cout<<"请输入内存分配空闲区个数"<<endl;   
  156.     cin>>length;   
  157.     int length_1=length;   
  158.     int num=0;   
  159.     printf("起始地址 分区长度/n");   
  160.        
  161.     while(length_1--)   
  162.     {   
  163.         //建立链表   
  164.         Empty_p = new LNode_Empty;   
  165.         Empty_p->next=NULL;   
  166.         Empty_q->next=Empty_p;   
  167.         Empty_q=Empty_p;   
  168.         scanf("%d%d",&Empty_p->start_add,&Empty_p->length);   
  169.     }   
  170. }   
  171.   
  172.   
  173.   
  174.   
  175. void Init_Proc(Process Proc_head)   
  176. {   
  177.     Process p,q;  //当前节点和上一节点   
  178.     p=q=Proc_head;   
  179.        
  180.     int length_2;   
  181.     cout<<"进程作业初始化:"<<endl;   
  182.     cout<<"请输入进程作业个数"<<endl;   
  183.     cin>>length_2;   
  184.     printf("作业进程号 请求长度/n");   
  185.        
  186.     while(length_2--)   
  187.     {   
  188.         p = new LNode_Proc;   
  189.         p->next=NULL;   
  190.         q->next=p;   
  191.         q=p;   
  192.         scanf("%s%d",p->Process,&p->length);   
  193.     }   
  194.        
  195.     p=Proc_head->next;   
  196.     cout<<"作业进程占有区"<<endl;   
  197.     while(p)   
  198.     {   
  199.         cout<<"["<<p->Process<<"]"<<"["<<p->length<<"]"<<"-";   
  200.         p=p->next;   
  201.     }   
  202.     cout<<endl;   
  203. }   
  204.   
  205.   
  206.   
  207. void Show_Empty(List_Empty Empty_head)   //显示   
  208. {   
  209.     List_Empty p=Empty_head->next;   
  210.     Empty_head->start_add=p->start_add;   
  211.     cout<<"["<<Empty_head->start_add<<"]"<<"-";   
  212.     while(p)   
  213.     {   
  214.            
  215.         cout<<"["<<p->length<<"]";//<<"["<<p->next->start_add<<"]";   
  216.         if(p->next==NULL)   
  217.             cout<<"[NULL]";   
  218.         else cout<<"["<<p->next->start_add<<"]"<<"-";   
  219.         p=p->next;   
  220.     }   
  221.     cout<<endl;   
  222. }   
  223.   
  224. void Show_Own(List_Own Own_head)   //显示   
  225. {   
  226.     if(Own_head->next==NULL)   
  227.     {   
  228.         cout<<"[NULL]"<<endl;   
  229.     }   
  230.     else  
  231.     {   
  232.         List_Own p=Own_head->next;   
  233.         Own_head->start_add=p->start_add;   
  234.         cout<<"["<<Own_head->start_add<<"]"<<"-";   
  235.         while(p)   
  236.         {   
  237.                
  238.             cout<<"["<<p->length<<"]"<<"["<<p->Process<<"]";   
  239.             if(p->next==NULL)   
  240.                 cout<<"[NULL]";   
  241.             else cout<<"["<<p->next->start_add<<"]"<<"-";   
  242.             p=p->next;   
  243.         }   
  244.         cout<<endl;   
  245.     }   
  246. }   
  247.   
  248. void FF(List_Empty &Empty_head,List_Own &Own_head,Process &Proc_head)   
  249. {   
  250.     List_Empty Empty_p=Empty_head->next;  //第一节点   
  251.     List_Own Own_q=Own_head;   
  252.     Process Proc_p=Proc_head->next;   
  253.     List_Empty Empty_q=Empty_head;    //首节点   
  254.     while(Proc_p)     
  255.     {   
  256.         while(Empty_p)   //从头到尾搜索   
  257.         {   
  258.             if(Empty_p->length > Proc_p->length)     
  259.             {   
  260.                 //建立链表   
  261.                 List_Own Own_p =new LNode_Own;   
  262.                 Own_p->next=NULL;   
  263.                 Own_q->next=Own_p;   
  264.                 Own_q=Own_p;      
  265.                 //保存作业到占有链表   
  266.                 Own_p->start_add=Empty_p->start_add;   
  267.                 Own_p->length=Proc_p->length;   
  268.                 strcpy(Own_p->Process,Proc_p->Process);   
  269.                 //起始地址+=作业请求长度   
  270.                 Empty_p->start_add+=Proc_p->length;   
  271.                 //分区长度-=作业请求长度   
  272.                 Empty_p->length-=Proc_p->length;   
  273.                 break;   
  274.             }   
  275.             else if(Empty_p->length == Proc_p->length)     
  276.             {   
  277.                 //建立链表   
  278.                 List_Own Own_p =new LNode_Own;   
  279.                 Own_p->next=NULL;   
  280.                 Own_q->next=Own_p;   
  281.                 Own_q=Own_p;      
  282.                 //保存作业到占有链表   
  283.                 Own_p->start_add=Empty_p->start_add;   
  284.                 Own_p->length=Proc_p->length;   
  285.                 strcpy(Own_p->Process,Proc_p->Process);   
  286.                    
  287.                 //销毁当前节点   
  288.                 Empty_q->next=Empty_p->next;   
  289.                 delete Empty_p;  //Empty_p是野指针无指向   
  290.                  break;   
  291.             }   
  292.             else  
  293.             {   //都不满足,链表下移   
  294.             Empty_q=Empty_p;   //保存当前节点   
  295.             Empty_p=Empty_p->next;    //移到下一节点   
  296.             }   
  297.         }   
  298.         Show_Empty(Empty_head);   
  299.         cout<<endl;   
  300.         Empty_p=Empty_head->next;//重新开始   
  301.         Proc_p=Proc_p->next;   
  302.     }   
  303.        
  304. }   
  305.   
  306. void Recycle(List_Empty &Empty_head,List_Own &Own_head)  //回收   
  307. {   
  308.     List_Empty Empty_p=Empty_head->next; //第一节点   
  309.     List_Empty Empty_q=Empty_head;  //头节点   
  310.     List_Own Own_p=Own_head->next;   
  311.     List_Own Own_q=Own_head;   
  312.     int flag=-1;   
  313.     while(Own_p)   
  314.     {   
  315.         while(Empty_p)   
  316.         {   
  317.             if(Empty_p->start_add >= Own_p->start_add)   
  318.             {   
  319.                 if(Empty_p->start_add ==Own_p->start_add + Own_p->length)  //下相邻区   
  320.                 {      
  321.                     Empty_p->start_add=Own_p->start_add;  //首地址为释放区地址   
  322.                     Empty_p->length += Own_p->length;   
  323.                     Own_p->length = Empty_p->length;   
  324.                     flag=0;   
  325.                 }   
  326.                 if(Own_p->start_add == Empty_q->start_add + Empty_q->length)  //上相邻区   
  327.                 {   
  328.                     //首地址为上相邻区首地址 Empty_q->start_add   
  329.                     //分区长度=释放区长度+分区长度   
  330.                     Empty_q->length+=Own_p->length;   
  331.                     if(flag==0) flag=1;   
  332.                 }   
  333.                    
  334.                 if(flag==-1)   
  335.                 {   //没有相邻区   
  336.                     Empty_p = new LNode_Empty;   
  337.                     Empty_p->next=Empty_q->next;   
  338.                     Empty_q->next=Empty_p;   
  339.                     Empty_p->start_add=Own_p->start_add;   
  340.                     Empty_p->length=Own_p->length;   
  341.                 }   
  342.                 //销毁当前占有节点和空闲节点   
  343.                 Own_q->next=Own_p->next;   
  344.                 delete Own_p;   
  345.                 Own_p=Own_q;   
  346.                 if(flag==1)  //有上下相邻区才销毁当前一个节点,再建立链表   
  347.                 {   
  348.                     Empty_q->next=Empty_p->next;   
  349.                     delete Empty_p;   
  350.                        
  351.                 }   
  352.                 break;   
  353.             }   
  354.             else  
  355.             {   
  356.                 Empty_q=Empty_p;   //保存当前节点   
  357.                 Empty_p=Empty_p->next;    //移到下一节点   
  358.                 if(Empty_p==NULL)   //到最后一个节点   
  359.                 {   
  360.                     Empty_p = new LNode_Empty;   
  361.                     Empty_p->next=NULL;   
  362.                     Empty_q->next=Empty_p;   
  363.                     Empty_q=Empty_p;   
  364.                     Empty_p->start_add=Own_p->start_add;   
  365.                     Empty_p->length = Own_p->length;   
  366.                     Empty_p=Empty_p->next;    //移到最后一个节点   
  367.                 }   
  368.             }   
  369.                
  370.         }   
  371.         Show_Empty(Empty_head);   
  372.         flag=-1;  //还原   
  373.         Empty_p=Empty_head->next;//重新开始   
  374.         Own_p=Own_p->next;   
  375.     }      
  376. }   
  377.   
  378. void BF(List_Empty &Empty_head,List_Own &Own_head,Process &Proc_head)   
  379. {   
  380.     //Sort_Up(Empty_head);   //搜索前先排序   
  381.     List_Empty Empty_p=Empty_head->next;   
  382.     List_Own Own_q=Own_head;   
  383.     Process Proc_p=Proc_head->next;   
  384.     List_Empty Empty_q=Empty_head;   
  385.     cout<<endl;   
  386.     while(Proc_p)     
  387.     {   
  388.         while(Empty_p)   //从头到尾搜索   
  389.         {   
  390.             if(Empty_p->length > Proc_p->length)   
  391.             {   
  392.                 List_Own Own_p =new LNode_Own;   
  393.                 Own_p->next=NULL;   
  394.                 Own_q->next=Own_p;   
  395.                 Own_q=Own_p;   //建立链表   
  396.                 //保存作业到占有链表   
  397.                 Own_p->start_add=Empty_p->start_add;   
  398.                 Own_p->length=Proc_p->length;   
  399.                 strcpy(Own_p->Process,Proc_p->Process);   
  400.                 //起始地址+=作业请求长度   
  401.                 Empty_p->start_add+=Proc_p->length;   
  402.                 //分区长度-=作业请求长度   
  403.                 Empty_p->length-=Proc_p->length;   
  404.                 break;   
  405.             }   
  406.             else if(Empty_p->length == Proc_p->length)   
  407.             {   
  408.                 List_Own Own_p =new LNode_Own;   
  409.                 Own_p->next=NULL;   
  410.                 Own_q->next=Own_p;   
  411.                 Own_q=Own_p;   //建立链表   
  412.                 //保存作业到占有链表   
  413.                 Own_p->start_add=Empty_p->start_add;   
  414.                 Own_p->length=Proc_p->length;   
  415.                 strcpy(Own_p->Process,Proc_p->Process);   
  416.                    
  417.                 //销毁当前节点   
  418.                 Empty_q->next=Empty_p->next;   
  419.                 delete Empty_p;   
  420.                  break;   
  421.             }   
  422.             else  
  423.             {   //都不满足,链表下移   
  424.             Empty_q=Empty_p;   //保存当前节点   
  425.             Empty_p=Empty_p->next;    //移到下一节点   
  426.             }   
  427.         }   
  428.         Sort_Up(Empty_head);   
  429.         Show_Empty(Empty_head);   
  430.         cout<<endl;   
  431.         Empty_p=Empty_head->next;//重新开始   
  432.         Proc_p=Proc_p->next;   
  433.     }   
  434. }   
  435.   
  436. void Sort_Up(List_Empty &Empty_head)   
  437. {   
  438.     List_Empty Empty_p=Empty_head;  //首节点   
  439.     List_Empty Empty_q=Empty_p->next->next;  //第二个节点   
  440.     int i_add,j_length;   
  441.     while(Empty_p->next)   
  442.     {   
  443.         while(Empty_q)   
  444.         {   
  445.             if(Empty_p->next->length >= Empty_q->length)   
  446.             {   
  447.                 i_add=Empty_p->start_add;   
  448.                 j_length=Empty_p->length;   
  449.   
  450.                 Empty_p->start_add=Empty_q->start_add;   
  451.                 Empty_p->length=Empty_q->length;   
  452.   
  453.                 Empty_q->start_add=i_add;   
  454.                 Empty_q->length =j_length;   
  455.             }   
  456.             Empty_q=Empty_q->next;   
  457.         }   
  458.         Empty_p=Empty_p->next;   
  459.         Empty_q=Empty_p->next;   
  460.            
  461.     }   
  462.        
  463. }   
  464.   
  465. void Sort_Down(List_Empty &Empty_head)   
  466. {   
  467.        
  468.     List_Empty Empty_p=Empty_head;  //首节点   
  469.     List_Empty Empty_q=Empty_p->next->next;  //第二个节点   
  470.     int i_add,j_length;   
  471.     while(Empty_p->next)   
  472.     {   
  473.         while(Empty_q)   
  474.         {   
  475.             //if(Empty_p == Empty_head) break;   
  476.             if(Empty_p->next->length <= Empty_q->length)   
  477.             {   
  478.                 i_add=Empty_p->start_add;   
  479.                 j_length=Empty_p->length;   
  480.                    
  481.                 Empty_p->start_add=Empty_q->start_add;   
  482.                 Empty_p->length=Empty_q->length;   
  483.                    
  484.                 Empty_q->start_add=i_add;   
  485.                 Empty_q->length =j_length;   
  486.             }   
  487.             Empty_q=Empty_q->next;   
  488.         }   
  489.         Empty_p=Empty_p->next;   
  490.         Empty_q=Empty_p->next;   
  491.            
  492.     }      
  493. }   
  494.   
  495. void WF(List_Empty &Empty_head,List_Own &Own_head,Process &Proc_head)   
  496. {   
  497.     List_Empty Empty_p=Empty_head->next;   
  498.     List_Own Own_q=Own_head;   
  499.     Process Proc_p=Proc_head->next;   
  500.     List_Empty Empty_q=Empty_head;   
  501.     cout<<endl;   
  502.     while(Proc_p)     
  503.     {   
  504.         while(Empty_p)   //从头到尾搜索   
  505.         {   
  506.             if(Empty_p->length > Proc_p->length)   
  507.             {   
  508.                 List_Own Own_p =new LNode_Own;   
  509.                 Own_p->next=NULL;   
  510.                 Own_q->next=Own_p;   
  511.                 Own_q=Own_p;   //建立链表   
  512.                 //保存作业到占有链表   
  513.                 Own_p->start_add=Empty_p->start_add;   
  514.                 Own_p->length=Proc_p->length;   
  515.                 strcpy(Own_p->Process,Proc_p->Process);   
  516.                 //起始地址+=作业请求长度   
  517.                 Empty_p->start_add+=Proc_p->length;   
  518.                 //分区长度-=作业请求长度   
  519.                 Empty_p->length-=Proc_p->length;   
  520.                 break;   
  521.             }   
  522.             else if(Empty_p->length == Proc_p->length)   
  523.             {   
  524.                 List_Own Own_p =new LNode_Own;   
  525.                 Own_p->next=NULL;   
  526.                 Own_q->next=Own_p;   
  527.                 Own_q=Own_p;   //建立链表   
  528.                 //保存作业到占有链表   
  529.                 Own_p->start_add=Empty_p->start_add;   
  530.                 Own_p->length=Proc_p->length;   
  531.                 strcpy(Own_p->Process,Proc_p->Process);   
  532.                    
  533.                 //销毁当前节点   
  534.                 Empty_q->next=Empty_p->next;   
  535.                 delete Empty_p;   
  536.                  break;   
  537.             }   
  538.             else  
  539.             {   //都不满足,链表下移   
  540.             Empty_q=Empty_p;   //保存当前节点   
  541.             Empty_p=Empty_p->next;    //移到下一节点   
  542.             }   
  543.         }   
  544.         Sort_Down(Empty_head);   
  545.         Show_Empty(Empty_head);   
  546.         cout<<endl;   
  547.         Empty_p=Empty_head->next;//重新开始   
  548.         Proc_p=Proc_p->next;   
  549.     }   
  550. }   
  551.   
  552. /*  
  553. {  
  554. List_Empty Empty_p=Empty_head->next;  //第一个节点  
  555.     List_Empty Empty_q=Empty_p->next;  //第二个节点  
  556.     int i_add,j_length;  
  557. while(Empty_p)  
  558.     {  
  559.         while(Empty_q)  
  560.         {  
  561.             if(Empty_p->length <= Empty_q->length)  
  562.             {  
  563.                 i_add=Empty_p->start_add;  
  564.                 j_length=Empty_p->length;  
  565.  
  566.                 Empty_p->start_add=Empty_q->start_add;  
  567.                 Empty_p->length=Empty_q->length;  
  568.  
  569.                 Empty_q->start_add=i_add;  
  570.                 Empty_q->length =j_length;  
  571.             }  
  572.             Empty_q=Empty_q->next;  
  573.         }  
  574.         Empty_p=Empty_p->next;  
  575.         if(Empty_p->next!=NULL)  
  576.         Empty_q=Empty_p->next;  
  577.           
  578.     }  
  579. }  
  580. */  

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值