江苏大学操作系统实验虚拟存储器 缺页中断 C#
时间:2025-04-04
时间:2025-04-04
这是本人用C#编写的;江苏大学操作系统上机实验二 虚拟存储器;
using System;
namespace ConsoleApplication1
{
class array1
{
//页表;页号、标志、主存块号、修改标志位、在磁盘上的位置
public static int[,] yb;
public array1()
{
//页表数组
yb = new int[,] { { 0, 1, 5, 0, 011 }, { 1, 1, 8, 0, 012 }, { 2, 1, 9, 0, 013 }, { 3, 1, 1, 0, 21 }, { 4, 0, -1, 0, 022 }, { 5, 0, -1, 0, 023 }, { 6, 0, -1, 0, 121 } };
}
//初始化页表
//是否产生缺页中断
public bool qy(string[] a)
{
for (int i = 0; i < 4; i++)
{
//标识号为1
if (yb[i, 1] == 1)
{
if (yb[i, 0] == Int32.Parse(a[1])) return false;
continue;
}
i++;
}
Console.WriteLine("产生缺页中断");
return true;
}
//输出页表情况
public void displayyb()
{
Console.WriteLine("页号" + " "+"主存块号" +" "+ "单元号" +" "+ "绝对地址");
for (int i = 0; i < 7; i++)
{
for (int j = 0; j < 5; j++)
{
Console.Write(yb[i, j] + " ");
}
Console.WriteLine();
}
}
//正常执行页表
public void exe(string[] a)
{
int k = 0;//记录页表执行页号下标
int addr = 0;//绝对地址
for (int i = 0; i < 4; i++)
{
if (yb[i, 0] == Int32.Parse(a[1]))
{
yb[i, 4] = Int32.Parse(a[2].ToString());
addr = (yb[i, 2] * 128 + yb[i,4]);
k = i;
}
}
char check = '0';
if (a[0] == "存" || a[0] == "取")
check = '0';//不改变
else
check = '1';//改变
switch (check)
{
case '0': Console.WriteLine(a[1] + "页号所形成的绝对地址为:" + addr);
break;
case '1':
yb[k, 3] = 1;
Console.WriteLine(a[1] + "页号所形成的绝对地址为:" + addr);
break;
}
}
public static void Main(String[] args)
{
int kk=0;//被替换的页表号
//执行表;操作符、页号、单元号
string [,]zxb=new string[,]{{"+","0","70"},{"+","1","50"},{"*","2","15"},{"存","3","21"},{"取","0","56"},{"-","6","40"},{"移位","4","053"},{"+","5","023"},{"存","1","037"},{"取","2","078"},{"+","4","001"},{"存","6","084"}};
array1 a1=new array1();
string[]a=new string[4]{null,null,null,null},b=new string[4]{null,null,null,null
};
Console.WriteLine("当前页表情况:");
a1.displayyb();
for(int j=0;j<12;j++)
{
Console.WriteLine();
Console.WriteLine();
for(int i
这是本人用C#编写的;江苏大学操作系统上机实验二 虚拟存储器;
=0;i<3;i++)
{
a[i]=zxb[j,i];
}
Console.WriteLine("当前访问情况操作符:"+a[0]+" 页号:"+a[1]+" 访问单元号:"+a[2]);
//缺页中断
b=a;
while((a1.qy(a)==true))
{
for(int ii=0;ii<7;ii++)
{
if(yb[ii,0]==Int32.Parse(a[1].ToString()))
{
yb[ii,0]=yb[kk,0];
yb[ii,2]=yb[kk,2];
yb[ii,3]=yb[kk,3];
yb[ii,4]=yb[kk,4];
}
continue;
}
yb[kk,0]=Int32.Parse(b[1].ToString());
yb[kk, 4] =
Int32.Parse(b[2].ToString());
kk++;kk=kk-kk/4;//记录替换页表
}
a1.exe(a);
Console.WriteLine("更新后的内存表:");
a1.displayyb();
}
}
}
}