{
if (X[j].Num != -1) X[j].Timer++; }
//输出数据
for (j = 0; j < size; j++) {
if (X[j].Num != -1)
FIFO_label.Text += (X[j].Num - 48).ToString(); else
FIFO_label.Text += \; }
FIFO_label.Text += \;
}
FIFOlosepage = (float)losecount / (float)(page.Length);//缺页率
FIFO_label.Text += \访问次数是:\ + page.Length + \页面置换次数:\ + changecount + \缺页中断次数:\ + losecount + \缺页率是:\ + FIFOlosepage;
} }
(3)LRU置换算法
private void LRU_button1_Click(object sender, EventArgs e) {
if (page.Length == 0 || strsize.Length == 0)
MessageBox.Show(\输入得页面序列或物理块数不能为空\, \提示\, MessageBoxButtons.OK); else {
//初始化数据,并访问第一个页面,并输出访问结果 int i, j, u, losecount, changecount = 0; for (i = 0; i < size; i++) {
X[i].Num = -1; X[i].Timer = 0; }
X[0].Num = page[0]; X[0].Timer = 1; losecount = 1;
LRU_label.Text = \ + (X[0].Num - 48).ToString() + \; //循环,按照页面序列依次访问页面,并输出访问结果 for (i = 1; i < page.Length; i++) {
u = 0;
//如果内存中存在要访问的页面,则置Timer为0,u为1 for (j = 0; j < size; j++)
{
if (X[j].Num == page[i]) {
X[j].Timer = 0;//因为该算法是将最近最久为使用的页面作为淘汰页
u = 1; break; } }
//若内存中不存在空余的空间,且不存在要访问的页面,则进行如下置换
换
if (u != 1 && X[size - 1].Num != -1) {
j = GetMaxTime(); X[j].Num = page[i]; X[j].Timer = 0;
losecount++;//发生缺页中断 changecount++;//发生页面置换 }
//若内存中,不存在要访问的页面,且有空闲的空间,则进行如下置if (u != 1 && X[size - 1].Num == -1) {
for (j = 0; j < size; j++) {
if (X[j].Num == -1) {
X[j].Num = page[i]; losecount++; break; } } }
//对内存中的页面呆的时间进行加1 for (j = 0; j < size; j++) {
if (X[j].Num != -1) {
X[j].Timer++; } }
//数据输出
for (j = 0; j < size; j++) {
if (X[j].Num != -1)
LRU_label.Text += (X[j].Num - 48).ToString();
else {
LRU_label.Text += \; }
}
LRU_label.Text += \; }
LRUlosepage = (float)losecount / (float)(page.Length);
LRU_label.Text+= \访问次数是:\ + page.Length + \页面置换次数:\ + changecount + \缺页中断次数:\ + losecount + \缺页率是:\ + LRUlosepage;
} }
(4)简单Clock置换算法
private void simpleClock_button1_Click(object sender, EventArgs e) {
if (page.Length == 0 || strsize.Length == 0)
MessageBox.Show(\输入得页面序列或物理块数不能为空\, \提示\, MessageBoxButtons.OK); else {
//初始化,并访问第一个页面,输出访问结果 int i, j, u, losecount, changecount = 0; for (i = 0; i < size; i++) {
X[i].Num = -1; X[i].Timer = 0; }
X[0].Num = page[0];
X[0].Timer = 1;//此算法中Timer的含义是访问位,表示该页是否被访问 losecount = 1;
simpleClock_label.Text = \简单Clock\\n\ + (X[0].Num - 48).ToString() + \;
//循环,按照页面序列进行访问,并输出访问结果 for (i = 1; i < page.Length; i++) {
u = 0;
//如果内存中存在要访问的页面,则置访问位为1,u位1,并退出循环 for (j = 0; j < size; j++) {
if (X[j].Num == page[i]) {
X[j].Timer = 1; u = 1; break;
} }
//若内存中不存在要访问的页面,且内存中无空闲的空间,则进行如下置换
if (u != 1 && X[size - 1].Num != -1) {
int tag = 0;
for (j = 0; j < size; j++) {
如下空间
if (X[j].Timer == 1) {
tag++; } else {
X[j].Num = page[i]; X[j].Timer = 1; losecount++; changecount++; break; } }
if (tag == size)//若内存中的页面访问位都为1,则进行如下操作 {
for (j = 0; j < size; j++) {
X[j].Timer = 0; } X[0].Num = page[i]; X[0].Timer = 1; losecount++; changecount++; } }
//若内存中的不存在要访问的页面,且内存中有空闲的空间,则进行if (u != 1 && X[size - 1].Num == -1) {
for (j = 0; j < size; j++) {
if (X[j].Num == -1) {
X[j].Num = page[i];
X[j].Timer = 1; losecount++; break; } } }
//输出数据
for (j = 0; j < size; j++) {
if (X[j].Num != -1)
simpleClock_label.Text += (X[j].Num - 48).ToString(); else {
simpleClock_label.Text += \; }
}
simpleClock_label.Text += \; }
Clocklosepage = (float)losecount / (float)(page.Length);
simpleClock_label.Text+= \访问次数是:\ + page.Length + \页面置换次数:\ + changecount + \缺页中断次数:\ + losecount + \缺页率是:\ + Clocklosepage;
} }
(5)LFU页面置换算法
private void LFU_button1_Click(object sender, EventArgs e) {
if (page.Length == 0 || strsize.Length == 0)
MessageBox.Show(\输入得页面序列或物理块数不能为空\, \提示\, MessageBoxButtons.OK); else {
//初始化,并访问第一个页面,输出访问结果 int i, j, u, losecount, changecount = 0; for (i = 0; i < size; i++) {
X[i].Num = -1; X[i].Timer = 0; X[i].Count = 0; }
X[0].Num = page[0]; X[0].Timer = 1;
X[0].Count = 1;//访问次数
LFU_label.Text = \ + (X[0].Num - 48).ToString() + \; losecount = 1;