- 71
- 0
- 约4.9千字
- 约 8页
- 2017-01-30 发布于重庆
- 举报
(A4纸或A3纸折叠)#includestdlib.h
#includetime.h
#includewindows.h
#includesys/timeb.h
#includestdio.h
HANDLE hFull;
HANDLE hEmpty;
HANDLE hMutex;
HANDLE hThreadP[50];
HANDLE hThreadC[50];
int iHandle_Count=0;//操作数
int iStore=0;//共享资源数
int iGood_id=0;//货物ID
/*******货物*********/
struct goods
{
int iGood_id;
int iProduct_id;
}goods[100];
/**********生产者和消费者信息*************/
typedef struct
{
int iId;
int iMax_Store;
}threadinfo;
/************运行函数*************/
DWORD WINAPI Producer(LPVOID *INFO)//生产者函数
{
int i;
threadinfo* info=(threadinfo*)INFO;
for(i=0;iinfo-iMax_Store;i++)
{
WaitForSingleObject(hEmpty,INFINITE);
WaitForSingleObject(hMutex,INFINITE);
iHandle_Count++;
iStore++;
iGood_id++;
goods[i].iGood_id=iGood_id;
goods[i].iProduct_id=info-iId;
printf(%d号生产者 生产了%d号产品 目前操作次数:%d 仓库中共享资源数量:%d\n
,goods[i].iProduct_id,goods[i].iGood_id,iHandle_Count,iStore);
ReleaseSemaphore(hFull,1,NULL);
ReleaseMutex(hMutex);
}
}
DWORD WINAPI Consumer(LPVOID *INFO)//消费者函数
{
int i;
threadinfo* info=(threadinfo*)INFO;
for(i=0;iinfo-iMax_Store;i++)
{
WaitForSingleObject(hFull,INFINITE);
WaitForSingleObject(hMutex,INFINITE);
iHandle_Count++;
iStore--;
printf(%d号消费者 消费了%d号产品 目前操作次数:%d 仓库中共享资源数量:%d\n
,info-iId,goods[i].iGood_id,iHandle_Count,iStore);
ReleaseSemaphore(hEmpty,1,NULL);
ReleaseMutex(hMutex);
}
}
void WINAPI run(int iMax_Store, int iProducer_num,int iConsumer_num)
{
threadinfo infoP[50];
threadinfo infoC[50];
int iProducer,iConsumer;
hMutex=CreateMutex(NULL,FALSE,NULL);
hFull=CreateSemaphore(NULL,0,iMax_Store,NULL);
hEmpty=CreateSemaphore(NULL,iMax_Store,iMax_Store,NULL);
WaitForSingleObject(hMutex,INFINITE);
/**********创建消费者和生产者线程**************/
printf(******创建生产者和消费者线程******\n);
for(iProducer=1;iProducer=iProducer_num;iProducer++)
{
infoP[iProducer-1].iId=iProducer;
infoP[iProducer-1].iMax_Store=iMax_Store;
hThreadP[iProducer-1]=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)Producer,infoP[iProducer-1],0,NULL);//创建生产
您可能关注的文档
- 计算机应用技术专业大学生职业生涯规划书.doc
- 山东大学操作系统实验5进程互斥实验.doc
- linux进程和线程通信.doc
- 辽宁工程技术大学大学软件项目实训报告.doc
- 操作系统第二次实验报告.doc
- 实验3 Windows虚拟内存.doc
- 北邮操作系统进程同步实验报告及源代码.docx
- 使用ptrace向已运行进程中注入.so并执行相关函数.doc
- 应用技术学院学生约谈制度.doc
- 南昌大学计算机应用技术调剂生复试经验.doc
- 小区绿化施工协议书.docx
- 墙面施工协议书.docx
- 1 古诗二首(课件)--2025-2026学年统编版语文二年级下册.pptx
- (2026春新版)部编版八年级道德与法治下册《3.1《公民基本权利》PPT课件.pptx
- (2026春新版)部编版八年级道德与法治下册《4.3《依法履行义务》PPT课件.pptx
- (2026春新版)部编版八年级道德与法治下册《6.2《按劳分配为主体、多种分配方式并存》PPT课件.pptx
- (2026春新版)部编版八年级道德与法治下册《6.1《公有制为主体、多种所有制经济共同发展》PPT课件.pptx
- 初三教学管理交流发言稿.docx
- 小学生课外阅读总结.docx
- 餐饮门店夜经济运营的社会责任报告(夜间贡献)撰写流程试题库及答案.doc
原创力文档

文档评论(0)