操作系统生产者消费者问题C语言.doc

上传人:豆**** 文档编号:28434446 上传时间:2022-07-28 格式:DOC 页数:35 大小:164.50KB
返回 下载 相关 举报
操作系统生产者消费者问题C语言.doc_第1页
第1页 / 共35页
操作系统生产者消费者问题C语言.doc_第2页
第2页 / 共35页
点击查看更多>>
资源描述

《操作系统生产者消费者问题C语言.doc》由会员分享,可在线阅读,更多相关《操作系统生产者消费者问题C语言.doc(35页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。

1、Four short words sum up what has lifted most successful individuals above the crowd: a little bit more.-author-date操作系统生产者消费者问题C语言操作系统概念第七版 中的实验项目:生产者消费者问题。本程序中,main()函数需要三个参数:主线程休眠时间;生产者线程数;消费者线程数。各线程的休眠等待时间是随机的。操作系统概念第七版 中的实验项目:生产者消费者问题。本程序中,main()函数需要三个参数:主线程休眠时间;生产者线程数;消费者线程数。各线程的休眠等待时间是随机的。程序代码

2、:#include#include#include#include#define BUFFER_SIZE 5typedef int buffer_item;struct vint i;buffer_item bufferBUFFER_SIZE+1;buffer_item front=0,rear=0;HANDLE mutex,empty,full;int insert_item(buffer_item item)/*insert item into bufferreturn 0 if successful,otherwise return -1 indicating an error cond

3、ition*/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 placing it in item return 0 if successful,otherwise reutrn -1 indication an error condition */if(front = rear)return 1;*item=buffer

4、front;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()%101*10);WaitForSingleObject(empty,INFINITE);WaitForSingleObject(mutex,INFINITE);rand1 =rand();printf(producer has producerd %d B

5、y %dn,rand1,data.i);if(insert_item(rand1)printf(insert data error!n);ReleaseMutex(mutex);ReleaseSemaphore(full,1,NULL);DWORD WINAPI consumer(PVOID Param)int rand1;struct v data=*(struct v *)Param;srand(unsigned)time(0);while (1)Sleep(rand()%101*10);WaitForSingleObject(full,INFINITE);WaitForSingleObj

6、ect(mutex,INFINITE);if(remove_item(&rand1)printf(remove data error! n);elseprintf(consumer consumed %d By %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 consume

7、r threads),argv3(sleep time)*/*Initialize buffer*/int sleeptime,pnum,snum;int *ThreadIdP,*ThreadIdS,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 * )mallo

8、c(pnum * sizeof(HANDLE);ThreadHandleS=(HANDLE * )malloc(snum * sizeof(HANDLE);ThreadIdP=(int * )malloc(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,

9、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;ipnum;i+)countpi+1.i=i+1;ThreadHandlePi=CreateThread(NULL,0,producer,&countpi+1,0,&ThreadIdPi);/*Create consumer thread(s)*/for(i=0;isnum;i+)countsi+1.i=i+

10、1;ThreadHandleSi=CreateThread(NULL,0,consumer,&countsi+1,0,&ThreadIdSi);/*Sleep*/Sleep(sleeptime);/*Exit*/return 0;#include stdafx.h#include#include#include#include#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

12、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);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 Param)int rand1;struct

14、 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,data.i);ReleaseMutex(mutex);ReleaseSemaphore(empty,1

15、,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 *countp,*counts;HANDLE *ThreadHandleP,*ThreadHandleS;/*s

16、leeptime=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 * sizeof(DWORD);ThreadIdS=(DWORD * )malloc(pnum * sizeof(

17、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;ipnum;i+)countpi+1.i=i+1;ThreadHandlePi=CreateThread(NULL,0,producer,&countpi+1,0,&ThreadIdPi);/*Create consumer thread(s)*/for(i=0;isnum;i+)countsi+1.i=i+1;ThreadHandleSi=CreateThread(NULL,0,consumer,&countsi+1,0,&ThreadIdSi);/*Sleep*/Sleep(sleeptime);/*Exit*/return 0; -

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 教育专区 > 小学资料

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知淘文阁网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号© 2020-2023 www.taowenge.com 淘文阁