LRU最近最久未使用算法

来源:互联网 发布:mac美国官网怎么下单 编辑:程序博客网 时间:2024/06/04 18:39

LRU最近最久未使用算法

标签: 算法操作系统
2082人阅读 评论(0)收藏举报
本文章已收录于:
操作系统知识库
分类:
作者同类文章X
    作者同类文章X

      算法过程

      最近最久未使用算法需要引入内存块时钟,即为每个内存块设定一个计时器,用于记录相应内存块中的页面已经存在的时间。每次置换选出所有内存块时钟中最大者作为被置换页面,当页面发生置换时,将其对应的计时器清零,并且其他计时器加1.可用等长的整型数组来表示每个内存块的计时器。

      在内存块初始化后,取出页面访问序列队列的队头。首先判断内存块中是否已经存在该队头页面,如果存在则直接显示内存块当前情况;否则,判断此时内存是否已满。如果内存未满,循环遍历找出空闲内存块,进行页面置换;若内存已满,选出所有内存块时钟中最大者,并置换之,缺页数加1. 每发生一次置换,被置换页面内存所对应的计时器清零,其他计时器加1. 如此循环迭代,直到页面访问序列队列为空时,整个算法执行完毕。最后计算并显示缺页率。其流程图如图所示:




      代码示例:

      [cpp] view plain copy
      print?
      1. #ifndef PAGEREPLACEMENT_H  
      2. #define PAGEREPLACEMENT_H  
      3.   
      4. #include <vector>  
      5.   
      6. class PageReplacement  
      7. {  
      8. public:  
      9.     PageReplacement();  
      10.     ~PageReplacement();  
      11.     void run();  
      12.     void LRU();  
      13.   
      14. private:  
      15.     void addInfo() const;  
      16.   
      17. private:  
      18.     int pages;                  // 虚拟内存的尺寸P  
      19.     int firstPageFramePos;      // 工作面的起始位置p  
      20.     int pageFrames;             // 工作面中包含的页数e  
      21.     int rateM;                  // 工作面移动率m  
      22.     std::vector<int> seqVec;  // 序列号  
      23.     std::vector<int> mem;     // 内存块  
      24. };  
      25.   
      26. #endif  // PAGEREPLACEMENT_H  

      [cpp] view plain copy
      print?
      1. #include "PageReplacement.h"  
      2. #include <iostream>  
      3. #include <cstdlib>  
      4. #include <list>  
      5.   
      6. PageReplacement::PageReplacement()  
      7.     : mem(3, -1)  
      8. {  
      9.     this->run();  
      10. }  
      11.   
      12. PageReplacement::~PageReplacement()  
      13. {  
      14. }  
      15.   
      16. void PageReplacement::run()  
      17. {  
      18.     std::cout << "请输入虚拟内存尺寸P:";  
      19.     std::cin >> pages;  
      20.     std::cout << "请输入工作面的起始位置p:";  
      21.     std::cin >> firstPageFramePos;  
      22.     std::cout << "请输入工作面中包含的页数e:";  
      23.     std::cin >> pageFrames;  
      24.     std::cout << "请输入工作面移动率m:";  
      25.     std::cin >> rateM;  
      26.     std::cout << "请输入在0和1之间的值t:";  
      27.     std::cin >> t;  
      28.   
      29.     for (int i = 0; i < rateM; ++i)  
      30.     {  
      31.         int randomNum = (rand() % pageFrames) + firstPageFramePos;  
      32.         seqVec.push_back(randomNum);  
      33.     }  
      34.   
      35.     std::cout << "序列号:";  
      36.     for (int i = 0; i < seqVec.size(); ++i)  
      37.     {  
      38.         std::cout << seqVec.at(i) << " ";  
      39.     }  
      40.   
      41.     std::cout << std::endl;  
      42. }  
      43.   
      44. void PageReplacement::LRU()  
      45. {  
      46.     int nLack = 0;                          // 缺页数  
      47.     std::list<int> seqList(seqVec.begin(), seqVec.end());  
      48.     int nTotal = seqList.size();  
      49.   
      50.     std::vector<int> timer(mem.size(), 0);        // 每个内存块对应的时钟  
      51.   
      52.     while (!seqList.empty())  
      53.     {  
      54.         int head = *seqList.begin();            // 去队头的页面  
      55.         seqList.pop_front();  
      56.         bool equal = false;                     // 标识内存中是否有与队头相等的页面  
      57.         int vacant = -1;                        // 标识内存中是否有空闲页面  
      58.   
      59.         for (int i = 0; i < mem.size(); ++i)  
      60.         {  
      61.             if (mem.at(i) == head)              // 如果找到相等的页面  
      62.             {  
      63.                 equal = true;  
      64.                 this->addInfo();             // 显示内存块  
      65.   
      66.                 timer.at(i) = -1;               // 相等的时钟清零,其他的时钟加1  
      67.                 for (int j = 0; j < timer.size(); ++j)  
      68.                 {  
      69.                     ++timer[j];  
      70.                 }  
      71.   
      72.                 break;  
      73.             }  
      74.   
      75.             else if (mem.at(i) == -1 && vacant == -1)       // 如果找到空闲位  
      76.             {  
      77.                 vacant = i;  
      78.             }  
      79.         }  
      80.   
      81.         if (equal)                  // 如果找到相等的页面,则进行下一个查找  
      82.         {  
      83.             continue;  
      84.         }  
      85.   
      86.         ++nLack;  
      87.   
      88.         if (vacant !=  -1)  
      89.         {  
      90.             mem[vacant] = head;         // 把队头的放入到空闲位中  
      91.             this->addInfo();  
      92.   
      93.             timer[vacant] = -1;         // 空闲位时钟清零,其他时钟加1  
      94.             for (int j = 0; j < timer.size(); ++j)  
      95.             {  
      96.                 ++timer[j];  
      97.             }  
      98.   
      99.             continue;  
      100.         }  
      101.   
      102.         else  
      103.         {  
      104.             int max = timer[0];  
      105.             int subIndex = 0;  
      106.             for (int p = 0; p < timer.size(); ++p)  
      107.             {  
      108.                 if (timer.at(p) > max)  
      109.                 {  
      110.                     max = timer.at(p);      // 找到最近最久没有被使用的内存块  
      111.                     subIndex = p;  
      112.                 }  
      113.             }  
      114.   
      115.             mem[subIndex] = head;           // 把队头的放入到最近最久未被使用的内存块中  
      116.             this->addInfo();  
      117.   
      118.             timer[subIndex] = -1;  
      119.             for (int j = 0; j < timer.size(); ++j)  
      120.             {  
      121.                 ++timer[j];  
      122.             }  
      123.   
      124.         }  
      125.   
      126.     }  
      127.   
      128.     std::cout << "缺页率: " << (double)nLack/nTotal * 100 << "%" << std::endl;  
      129. }  
      130.   
      131. void PageReplacement::addInfo() const  
      132. {   
      133.     std::cout << mem.at(0) << "    " <<  mem.at(1) << "    "  << mem.at(2) << std::endl;  
      134. }  


      [cpp] view plain copy
      print?
      1. #include <iostream>  
      2. #include "PageReplacement.h"  
      3.   
      4. int main()  
      5. {  
      6.     PageReplacement pageReplacement;  
      7.   
      8.     pageReplacement.LRU();  
      9.   
      10.     return 0;  
      11. }  






      0
      0
       
       

      我的同类文章

      http://blog.csdn.net
      • Pthread 互斥2015-08-11
      • POSIX 线程小结2015-08-11
      • 线程模式2015-08-09
      • I/O多路复用之总结2015-08-08
      • I/O 多路复用之select2015-08-08
      • 改进型Clock算法2015-06-16
      • POSIX 线程小结(续)2015-08-11
      • 线程模型2015-08-09
      • 线程小结2015-08-09
      • I/O 多路复用之poll2015-08-08
      • 分散/聚集 I/O(scatter-gather I/O)2015-08-06
      更多文章
      0 0
      原创粉丝点击