資源簡介
經典的操作系統生產者消費者問題,大多數都是控制臺實現,我用的mfc實現了界面操作,結果操作簡單易懂。不明白的可以問我-合工大07-3劉俊杰 mylioujunjie@163.com

代碼片段和文件信息
//?P_C.cpp?:?Defines?the?class?behaviors?for?the?application.
//
#include?“stdafx.h“
#include?“P_C.h“
#include?“P_CDlg.h“
#ifdef?_DEBUG
#define?new?DEBUG_NEW
#define?M?10
#undef?THIS_FILE
static?char?THIS_FILE[]?=?__FILE__;
#endif
#define?WM_UPDATEDATA???WM_USER?+?1
/////////////////////////////////////////////////////////////////////////////
//?CP_CApp
BEGIN_MESSAGE_MAP(CP_CApp?CWinApp)
//{{AFX_MSG_MAP(CP_CApp)
//?NOTE?-?the?ClassWizard?will?add?and?remove?mapping?macros?here.
//????DO?NOT?EDIT?what?you?see?in?these?blocks?of?generated?code!
//}}AFX_MSG
ON_COMMAND(ID_HELP?CWinApp::onhelp)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
//?CP_CApp?construction
CP_CApp::CP_CApp()
{
//?TODO:?add?construction?code?here
//?Place?all?significant?initialization?in?InitInstance
}
/////////////////////////////////////////////////////////////////////////////
//?The?one?and?only?CP_CApp?object
CP_CApp?theApp;
?int?in??;???//?生產者放置產品的位置
?int?out;?//?消費者取產品的位置
//sem_t?empty_sem;?//?同步信號量,當滿了時阻止生產者放產品
?HANDLE?empty_sem;
//sem_t?full_sem;???//?同步信號量,當沒產品時阻止消費者消費
?HANDLE?full_sem;
//pthread_mutex_t?mutex;?//?互斥信號量,?一次只有一個線程訪問緩沖
?HANDLE?mutex;
?int?ID_producer;
?int?ID_consumer;
?int?buff[M];
CWinThread*?pcwinthread1;
CWinThread*?pcwinthread2;
CWinThread*?pcwinthread3;
CWinThread*?ccwinthread1;
CWinThread*?ccwinthread2;
CWinThread*?ccwinthread3;
CString?str_display;
CString?str_buff;
UINT?Producer(?LPVOID?pParam?){
//生產者
CP_CDlg*???pdlg=(CP_CDlg*)pParam;
int?idp?=?ID_producer?;
CString?s1;
CString?s2;
?while(1)
?{
????//?用sleep的數量可以調節生產和消費的速度,便于觀察
???
?//確認有空緩沖區可供生產,同時將空位置數empty減1;用于生產者和消費者的同步;
?WaitForSingleobject(empty_sem-1);
???????
??//互斥訪問下一個可用于生產的空臨界區,實現寫寫互斥;
??WaitForSingleobject(mutex-1);
??
??//這二句如果顛倒的話可能出現一種異常情況
?
?//當進入了緩沖區為滿時顛倒之后會對緩沖區先加鎖然后進程由于緩沖區為買沒法生產產品在這里被阻塞而消費者又無法獲得緩沖區的鎖進入緩沖區因而會出現死鎖
??
????in?=?in?%?M;
????//printf(“product%d?in?%d.?like:?\t“?id?in);
????s1?=“product“;
s2.Format(“%d“idp);
s1?=?s1?+?s2;
s2.Format(“%d“in+1);
s1?=?s1?+?“?in?“?+?s2?+?“\r\n“;
str_display?=?str_display+s1;
????buff[in]?=?1;?
pdlg->SendMessage(WM_UPDATEDATA);
???
????++in;
????ReleaseMutex(mutex);
????ReleaseSemaphore(full_sem1NULL);
Sleep(3000);
?} ?
?return?0;
}
UINT?Consumer(?LPVOID?pParam?){
//消費者
CP_CDlg*???pdlg=(CP_CDlg*)pParam;
int?idc?=?ID_consumer;
CString?s2;
CString?s1;
????while(1)
?{
????//?用sleep的數量可以調節生產和消費的速度,便于觀察
????
?
????WaitForSingleobject(full_sem-1);?????
//??這二句如果顛倒的話可能出現一種異常情況
?
?//當進入了緩沖區為空時顛倒之后會對緩沖區先加鎖然后進程由于緩沖區為空沒發消費產品在這里被阻塞而生產者又無法獲得緩沖區的鎖進入緩沖區因而會出現死鎖
?
????WaitForSingleobject(mutex-1);
????out?=?out?%?M;
??//printf(“prochase%d?in?%d.?like:?\t“?id?out);
????s1?=“consum“;
s2.Format(“%d“idc);
s1?=?s1?+?s2;
s2.Format(“%d“out+1);
s1?=?s1?+?“?in?“?+?s2?+?“\r\n“;
str_display?=?str_displ
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件?????114770??2010-06-27?14:14??P_C\Debug\P_C.exe
?????文件?????361980??2010-06-27?14:14??P_C\Debug\P_C.ilk
?????文件??????28285??2010-06-27?14:13??P_C\Debug\P_C.obj
?????文件????5502316??2010-06-26?16:14??P_C\Debug\P_C.pch
?????文件?????410624??2010-06-27?14:14??P_C\Debug\P_C.pdb
?????文件???????3000??2010-06-27?14:11??P_C\Debug\P_C.res
?????文件??????52255??2010-06-27?14:14??P_C\Debug\P_CDlg.obj
?????文件?????105744??2010-06-26?16:14??P_C\Debug\StdAfx.obj
?????文件?????214016??2010-06-27?14:14??P_C\Debug\vc60.idb
?????文件?????372736??2010-06-27?14:14??P_C\Debug\vc60.pdb
?????文件??????36272??2010-06-27?14:06??P_C\P_C.APS
?????文件???????1545??2010-06-27?14:14??P_C\P_C.clw
?????文件???????4605??2010-06-27?13:44??P_C\P_C.cpp
?????文件???????4105??2010-06-26?16:06??P_C\P_C.dsp
?????文件????????514??2010-06-26?16:06??P_C\P_C.dsw
?????文件???????1291??2010-06-26?16:06??P_C\P_C.h
?????文件??????58368??2010-06-27?14:14??P_C\P_C.ncb
?????文件??????54784??2010-06-27?14:14??P_C\P_C.opt
?????文件????????946??2010-06-27?14:14??P_C\P_C.plg
?????文件???????6077??2010-06-27?14:06??P_C\P_C.rc
?????文件????????327??2010-06-27?09:26??P_C\P_C.sln
????..A..H.??????6656??2010-06-27?09:26??P_C\P_C.suo
?????文件??????12612??2010-06-27?14:14??P_C\P_CDlg.cpp
?????文件???????1790??2010-06-27?14:12??P_C\P_CDlg.h
?????文件???????3525??2010-06-26?16:06??P_C\ReadMe.txt
?????文件???????1078??2010-06-26?16:06??P_C\res\P_C.ico
?????文件????????395??2010-06-26?16:06??P_C\res\P_C.rc2
?????文件???????1095??2010-06-27?13:19??P_C\resource.h
?????文件????????205??2010-06-26?16:06??P_C\StdAfx.cpp
?????文件???????1054??2010-06-26?16:06??P_C\StdAfx.h
............此處省略6個文件信息
- 上一篇:opencv MFC 攝像頭 人臉識別
- 下一篇:MFC中B樣條繪制
評論
共有 條評論