先进先出置换算法C++简单实现
时间: 2024-06-19 22:04:40 浏览: 122
先进先出置换算法是操作系统中的一种页面置换算法,其原理是将最早进入内存的页面置换出去。以下是C++中先进先出置换算法的简单实现代码:
```c++
#include<iostream>
using namespace std;
int main()
{
int pages[20], frames[10], total_pages, m, n, position, flag, faults = 0;
cout << "输入页面总数: ";
cin >> total_pages;
cout << "输入页面序列: ";
for(m = 0; m < total_pages; m++)
cin >> pages[m];
cout << "输入帧数: ";
cin >> n;
for(m = 0; m < n; m++)
frames[m] = -1;
position = 0;
cout << "\n页面调度过程如下: \n";
for(m = 0; m < total_pages; m++)
{
flag = 0;
for(n = 0; n < position; n++)
{
if(frames[n] == pages[m])
{
flag = 1;
break;
}
}
if(flag == 0)
{
frames[position] = pages[m];
position++;
faults++;
for(n = 0; n < position; n++)
cout << frames[n] << "\t";
cout << endl;
}
if(position == n)
position = 0;
}
cout << "\n页面错误次数为: " << faults << endl;
return 0;
}
```
阅读全文
相关推荐















