⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 system3dlg.cpp

📁 操作系统实验之一
💻 CPP
字号:
// System3Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "System3.h"
#include "System3Dlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSystem3Dlg dialog

CSystem3Dlg::CSystem3Dlg(CWnd* pParent /*=NULL*/)
	: CDialog(CSystem3Dlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CSystem3Dlg)
	m_PageSize = -1;
	m_TotlePage = 0;
	m_ActivePage = 0;
	m_AccessTimes = 0;
	m_InterrupTimes = 0;
	m_InterrupRate = 0.0f;
	m_MemSize = 0;
	m_Type = -1;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	

}

void CSystem3Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSystem3Dlg)
	DDX_Control(pDX, IDC_LIST_OUTPUT, m_Output);
	DDX_Radio(pDX, IDC_RADIO_PAGESIZE1, m_PageSize);
	DDX_Text(pDX, IDC_EDIT_TOTLEPAGE, m_TotlePage);
	DDX_Text(pDX, IDC_EDIT_ACTIVEPAGE, m_ActivePage);
	DDX_Text(pDX, IDC_EDIT_ACCESSTIMES, m_AccessTimes);
	DDX_Text(pDX, IDC_EDIT_INTERRUPTIMES, m_InterrupTimes);
	DDX_Text(pDX, IDC_EDIT_INTERRUPRATE, m_InterrupRate);
	DDX_Text(pDX, IDC_EDIT_MEMSIZE, m_MemSize);
	DDX_Radio(pDX, IDC_RADIO_FIFO, m_Type);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CSystem3Dlg, CDialog)
	//{{AFX_MSG_MAP(CSystem3Dlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BTN_RESET, OnBtnReset)
	ON_BN_CLICKED(IDC_RADIO_PAGESIZE1, OnRadioPagesize)
	ON_BN_CLICKED(IDC_RADIO_PAGESIZE2, OnRadioPagesize)
	ON_BN_CLICKED(IDC_RADIO_PAGESIZE3, OnRadioPagesize)
	ON_BN_CLICKED(IDC_RADIO_PAGESIZE4, OnRadioPagesize)
	ON_EN_CHANGE(IDC_EDIT_MEMSIZE, OnChangeEditMemsize)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSystem3Dlg message handlers

BOOL CSystem3Dlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	srand((unsigned)time(NULL));
	m_PageSize=0;
	m_AccessTimes=256;
	m_MemSize=32;
	m_Type=0;
	m_TotlePage=(int)640/GetPageSize();			//页面总数
	m_ActivePage=(int)m_MemSize/GetPageSize();	//活动页面数

	ListView_SetExtendedListViewStyle( m_Output.m_hWnd, 
		LVS_EX_FULLROWSELECT|LVS_EX_ONECLICKACTIVATE|LVS_EX_UNDERLINEHOT );
	m_Output.InsertColumn(0, "次数");
	m_Output.SetColumnWidth(0, 50);
	m_Output.InsertColumn(1, "访问页面次序");
	m_Output.SetColumnWidth(1, 100);
	m_Output.InsertColumn(2, "             页面交换情况");
	m_Output.SetColumnWidth(2,400);
	//m_Output.SetColumnWidth(2, LVSCW_AUTOSIZE_USEHEADER);
	m_Output.InsertColumn(3, "淘汰页面");
	m_Output.SetColumnWidth(3, 80);

	UpdateData(FALSE);

	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CSystem3Dlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CSystem3Dlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CSystem3Dlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CSystem3Dlg::OnOK() 
{
	// TODO: Add extra validation here
	//CDialog::OnOK();
	this->OnBtnReset();
	UpdateData(TRUE);

	int i;
	m_AccessArray=new int[m_AccessTimes];
	for(i=0;i<m_AccessTimes;i++)
		m_AccessArray[i]=rand()%m_TotlePage+1;
	m_LoadedPage=new Page[m_ActivePage];
	for(i=0;i<m_ActivePage;i++)
	{
		m_LoadedPage[i].num=0;
		m_LoadedPage[i].time=0;
	}
	if(m_Type==0)this->FIFO();
	if(m_Type==1)this->LRU();
	if(m_Type==2)this->Randomize();
	if(m_Type==3)this->Clock();

	m_InterrupRate=(float)100*m_InterrupTimes/m_AccessTimes;
	UpdateData(FALSE);
	delete [] m_LoadedPage;
	delete [] m_AccessArray;
}

int CSystem3Dlg::GetPageSize()	//返回页面大小
{
	return (int)pow(2,m_PageSize);
}

int CSystem3Dlg::Search(int n ,CSystem3Dlg::Page *page)
{
	for(int i=0;i<m_ActivePage;i++)	//在活动页面查找
		if(n==page[i].num)
			return i;
	return -1;
}

CString CSystem3Dlg::PrintPage(CSystem3Dlg::Page *page)
{
	CString temp(""),ret("");
	for(int i=0;i<m_ActivePage;i++)
	{
		temp.Format(" %3d ",page[i].num);
		ret+=temp;
	}
	return ret;
}

void CSystem3Dlg::OnBtnReset() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	m_InterrupTimes=0;
	m_InterrupRate=0;
	m_Output.DeleteAllItems();
	UpdateData(FALSE);
}

void CSystem3Dlg::FIFO()	//先进先出页面调度算法
{
	CString remove;
	int num=0;
	for(int i=0;i<m_AccessTimes;i++)
	{
		if(Search(m_AccessArray[i],m_LoadedPage)<0)
		{
			m_InterrupTimes++;
			remove.Format("淘汰页面%3d",m_LoadedPage[num%=m_ActivePage].num);
			m_LoadedPage[num].num=m_AccessArray[i];
			num++;
		}
		else
			remove.Format(" -----");
		PrintItem(i,remove);
	}
}

void CSystem3Dlg::LRU()		//LRU(最近未使用)页面调度算法
{
	CString remove;
	int i=0,t=0;
	for(i=0;i<m_AccessTimes;i++)
	{
		t=Search(m_AccessArray[i],m_LoadedPage);
		if(t>=0)
		{
			m_LoadedPage[t].time=0;
			remove.Format(" -----");
		}
		else
		{
			m_InterrupTimes++; 
			t=Max(m_LoadedPage);	//查找最近没有使用的页面
			remove.Format("淘汰页面%3d",m_LoadedPage[t].num);
			m_LoadedPage[t].num=m_AccessArray[i];
			m_LoadedPage[t].time=0;
		}
		for(int j=0;j<m_ActivePage;j++)
			if(j!=t)m_LoadedPage[j].time++;
		PrintItem(i,remove);
	}
}

void CSystem3Dlg::Randomize()	//随机页面替换算法
{
	CString remove;
	for(int i=0;i<m_AccessTimes;i++)
	{
		if(Search(m_AccessArray[i],m_LoadedPage)<0)
		{
			int num=rand()%m_ActivePage;
			m_InterrupTimes++;
			remove.Format("淘汰页面%3d",m_LoadedPage[num].num);
			m_LoadedPage[num].num=m_AccessArray[i];
		}
		else
			remove.Format(" -----");
		PrintItem(i,remove);
	}
}

void CSystem3Dlg::Clock()		//时钟页面替换算法
{
	CString remove;
	int i=0,cur=0,j;
	for(i=0;i<m_AccessTimes;i++)
	{
		if((j=Search(m_AccessArray[i],m_LoadedPage))>=0)
		{
			cur=j;						//找到时修改帧指针
			m_LoadedPage[cur].time=1;	//使用计数
			cur=(cur+1)%m_ActivePage;
			remove.Format(" -----");
		}
		else
		{
			m_InterrupTimes++;
		//	remove.Format("淘汰页面%3d",m_LoadedPage[cur].num);
			while(1)
			{
				if(m_LoadedPage[cur].time==0)
				{
					remove.Format("淘汰页面%3d",m_LoadedPage[cur].num);
					m_LoadedPage[cur].num=m_AccessArray[i];
					m_LoadedPage[cur].time=1;
					cur=(cur+1)%m_ActivePage;
					break;
				}
				else
					m_LoadedPage[cur].time=0;
				cur=(cur+1)%m_ActivePage;
			}
		}
		PrintItem(i,remove);
	}
}

int CSystem3Dlg::Max(CSystem3Dlg::Page *page)
{
	int num=0;
	for(int i=1,time=page[0].time;i<m_ActivePage;i++)
		if(time<page[i].time)
		{
			time=page[i].time;
			num=i;
		}
	return num;
}

void CSystem3Dlg::PrintItem(int i,CString remove)
{
	CString times,sequence,processing;
	times.Format("%3d",i+1);
	sequence.Format("访问页面%3d",m_AccessArray[i]);
	processing=PrintPage(m_LoadedPage);
	int nItem = m_Output.InsertItem( m_Output.GetItemCount()+1,times);
	m_Output.SetItemText(nItem, 1, sequence);
	m_Output.SetItemText(nItem, 2, processing);
	m_Output.SetItemText(nItem, 3, remove);
}

void CSystem3Dlg::OnRadioPagesize()
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	m_TotlePage=(int)640/GetPageSize();	//页面总数
	m_ActivePage=(int)m_MemSize/GetPageSize();	//活动页面数
	UpdateData(FALSE);
}



void CSystem3Dlg::OnChangeEditMemsize() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	
	// TODO: Add your control notification handler code here
	this->OnRadioPagesize();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -