Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
LRU页面替换算法C#_C#_Algorithm_Operating System_Lru_Page Replacement - Fatal编程技术网

LRU页面替换算法C#

LRU页面替换算法C#,c#,algorithm,operating-system,lru,page-replacement,C#,Algorithm,Operating System,Lru,Page Replacement,我试图写一个函数来模拟LRU页面替换。我很了解LRU,但在编码上有问题。以下内容正在传递到LRU函数中。用户指定#的1-9的20个字符的引用字符串,该字符串存储在一个名为refString(大小为20)的数组中。用户输入的帧数(1-7)存储在变量numFrames中。最后,传入一个称为frame的大小为7的数组 这是我的代码,我得到了一个接近的数字,但不完全是。也许有人能帮忙 private static void LRU(int numFrames, int[] refString, int[

我试图写一个函数来模拟LRU页面替换。我很了解LRU,但在编码上有问题。以下内容正在传递到LRU函数中。用户指定#的1-9的20个字符的引用字符串,该字符串存储在一个名为refString(大小为20)的数组中。用户输入的帧数(1-7)存储在变量numFrames中。最后,传入一个称为frame的大小为7的数组

这是我的代码,我得到了一个接近的数字,但不完全是。也许有人能帮忙

private static void LRU(int numFrames, int[] refString, int[] frame)
{
    int i, j = 0, k, m, flag = 0, count = 0, top = 0;

    for (i = 0; i < 20; i++)
    {
        for (k = 0; k < numFrames; k++)
        {
            if (frame[k] == refString[i])
            {
                flag = 1;
                break;
            }
        }

        if (j != numFrames && flag != 1)
        {
            frame[top] = refString[i];
            j++;

            if (j != numFrames)
            {
                top++;
            }
        }

        else
        {
            if (flag != 1)
            {
                for (k = 0; k < top; k++)
                {
                    frame[k] = frame[k + 1];
                }

                frame[top] = refString[i];
            }

            if (flag == 1)
            {
                for (m = k; m < top; m++)
                {
                    frame[m] = frame[m + 1];
                }

                frame[top] = refString[i];
            }
        }

        if (flag == 0)
        {
            count++;
        }
        else
        {
            flag = 0;
        }

    }

    Console.WriteLine("\nThe number of page faults with LRU is: " + count);
}
private静态void LRU(int numFrames,int[]refString,int[]frame)
{
int i,j=0,k,m,flag=0,count=0,top=0;
对于(i=0;i<20;i++)
{
对于(k=0;k
您的代码中几乎没有错误:-

if (top < numFrames)
        {
            frame[top++] = refString[i];
            fault++;
        }

算出答案,并将其标记为已回答!感谢并欢迎来到Stackoverflow!谢谢你的回复!您提到了一些错误,但只列出了一个。我还缺什么?另外,如果当前refString[I]已经在帧[]中,我应该在哪里添加函数检查?我有点理解你发布的伪代码,但它与我使用的算法有点不同,我很难理解如何修复它。请解释一下你使用的是什么算法。很难从代码中分辨出来。我想你没有维护一个计数来检查哪一帧最近使用最少谢谢用于解释伪代码。我想我能理解这个算法。我要看看我是否可以通过添加一个计数来轻松修复我的算法。如果没有,我应该能够使用伪代码来模拟类似的算法
void LRU(int numframes,int refString[],int frames[]) {

   int top = 0,fault=0;
   int* count = new int[numframes];

   for(int i=0;i<refString.length;i++) {

       int k = findmax(refString[i],frames,count,top,numframes);

       if(k<0) {
          count[top] = 0;
          frames[top++] = refString[i];
          fault++;   
       }

       else if(frames[k]!=refString[i]) {

           count[k] = 0;
           frames[k] = refString[i];
           fault++;

       }
      else count[k] = 0;

     for(int j=0;j<top;j++) {
          count[j]++;  

     }

   }

   return(fault);

}


int findmax(int keyframe,int frames[],int count,int top,int numframes) {

     int max = 0;
     for(int i=0;i<top;i++) {

        if(frames[i]==keyframe) {

             return(i);
        }
        if(count[max]<count[i])
            max = i;

     }

     if(top<numframes)
           return(-1);
     return(max);
}
1. check if current requested frame is in cache and if yes then get its index
2. if frame is present then set its count to zero so as to indicate it is used very recently, higher the count the more least recently frame is used.
3. if frame is not present in cache then
   a. check if cache is full if not add new frame to end of cache and increment the fault and top of cache
   b. else if chace is full then get frame with maximum count(LRU frame) and replace it with new frame and reset count to zero and increment fault.
4. increment all the counts
5. do 1 to 4 till end of all requests
6. output the no of faults