生产者消费者问题C语言_第1页
生产者消费者问题C语言_第2页
生产者消费者问题C语言_第3页
生产者消费者问题C语言_第4页
生产者消费者问题C语言_第5页
已阅读5页,还剩1页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

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 fr

2、ont=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 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)

3、/*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=bufferfront;front=(front+1) % (BUFFER_SIZE+1);return 0;DWORD WINAPI producer(PVOID Param)int rand1;struct v data=*(struct v *)Param;srand(unsign

4、ed)time(0);while (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,N

5、ULL);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);WaitForSingleObject(mutex,INFINITE);if(remove_item(&rand1)printf("remove data error! n");elseprintf("consumer c

6、onsumed %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 consumer threads),argv3(sleep time)*/*Initialize buffer*/int sleeptime,pnum,snum;int *ThreadI

7、dP,*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 * )malloc(pnum * sizeof(HANDLE);ThreadHandleS=(HANDLE * )malloc(snum * sizeof(HANDLE);ThreadId

8、P=(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,NULL);/*Create producer thread(s)*/countp=(struct v *)malloc(pnum+1)*sizeof(struct v);

9、counts=(struct 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,&Th

10、readIdSi);/*Sleep*/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

11、 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 object

12、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);while (1

13、) 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 WINAPI c

14、onsumer(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 %d n&

15、quot;,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;DWORD *ThreadIdP,*ThreadIdS,i;s

16、truct 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=(DWORD * )mallo

17、c(pnum * sizeof(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;T

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论