سؤال

I am writing a program to find page faults using FIFO in C#. The user either provides a 20 character reference string or a random one is generated. The user also inputs the number of frames.

So, I am passing in the array of 20 single digit numbers, array of frames, and # of frames to my FIFO function. For some reason, my number is off and I'm not sure what I am doing wrong. I am using the reference string of 1 2 3 4 2 1 5 6 2 1 2 3 7 6 3 2 1 2 3 6 with 4 frames to test, and I seem to be getting 56, when I should be getting 14 faults. Below is my FIFO function.

        private static void FIFO(int numFrames, int []refString, int []frame)
    {
        int i, j = 0, k, fault = 0, flag = 0;

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

            if (flag == 0)
            {
                frame[j] = refString[i];
                j++;

                for (k = 0; k < numFrames; k++)
                {
                    fault++;
                }
            }

            else
            {
                flag = 0;
            }

            if (j == numFrames)
            {
                j = 0;
            }

        }
        Console.WriteLine("\nThe number of page faults with FIFO is: " + fault);
    }
هل كانت مفيدة؟

المحلول

The reason is this bit:

for (k = 0; k < numFrames; k++)
{
    fault++;
}

you raise 4 faults for each miss of the cache (hence you're 56 = 14 * 4 )

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top