您的位置:首页 > 运维架构

操作系统模拟页面调度算法(OPT、FIFO、LRU)演示(vc2010调试通过)(二)

2014-05-23 01:09 537 查看
#include <iostream>
#include <vector>
#include <list>
#include <iomanip>

/************************************************************************
*@description:  模拟虚拟存储管理器的页面调度
*@author     :  kernel_main
*@create time:  2014.05.22
*@email      :  zww0815@gmail.com
************************************************************************/

using namespace std;

typedef list<int> LIST_INT;
typedef list<int>::iterator ITER_LIST_INT;

typedef vector<int> VEC_INT;
typedef vector<int>::iterator ITER_VEC_INT;

#define PAGE_NUM 3

typedef enum
{
RETURN_FIND,
RETURN_NOT_FIND,
} tag_FIND_FLAG;

void FIFO(VEC_INT arr)
{
tag_FIND_FLAG flag = RETURN_NOT_FIND;
LIST_INT list;
int cur_cnt = 0;
int not_find_cnt = 0;
int cnt = 0;

list.assign(PAGE_NUM,0);

for (ITER_VEC_INT iter=arr.begin();iter!=arr.end();iter++)
{
cout << setw(2) << ++cnt << "--------------push" << setw(3) << *iter << " ==> ";
flag = RETURN_NOT_FIND;
for (ITER_LIST_INT iter_list=list.begin();iter_list!=list.end();iter_list++)
{
cout << *iter_list << " ";
if (*iter_list == *iter)
{
flag = RETURN_FIND;
}
}

if (RETURN_FIND == flag)
{
cout << setw(10) << "命中*" << endl;
}
else
{
cout << setw(10) << " 缺页中断^" << endl;

not_find_cnt++;

if (PAGE_NUM == list.size())
{
list.pop_front();
list.push_back(*iter);
}
else
{
list.push_back(*iter);
}
}
}

cout << endl << "总共中断次数: " << not_find_cnt << endl;
}

int main()
{
VEC_INT vec;
int arr[] = {1,2,3,4,2,1,5,6,2,1,2,3,7,6,3,2,1,2,3,6};

for (int i=0;i<sizeof(arr)/sizeof(int);i++)
{
vec.push_back(arr[i]);
}

FIFO(vec);
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: