操作系统实验三-存储管理

  • 示例实验程序中模拟两种置换算法:LRU 算法和 FIFO 算法
  • 给定任意序列不同的页面引用序列和任意分配页面数目,显示两种算法的页置换过程
  • 能统计和报告不同置换算法情况下依次淘汰的页号缺页次数(页错误数)缺页率

注意,缺页次数不是置换次数,只要不在就算

#include 

using namespace std;

typedef struct page {
  union {
    int num;
    int size;
  } Meta;
  struct page* next;
} Page, *Memory;

int N, M, F;

void FIFO(int num, Memory front, Memory rear);
void LRU(int num, Memory front, Memory rear);
void inline PageJoin(int num, Memory front, Memory rear);
int inline PageReplace(int num, Memory front, Memory rear);
void inline PageAdvance(int num, Memory page_front, Memory rear);
void Print(Memory front);

int main(int argc, char* argv[]){
  if (argc < 2) {
    cout << "Please input page replacement algorithm and retry" << endl;
    return 1;
  }
  F = 0;
  Memory front = new Page;
  Memory rear = new Page;
  front->Meta.size = 0;
  front->next = NULL;
  rear->next = front;
  string mode = argv[1];
  cout << mode << endl;
  cin >> N >> M;
  for (int i = 0; i < N; i++) {
    int num;
    cin >> num;
    if (mode == "FIFO") {
      FIFO(num, front, rear);
    }
    else if (mode == "LRU") {
      LRU(num, front, rear);
    }
  }
  cout << "Page fault number:" << F << endl;
  cout << "Page fault rate:" << F * 1.0 / N << endl;
}

void FIFO(int num, Memory front, Memory rear) {
  Memory tmp;
  int outPage = -1;
  bool found = false;
  tmp = front->next;

  // 寻找是否已在内存
  while (tmp) {
    if (tmp->Meta.num == num) {
      found = true;
      break;
    }
    tmp = tmp->next;
  }
  if (!found) {
    F++;
    if (front->Meta.size < M) {
      // 页面加入
      PageJoin(num, front, rear);
    }
    else {
      // 页面置换
      outPage = PageReplace(num, front, rear);
    }
  }
  cout << "> " << num << " ";
  Print(front);
  if (outPage != -1) {
    cout << " Out:" << outPage << endl;
  }
}

void LRU(int num, Memory front, Memory rear) {
  Memory tmp;
  int outPage = -1;
  bool found = false;
  tmp = front;

  // 寻找是否已在内存
  while (tmp->next) {
    if (tmp->next->Meta.num == num) {
      found = true;
      break;
    }
    tmp = tmp->next;
  }
  if (!found) {
    F++;
    if (front->Meta.size < M) {
      // 页面加入
      PageJoin(num, front, rear);
    }
    else {
      // 页面置换
      outPage = PageReplace(num, front, rear);
    }
  }
  else {
    // 页面提前
    PageAdvance(num, tmp, rear);
  }
  cout << "> " << num << " ";
  Print(front);
  if (outPage != -1) {
    cout << "Out:" << outPage << endl;
  }
}

void Print(Memory front) {
  Memory tmp = new Page;
  tmp = front->next;
  cout << "[";
  while (tmp) {
    cout << " " << tmp->Meta.num;
    tmp = tmp->next;
  }
  cout << " ]" << endl;
  delete tmp;
}

void inline PageJoin(int num, Memory front, Memory rear) {
  Memory newPage = new Page;
  newPage->Meta.num = num;
  newPage->next = NULL;
  rear->next->next = newPage;
  rear->next = newPage;
  front->Meta.size++;
}

int inline PageReplace(int num, Memory front, Memory rear) {
  int outPage;
  Memory t = front->next;
  front->next = front->next->next;
  outPage = t->Meta.num;
  delete t;
  Memory newPage = new Page;
  newPage->Meta.num = num;
  newPage->next = NULL;
  rear->next->next = newPage;
  rear->next = newPage;
  return outPage;
}

void inline PageAdvance(int num, Memory page_front, Memory rear) {
  Memory page = page_front->next;
  page_front->next = page_front->next->next;
  page->next = NULL;
  rear->next->next = page;
  rear->next = page;
}

  • Usage:

    g++ test3.cpp && ./a.out <algorithm>
    N M
    <P1> <P2> ... <PN>
    
  • Sample1:

    g++ test3.cpp -o tmp/tmp && tmp\\tmp FIFO
    20 3
    7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1
    
    
  • Sample2:

    g++ test3.cpp -o tmp/tmp && tmp\\tmp LRU
    11 5
    4 7 0 7 1 0 1 2 1 2 6
    
    

完整代码见Github

你可能感兴趣的:(OS)