【问题标题】:Not Used Recently (NUR) Page Replacement Algorithm [closed]最近未使用(NUR)页面替换算法[关闭]
【发布时间】:2015-08-31 20:27:56
【问题描述】:

如何使用任何高级编程语言(c、c++ 或 java)实现 NUR(最近未使用的页面替换算法)

【问题讨论】:

  • 向我们展示您的尝试,您遇到的困难并寻求我们的帮助。 选择一种语言。
  • 坦率地说,我什至从未尝试过实现它,它是虚拟内存页面替换技术的一部分,作为我学习过程的一部分,我希望获得有关如何实现的链接他们,或者,甚至得到一个完整的可实现代码
  • 欢迎来到 Stack Overflow!请拨打tour 并阅读How to Ask 以了解我们对这里问题的期望。请注意,我们这里不提供从头开始编码服务。请告诉我们您已经尝试过什么,它是如何失败的,我们也许可以提供帮助。:-)
  • 感谢您的 cmets

标签: java c++ c operating-system


【解决方案1】:

通常页面替换算法有一个bufferpages 放入buffer

缓冲区大小固定为 4。现在我们继续将页面添加到缓冲区中,如果它们已经在缓冲区中,我们将忽略它们并继续插入下一页。

如果它们不在缓冲区中。如果有空间,我们必须插入它。如果没有空格,则我们搜索替换元素。在这种情况下是最不常用的。

我们如何计算最不常用的? 使用计数数组。

public class Demo {

public static void main(String[] args) {
    int size = 4;
    int []buffer = new int[size];
    int []bufferCount = new int[size];
    int[] pages = {1,2,3,4,5,1,2,1,2,3,4,6,1,2,1,5,1};
    int pos = 0;
    for(int i = 0; i < pages.length; i++)
    {
        System.out.print("For page: "+pages[i]+"\t");
        if((pos = contains(buffer, pages[i])) > 0)
        {
            //already in buffer increment the count
            bufferCount[pos] ++;
        }
        else
        {
            //choose the element with least usage and remove it
            int toRemove = getLeastUsed(bufferCount);
            bufferCount[toRemove] = 1;
            buffer[toRemove] = pages[i];
        }
        printBuffer(buffer);
        System.out.print("\t");
        printBuffer(bufferCount);
        System.out.println();
    }
}

private static void printBuffer(int[] buffer) {
    // TODO Auto-generated method stub
    for(int i = 0; i < buffer.length; i++)
        System.out.print(buffer[i] + "");
}

private static int getLeastUsed(int[] bufferCount) {
    // TODO Auto-generated method stub
    int index = 0;
    int min = bufferCount[0];
    for(int i = 0; i < bufferCount.length; i++)
        if(bufferCount[i] < min)
        {
            min = bufferCount[i];
            index = i;
        }
    return index;
}

private static int contains(int[] buffer, int key) {
    // TODO Auto-generated method stub
    for(int i = 0; i < buffer.length; i++)
        if(buffer[ i ] == key)
            return i;
    return -1;
}

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-16
    • 1970-01-01
    • 2011-06-20
    • 2023-04-05
    • 1970-01-01
    • 2013-12-06
    • 1970-01-01
    • 2013-12-06
    相关资源
    最近更新 更多