C#实现页面置换算法FIFO,LRU,LFU,OPT

废话不多说,直接上代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
/*
 * @@ClassName: Pagereplace
 * @author HWX
 *@Description:操作系统课设,页面置换算法
 *@version 1.0
 *@data:2018.1.3
 * 
 */
namespace Pagereplace
{
    public partial class Main : Form
    {
        private int PageNum;//页面序列个数
        private int MemoNum = 3;//内存页面序列,物理块


        private int visitList;//访问页次数
        private string[] visitPage = null;//内存页框


        private int memoTime;//内存存取时间
        private int interTime;//中断时间


        //第1个栈的初始位置和当前位置
        int Stack1InitialX;
        int Stack1InitialY;
        int Stack1CurrentX;
        int Stack1CurrentY;


        //第2个栈的初始位置和当前位置
        int Stack2InitialX;
        int Stack2InitialY;
        int Stack2CurrentX;
        int Stack2CurrentY;


        //第3个栈的初始位置和当前位置
        int Stack3InitialX;
        int Stack3InitialY;
        int Stack3CurrentX;
        int Stack3CurrentY;


        //第4个栈的初始位置和当前位置
        int Stack4InitialX;
        int Stack4InitialY;
        int Stack4CurrentX;
        int Stack4CurrentY;


        Graphics g1;
        Graphics g2;
        Graphics g3;
        Graphics g4;
        Pen pen;
        Font font = new Font("宋体", 10, FontStyle.Bold);
        Font font1 = new Font("宋体", 8, FontStyle.Bold);
        Brush brush = new SolidBrush(Color.Black);
        Brush brushred = new SolidBrush(Color.Red);
        int dx = 20;//高
        int dy = 20;//宽


        //定义线程
        Mutex m = new Mutex();
        Thread threadFIFO = null;
        Thread threadLRU = null;
        Thread threadLFU = null;
        Thread threadOPTION = null;








        public Main()
        {
            InitializeComponent();
            //定义开始,暂停按钮
            #region
            buttonStartFIFO.Enabled = true;
            buttonStartLRU.Enabled = true;
            buttonStartLFU.Enabled = true;
            buttonStartOPTION.Enabled = true;
            buttonStartALL.Enabled = true;


            buttonStopFIFO.Enabled = false;
            buttonStopLRU.Enabled = false;
            buttonStopLFU.Enabled = false;
            buttonStopOPTION.Enabled = false;
            buttonStopALL.Enabled = false;
            #endregion
        }
        private void buttonInitialize_Click(object sender, EventArgs e)
        {//序列格式
            visitPage = txtVisitList.Text.Trim().Split(new string[] { "," }, StringSplitOptions.None);
            visitList = visitPage.Count();
            //progressBar内的内容
            PageNum = int.Parse(txtPageNum.Text);
            int.TryParse(txtmemoTime.Text, out memoTime);
            int.TryParse(txtinterTime.Text, out interTime);
            //四个栈的赋值
            Stack1InitialX = 20;
            Stack1InitialY = 20;
            Stack1CurrentX = 20;
            Stack1CurrentY = 20;


            Stack2InitialX = 20;
            Stack2InitialY = 30;
            Stack2CurrentX = 20;
            Stack2CurrentY = 30;


            Stack3InitialX = 20;
            Stack3InitialY = 30;
            Stack3CurrentX = 20;
            Stack3CurrentY = 30;


            Stack4InitialX = 20;
            Stack4InitialY = 30;
            Stack4CurrentX = 20;
            Stack4CurrentY = 30;
            //对画笔的一些属性进行定义
            dx = 20;
            dy = 20;
            pen = new Pen(Color.Black);
            g1 = panel1.CreateGraphics();
            g2 = panel2.CreateGraphics();
            g3 = panel3.CreateGraphics();
            g4 = panel4.CreateGraphics();


            g1.Clear(this.BackColor);
            g2.Clear(this.BackColor);
            g3.Clear(this.BackColor);
            g4.Clear(this.BackColor);


            label2.Text = "置换图:";
            label3.Text = "算法,序列";
            label4.Text = "置换图:";
            label5.Text = "算法,序列";
            label6.Text = "置换图:";
            label7.Text = "算法,序列";
            label8.Text = "置换图:";
            label9.Text = "算法,序列";




            # region   各个栈初始化
            //Stack1
            g1.DrawRectangle(pen, Stack1CurrentX, Stack1CurrentY, 70, dy);
            g1.DrawString("序列", font, brush, Stack1CurrentX + 4, Stack1CurrentY + 4);
            int i;
            for (i = 0; i < MemoNum; i++)
            {
                Stack1CurrentY += dy;
                g1.DrawRectangle(pen, Stack1CurrentX, Stack1CurrentY, 70, dy);
                g1.DrawString("物理块" + (i + 1).ToString(), font, brush, Stack1CurrentX + 4, Stack1CurrentY + 4);
            }
            Stack1CurrentY += dy;
            g1.DrawRectangle(pen, Stack1CurrentX, Stack1CurrentY, 70, dy);
            g1.DrawString("缺页", font, brush, Stack1CurrentX + 4, Stack1CurrentY + 4);
            Stack1CurrentY += dy;
            g1.DrawRectangle(pen, Stack1CurrentX, Stack1CurrentY, 70, dy);
            g1.DrawString("缺页次数", font, brush, Stack1CurrentX + 4, Stack1CurrentY + 4);


            //Stack2
            g2.DrawRectangle(pen, Stack2CurrentX, Stack2CurrentY, 80, dy);
            g2.DrawString("序列", font, brush, Stack2CurrentX + 4, Stack2CurrentY + 4);
            for (i = 0; i < 3; i++)
            {
                Stack2CurrentY += dy;
                g2.DrawRectangle(pen, Stack2CurrentX, Stack2CurrentY, 80, dy);
                g2.DrawString("物理块" + (i + 1).ToString(), font, brush, Stack2CurrentX + 4, Stack2CurrentY + 4);
            }
            Stack2CurrentY += dy;
            g2.DrawRectangle(pen, Stack2CurrentX, Stack2CurrentY, 80, dy);
            g2.DrawString("缺页", font, brush, Stack2CurrentX + 4, Stack2CurrentY + 4);
            Stack2CurrentY += dy;
            g2.DrawRectangle(pen, Stack2CurrentX, Stack2CurrentY, 80, dy);
            g2.DrawString("缺页次数", font, brush, Stack2InitialX + 4, Stack2CurrentY + 4);
            //stack3
            g3.DrawRectangle(pen, Stack3CurrentX, Stack3CurrentY, 80, dy);
            g3.DrawString("序列", font, brush, Stack3CurrentX + 4, Stack3CurrentY + 4);
            for (i = 0; i < 3; i++)
            {
                Stack3CurrentY += dy;
                g3.DrawRectangle(pen, Stack3CurrentX, Stack3CurrentY, 80, dy);
                g3.DrawString("物理块" + (i + 1).ToString(), font, brush, Stack3CurrentX + 4, Stack3CurrentY + 4);
            }
            Stack3CurrentY += dy;
            g3.DrawRectangle(pen, Stack3CurrentX, Stack3CurrentY, 80, dy);
            g3.DrawString("缺页", font, brush, Stack3CurrentX + 4, Stack3CurrentY + 4);
            Stack3CurrentY += dy;
            g3.DrawRectangle(pen, Stack3CurrentX, Stack3CurrentY, 80, dy);
            g3.DrawString("缺页次数", font, brush, Stack3CurrentX + 4, Stack3CurrentY + 4);
            //stack4
            g4.DrawRectangle(pen, Stack4CurrentX, Stack4CurrentY, 80, dy);
            g4.DrawString("序列", font, brush, Stack4CurrentX + 4, Stack4CurrentY + 4);
            for (i = 0; i < MemoNum; i++)
            {
                Stack4CurrentY += dy;
                g4.DrawRectangle(pen, Stack4CurrentX, Stack4CurrentY, 80, dy);
                g4.DrawString("物理块" + (i + 1).ToString(), font, brush, Stack4CurrentX + 4, Stack4CurrentY + 4);
            }
            Stack4CurrentY += dy;
            g4.DrawRectangle(pen, Stack4CurrentX, Stack4CurrentY, 80, dy);
            g4.DrawString("缺页", font, brush, Stack4CurrentX + 4, Stack4CurrentY + 4);
            Stack4CurrentY += dy;
            g4.DrawRectangle(pen, Stack4CurrentX, Stack4CurrentY, 80, dy);
            g4.DrawString("缺页次数", font, brush, Stack4CurrentX + 4, Stack4CurrentY + 4);
            #endregion


            #region
            buttonStartFIFO.Enabled = true;
            buttonStartLRU.Enabled = true;
            buttonStartLFU.Enabled = true;
            buttonStartOPTION.Enabled = true;
            buttonStartALL.Enabled = true;


            buttonStopFIFO.Enabled = false;
            buttonStopLRU.Enabled = false;
            buttonStopLFU.Enabled = false;
            buttonStopOPTION.Enabled = false;
            buttonStopALL.Enabled &

  • 2
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值