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

📄 processdlg.cpp

📁 一个学习操作系统原理很好的演示程序.演示进程调度与读写者的同步问题
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// ProcessDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Process.h"
#include "ProcessDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define PRIORITY 1
#define ROUND 2
#define OffSetX 14
#define XXX 4
#define OffSetY 1
#define TextHeight 16
#define Text	10
#define HEIGHT 700
int timeLong=(680/Text)-1;
int count=0;
int startTime=0;
int startNum=0;
int run[5000];
COLORREF back=RGB(0,200,200);
COLORREF white=RGB(255,255,255);
int total;
PPCB curSel=NULL;
bool IsRunning=false;
int engineType=PRIORITY;
CFont progressFont;
CFont inforFont;
CFont font;
CFont font1;
CFont mainFont;
int speed[]={10,50,200,500,1000,3000};
int curSpeed=50;
/////////////////////////////////////////////////////////////////////////////
// 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()

/////////////////////////////////////////////////////////////////////////////
// CProcessDlg dialog

CProcessDlg::CProcessDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CProcessDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CProcessDlg)
	proName = _T("");
	timeStart = 0;
	timeExe = 0;
	timeUsed = 0;
	timeLeft = 0;
	priority = 0;
	state = _T("");
	num = 18;
	maxTimeStart = 300;
	maxTimeExe = 100;
	maxPriority = 20;
	avr = 0;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);

}

void CProcessDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CProcessDlg)
	DDX_Control(pDX, IDC_COMBO2, com2);
	DDX_Control(pDX, IDC_BUTTON3, b3);
	DDX_Control(pDX, IDC_BUTTON2, b2);
	DDX_Control(pDX, IDC_BUTTON1, b1);
	DDX_Control(pDX, IDC_COMBO1, com);
	DDX_Control(pDX, IDC_LIST1, list);
	DDX_Control(pDX, IDC_SCROLLBAR1, m_scr1);
	DDX_Control(pDX, IDC_SCROLLBAR2, m_scr2);
	DDX_Text(pDX, IDC_EDIT1, proName);
	DDX_Text(pDX, IDC_EDIT2, timeStart);
	DDX_Text(pDX, IDC_EDIT3, timeExe);
	DDX_Text(pDX, IDC_EDIT4, timeUsed);
	DDX_Text(pDX, IDC_EDIT5, timeLeft);
	DDX_Text(pDX, IDC_EDIT6, priority);
	DDX_Text(pDX, IDC_EDIT7, state);
	DDX_Text(pDX, IDC_EDIT8, num);
	DDX_Text(pDX, IDC_EDIT9, maxTimeStart);
	DDX_Text(pDX, IDC_EDIT10, maxTimeExe);
	DDX_Text(pDX, IDC_EDIT11, maxPriority);
	DDX_Text(pDX, IDC_EDIT12, avr);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CProcessDlg, CDialog)
	//{{AFX_MSG_MAP(CProcessDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_CREATE()
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_WM_TIMER()
	ON_WM_LBUTTONDOWN()
	ON_WM_VSCROLL()
	ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
	ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
	ON_CBN_SELCHANGE(IDC_COMBO1, OnSelchangeCombo1)
	ON_CBN_SELCHANGE(IDC_COMBO2, OnSelchangeCombo2)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CProcessDlg message handlers

BOOL CProcessDlg::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
	CDC * dc=this->GetDC();
	bitmap.CreateCompatibleBitmap(dc,400,HEIGHT);
	tdc.CreateCompatibleDC(dc);
	tdc.SelectObject(&bitmap);
	progressFont.CreateFont(Text-2,5,0,0,FW_NORMAL,FALSE,FALSE,FALSE,ANSI_CHARSET,OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,DEFAULT_PITCH|FF_SWISS,"Arial");
	inforFont.CreateFont(17,7,0,0,FW_NORMAL,FALSE,FALSE,FALSE,ANSI_CHARSET,OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,DEFAULT_PITCH|FF_SWISS,"Arial");
	font.CreateFont(16,6,0,0,FW_NORMAL,FALSE,FALSE,FALSE,ANSI_CHARSET,OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,DEFAULT_PITCH|FF_SWISS,"Arial");
	font1.CreateFont(16,5,0,0,FW_NORMAL,FALSE,FALSE,FALSE,ANSI_CHARSET,OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,DEFAULT_PITCH|FF_SWISS,"Arial");
	mainFont.CreateFont(20,8,0,0,FW_BOLD,FALSE,FALSE,FALSE,ANSI_CHARSET,OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,DEFAULT_PITCH|FF_SWISS,"Arial");
	tdc.SelectObject(&mainFont);
	eng1=new PriorityEngine();
	eng2=new RoundEngine();
	eng=eng1;
	engineType=PRIORITY;
	// TODO: Add extra initialization here
	com.SetCurSel(0);
	com2.SetCurSel(1);
	Init();
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CProcessDlg::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 CProcessDlg::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();
	}
	Draw();
}

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

int CProcessDlg::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CDialog::OnCreate(lpCreateStruct) == -1)
		return -1;
	return 0;
}

void CProcessDlg::Draw()
{
	PPCB newHead=eng->getNewHead();
	PPCB readyHead=eng->getReadyHead();
	PPCB running=eng->getRunning();
	PPCB endedHead=eng->getEndedHead();
	int x0=10,x1=110,x2=210,x3=310;
	int h=35;
	int y0=HEIGHT-40-h;int y=y0;
	CPen redPen(PS_SOLID,1,RGB(255,0,0));
	CPen greenPen(PS_SOLID,1,RGB(0,255,0));
	CPen bluePen(PS_SOLID,1,RGB(0,0,255));
	CPen pen(PS_SOLID,1,RGB(255,0,255));
	CPen * oldPen;
	CFont * oldFont;
	CClientDC dc(this);
	tdc.BitBlt(0,0,400,HEIGHT,NULL,0,0,WHITENESS);
	tdc.Rectangle(0,0,400,HEIGHT);
	CString s;
	tdc.TextOut(x0,HEIGHT-30,"  任务队列");
	CBrush brush;
	CBrush * oldBrush;
	brush.CreateSolidBrush(back);
	while (newHead!=NULL)
	{
		oldPen=tdc.SelectObject(&pen);
		if (newHead==curSel) 
		{
			oldBrush=tdc.SelectObject(&brush);
			tdc.Rectangle(x0,y,x0+80,y+h);
			tdc.SelectObject(oldBrush);
			tdc.SetBkColor(back);
		}
		else{
			tdc.SetBkColor(white);
			tdc.Rectangle(x0,y,x0+80,y+h);
		}
		tdc.SelectObject(oldPen);
		tdc.TextOut(x0+OffSetX,y+OffSetY,newHead->name);
		oldFont=tdc.SelectObject(&font);
		s.Format("开始时间: %d",newHead->timeStart);
		tdc.TextOut(x0+1,y+OffSetY+TextHeight,s);
		tdc.SelectObject(oldFont);
		y-=h;
		newHead=newHead->next;
	}
	y=y0;
	tdc.SetBkColor(white);
	tdc.TextOut(x1,HEIGHT-30,"  就绪队列");
	while (readyHead!=NULL)
	{
		oldPen=tdc.SelectObject(&redPen);
		if (readyHead==curSel) 
		{
			oldBrush=tdc.SelectObject(&brush);
			tdc.Rectangle(x1,y,x1+80,y+h);
			tdc.SelectObject(oldBrush);
			tdc.SetBkColor(back);
		}
		else{
			tdc.SetBkColor(white);
			tdc.Rectangle(x1,y,x1+80,y+h);
		}
		tdc.SelectObject(oldPen);
		tdc.TextOut(x1+OffSetX,y+OffSetY,readyHead->name);
		oldFont=tdc.SelectObject(&font);
		if (engineType==PRIORITY) s.Format("优先级别: %d",readyHead->priority);
		else s.Format(" 轮转片:%d",readyHead->round);
		tdc.TextOut(x1+XXX,y+OffSetY+TextHeight,s);
		tdc.SelectObject(oldFont);
		y-=h;
		readyHead=readyHead->next;
	}
	y=y0;
	tdc.SetBkColor(white);
	tdc.TextOut(x2,HEIGHT-30,"  运行进程");
	if (running!=NULL)
	{
		oldPen=tdc.SelectObject(&greenPen);
		if (running==curSel) 
		{
			oldBrush=tdc.SelectObject(&brush);
			tdc.Rectangle(x2,y,x2+80,y+h);
			tdc.SelectObject(oldBrush);
			tdc.SetBkColor(back);
		}
		else{
			tdc.SetBkColor(white);
			tdc.Rectangle(x2,y,x2+80,y+h);
		}
		tdc.SelectObject(oldPen);
		tdc.TextOut(x2+OffSetX,y+OffSetY,running->name);
		oldFont=tdc.SelectObject(&font);
		s.Format("剩余时间: %d",running->timeLeft);
		tdc.TextOut(x2+XXX-2,y+OffSetY+TextHeight,s);
		tdc.SelectObject(oldFont);
	}
	tdc.SetBkColor(white);
	tdc.TextOut(x3,HEIGHT-30,"   已完成");
	while (endedHead!=NULL)
	{
		oldPen=tdc.SelectObject(&bluePen);
		if (endedHead==curSel) 
		{
			oldBrush=tdc.SelectObject(&brush);
			tdc.Rectangle(x3,y,x3+80,y+h);
			tdc.SelectObject(oldBrush);
			tdc.SetBkColor(back);
		}
		else{
			tdc.SetBkColor(white);
			tdc.Rectangle(x3,y,x3+80,y+h);
		}
		tdc.SelectObject(oldPen);
		tdc.TextOut(x3+OffSetX,y+OffSetY,endedHead->name);
		oldFont=tdc.SelectObject(&font1);
		s.Format("结束时间: %d",endedHead->timeEnded);
		tdc.TextOut(x3+XXX,y+OffSetY+TextHeight,s);
		tdc.SelectObject(oldFont);
		y-=h;
		endedHead=endedHead->next;
	}
	dc.BitBlt(0,0,400,HEIGHT,&tdc,0,0,SRCCOPY);
	DrawProgress(&dc);
	DrawInformation(&dc);
	if (curSel!=NULL)
	{
		proName.Format("%s",curSel->name);
		this->timeStart=curSel->timeStart;
		this->timeExe=curSel->timeLeft+curSel->timeUsed;
		this->timeLeft=curSel->timeLeft;
		this->timeUsed=curSel->timeUsed;
		if (engineType==PRIORITY) this->priority=curSel->priority;
		else this->priority=curSel->round;
		switch(curSel->state)
		{
		case NEW:state="未开始";break;
		case READY:state="就绪";break;
		case RUNNING:state="运行";break;
		case ENDED:state="已完成";break;
		}
		this->UpdateData(FALSE);
	}
}

void CProcessDlg::OnButton1() 
{
	if (curSpeed==-1)
	{
		b3.EnableWindow();
		if (!IsRunning) IsRunning=true;
		this->OnTimer(1);
		return;
	}
	if (!IsRunning)
	{
		IsRunning=true;
		b1.SetWindowText("暂停");
		b2.EnableWindow(FALSE);

⌨️ 快捷键说明

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