For the FIFO you can use nn array of ints, but for the LRU you need an array or linked list of structures. If you use the STL in C++, the list management becomes a no brainer.
For instance, for the FIFO, you could do this:
#include
#include
const int NFRAMES = 4;
static void fifo_add( int item, int& rfaults );
static std::list fifo_list;
int main(int argc, char* argv[])
{
int i, nfaults;
int data[] = {7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 1, 2, 7, 4, 6, 5, 4, 6, -1};
nfaults = 0;
fifo_list.clear();
for (i=0; data[i] != -1; i++)
fifo_add(data[i], nfaults);
printf("\n%d page faults.\n", nfaults);
getchar();
return 0;
}
static void fifo_add( int item, int& rfaults )
{
std::list::iterator it;
it = find(fifo_list.begin(), fifo_list.end(), item);
if (it == fifo_list.end())
{
// not found
if (fifo_list.size() == NFRAMES)
fifo_list.erase(fifo_list.begin());
fifo_list.push_back(item);
printf("%d\n", item);
rfaults++;
}
}
BTW, if you don't see the subscript operators, it's because they were removed when I posted this message. Don't ask me why.