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

📄 system2dlg.cpp

📁 操作系统实验之一
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// System2Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "System2.h"
#include "System2Dlg.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 };
	CString	m_str;
	//}}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)
	m_str = _T("1.自定义PCB的数据结构。\r\n");
	m_str+="\tstruct pcb\r\n";
	m_str+="\t  {		//PCB\r\n";
	m_str+="\t  int id;	//标识符\r\n";
	m_str+="\t  int state;	//状态\r\n";
	m_str+="\t  int priority;	//优先级\r\n";
	m_str+="\t  int life;	//生命期\r\n";
	m_str+="\t  pcb *next;	//指针\r\n";
	m_str+="\t  };\r\n";
	m_str+="2.系统使用带优先级的时间片轮转法调度进程;每运行一个时间片,优先级减一。\r\n";
	m_str+="3.功能:\r\n";
	m_str+="\tA.随机创建进程,优先级和生命期随机决定。\r\n";
	m_str+="\tB.挂起和恢复进程。\r\n";
	m_str+="\tC.杀死进程。\r\n";
	m_str+="\tD.实时查看各个进程的状态。";
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	DDX_Text(pDX, IDC_EDIT1, m_str);
	//}}AFX_DATA_MAP
}

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

/////////////////////////////////////////////////////////////////////////////
// CSystem2Dlg dialog

CSystem2Dlg::CSystem2Dlg(CWnd* pParent /*=NULL*/)
	: CDialog(CSystem2Dlg::IDD, pParent)
{
	InitProcess();
	//{{AFX_DATA_INIT(CSystem2Dlg)
		// 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 CSystem2Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSystem2Dlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CSystem2Dlg, CDialog)
	//{{AFX_MSG_MAP(CSystem2Dlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(ID_BTN_CREATE1, OnBtnCreate1)
	ON_BN_CLICKED(IDC_ABOUT, OnAbout)
	ON_BN_CLICKED(ID_BTN_SLEEP1, OnBtnSleep1)
	ON_BN_CLICKED(ID_BTN_CREATE2, OnBtnCreate2)
	ON_BN_CLICKED(ID_BTN_SLEEP2, OnBtnSleep2)
	ON_BN_CLICKED(ID_BTN_CREATE3, OnBtnCreate3)
	ON_BN_CLICKED(ID_BTN_SLEEP3, OnBtnSleep3)
	ON_BN_CLICKED(ID_BTN_CREATE4, OnBtnCreate4)
	ON_BN_CLICKED(ID_BTN_SLEEP4, OnBtnSleep4)
	ON_BN_CLICKED(ID_BTN_CREATE5, OnBtnCreate5)
	ON_BN_CLICKED(ID_BTN_SLEEP5, OnBtnSleep5)
	ON_BN_CLICKED(ID_BTN_OK, OnOK)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSystem2Dlg message handlers

BOOL CSystem2Dlg::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
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CSystem2Dlg::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 CSystem2Dlg::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 CSystem2Dlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CSystem2Dlg::InitProcess()
{
	//初始化各进程
	srand((unsigned)time(NULL));
	for(int i=0;i<5;i++)
	{
		m_process[i].id=0;
		m_process[i].life=0;
		m_process[i].state=0;
		m_process[i].priority=0;
		m_process[i].next=NULL;
		m_IsCreate[i]=false;
		m_IsSleep[i]=false;
		m_ActionProcess=0;
		rand();
	}
	AfxBeginThread(MyThread,this);
}

void CSystem2Dlg::OnOK() 
{
	// TODO: Add extra validation here
	
	CDialog::OnOK();
}



void CSystem2Dlg::OnAbout() 
{
	// TODO: Add your control notification handler code here
	CAboutDlg dlgAbout;
	dlgAbout.DoModal();
}

UINT CSystem2Dlg::MyThread(LPVOID pParam)
{
	Sleep(1000);
	CSystem2Dlg *dlg=(CSystem2Dlg *)pParam;
	dlg->RunThread();
	return 0;
}

void CSystem2Dlg::RunThread()
{
	while(1)
	{
		//Run
		this->ChooseProcess();
		//Paint
		for(int i=0;i<5;i++)
			this->UpdateProcess(i);
		Sleep(50);
	}
}

void CSystem2Dlg::CreateProcess(int n)
{
	m_process[n].id=rand();
	m_process[n].life=rand()%1000+1000;
	m_process[n].state=READY;
	m_process[n].priority=rand()%20+10;
	m_process[n].next=NULL;
}



CString CSystem2Dlg::getState(int state)
{
	switch(state)
	{
	case 1: 
		return "Ready";
	case 2:
		return "Run";
	case 3:
		return "Sleep";
	case 4:
		return "Finish";
	default:
		return "Unknow";
	}
}

void CSystem2Dlg::UpdateProcess(int n)
{
	CString str;
	switch(n)
	{
	case 0:
		str.Format("%d",m_process[n].id);
		this->SetDlgItemText(IDC_EDIT_PID1,str);
		this->SetDlgItemText(IDC_EDIT_PSTATE1,getState(m_process[n].state));
		str.Format("%d",m_process[n].priority);
		this->SetDlgItemText(IDC_EDIT_PPRI1,str);
		str.Format("%d",m_process[n].life);
		this->SetDlgItemText(IDC_EDIT_PLIFE1,str);
		break;
	case 1:
		str.Format("%d",m_process[n].id);
		this->SetDlgItemText(IDC_EDIT_PID2,str);
		this->SetDlgItemText(IDC_EDIT_PSTATE2,getState(m_process[n].state));
		str.Format("%d",m_process[n].priority);
		this->SetDlgItemText(IDC_EDIT_PPRI2,str);
		str.Format("%d",m_process[n].life);
		this->SetDlgItemText(IDC_EDIT_PLIFE2,str);
		break;
	case 2:
		str.Format("%d",m_process[n].id);
		this->SetDlgItemText(IDC_EDIT_PID3,str);

⌨️ 快捷键说明

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