语言:C#
工具:VS2017
代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp2
{
public partial class Form1 : Form
{
int[] Memory = new int[100005];
int[] vist = new int[10005];
int[,] process = new int[10005, 10005];
int[] proPageNum = new int[10005];
int[,] runad = new int[10005, 10005];
int[,] processhave = new int[10005, 10005];//暂时分配的页数
int[] visid = new int[10005];
int[] que = new int[10005];
int[] Mpr = new int[10005];
int frameSize;
int frameNum;
string []binaryAd=new string[100];
int tot,ad;
int ans = 0;
int par=0;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string kas = textBox1.Text;
if (kas.Equals(""))
{
MessageBox.Show("请输入有效值!");
return;
}
frameSize = int.Parse(kas);
kas = textBox2.Text;
if (kas.Equals(""))
{
MessageBox.Show("请输入有效值!");
return;
}
int b = int.Parse(kas);
if (frameSize == 0)
{
MessageBox.Show("非法!");
return;
}
else
{
for(int i=0;i<10005;i++)
{
for(int j=0;j<10005;j++)
{
process[i, j] = -1;
}
}
frameNum = b / frameSize;
tot = frameNum;
MessageBox.Show("内存初始化完成,主存共有" + frameNum + "个物理框。");
ad = 0;
int qd = frameSize;
while(qd!=0)
{
ad++;
qd /= 2;
}
ad--;
}
progressBar1.Maximum =b;//设置最大长度值
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
string s = textBox3.Text;
if (s.Equals(""))
{
MessageBox.Show("请输入有效值!");
return;
}
int id = int.Parse(s);
string ss = textBox4.Text;
if (ss.Equals(""))
{
MessageBox.Show("请输入有效值!");
return;
}
int size = int.Parse(ss);
if (frameSize==0)
{
MessageBox.Show("非法!");
}
else
{
size = (int)Math.Ceiling((double)size/ frameSize);
int icv = size;
if (visid[id] == 1)
{
MessageBox.Show("进程号唯一,请重新输入作业号(从1开始)");
return;
}
else if (size > tot)
{
MessageBox.Show("主存剩余空间不能装下该作业,请求失败.");
return;
}
else
{
for(int i=0;i<10005;i++)
{
process[id, i] = -1;
}
tot -= size;
visid[id] = 1;
MessageBox.Show("分配成功!将顺序为此进程分配" + size + "个物理框");
proPageNum[id] = size;
for (int i = 0, k = 0; i < frameNum && k < size; i++)
{
if (Memory[i] == 0)
{
process[id, k] = i;
Memory[i] = id;
k++;
}
}
}
if (progressBar1.Value + icv <= progressBar1.Maximum)
{
progressBar1.Value += icv*frameSize; ;//设置当前值
}
}
}
private void label5_Click(object sender, EventArgs e)
{
}
private void button3_Click(object sender, EventArgs e)
{
string s = string.Empty;
for (int i = 0; i < frameNum; i++)
{
if (Memory[i] == 0) s += " N ";
else { s+=" "+Memory[i].ToString()+" "; }
if ((i+1 )% 5 == 0)
{
s += "\r\n";
}
}
textBox5.Text = s;
}
private void button4_Click(object sender, EventArgs e)
{
int processid;
string s = textBox7.Text;
if (s.Equals(""))
{
MessageBox.Show("请输入有效值!");
return;
}
processid = int.Parse(s);
string rs = string.Empty;
if (visid[processid] == 0)
{
MessageBox.Show("没有该进程!");
}
else
{
rs = " 页号 物理框号\r\n\r\n";
for (int i = 0; i <10005; i++)
{
if (process[processid, i] == -1) continue;
rs += " "+i+" " + process[processid, i].ToString() + "\r\n";
}
textBox6.Text = rs;
}
}
private void 跑进程_Click(object sender, EventArgs e)
{
}
private void button5_Click(object sender, EventArgs e)
{
if(radioButton1.Checked==true)
{
//MessageBox.Show("FIFO");
Mpr = new int[1000];
vist = new int[1000];
string s = string.Empty;
if (textBox8.Text.Equals("")|| textBox9.Text.Equals("")||textBox10.Text.Equals(""))
{
MessageBox.Show("请输入有效值!");
return;
}
int number = int.Parse(textBox8.Text);//进程号
if(visid[number]==1)
{
MessageBox.Show("进程号唯一,请重新输入。");
return;
}
int fnum =int.Parse( textBox9.Text);//物理框数
if(fnum>tot)
{
MessageBox.Show("主存剩余空间不能装下该作业,请求失败.");
return;
}
visid[number] = 1;
for (int i = 0; i <10005; i++)
{
process[number, i] = -1;
}
for (int i = 0, k = 1; i < frameNum && k <= fnum; i++)
{
if (Memory[i] == 0)
{
processhave[number,k] = i;
k++;
}
}
string os = textBox10.Text;
runad = new int[1000, 1000];
int a = 0;
int cnt = 0;
for (int i = 0; i < os.Length; i++)
{
if (os[i] != ' ')
{
a = a * 10 + os[i] - '0';
}
else
{
if (a > 0)
{
runad[number, ++cnt] = a;
}
a = 0;
}
}
if (a > 0) runad[number, ++cnt] = a;
int cntt = cnt;
s += "物理框号 ";
for (int i = 1; i <= fnum; i++)
{
s+=processhave[number,i].ToString()+" ";
}
s += " 状态\r\n\r\n";
int cas = 0;
int top = 0;
int tail = 0;
int ming = 0;
for (int i = 1; i <= cnt; i++)
{
cas = 0;
s += " " + runad[number, i].ToString() + "| ";
if (vist[runad[number,i]]==1)
{
for (int j = 1; j <= fnum; j++)
{
s+= Mpr[processhave[number,j]].ToString()+ " ";
}
s +=" 命中\r\n";
ming++;
continue;
}
for (int j = 1; j <= fnum; j++)
{
//cout<<j<<"++"<<number<<"++"<<Memory[processhave[number][j]]<<" ";
if (processhave[number, j]!=-1&&Memory[processhave[number,j]] == 0)
{
process[number,runad[number,i]] = processhave[number,j];
Memory[processhave[number,j]] = number;
que[tail++] = runad[number,i];
Mpr[processhave[number,j]] = runad[number,i];
vist[runad[number,i]] = 1;
break;
}
else cas++;
}
int toppage=0;
if (cas == fnum)
{
toppage = que[top++];
// cout<<"++"<<toppage<<endl;
vist[toppage] = 0;
process[number,runad[number,i]] = process[number,toppage];
vist[runad[number,i]] = 1;
Mpr[process[number,toppage]] = runad[number,i];
que[tail++] = runad[number,i];
process[number,toppage] = -1;
//cout<<"淘汰的页面"<<endl;
}
for (int j = 1; j <= fnum; j++)
{
s+=Mpr[processhave[number,j]].ToString()+ " ";
}
if (cas == fnum) s += " 置换" + toppage.ToString();
else
{
s += " 直接调页";
}
s += "\r\n";
}
int maxl =0;
for(int i=0;i<10005;i++)
{
if (process[number, i] != -1) maxl++;
}
proPageNum[number] =maxl;
double hide = Convert.ToDouble(ming)/Convert.ToDouble(cnt);
MessageBox.Show(s+"\r\n利用FIFO置换算法,其中命中"+ming+"次"+",命中率为"+hide.ToString("0.00"));
progressBar1.Value += frameSize * fnum;
tot -= fnum;
}
else if(radioButton2.Checked==true)
{
//MessageBox.Show("LRU");
/*fkdsnfgkodsg*/
Mpr = new int[1000];
vist = new int[1000];
string s = string.Empty;
if (textBox8.Text.Equals("") || textBox9.Text.Equals("") || textBox10.Text.Equals(""))
{
MessageBox.Show("请输入有效值!");
return;
}
int number = int.Parse(textBox8.Text);//进程号
if (visid[number] == 1)
{
MessageBox.Show("进程号唯一,请重新输入。");
return;
}
int fnum = int.Parse(textBox9.Text);//物理框数
if (fnum > tot)
{
MessageBox.Show("主存剩余空间不能装下该作业,请求失败.");
return;
}
visid[number] = 1;
for (int i = 0; i < 10005; i++)
{
process[number, i] = -1;
}
for (int i = 0, k = 1; i < frameNum && k <= fnum; i++)
{
if (Memory[i] == 0)
{
processhave[number, k] = i;
k++;
}
}
string os = textBox10.Text;
runad = new int[1000, 1000];
int a = 0;
int cnt = 0;
for (int i = 0; i < os.Length; i++)
{
if (os[i] != ' ')
{
a = a * 10 + os[i] - '0';
}
else
{
if (a > 0)
{
runad[number, ++cnt] = a;
}
a = 0;
}
}
if (a > 0) runad[number, ++cnt] = a;
int cntt = cnt;
s += "物理框号 ";
for (int i = 1; i <= fnum; i++)
{
s += processhave[number, i].ToString() + " ";
}
s += " 状态\r\n\r\n";
int cas = 0;
int ming = 0;
for (int i = 1; i <= cnt; i++)
{
cas = 0;
s += " " + runad[number, i].ToString() + "| ";
if (vist[runad[number, i]] == 1)
{
for (int j = 1; j <= fnum; j++)
{
s += Mpr[processhave[number, j]].ToString() + " ";
}
s += " 命中\r\n";
ming++;
continue;
}
for (int j = 1; j <= fnum; j++)
{
//cout<<j<<"++"<<number<<"++"<<Memory[processhave[number][j]]<<" ";
if (processhave[number, j] != -1 && Memory[processhave[number, j]] == 0)
{
process[number, runad[number, i]] = processhave[number, j];
Memory[processhave[number, j]] = number;
Mpr[processhave[number, j]] = runad[number, i];
vist[runad[number, i]] = 1;
break;
}
else cas++;
}
int toppage = 0;
if (cas == fnum)
{
int pos = i;
int knum = 0;
while(knum<fnum)
{
if (runad[number, pos] != runad[number, pos - 1]) knum++;
if (knum == fnum)
{
toppage = runad[number, pos-1];
break;
}
pos--;
}
vist[toppage] = 0;
process[number, runad[number, i]] = process[number, toppage];
vist[runad[number, i]] = 1;
Mpr[process[number, toppage]] = runad[number, i];
process[number, toppage] = -1;
//cout<<"淘汰的页面"<<endl;
}
for (int j = 1; j <= fnum; j++)
{
s += Mpr[processhave[number, j]].ToString() + " ";
}
if (cas == fnum) s += " 置换" + toppage.ToString();
else
{
s += " 直接调页";
}
s += "\r\n";
}
int maxl = 0;
for (int i = 0; i < 10005; i++)
{
if (process[number, i] != -1) maxl++;
}
proPageNum[number] = maxl;
double hide = Convert.ToDouble(ming) / Convert.ToDouble(cnt);
MessageBox.Show(s + "\r\n利用LRU置换算法,其中命中" + ming + "次" + ",命中率为" + hide.ToString("0.00"));
progressBar1.Value += frameSize * fnum;
tot -= fnum;
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void progressBar1_Click(object sender, EventArgs e)
{
}
private void label6_Click(object sender, EventArgs e)
{
}
private void button6_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
}
private Point mousePoint = new Point();
private void panel1_MouseMove(object sender, MouseEventArgs e)
{
base.OnMouseMove(e);
if (e.Button == MouseButtons.Left)
{
this.Top = Control.MousePosition.Y - mousePoint.Y;
this.Left = Control.MousePosition.X - mousePoint.X;
}
}
private void panel1_MouseDown(object sender, MouseEventArgs e)
{
base.OnMouseDown(e);
this.mousePoint.X = e.X;
this.mousePoint.Y = e.Y;
}
private void button6_Click_1(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Minimized;
}
private void button7_Click(object sender, EventArgs e)
{
DialogResult result = MessageBox.Show("是否退出?", "操作提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (result == DialogResult.Yes)
{
//this.Dispose();
Application.Exit();
}
}
private void button8_Click(object sender, EventArgs e)
{
int id = int.Parse(textBox12.Text);
int sub = proPageNum[id];
for (int i = 0; i <10005; i++)
{
if(process[id,i]!=-1)
Memory[process[id,i]] = 0;
process[id,i] = -1;
}
visid[id] = 0;
tot += proPageNum[id];
proPageNum[id] = 0;
MessageBox.Show("回收完毕!");
progressBar1.Value -= sub * frameSize;
}
private void button9_Click(object sender, EventArgs e)
{
Dictionary<string, string> M = new Dictionary<string, string>();
//添加键值对数据,键必须唯一,值可重复
M.Add("0000", "0");
M.Add("0001", "1");
M.Add("0010", "2");
M.Add("0011", "3");
M.Add("0100", "4");
M.Add("0101", "5");
M.Add("0110", "6");
M.Add("0111", "7");
M.Add("1000", "8");
M.Add("1001", "9");
M.Add("1010", "A");
M.Add("1011", "B");
M.Add("1100", "C");
M.Add("1101", "D");
M.Add("1110", "E");
M.Add("1111", "F");
binaryAd['0'] = "0000";
binaryAd['1'] = "0001";
binaryAd['2'] = "0010";
binaryAd['3'] = "0011";
binaryAd['4'] = "0100";
binaryAd['5'] = "0101";
binaryAd['6'] = "0110";
binaryAd['7'] = "0111";
binaryAd['8'] = "1000";
binaryAd['9'] = "1001";
binaryAd['A'] = "1010";
binaryAd['B'] = "1011";
binaryAd['C'] = "1100";
binaryAd['D'] = "1101";
binaryAd['E'] = "1110";
binaryAd['F'] = "1111";
int number=int.Parse(textBox13.Text);
string s, ds = "";
s = textBox11.Text;
int adlen = 10 + ad;
int totalen = (s.Length - 1) * 4;
for (int i = 0; i < s.Length - 1; i++)
{
ds += binaryAd[s[i]];
}
int pagenum = 0;
int two = 1;
string ss = "";
for (int i = totalen - adlen - 1; i >= 0; i--)
{
if (i != totalen - adlen - 1)
two *= 2;
pagenum += two * (ds[i] - '0');
ss += ds[i];
}
//cout<<ss<<endl;
textBox14.Text = pagenum.ToString();
//cout << "程序页号:" << pagenum << endl;
string res = ds.Substring(totalen - adlen, adlen);
textBox16.Text = res;
//cout << "偏移地址:" << res << endl;
if (process[number,pagenum] == -1)
{
MessageBox.Show("该程序此页没有调入主存。");
return;
// cout << "此页没有调入主存内。" << endl;
}
else
{
int mappage = process[number,pagenum];
int mappage2 = mappage;
textBox15.Text = mappage2.ToString();//物理框号
ss = "";
while (mappage!=0)
{
ss += mappage % 2 ;
mappage /= 2;
}
string sss = "";
for(int i=ss.Length-1;i>=0;i--)
{
sss += ss[i];
}
ss = sss;
MessageBox.Show(ss);
// Reverse(ss.begin(), ss.end());
ss += res;
//MessageBox.Show(ss);
res = "";
int ads = ss.Length % 4;
if(ads>0)
{
ads = 4 - ads;
}
for (int i = 1; i <=ads; i++)
{
res += '0';
}
res += ss;
//cout << res << endl;
//MessageBox.Show(res);
string ans = "";
string ws;
for (int i = 0; i < res.Length; i++)
{
ws= "";
ws+= res[i];
ws+= res[i + 1];
ws+= res[i + 2];
ws+= res[i + 3];
i += 3;
ans += M[ws];
}
textBox17.Text = ans+"H";
// cout << ans << endl;
}
}
}
}
实现结果: