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

📄 memmanagerdlg.cpp

📁 操作系统实验 动态形象模拟内存管理
💻 CPP
字号:
// 作者:郑钟武(20042104010)
//完成日期:2007.2.10

#include "stdafx.h"
#include "MemManager.h"
#include "MemManagerDlg.h"

#include<windows.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()

/////////////////////////////////////////////////////////////////////////////
// CMemManagerDlg dialog

CMemManagerDlg::CMemManagerDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMemManagerDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMemManagerDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CMemManagerDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMemManagerDlg)
	DDX_Control(pDX, IDC_LIST_OP, m_listboxOP);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMemManagerDlg, CDialog)
	//{{AFX_MSG_MAP(CMemManagerDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_BUTTON_STEP, OnButtonStep)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMemManagerDlg message handlers

BOOL CMemManagerDlg::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

	SetInitiaInfo();//初始化
	SetTimer (1,1000, NULL) ;//1为该计时器的标志

	//备注
	SetDlgItemText(IDC_STATIC_INTRO1,"本实验保留页面1000个(4M),锁页面10(40K)(最多可锁30个),每1秒钟刷新数据一次");
	SetDlgItemText(IDC_STATIC_INTRO2,"结果分析:保留操作将减少可用自由区域大小;提交操作将减少已提交页面大小;");
	SetDlgItemText(IDC_STATIC_INTRO3,"锁操作将只减少可用物理内存大小;解锁操作将有可能增加可物理内存大小");
	SetDlgItemText(IDC_STATIC_INTRO4,"回收操作只增加可用物理内存大小和可用自由区域大小;释放操作只增加可用自由区域大小");
		

	//列表框元素
	m_listboxOP.InsertString(0,"保留操作");
	m_listboxOP.InsertString(1,"提交操作");
	m_listboxOP.InsertString(2,"锁操作");
	m_listboxOP.InsertString(3,"解锁操作");
	m_listboxOP.InsertString(4,"回收操作");
	m_listboxOP.InsertString(5,"释放操作");
	

	//初始化演示操作
	SelectCount=0;
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CMemManagerDlg::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 CMemManagerDlg::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 CMemManagerDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}
//初始化函数
void CMemManagerDlg::SetInitiaInfo()
{
		CString s;

		//获取系统信息		
		GetSystemInfo(&info);

		s.Format("Ox%x",info.lpMaximumApplicationAddress);
		SetDlgItemText(IDC_STATIC_Max,s);

		s.Format("Ox%x",info.lpMinimumApplicationAddress);
		SetDlgItemText(IDC_STATIC_Min,s);

		s.Format("%.d",info.dwProcessorType);
		SetDlgItemText(IDC_STATIC_CPU,s);

		s.Format("%d",info.dwPageSize/1024);
		SetDlgItemText(IDC_STATIC_PAGESIZE,s);

		//当前内存状态的动态信息
		GlobalMemoryStatus(&status);

		s.Format("%d",status.dwTotalPageFile/1024/1024);
		SetDlgItemText(IDC_STATIC_TotalPageFile,s);

		s.Format("%d",status.dwTotalPhys/1024/1024);
		SetDlgItemText(IDC_STATIC_TotalPhys,s);

		s.Format("%d",status.dwTotalPhys/1024-status.dwAvailPhys/1024);
		SetDlgItemText(IDC_STATIC_AvailPhys,s);

		s.Format("%.2d",status.dwTotalPageFile/1024/1024-status.dwAvailPageFile/1024/1024);
		SetDlgItemText(IDC_STATIC_AvailPageFile,s);

		s.Format("%d",status.dwAvailVirtual/1024/1024);
		SetDlgItemText(IDC_STATIC_AvailVirtual,s);

		s.Format("%d",status.dwTotalVirtual/1024/1024);
		SetDlgItemText(IDC_STATIC_TotalVirtual,s);

}
//刷新数据
void CMemManagerDlg::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
    SetInitiaInfo();
	CDialog::OnTimer(nIDEvent);
}
//单步演示
void CMemManagerDlg::OnButtonStep() 
{
	// TODO: Add your control notification handler code here
	m_listboxOP.SetCurSel(SelectCount);
	CString s;
	size=info.dwPageSize*1000;
	switch(SelectCount)
	{
	case 0://保留页面
		start=VirtualAlloc(NULL,size,MEM_RESERVE,PAGE_NOACCESS);
		break;
	case 1://提交页面
		start=VirtualAlloc(start,size,MEM_COMMIT,PAGE_EXECUTE_READWRITE);
		break;
	case 2://锁
		if(!VirtualLock(start,info.dwPageSize*10))
		{
			s.Format("错误代号:%d",GetLastError());
			MessageBox(s);
		}
		break;
	case 3://解锁
		if(!VirtualUnlock(start,info.dwPageSize*10))
		{
			s.Format("错误代号:%d",GetLastError());
			MessageBox(s);
		}
		break;
	case 4://回收提交页面
		if(!VirtualFree(start,size,MEM_DECOMMIT))
		{
			s.Format("错误代号:%d",GetLastError());
			MessageBox(s);
		}
		break;
	case 5://释放保留页面
		if(!VirtualFree(start,0,MEM_RELEASE))
		{
			s.Format("错误代号:%d",GetLastError());
			MessageBox(s);
		}
		break;
	}
	SetInitiaInfo();
	SelectCount++;
	if(SelectCount==6)
		SelectCount=0;
}

void CMemManagerDlg::OnCancel() 
{
	// TODO: Add extra cleanup here
	CDialog::OnCancel();
}

⌨️ 快捷键说明

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