版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、操作系统概念第七版 中的实验项目:生产者消费者问题。本程序中,main()函数需要三个参数:主线程休眠时间;生产者线程数;消费者线程数。各线程的休眠等待时间是随机的。程序代码:#include<stdio.h>#include<stdlib.h>#include<time.h>#include<windows.h>#define BUFFER_SIZE 5typedef int buffer_item;struct vint i;buffer_item bufferBUFFER_SIZE+1;buffer_item front=0,rear=0;
2、HANDLE mutex,empty,full;int insert_item(buffer_item item)/*insert item into bufferreturn 0 if successful,otherwise return -1 indicating an error condition*/if(rear+1)%(BUFFER_SIZE+1)=front)return 1;bufferrear=item;rear=(rear+1)%(BUFFER_SIZE+1);return 0;int remove_item(buffer_item *item)/*remove an o
3、bject from buffer placing it in item return 0 if successful,otherwise reutrn -1 indication an error condition */if(front = rear)return 1;*item=bufferfront;front=(front+1) % (BUFFER_SIZE+1);return 0;DWORD WINAPI producer(PVOID Param)int rand1;struct v data=*(struct v *)Param;srand(unsigned)time(0);wh
4、ile (1) Sleep(rand()%101*10);WaitForSingleObject(empty,INFINITE);WaitForSingleObject(mutex,INFINITE);rand1 =rand();printf("producer has producerd %d By %dn",rand1,data.i);if(insert_item(rand1)printf("insert data error!n");ReleaseMutex(mutex);ReleaseSemaphore(full,1,NULL);DWORD WI
5、NAPI consumer(PVOID Param)int rand1;struct v data=*(struct v *)Param;srand(unsigned)time(0);while (1)Sleep(rand()%101*10);WaitForSingleObject(full,INFINITE);WaitForSingleObject(mutex,INFINITE);if(remove_item(&rand1)printf("remove data error! n");elseprintf("consumer consumed %d By
6、 %d n",rand1,data.i);ReleaseMutex(mutex);ReleaseSemaphore(empty,1,NULL);int main(int argc,char *argv)/*Get command line arguments argv1)(the number of producer threads),argv2(the number of consumer threads),argv3(sleep time)*/*Initialize buffer*/int sleeptime,pnum,snum;int *ThreadIdP,*ThreadIdS
7、,i;struct v *countp,*counts;HANDLE *ThreadHandleP,*ThreadHandleS;sleeptime=atoi(argv1);pnum=atoi(argv2);snum=atoi(argv3);/*srand(time(NULL);sleeptime=9000;pnum=3;snum=3;*/ThreadHandleP=(HANDLE * )malloc(pnum * sizeof(HANDLE);ThreadHandleS=(HANDLE * )malloc(snum * sizeof(HANDLE);ThreadIdP=(int * )mal
8、loc(pnum * sizeof(int);ThreadIdS=(int * )malloc(pnum * sizeof(int);mutex=CreateMutex(NULL,FALSE,NULL);empty=CreateSemaphore(NULL,BUFFER_SIZE,BUFFER_SIZE,NULL);full=CreateSemaphore(NULL,0,BUFFER_SIZE+1,NULL);/*Create producer thread(s)*/countp=(struct v *)malloc(pnum+1)*sizeof(struct v);counts=(struc
9、t v *)malloc(snum+1)*sizeof(struct v);for(i=0;i<pnum;i+)countpi+1.i=i+1;ThreadHandlePi=CreateThread(NULL,0,producer,&countpi+1,0,&ThreadIdPi);/*Create consumer thread(s)*/for(i=0;i<snum;i+)countsi+1.i=i+1;ThreadHandleSi=CreateThread(NULL,0,consumer,&countsi+1,0,&ThreadIdSi);/*S
10、leep*/Sleep(sleeptime);/*Exit*/return 0;#include "stdafx.h"#include<stdio.h>#include<stdlib.h>#include<time.h>#include<windows.h>#define BUFFER_SIZE 5typedef int buffer_item;struct vint i;buffer_item bufferBUFFER_SIZE+1;buffer_item front=0,rear=0;HANDLE mutex,empty,
11、full;int insert_item(buffer_item item)/*insert item into bufferreturn 0 if successful,otherwise return -1 indicating an error condition*/if(rear+1)%(BUFFER_SIZE+1)=front)return 1;bufferrear=item;rear=(rear+1)%(BUFFER_SIZE+1);return 0;int remove_item(buffer_item *item)/*remove an object from buffer p
12、lacing it in item return 0 if successful,otherwise reutrn -1 indication an error condition */if(front = rear)return 1;*item=bufferfront;front=(front+1) % (BUFFER_SIZE+1);return 0;DWORD WINAPI producer(PVOID Param)int rand1;struct v data=*(struct v *)Param;srand(unsigned)time(0);while (1) Sleep(rand(
13、)%101*10);WaitForSingleObject(empty,INFINITE);WaitForSingleObject(mutex,INFINITE);rand1 =rand();printf("producer has producerd %d By %dn",rand1,data.i);if(insert_item(rand1)printf("insert data error!n");ReleaseMutex(mutex);ReleaseSemaphore(full,1,NULL);DWORD WINAPI consumer(PVOID
14、 Param)int rand1;struct v data=*(struct v *)Param;srand(unsigned)time(0);while (1)Sleep(rand()%101*10);WaitForSingleObject(full,INFINITE);WaitForSingleObject(mutex,INFINITE);if(remove_item(&rand1)printf("remove data error! n");elseprintf("consumer consumed %d By %d n",rand1,d
15、ata.i);ReleaseMutex(mutex);ReleaseSemaphore(empty,1,NULL);int main(int argc,char *argv)/*Get command line arguments argv1)(the number of producer threads),argv2(the number of consumer threads),argv3(sleep time)*/*Initialize buffer*/int sleeptime,pnum,snum;DWORD *ThreadIdP,*ThreadIdS,i;struct v *coun
16、tp,*counts;HANDLE *ThreadHandleP,*ThreadHandleS;/*sleeptime=atoi(argv1);pnum=atoi(argv2);snum=atoi(argv3);*/srand(time(NULL);sleeptime=9000;pnum=3;snum=3;ThreadHandleP=(HANDLE * )malloc(pnum * sizeof(HANDLE);ThreadHandleS=(HANDLE * )malloc(snum * sizeof(HANDLE);ThreadIdP=(DWORD * )malloc(pnum * size
17、of(DWORD);ThreadIdS=(DWORD * )malloc(pnum * sizeof(DWORD);mutex=CreateMutex(NULL,FALSE,NULL);empty=CreateSemaphore(NULL,BUFFER_SIZE,BUFFER_SIZE,NULL);full=CreateSemaphore(NULL,0,BUFFER_SIZE+1,NULL);/*Create producer thread(s)*/countp=(struct v *)malloc(pnum+1)*sizeof(struct v);counts=(struct v *)malloc(snum+1)*sizeof(struct v);for(i=0;i<pnum;i+)countpi+1.i=i+1;ThreadHandle
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2024会议合作协议书范本
- 2024年度企业研发成果转化服务合同
- 2024年企业员工岗位义务与敬业承诺协议版
- 2024年度地产项目代理销售合同
- 2024年医疗器材质量保障协议范本
- 2024年度产品研发与委托制造合同
- 2024年工程劳务分包详细协议范本版B版
- 2024年度企业互借资金协议模板一
- 2024专业离婚合同书:女方权益保障实例一
- 2024员工股权激励协议书
- 物管物业交接验收明细表格(全)模板
- 学校矛盾纠纷排查化解工作方案(3篇)
- 万物之理-爱因斯坦之梦智慧树知到期末考试答案章节答案2024年中国海洋大学
- 6人小品《没有学习的人不伤心》台词完整版
- 《王戎不取道旁李》课件完美版
- 口腔科诊断证明书模板
- 新苏教版2021-2022四年级科学上册《15生活中的电》教案
- 小学语文课标目标解读PPT学习教案
- 最新小学一年级道德与法治上册第一次月考试卷(完美版)
- 最新小学一年级道德与法治(上册)期末模拟试卷及答案
- 面神经炎(面瘫病)病程模板
评论
0/150
提交评论