操作系统程序设计(编程描述页面置换算法——先进先出算法 )

?using System;
using System.Collections.Generic;
using https://www.360docs.net/doc/cf11559938.html,ponentModel;
using System.Data;
using System.Drawing;
//using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace 操作系统课程设计__难得糊涂
{


public partial class Form1 : Form
{

public Form1()
{
InitializeComponent();
}

bool[] shuz = new bool[200];//标记内存的使用情况
class page
{
public page()
{
bInMemoryFlag=false;
nFrameNo=0;
nDiskRelBlockNo=0;
nNextPageNo=0;
bModifiedFlag = false;
}
//构造函数
public bool bInMemoryFlag;//该页是否在内存
public int nFrameNo;//内存的快号
public int nDiskRelBlockNo;//磁盘的相对块号(可以随机给定)
public int nNextPageNo;//下一调进内存页的页号
public bool bModifiedFlag;//修改位(可以随机给定)
}; //页面
class SProcess //进程的信息
{
public SProcess()
{
nPID=0;
szProcessName = "epic";
nPageCountInMemory=0;
nPageNoHead=0;
nPageNoTail=0;
PageTable=null;
nRunTime = 0;
nControlNo=0;
page s = new page();
Max_zu = new int[50] ;
//PageTable =Enumerable.Repeat(s, 200).ToArray();
hero = new int[50];

}
public page[] PageTable=new page[200];
public void InitProcess(int no,int pagetotal,int runtime)
{
nControlNo=no;
nPageCountLogicalSpace=pagetotal;//逻辑地址空间的页数
nRunTime=runtime;//运行时间(运行步数)
PageTable=new page[nPageCountLogicalSpace];//根据实际需要创建页表
}
//public int GetOneLogicalAddress();//得到下一要访问的页的页号
public int[] hero ;
public int nPID;//进程标识符
public string szProcessName;//进程名
public int nControlNo;
public int[] Max_zu;
public int nPageCountLogicalSpace;//逻辑地址空间的页数
public int nPageCountInMemory;//调入内存的页数
public int nRunTime;//运行时间(运行步数)
public int nPageNoHead;//调入内存的首页
public int nPageNoTail;//调入内存的末页
};
#region 界面处理
private int x = 50, y = 320;
private void zi1(string s,int x,int y,Color n)
{
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Near;
//设置字体格式
Brush g =new SolidBrush(n);

Graphics h = this.groupBox1.CreateGraphics(); //创建画布
h.DrawString(s, new Font(new FontFamily("黑体"), 8), g, x, y, sf);

}
//以groupBox1为画板指定坐标
private void zi(string s, int x, int y,Color n)
{
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Near; //设置字体格式
Graphics h = this.CreateGraphics(); //创建画布
Brush g = new SolidBrush(n);
h.DrawString(s, new Font(new FontFamily("宋体"), 10), g, 50 + y * 20, 320 + x * 20, sf);

}
//以this为画板指定坐标
private void cg()
{
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Near; //设置字体格式
Graphics h = this.CreateGraphics(); //创建画布
h.DrawString("模拟成功!!", new Font(new FontFamily("楷体"), 30), Brushes.Red, 200, 50, sf);
}
//模拟成功信息提示
private void hua(int x ,int y,Color n)
{
Graphics h = this.CreateGraphics(); //创建画布
Brush g = new SolidBrush(n);//创建画刷
RectangleF ju = new RectangleF(50+y*20,320+x*20, 19, 19);
h.FillRectangle(g, ju);
h.Dispose();
g.Dispose();


}
//在form1指定位置着色
private void hua1(int x, int y, Color n)
{
Graphics h = this.groupBox1.CreateGraphics(); //创建画布
Brush g = new SolidBrush(n);//创建画刷
RectangleF ju = new RectangleF(x , y, 19, 19);
h.FillRectangle(g, ju);
h.Dispose();
g.Dispose();


}
//在groupbox1指定位置着色

private void sc()
{
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Near; //设置字体格式
Graphics h = this.CreateGraphics(); //创建画布
Brush g = new SolidBrush(Color.Silver );//创建画刷
int a = x;
for (int i = 0; i < 25; i++)
{

h.DrawString((i + 1).ToString(), new Font(new FontFamily("宋体"), 10), Brushes.Black, a, 300, sf);
a += 20;
}//初始化坐标
int c = y;
for (int j = 0; j < 8; j++)
{
int b = x;

h.DrawString((j + 1).ToString(), new Font(new FontFamily("宋体"), 10), Brushes.Black, 30, c, sf);

for (int i = 0; i < 25; i++)
{

RectangleF ju = new RectangleF(b, c, 19, 19);
h.FillRectangle(g, ju);
b += 20;
}
c += 20;
}
}
//

初始化内存状态样式
private void sd()
{
Graphics h = this.CreateGraphics(); //创建画布
Brush g = new SolidBrush(Color.Yellow);//创建画刷
int c = y;
for (int j = -1; j < Convert.ToInt32(nc.Text) / 25; j++)
{
int b = x;
if (j+1 != Convert.ToInt32(nc.Text) / 25)
{
for (int i = 0; i < 25; i++)
{
RectangleF ju = new RectangleF(b, c, 19, 19);
h.FillRectangle(g, ju);
b += 20;
}
c += 20;
}
else
for (int i = 0; i {
RectangleF ju = new RectangleF(b, c, 19, 19);
h.FillRectangle(g, ju);
b += 20;
}

}
}
//可用内存标为黄色
private void so()
{
Graphics h = this.CreateGraphics(); //创建画布
Brush g = new SolidBrush(Color.Red);//创建画刷
int c = y;
for (int j = -1; j < Convert.ToInt32(os.Text) / 25; j++)
{
int b = x;
if (j + 1 != Convert.ToInt32(os.Text) / 25)
{
for (int i = 0; i < 25; i++)
{
RectangleF ju = new RectangleF(b, c, 19, 19);
h.FillRectangle(g, ju);
b += 20;
}
c += 20;
}
else
for (int i = 0; i < Convert.ToInt32(os.Text) % 25; i++)
{
RectangleF ju = new RectangleF(b, c, 19, 19);
h.FillRectangle(g, ju);
b += 20;
}

}
for(int i=0;ishuz[i] = true;
for (int i = Convert.ToInt32(nc.Text); i < 200; i++)
shuz[i] = true;
}
//将系统已经占用的内存标为红色
#endregion
#region 模拟程序算法
private bool pan1()
{

try {
int a = Convert.ToInt32(os.Text) + Convert.ToInt32(nc.Text) + Convert.ToInt32(Max_kuai.Text) + Convert.ToInt32(jcs.Text);
if (Convert.ToInt32(os.Text) > Convert.ToInt32(nc.Text) || Convert.ToInt32(nc.Text) > 200 || Convert.ToInt32(Max_kuai.Text) > 200 || Convert.ToInt32(jcs.Text)>3)
return false;
button2.Enabled = true;
button1.Enabled = false;
nc.Enabled = false;
os.Enabled = false;
Max_kuai.Enabled = false;
jcs.Enabled = false;
button3.E

nabled = true;
return true ;
}
catch {

return false ;
}

}
//检查输入的信息是否正确
private bool pan2()
{

try
{
int a = Convert.ToInt32(jc01.Text) + Convert.ToInt32(jc02.Text) + Convert.ToInt32(jc11.Text) + Convert.ToInt32(jc12.Text);
a += Convert.ToInt32(jc21.Text) + Convert.ToInt32(jc22.Text);
button2.Enabled = false;
return true;
}
catch
{
MessageBox.Show("你的输入有误!");
return false;
}
}
//检查输入的信息是否正确
private int sjs()
{
Random s = new Random();
return s.Next(Convert .ToInt32 (os.Text)+1,Convert .ToInt32 (nc.Text ));
}
// 产生随机数
private int cgs = 0;//已完成进程数
SProcess pros1 = new SProcess();
SProcess pros2 = new SProcess();
SProcess pros3 = new SProcess();
string s1 = "";
string s2 = "";
string s3 = "";

private int sjy(int a)
{
Random s = new Random();
if (a== 1)
{
return s.Next(1, Convert.ToInt32(jc01.Text) + 1);
}
if (a == 2)
{
return s.Next(1, Convert.ToInt32(jc11.Text)+1);
}
else
return s.Next(1, Convert.ToInt32(jc21.Text)+1);

}
//产生随机数
private int wsm(SProcess af,int sf)
{
for (int iw = 1; iw < Convert.ToInt32(Max_kuai.Text) + 1; iw++)
if (af.hero[iw] == sf)
return iw;
return 0;
}
int sb = 310;
private void jchero(int m)
{
if (m > 0)
{
int g1 = sjy(1);
int sui = sjs();
while (shuz[sui%200] == true)
sui = (sjs() + 1) % Convert.ToInt32(nc.Text);
shuz[sui] = true;
//pros1.PageTable[1].nFrameNo = sui;
//pros1.PageTable[1].bInMemoryFlag = true;
pros1.nPID = 1;
pros1.InitProcess(1, Convert.ToInt32(jc01.Text), Convert.ToInt32(jc02.Text));
pros1.nPageNoHead = sui;
pros1.nRunTime--;
hua(sui / 25, sui % 25, Color.Blue);
zi(g1.ToString(), sui / 25, sui % 25, Color.White);
zi1(sui.ToString(), sb, 60,Color .Blue);
zi1(g1.ToString(), sb, 50, Color.Blue);
pros1.Max_zu[1] = sui;
zi1(g1.ToString() + "-", 18, 75, Color.Blue);
p

ros1.hero[1] = g1;
zi1("↓".ToString(), 310, 40, Color.Red);
pros1.nControlNo = 310;
}

if (m > 1 )
{Thread.Sleep(100);

int g2 = sjy(2);
int sui = sjs() + 1;
while (shuz[sui%200] == true)
sui = (sjs() + 1) % Convert.ToInt32(nc.Text);
shuz[sui] = true;
// pros2.PageTable[1].nFrameNo = sui;
//pros2.PageTable[1].bInMemoryFlag = true;
pros2.nPID = 2;
pros2.InitProcess(2, Convert.ToInt32(jc11.Text), Convert.ToInt32(jc12.Text));
pros2.nPageNoHead = sui;
pros2.nRunTime--;
hua(sui / 25, sui % 25, Color.YellowGreen);
zi(g2.ToString(), sui / 25, sui % 25, Color.White);
zi1(sui.ToString(), sb, 100, Color.Blue);
zi1(g2.ToString(), sb, 90, Color.Blue);
pros2.Max_zu[1] = sui;
zi1(g2.ToString() + "-", 18, 115, Color.YellowGreen);
pros2.hero[1] = g2;
pros2.hero[1] = g2;
zi1("↓".ToString(), 310, 80, Color.Red);
pros2.nControlNo = 310;
}

if (m > 2)
{Thread.Sleep(100);
int g3 = sjy(3);
int sui = sjs() + 2;
while (shuz[sui%200] == true)
sui = (sjs() + 1) % Convert.ToInt32(nc.Text);
shuz[sui] = true;
//pros3.PageTable[1].nFrameNo = sui;
//pros3.PageTable[1].bInMemoryFlag = true;
pros3.nPID = 3;
pros3.InitProcess(3, Convert.ToInt32(jc21.Text), Convert.ToInt32(jc22.Text));
pros3.nPageNoHead = sui;
pros3.nRunTime--;
hua(sui / 25, sui % 25, Color.Black);
zi(g3.ToString(), sui / 25, sui % 25, Color.White);
zi1(sui.ToString(), sb, 140, Color.Blue);
zi1(g3.ToString(), sb, 130, Color.Blue);
pros3.Max_zu[1] = sui;
zi1(g3.ToString() + "-", 18, 155, Color.Black);
pros3.hero[1] = g3;
pros3.hero[1] = g3;
zi1("↓".ToString(), 310, 120, Color.Red);
pros3.nControlNo = 310;
}
}
//进程初始化,第一步

private void pro1()
{
int flag = 1;
int sb = 290;
Color huans = ((sb - 290) / (20 * Convert.ToInt32(Max_kuai.Text))) % 2 == 1 ? Color.GreenYellow : Color.Pink;
int p = Convert.ToInt32(Max_kuai.Text);
for (int i =

2; pros1.nRunTime > 0; i++)
{
if (Convert.ToInt32(jcs.Text) < 1)
break;
//if (i == Convert.ToInt32(Max_kuai.Text) + 1)
// break;
Thread.Sleep(500);
int g1 = sjy(1);
int sui = sjs();
while (wsm(pros1, g1) != 0 && pros1.nRunTime > 0)
{
zi1(g1.ToString() + "-", i*18, 75, Color.Blue);
pros1.nRunTime--;
i++;
zi1("■".ToString(), pros1.nControlNo, 40, SystemColors.Control);
zi1("↓".ToString(), 290 + 20 * wsm(pros1, g1), 40, Color.Red);
pros1.nControlNo = 290 + 20 * wsm(pros1, g1);
Thread.Sleep(500);
g1 = sjy(1);
}

if(flag == Convert.ToInt32(Max_kuai.Text)&&pros1.nRunTime >0)
{

sb += 20;
int zih = 310 + (sb - 290) % (20 * Convert.ToInt32(Max_kuai.Text));
Thread.Sleep(500);
sui = pros1.Max_zu[p % Convert.ToInt32(Max_kuai.Text) + 1];
pros1.nRunTime--;
hua(sui / 25, sui % 25, Color.Blue);
zi(g1.ToString(), sui / 25, sui % 25, Color.White);
hua1(zih, 50, huans);
zi1(sui.ToString(), zih, 60, Color.Blue);
zi1(g1.ToString(), zih, 50, Color.Blue);
zi1("■".ToString(), pros1.nControlNo, 40, SystemColors.Control);
zi1("↓".ToString(), zih, 40, Color.Red);
pros1.nControlNo = zih;
zi1(g1.ToString() + "-", i * 18, 75, Color.Blue);
p++;
}
if (pros1.nRunTime > 0 && flag {

flag++;
int zih = 330 + (sb - 290) % (20 * Convert.ToInt32(Max_kuai.Text));

zi1(g1.ToString() + "-",i*18, 75, Color.Blue);
while (shuz[sui] == true)
sui = (sjs() + 1) % Convert.ToInt32(nc.Text);
shuz[sui] = true;
pros1.nRunTime--;
hua(sui / 25, sui % 25, Color.Blue);
zi(g1.ToString(), sui / 25, sui % 25, Color.White);
zi1(sui.ToString(),zih, 60, Color.Blue);
pros1.Max_zu[flag] = sui;
zi1(g1.ToString(), zih, 50, Color.Blue);
pros1.hero[flag] = g1;
// zi1("■".ToString(), 290, 40, Color .Red);
zi1("■".ToString(), pros1.nControlNo, 40, SystemColors .Control);
zi1("↓".ToString(), zih, 4

0, Color.Red);
pros1.nControlNo = zih;
sb += 20;
}
}
if(++cgs==3)
cg();
}
private void pro2()
{
int flag = 1;
int sb = 290;
Color huans = ((sb - 290) / (20 * Convert.ToInt32(Max_kuai.Text))) % 2 == 1 ? Color.GreenYellow : Color.Pink;
int p = Convert.ToInt32(Max_kuai.Text);
for (int i = 2; pros2.nRunTime > 0; i++)
{
if (Convert.ToInt32(jcs.Text) < 1)
break;
//if (i == Convert.ToInt32(Max_kuai.Text) + 1)
// break;
Thread.Sleep(500);
int g1 = sjy(2);
int sui = sjs();
while (wsm(pros2, g1) != 0 && pros2.nRunTime > 0)
{
zi1(g1.ToString() + "-", i * 18, 115, Color.YellowGreen);
pros2.nRunTime--;
i++;
zi1("■".ToString(), pros2.nControlNo, 80, SystemColors.Control);
zi1("↓".ToString(), 290 + 20 * wsm(pros2, g1), 80, Color.Red);
pros2.nControlNo = 290 + 20 * wsm(pros2, g1);
Thread.Sleep(500);
g1 = sjy(2);
}

if (flag == Convert.ToInt32(Max_kuai.Text) && pros2.nRunTime > 0)
{

sb += 20;
int zih = 310 + (sb - 290) % (20 * Convert.ToInt32(Max_kuai.Text));
Thread.Sleep(500);
sui = pros2.Max_zu[p % Convert.ToInt32(Max_kuai.Text) + 1];
pros2.nRunTime--;
hua(sui / 25, sui % 25, Color.YellowGreen);
zi(g1.ToString(), sui / 25, sui % 25, Color.White);
hua1(zih, 90, huans);
zi1(sui.ToString(), zih, 100, Color.Blue);
zi1(g1.ToString(), zih, 90, Color.Blue);
zi1("■".ToString(), pros2.nControlNo, 80, SystemColors.Control);
zi1("↓".ToString(), zih, 80, Color.Red);
pros2.nControlNo = zih;
zi1(g1.ToString() + "-", i * 18, 115, Color.YellowGreen);
p++;
}
if (pros2.nRunTime > 0 && flag < Convert.ToInt32(Max_kuai.Text))
{

flag++;
int zih = 330 + (sb - 290) % (20 * Convert.ToInt32(Max_kuai.Text));

zi1(g1.ToString() + "-", i * 18, 115, Color.YellowGreen);
while (shuz[sui] == true)
sui = (sjs() + 1) % Convert.ToInt32(nc.Text);
shuz[sui] = true;
pros2.nRunTime--;
hua(sui / 25, sui % 25, Color.YellowGreen);
zi(g1.ToString(), sui / 25, sui % 25, Col

or.White);
zi1(sui.ToString(), zih, 100, Color.Blue);
pros2.Max_zu[flag] = sui;
zi1(g1.ToString(), zih, 90, Color.Blue);
pros2.hero[flag] = g1;
// zi1("■".ToString(), 290, 80, Color .Red);
zi1("■".ToString(), pros2.nControlNo, 80, SystemColors.Control);
zi1("↓".ToString(), zih, 80, Color.Red);
pros2.nControlNo = zih;
sb += 20;
}
}
if (++cgs == 3)
cg();
}
private void pro3()
{
int flag = 1;
int sb = 290;
Color huans = ((sb - 290) / (20 * Convert.ToInt32(Max_kuai.Text))) % 2 == 1 ? Color.GreenYellow : Color.Pink;
int p = Convert.ToInt32(Max_kuai.Text);
for (int i = 2; pros3.nRunTime > 0; i++)
{
if (Convert.ToInt32(jcs.Text) < 1)
break;
//if (i == Convert.ToInt32(Max_kuai.Text) + 1)
// break;
Thread.Sleep(500);
int g1 = sjy(3);
int sui = sjs();
while (wsm(pros3, g1) != 0 && pros3.nRunTime > 0)
{
zi1(g1.ToString() + "-", i * 18, 155, Color.Black);
pros3.nRunTime--;
i++;
zi1("■".ToString(), pros3.nControlNo, 120, SystemColors.Control);
zi1("↓".ToString(), 290 + 20 * wsm(pros3, g1), 120, Color.Red);
pros3.nControlNo = 290 + 20 * wsm(pros3, g1);
Thread.Sleep(500);
g1 = sjy(3);
}

if (flag == Convert.ToInt32(Max_kuai.Text) && pros3.nRunTime > 0)
{

sb += 20;
int zih = 310 + (sb - 290) % (20 * Convert.ToInt32(Max_kuai.Text));
Thread.Sleep(500);
sui = pros3.Max_zu[p % Convert.ToInt32(Max_kuai.Text) + 1];
pros3.nRunTime--;
hua(sui / 25, sui % 25, Color.Black);
zi(g1.ToString(), sui / 25, sui % 25, Color.White);
hua1(zih, 130, huans);
zi1(sui.ToString(), zih, 140, Color.Blue);
zi1(g1.ToString(), zih, 130, Color.Blue);
zi1("■".ToString(), pros3.nControlNo, 120, SystemColors.Control);
zi1("↓".ToString(), zih, 120, Color.Red);
pros3.nControlNo = zih;
zi1(g1.ToString() + "-", i * 18, 155, Color.Black);
p++;
}
if (pros3.nRunTime > 0 && flag < Convert.ToInt32(Max_kuai.Text))
{

flag++;
int zih = 330 + (sb - 290) % (20 * Convert.

ToInt32(Max_kuai.Text));

zi1(g1.ToString() + "-", i * 18, 155, Color.Black);
while (shuz[sui] == true)
sui = (sjs() + 1) % Convert.ToInt32(nc.Text);
shuz[sui] = true;
pros3.nRunTime--;
hua(sui / 25, sui % 25, Color.Black);
zi(g1.ToString(), sui / 25, sui % 25, Color.White);
zi1(sui.ToString(), zih, 140, Color.Blue);
pros3.Max_zu[flag] = sui;
zi1(g1.ToString(), zih, 130, Color.Blue);
pros3.hero[flag] = g1;
// zi1("■".ToString(), 290, 120, Color .Red);
zi1("■".ToString(), pros3.nControlNo, 120, SystemColors.Control);
zi1("↓".ToString(), zih, 120, Color.Red);
pros3.nControlNo = zih;
sb += 20;
}
}
if (++cgs == 3)
cg();
}
//超出最大可用块,页面置换
#endregion
private void button1_Click(object sender, EventArgs e)
{
if (pan1())
{
sc();
sd();
so();
}
else
MessageBox.Show("你的输入有误!");


}
private void button2_Click(object sender, EventArgs e)
{
Thread pr1=new Thread(pro1);
Thread pr2 = new Thread(pro2);
Thread pr3 = new Thread(pro3);
int m = Convert.ToInt32(jcs.Text);
if (pan2())
{
jchero(m);
pr1 .Start();
pr2.Start();
pr3.Start();
// while (flag !=1)


}

}
private void button3_Click(object sender, EventArgs e)
{
//InitializeComponent();
this.Close();
}

private void Form1_Load(object sender, EventArgs e)
{

}

}
}

相关文档
最新文档