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

📄 实验室dlg.cpp

📁 操作系统的几个实验
💻 CPP
字号:
// 实验室Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "实验室.h"
#include "实验室Dlg.h"

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



HANDLE hThread[5];
	class container con;
	class CConsumer C2;
	class CConsumer C1;
	class Producer P1;
	class Producer Pa;
	class Producer PA;
	HANDLE hMutex;













/////////////////////////////////////////////////////////////////////////////
// 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()

/////////////////////////////////////////////////////////////////////////////
// CMyDlg dialog

CMyDlg::CMyDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMyDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMyDlg)
	m_buffer = 0;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CMyDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMyDlg)
	DDX_Control(pDX, IDC_C2_STATE, m_c2_state);
	DDX_Control(pDX, IDC_C1_STATE, m_c1_state);
	DDX_Control(pDX, IDC_PSMALL_STATE, m_psmall_state);
	DDX_Control(pDX, IDC_PBIG_STATE, m_pbig_state);
	DDX_Control(pDX, IDC_P1_STATE, m_p1_state);
	DDX_Control(pDX, IDC_BUFFER, m_buffer_control);
	DDX_Control(pDX, IDC_CONSUMER2_LIST, m_consumer2_list);
	DDX_Control(pDX, IDC_CONSUMER1_LIST, m_consumer1_list);
	DDX_Control(pDX, IDC_CONTAINER_LIST, m_container_list);
	DDX_Control(pDX, IDC_PRODUCER3_LIST, m_producer_list3);
	DDX_Control(pDX, IDC_PRODUCER2_LIST, m_producer_list2);
	DDX_Control(pDX, IDC_PRODUCER1_LIST, m_producer_list);
	DDX_Text(pDX, IDC_BUFFER, m_buffer);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMyDlg, CDialog)
	//{{AFX_MSG_MAP(CMyDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUILD, OnBuild)
	ON_BN_CLICKED(IDC_STOP, OnStop)
	ON_BN_CLICKED(IDC_REFLASH, OnReflash)
	ON_BN_CLICKED(IDC_ABOUT, OnAbout)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMyDlg message handlers

BOOL CMyDlg::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
	P1state=0;
	Pastate=0;
	PAstate=0;
	CurrentTime=0;
	m_producer_list.SetExtendedStyle(LVS_EX_FLATSB
		|LVS_EX_FULLROWSELECT
		|LVS_EX_HEADERDRAGDROP
		|LVS_EX_ONECLICKACTIVATE
		|LVS_EX_GRIDLINES);

	m_producer_list.InsertColumn(0, "生产时间", LVCFMT_LEFT, 65, 0);
	m_producer_list.InsertColumn(1, "消息内容", LVCFMT_LEFT, 80, 1);

	m_producer_list2.SetExtendedStyle(LVS_EX_FLATSB
		|LVS_EX_FULLROWSELECT
		|LVS_EX_HEADERDRAGDROP
		|LVS_EX_ONECLICKACTIVATE
		|LVS_EX_GRIDLINES);

	m_producer_list2.InsertColumn(0, "生产时间", LVCFMT_LEFT, 65, 0);
	m_producer_list2.InsertColumn(1, "消息内容", LVCFMT_LEFT, 80, 1);
	

	m_producer_list3.SetExtendedStyle(LVS_EX_FLATSB
		|LVS_EX_FULLROWSELECT
		|LVS_EX_HEADERDRAGDROP
		|LVS_EX_ONECLICKACTIVATE
		|LVS_EX_GRIDLINES);

	m_producer_list3.InsertColumn(0, "生产时间", LVCFMT_LEFT, 65, 0);
	m_producer_list3.InsertColumn(1, "消息内容", LVCFMT_LEFT, 80, 1);



	m_container_list.SetExtendedStyle(LVS_EX_FLATSB
		|LVS_EX_FULLROWSELECT
		|LVS_EX_HEADERDRAGDROP
		|LVS_EX_ONECLICKACTIVATE
		|LVS_EX_GRIDLINES);

	m_container_list.InsertColumn(0, "存入时间", LVCFMT_LEFT, 65, 0);
	m_container_list.InsertColumn(1, "消息内容", LVCFMT_LEFT, 90, 1);
	m_container_list.InsertColumn(2, "生产者", LVCFMT_LEFT, 80, 1);


	m_consumer1_list.SetExtendedStyle(LVS_EX_FLATSB
		|LVS_EX_FULLROWSELECT
		|LVS_EX_HEADERDRAGDROP
		|LVS_EX_ONECLICKACTIVATE
		|LVS_EX_GRIDLINES);

	m_consumer1_list.InsertColumn(0, "消费时间", LVCFMT_LEFT, 65, 0);
	m_consumer1_list.InsertColumn(1, "消息内容", LVCFMT_LEFT, 90, 1);

	m_consumer2_list.SetExtendedStyle(LVS_EX_FLATSB
		|LVS_EX_FULLROWSELECT
		|LVS_EX_HEADERDRAGDROP
		|LVS_EX_ONECLICKACTIVATE
		|LVS_EX_GRIDLINES);

	m_consumer2_list.InsertColumn(0, "消费时间", LVCFMT_LEFT, 65, 0);
	m_consumer2_list.InsertColumn(1, "消息内容", LVCFMT_LEFT, 90, 1);
	
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

void CMyDlg::OnBuild() 
{
	// TODO: Add your control notification handler code here

	GetDlgItem(IDC_BUILD)->EnableWindow(FALSE);
	GetDlgItem(IDC_STOP)->EnableWindow(TRUE);
	GetDlgItem(IDC_REFLASH)->EnableWindow(FALSE);

	CurrentTime=GetTickCount();
	hMutex=CreateMutex(NULL,FALSE,NULL);
	hThread[0]=CreateThread(NULL,0,CMyDlg::ProducerPAProc,this,0,NULL);
	hThread[1]=CreateThread(NULL,0,CMyDlg::ProducerPaProc,this,0,NULL);
	hThread[2]=CreateThread(NULL,0,CMyDlg::ProducerP1Proc,this,0,NULL);
	hThread[3]=CreateThread(NULL,0,CMyDlg::Consumer01Proc,this,0,NULL);
	hThread[4]=CreateThread(NULL,0,CMyDlg::Consumer02Proc,this,0,NULL);

}


DWORD WINAPI CMyDlg::ProducerPAProc(LPVOID pParam)
{
	CMyDlg *p=(CMyDlg *)pParam;
	int buffer;
	char b[10];
	CString str;
	DWORD now;

		while(TRUE)
		{	
		
		WaitForSingleObject(p->hMutex,INFINITE);

		if(p->PAstate==0)
		{
		p->PA.RandomAlphabetBig();
		p->PAstate=1;
		}
	
    	if(p->con.HowManyMemory()>=(p->PA.len+3))
		{
			p->PA.InputData(&(p->con));
			p->PAstate=0;
		p->m_pbig_state.SetWindowText("生产");

		buffer=p->con.HowManyMemory();
		itoa(buffer,b,10);
		p->m_buffer_control.SetWindowText(b);
		
		now=GetTickCount();
		now=now-p->CurrentTime;
		str.Format("%u",now);

			p->m_producer_list2.InsertItem(0,str);
			p->m_producer_list2.SetItemText(0,1,p->PA.message);
			p->m_container_list.InsertItem(0,str);
			p->m_container_list.SetItemText(0,1,p->PA.message);
			p->m_container_list.SetItemText(0,2,"生产者(3)");
			
		}
		Sleep(rand()%1000);

		p->m_pbig_state.SetWindowText("休眠");
	
		ReleaseMutex(p->hMutex);
	
	}
	
	


	return 0;
}

DWORD WINAPI CMyDlg::ProducerPaProc(LPVOID pParam)
{
	CMyDlg *p=(CMyDlg *)pParam;
	int buffer;
	char b[10];
	CString str;
	DWORD now;
	

	while(1)
	{
	WaitForSingleObject(p->hMutex,INFINITE);
	
		if(p->Pastate==0)
			{
			p->Pa.RandomAlphabetSmall();
			p->Pastate=1;
			}		
		if(p->con.HowManyMemory()>=(p->Pa.len+3))
		{
			p->Pa.InputData(&(p->con));
			p->Pastate=0;
			p->m_psmall_state.SetWindowText("生产");

			buffer=p->con.HowManyMemory();
			itoa(buffer,b,10);
			p->m_buffer_control.SetWindowText(b);


			now=GetTickCount();
			now=now-p->CurrentTime;
			str.Format("%u",now);

			p->m_producer_list3.InsertItem(0,str);
			p->m_producer_list3.SetItemText(0,1,p->Pa.message);
			p->m_container_list.InsertItem(0,str);
			p->m_container_list.SetItemText(0,1,p->Pa.message);
			p->m_container_list.SetItemText(0,2,"生产者(2)");
		
		}		
		Sleep(rand()%1050);	
		
		p->m_psmall_state.SetWindowText("休眠");
		
		ReleaseMutex(p->hMutex);
	}
	
	
	return 0;
}

DWORD WINAPI CMyDlg::ProducerP1Proc(LPVOID pParam)
{
	CMyDlg *p=(CMyDlg *)pParam;
	int buffer;
	char b[10];
	CString str;
	DWORD now;


		while(1)
	{
		
		WaitForSingleObject(p->hMutex,INFINITE);
	
		if(p->P1state==0)
		{
		p->P1.RandomNumber();
		
		p->P1state=1;
		}
		
		
			
		if(p->con.HowManyMemory()>=(p->P1.len+3))
		{
			
			p->P1.InputData(&(p->con));
			p->P1state=0;
			p->m_p1_state.SetWindowText("生产");
			buffer=p->con.HowManyMemory();
			itoa(buffer,b,10);
			p->m_buffer_control.SetWindowText(b);
		
			now=GetTickCount();
			now=now-p->CurrentTime;
			str.Format("%u",now);

			p->m_producer_list.InsertItem(0,str);
			p->m_producer_list.SetItemText(0,1,p->P1.message);
			p->m_container_list.InsertItem(0,str);
			p->m_container_list.SetItemText(0,1,p->P1.message);
			p->m_container_list.SetItemText(0,2,"生产者(1)");
		
		}
		Sleep(rand()%900);		
	    p->m_p1_state.SetWindowText("休眠");
		ReleaseMutex(p->hMutex);
      
	}
	
	
	return 0;
}

DWORD WINAPI CMyDlg::Consumer01Proc(LPVOID pParam)
{
	CMyDlg *p=(CMyDlg *)pParam;
	int buffer;
	char b[10];

	CString str;
	DWORD now;


	while(1)
	{
		
		WaitForSingleObject(p->hMutex,INFINITE);

		if(2000-p->con.HowManyMemory()>0)
		{
			p->C1.OutputData(&(p->con));	
			p->m_c1_state.SetWindowText("消费");
			
			buffer=p->con.HowManyMemory();
			itoa(buffer,b,10);
			p->m_buffer_control.SetWindowText(b);

			now=GetTickCount();
			now=now-p->CurrentTime;
			str.Format("%u",now);
		
			p->m_consumer1_list.InsertItem(0,str);
			p->m_consumer1_list.SetItemText(0,1,p->C1.message);
			p->m_container_list.DeleteItem(p->m_container_list.GetItemCount()-1);
			
		}
			Sleep(rand()%1000);
			p->m_c1_state.SetWindowText("休眠");
			ReleaseMutex(p->hMutex);
	

	
	}


	
	return 0;
}

DWORD WINAPI CMyDlg::Consumer02Proc(LPVOID pParam)
{
	CMyDlg *p=(CMyDlg *)pParam;
	int buffer;
	char b[10];
	
	CString str;
	DWORD now;

	while(1)
	{

		WaitForSingleObject(p->hMutex,INFINITE);

		if(2000-p->con.HowManyMemory()>0)
		{
			p->C2.OutputData(&(p->con));
			p->m_c2_state.SetWindowText("消费");

			buffer=p->con.HowManyMemory();
			itoa(buffer,b,10);
			p->m_buffer_control.SetWindowText(b);

			now=GetTickCount();
			now=now-p->CurrentTime;
			str.Format("%u",now);
			
			p->m_consumer2_list.InsertItem(0,str);
			p->m_consumer2_list.SetItemText(0,1,p->C2.message);		
			p->m_container_list.DeleteItem(p->m_container_list.GetItemCount()-1);
			
		}
		Sleep(rand()%1500);
		p->m_c2_state.SetWindowText("休眠");
		ReleaseMutex(p->hMutex);		
	}
	
	return 0;
}

void CMyDlg::OnStop() 
{
	// TODO: Add your control notification handler code here
	GetDlgItem(IDC_STOP)->EnableWindow(FALSE);
	GetDlgItem(IDC_REFLASH)->EnableWindow(TRUE);
	TerminateThread(hThread[0],0);
	TerminateThread(hThread[1],0);
	TerminateThread(hThread[2],0);
	TerminateThread(hThread[3],0);
	TerminateThread(hThread[4],0);
	CloseHandle(hMutex);
	CloseHandle(hThread[0]);
	CloseHandle(hThread[1]);
	CloseHandle(hThread[2]);
	CloseHandle(hThread[3]);
	CloseHandle(hThread[4]);
}


void CMyDlg::OnReflash() 
{
	// TODO: Add your control notification handler code here
	GetDlgItem(IDC_BUILD)->EnableWindow(TRUE);
	m_consumer1_list.DeleteAllItems();
	m_consumer2_list.DeleteAllItems();
	m_producer_list.DeleteAllItems();
	m_producer_list2.DeleteAllItems();
	m_producer_list3.DeleteAllItems();
	m_consumer2_list.DeleteAllItems();
	m_container_list.DeleteAllItems();
	con.ReFlash();

}

void CMyDlg::OnAbout() 
{
	// TODO: Add your control notification handler code here

	CAboutDlg dlg;
	dlg.DoModal();
	
}

⌨️ 快捷键说明

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