/*
FIFO algorithm run command:
./PageReplacement input.txt 4096 3 FIFO
The first parameter specifies the input file name as input.txt,
the second parameter specifies the page size to be 4096,
the third parameter specifies the number of pages to be 3,
and the fourth parameter specifies the use of the FIFO algorithm.
./PageReplacement input.txt 4096 3 LRU
The first parameter specifies the input file name as input.txt,
the second parameter specifies the page size to be 4096,
the third parameter specifies the number of pages to be 3,
and the fourth parameter specifies the LRU algorithm.
./PageReplacement input.txt 4096 3 ARB 4 3
The first parameter specifies the input file name is input.txt,
the second parameter specifies the page size to be 4096,
the third parameter specifies the number of pages to be 3,
the fourth parameter specifies the use of the ARB algorithm,
and the fifth parameter specifies Number of register bits,
the sixth parameter specifies the shift register refresh frequency bit 3
./PageReplacement input.txt 4096 3 WSARB 4 3 3
The first parameter specifies the input file name is input.txt,
the second parameter specifies the page size to be 4096,
the third parameter specifies the number of pages to be 3,
the fourth parameter specifies the WSARB algorithm,
and the fifth parameter specifies The number of register bits,
the sixth parameter specifies the shift register refresh frequency bit 3,
and the seventh parameter specifies the window to be 3
*/
#include <iostream>
#include <vector>
#include <string>
#include <string.h>
#include <fstream>
using namespace std;
int events=0;
int reads=0;
int writes=0;
int faults=0;
struct Frame
{
long long num;
bool dirty;
unsigned char re;
unsigned char cnt;
Frame(long long n=-1,bool d=false,unsigned char r=0,unsigned char c=0)
{
num=n;
dirty=d;
re=r;
cnt=c;
}
};
void PageFIFO(vector<Frame> &frames,long long framenum,bool write)
{
int i;
// Traversing the page in the current dynamic array to find out if the currently accessed page is inside
for(i=0;i<frames.size();i++)
{
// Find the page you are currently visiting
if(frames[i].num==framenum)
{
// To write, mark the current page as dirty page
if(write==true)
frames[i].dirty=write;
break;
}
}
// If i is greater than or equal to the size of the page, the page to be accessed is not in memory, and page replacement is required.
if(i>=frames.size())
{
// Write a disk if it is dirty
if(frames[0].dirty)
writes++;
// Delete the first page you entered and add the new page to the end
frames.erase(frames.begin());
frames.push_back(Frame(framenum,write));
reads++;
faults++;
}
events++;
}
// LUR algorithm
void PageLRU(vector<Frame> &frames,long long framenum,bool write)
{
int i;
// Traversing the page in the current dynamic array to find out if the currently accessed page is inside
for(i=0;i<frames.size();i++)
{
// Find the page that is currently visiting
if(frames[i].num==framenum)
{
// Write a disk if it is dirty
if(write==true)
frames[i].dirty=write;
if(i<frames.size()-1)
{
// Move the current page to the end
Frame f=frames[i];
frames.erase(frames.begin()+i);
frames.push_back(f);
}
break;
}
}
// If i is greater than or equal to the size of the page, the page to be accessed is not in memory, and page replacement is required.
if(i>=frames.size())
{
// Write a disk if it is dirty
if(frames[0].dirty)
writes++;
// Move the current page to the end
frames.erase(frames.begin());
frames.push_back(Frame(framenum,write));
reads++;
faults++;
}
events++;
}
// ARB algorithm:
void PageARB(vector<Frame> &frames,long long framenum,bool write,int bit,int interval)
{
int i;
// Judging that the refresh time has arrived
if(events%interval==0)
{
// All pages are traversed and the shift register is shifted one bit to the right
for(i=0;i<frames.size();i++)
frames[i].re>>=1;
}
// Iterates through the current dynamic array to see if the currently accessed page is in it
for(i=0;i<frames.size();i++)
{
// Find the currently accessed page
if(frames[i].num==framenum)
{
// Write a disk if it is dirty
if(write==true)
frames[i].dirty=write;
// Update the register to the highest bit of 1
frames[i].re|=1<<(bit-1);
break;
}
}
// If I is larger than or equal to the size of the page,
// the current page to be accessed is not in memory and a page replacement is required
if(i>=frames.size())
{
int minindex=0;
// Traverse the current page to find the item with the smallest register value
for(i=0;i<frames.size();i++)
{
if(frames[minindex].re>frames[i].re)
minindex=i;
}
// Determine if it is a dirty page and write to disk
if(frames[minindex].dirty)
writes++;
// Delete the item with the smallest register value, eliminate it, and add the new page to the end
frames.erase(frames.begin()+minindex);
frames.push_back(Frame(framenum,write,1<<(bit-1)));
reads++;
faults++;
}
events++;
}
// WSARB algorithm:
void PageWSARB(vector<Frame> &frames,long long framenum,bool write,int bit,int interval,int window)
{
int i;
// Determine if the refresh time is up
if(events%interval==0)
{
// All pages are traversed and the shift register is shifted one bit to the right
for(i=0;i<frames.size();i++)
frames[i].re>>=1;
}
// Iterate through the page, updating the window value -1
for(i=0;i<frames.size();i++)
{
if(frames[i].cnt>0)
frames[i].cnt-=1;
}
// Iterates through the current dynamic array to see if the currently accessed page is in it
for(i=0;i<frames.size();i++)
{
// Find the currently accessed page
if(frames[i].num==framenum)
{
// Write a disk if it is dirty
if(write==true)
frames[i].dirty=write;
// The update register value has a maximum value of 1, and the update window value has a maximum value
frames[i].re|=1<<(bit-1);
frames[i].cnt=window;
break;
}
}
// If I is larger than or equal to the size of the page, the current page to be accessed is not in memory and a page replacement is required
if(i>=frames.size())
{
int minindex=0;
// Traverse the current page to find the item with the smallest window value
for(i=0;i<frames.size();i++)
{
if(frames[minindex].cnt>frames[i].cnt)
minindex=i;
}
// Traverse the current page to find the item with the smallest register value in the same case as the window minimum
for(i=minindex;i<frames.size();i++)
{
if(frames[minindex].cnt!=frames[i].cnt)
continue;
if(frames[minindex].re>frames[i].re)
minindex=i;
}