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

📄 mainfrm.cpp

📁 这是软件工程的关于模拟进程调度的课程论文
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// MainFrm.cpp : implementation of the CMainFrame class
//

#include "stdafx.h"
#include "ProcessManager.h"

#include "MainFrm.h"

//Set dialog
#include "SetDialog.h"

#include "afxcview.h"
#include "MyListView.h"

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

/////////////////////////////////////////////////////////////////////////////
// CMainFrame

IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)

BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
	//{{AFX_MSG_MAP(CMainFrame)
	ON_WM_CREATE()
	ON_COMMAND(IDM_NEW, OnNewProc)
	ON_COMMAND(IDM_PRINT, OnPrint)
	ON_COMMAND(IDM_KILL, OnKillProc)
	ON_COMMAND(IDM_MODIFY, OnModifyProc)
	ON_COMMAND(IDM_AUTO, OnStepRunning)
	ON_WM_TIMER()
	ON_COMMAND(IDM_CPU_START, OnCpuStart)
	ON_COMMAND(IDM_CPU_STOP, OnCpuStop)
	ON_WM_DESTROY()
	ON_COMMAND(IDM_READFILE, OnReadfile)
	ON_COMMAND(IDM_SUPPOND, OnSuppond)
	//}}AFX_MSG_MAP
	ON_MESSAGE(UM_SENDINFO, OnReceiveInfo)
END_MESSAGE_MAP()

static UINT indicators[] =
{
	ID_SEPARATOR,           // status line indicator
	ID_INDICATOR_CAPS,
	ID_INDICATOR_NUM,
	ID_INDICATOR_SCRL,
};

/////////////////////////////////////////////////////////////////////////////
// CMainFrame construction/destruction

CMainFrame::CMainFrame()
{
	// TODO: add member initialization code here
	memset(pcblist,0,256*sizeof(struct PCB *));
	UPID = 0;			  //进程ID计数器
	strTotalProc = _T("");//进程记录集
	cpuState = CPU_STOP;  //CPU状态
	hMutex = CreateMutex(NULL,FALSE,"readpro");
	
	QueueList = new QueueNode();
	ASSERT(QueueList!=NULL);
	QueueList->next = QueueList->prev = QueueList;
	
	pCurrent = NULL;
	
	WaitForSingleObject(hMutex,INFINITE);
	
}

CMainFrame::~CMainFrame()
{
	m_bitmap.DeleteObject();
	

	struct PCB* pstart = NULL;	//释放结构体

	WaitForSingleObject(hMutex,INFINITE);
	
	
	pCurrent = QueueList->next;
	while(pCurrent != QueueList)
	{
		QueueList->next = pCurrent->next;
		delete pCurrent;
		pCurrent = QueueList->next;
	}
		
	for(int i = 0; i < 256; i++)
	{
		pstart = pcblist[i];
		while(pstart)
		{
			pstart = RemovePCB(i);
			delete pstart;
			pstart = pcblist[i];
		}
	}
	ReleaseMutex(hMutex);

	ReleaseMutex(hMutex);

	delete QueueList;
}

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
		| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
		!m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
	{
		TRACE0("Failed to create toolbar\n");
		return -1;      // fail to create
	}

	if (!m_wndStatusBar.Create(this) ||
		!m_wndStatusBar.SetIndicators(indicators,
		  sizeof(indicators)/sizeof(UINT)))
	{
		TRACE0("Failed to create status bar\n");
		return -1;      // fail to create
	}

	this->m_bitmap.LoadBitmap(IDB_BEAUTY);
	m_wndToolBar.SetBitmap((HBITMAP) m_bitmap);
	// TODO: Delete these three lines if you don't want the toolbar to
	//  be dockable
	m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
	EnableDocking(CBRS_ALIGN_ANY);
	DockControlBar(&m_wndToolBar);

	m_nTimer = SetTimer(1,2000,NULL);
	return 0;
}

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
	if( !CFrameWnd::PreCreateWindow(cs) )
		return FALSE;
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	cs.cx = 800;
	cs.cy = 500;
	cs.lpszName = "模拟进程管理器";
	cs.style = WS_OVERLAPPEDWINDOW&(~WS_MAXIMIZEBOX)&(~WS_THICKFRAME);

	return TRUE;
}

/////////////////////////////////////////////////////////////////////////////
// CMainFrame diagnostics

#ifdef _DEBUG
void CMainFrame::AssertValid() const
{
	CFrameWnd::AssertValid();
}

void CMainFrame::Dump(CDumpContext& dc) const
{
	CFrameWnd::Dump(dc);
}

#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CMainFrame message handlers


void CMainFrame::OnNewProc() 
{
	// TODO: Add your command handler code here

	CSetDialog *pSetDlg = new CSetDialog(this);
	
	ASSERT(pSetDlg!=NULL);
	pSetDlg->Create(IDD_NEWDLG);
	::SendMessage(pSetDlg->m_hWnd,UM_RECEIVEINFO,0,MODE_CREATE);
	pSetDlg->ShowWindow(SW_RESTORE);
}



LRESULT CMainFrame::OnReceiveInfo(WPARAM wparam, LPARAM lparam)
{
	CString str;

	struct PCB* pstart = NULL;
	struct PCB* pnew = NULL;
	int i = 0;

	ProInfo * ppi = (ProInfo*)wparam;
	UINT ustate = (UINT)lparam;
	ASSERT(ppi != NULL);
	ASSERT(ppi->nprio < 256);

	//WaitForSingleObject(hMutex,INFINITE);	//访问互斥
	
	switch(ustate)
	{
	case MODE_CREATE:	//创建一个新的进程
		ppi->npid = ++UPID;
		pnew = new PCB(ppi->nprio,ppi->nptime,ppi->npid, ppi->strname);//创建新的进程
		if(AddNewPCB(pnew)!= pnew->nid)
			::AfxMessageBox("  错误,创建新进程失败!  ");
		break;

	case MODE_KILL:
		if(SearchByPid(ppi->npid,MODE_KILL) == MODE_KILL)
			::AfxMessageBox("  成功删除指定的进程!  ");
		else
			::AfxMessageBox("  错误,尝试删除不存在的进程!  ");

		break;

	case MODE_SUPPOND:
		//::AfxMessageBox("  功能未完善!  ");
		if(SearchByPid(ppi->npid,MODE_SUPPOND) == MODE_SUPPOND)
			::AfxMessageBox("  成功挂起指定的进程!  ");
		else
			::AfxMessageBox("  错误,尝试挂起不存在的进程!  ");
		break;

	case MODE_MODIFY:
		if(SearchByPid(ppi->npid,MODE_MODIFY,ppi->nprio) == MODE_MODIFY)
			::AfxMessageBox("  成功修改指定的进程!  ");
		else
			::AfxMessageBox("  错误,尝试修改不存在的进程!  ");

		break;

	default:
		break;
	}
	//ReleaseMutex(hMutex);	//释放互斥访问

	delete ((ProInfo*)wparam);

	return NULL;
}

	

void CMainFrame::OnPrint() 
{
	// TODO: Add your command handler code here
	CString strInfo;
	int nCount = 0;

	strInfo.Format("所有进程列表:\n");
	struct PCB* pstart = NULL;
	
	WaitForSingleObject(hMutex,INFINITE);
	for(int i = 0; i < 256; i++)
	{
		pstart = pcblist[i];

		while(pstart)
		{
			CString str;
			str.Format("进程名字:"+pstart->strname+"\t\r\n");
			nCount++;
			strInfo += str;
			pstart = pstart->next;
		}
	}
	ReleaseMutex(hMutex);	//释放互斥

	if(0 == strInfo.CompareNoCase("状态如下:\n"))
		::AfxMessageBox("当前无任何运行进程。\t");
	else
	{
		CString str;
		str.Format("\n当前共有进程 %d 个!",nCount);
		strInfo += str;
		::AfxMessageBox(strInfo);
	}
}

void CMainFrame::OnKillProc() 
{
	// TODO: Add your command handler code here
	CSetDialog *pSetDlg = new CSetDialog(this);
	
	ASSERT(pSetDlg!=NULL);
	pSetDlg->Create(IDD_NEWDLG);
	::SendMessage(pSetDlg->m_hWnd,UM_RECEIVEINFO,0,MODE_KILL);
	pSetDlg->ShowWindow(SW_RESTORE);
}



void CMainFrame::OnModifyProc() 
{
	// TODO: Add your command handler code here
	CSetDialog *pSetDlg = new CSetDialog(this);
	
	ASSERT(pSetDlg!=NULL);
	pSetDlg->Create(IDD_NEWDLG);
	::SendMessage(pSetDlg->m_hWnd,UM_RECEIVEINFO,0,MODE_MODIFY);
	pSetDlg->ShowWindow(SW_RESTORE);
}


/*
 * SearchByPid(UINT pid, UINT mode, UINT value)
 *
 * 函数通过遍历整个进程结构,执行删除或修改的操作
 *
 * 返回值与传入的mode值一致,如果不一致则说明操作失败
 */
UINT CMainFrame::SearchByPid(UINT pid, UINT mode, UINT value)
{
	int i = 0;
	struct PCB* pstart = NULL;
	struct PCB* ptemp = NULL;

	WaitForSingleObject(hMutex,INFINITE);

	for(i = 0; i < 256; i++)
	{
		pstart = pcblist[i];

		if(pstart)
		{
			if(pstart->nid == pid)	//首元素情况处理
			{
				switch(mode)
				{
				case MODE_KILL:		//元素删除
					pstart = RemovePCB(i);
					if(pstart->state == PS_ACTIVE||pstart->state==PS_RUNNING)
						DelQueueNode(pstart);
					delete pstart; 
					ReleaseMutex(hMutex);
					return MODE_KILL;

				case MODE_MODIFY:
					pstart = RemovePCB(i);	 //从原队出移除
					pstart->priority = value;//修改优先级
					AddNewPCB(pstart);		 //加入新的队列中
					ReleaseMutex(hMutex);
					return MODE_MODIFY;

				case MODE_SUPPOND:
					if(pstart->state == PS_ACTIVE||pstart->state==PS_RUNNING)
						DelQueueNode(pstart);
					pstart->state = PS_SUPPOND;
					ReleaseMutex(hMutex);
					return MODE_SUPPOND;
				default:
					ReleaseMutex(hMutex);
					return -1;
				}
			}

			pstart = pstart->next;	//非首元素情况处理
			while(pstart)
			{
				if(pstart->nid == pid)
				{
					switch(mode)
					{
					case MODE_KILL:		//元素删除
						pstart = RemovePCB(pstart);
					if(pstart->state == PS_ACTIVE||pstart->state==PS_RUNNING)
							DelQueueNode(pstart);
						delete pstart;
						ReleaseMutex(hMutex);
						return MODE_KILL;
						
					case MODE_MODIFY:
						pstart = RemovePCB(pstart);	//从原队出移除
						pstart->priority = value;	//修改优先级
						AddNewPCB(pstart);			//加入新的队列中
						ReleaseMutex(hMutex);
						return MODE_MODIFY;

					case MODE_SUPPOND:		//进程挂起
					if(pstart->state == PS_ACTIVE||pstart->state==PS_RUNNING)
							DelQueueNode(pstart);
						pstart->state = PS_SUPPOND;
						ReleaseMutex(hMutex);
						return MODE_SUPPOND;
					default:
						ReleaseMutex(hMutex);
						return -1;
					}
				}
				pstart = pstart->next;
			}
		}
	}
	ReleaseMutex(hMutex);
	return -1;
}

void CMainFrame::OnStepRunning() 
{
	// TODO: Add your command handler code here
	int value = 0;
	struct PCB* pstart = NULL;
	struct PCB* ptemp = NULL;
	
	if(cpuState == CPU_RUN)
	{
		if(MessageBox("当前状态为CPU运行状态,是否转化为手动运行状态!","提示",MB_YESNO|MB_ICONQUESTION)==IDYES)
		{
			cpuState = CPU_STOP;
		}
		else
			return ;
	}

	value = this->Schedule();
	
	if(value == 0)
	{
		::AfxMessageBox("当前无任何运行进程。\t");
		return ;
	}

	WaitForSingleObject(hMutex,INFINITE);
	if(QueueList->next!=QueueList)
		((CMyListView*)this->GetActiveView())->AddNewItems(QueueList);
	else 
		((CMyListView*)this->GetActiveView())->AddNewItems(NULL);
	ReleaseMutex(hMutex);

	//已经记录进程的信息,需要显示当前的进程的信息

}


//专用于删除队头元素
struct PCB* CMainFrame::RemovePCB(UINT list_index)
{
	struct PCB * pstart = pcblist[list_index];
	UINT value = pstart->nid;
	pcblist[list_index] = pstart->next;
	if(pstart->next)
		pstart->next->prev = pcblist[list_index];
	return pstart;
}


//专用于删除队中元素
struct PCB* CMainFrame::RemovePCB(struct PCB *delpt)
{
	UINT value = delpt->nid;
	struct PCB * pstart = delpt;
	
	if(!delpt)	return NULL;

	pstart->prev->next = pstart->next;
	if(pstart->next)	//注意节点存在的判断
		pstart->next->prev = pstart->prev;
	return pstart;
}

⌨️ 快捷键说明

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