操作系统生产者消费者问题C语言_第1页
操作系统生产者消费者问题C语言_第2页
操作系统生产者消费者问题C语言_第3页
操作系统生产者消费者问题C语言_第4页
操作系统生产者消费者问题C语言_第5页
免费预览已结束,剩余2页可下载查看

下载本文档

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

文档简介

1、操作系统概念第七版中的实验项目:生产者消费者问题。本程序中,main()函数需要三个参数:主线程休眠时间;生产者线程数;消费者线程数。各线程的休眠等待时间是随机的。程序代码:#include<stdio.h>#include<stdlib.h>#include<time.h>#include<windows.h>#defineBUFFER_SIZE5typedefintbuffer_item;structvinti;buffer_itembufferBUFFER_SIZE+1;buffer_itemfront=0,rear=0;HANDLEmut

2、ex,empty,full;intinsert_item(buffer_itemitem)/*insertitemintobufferreturn0ifsuccessful,otherwisereturn-1indicatinganerrorcondition*/if(rear+1)%(BUFFER_SIZE+1)=front)return1;bufferrear=item;rear=(rear+1)%(BUFFER_SIZE+1);return0;intremove_item(buffer_item*item)/*removeanobjectfrombufferplacingitinitem

3、return0ifsuccessful,otherwisereutrn-1indicationanerrorcondition*/if(front=rear)return1;*item=bufferfront;front=(front+1)%(BUFFER_SIZE+1);return0;DWORDWINAPIproducer(PVOIDParam)(intrandl;structvdata=*(structv*)Param;srand(unsigned)time(0);while(1)(Sleep(rand()%101*10);WaitForSingleObject(empty,INFINI

4、TE);WaitForSingleObject(mutex,INFINITE);rand1=rand();printf("producerhasproducerd%dBy%dn",rand1,data.i);if(insert_item(rand1)printf("insertdataerror!n");ReleaseMutex(mutex);ReleaseSemaphore(full,1,NULL);)DWORDWINAPIconsumer(PVOIDParam)(intrand1;structvdata=*(structv*)Param;srand(

5、unsigned)time(0);while(1)(Sleep(rand()%101*10);WaitForSingleObject(full,INFINITE);WaitForSingleObject(mutex,INFINITE);if(remove_item(&rand1)printf("removedataerror!n");elseprintf("consumerconsumed%dBy%dn",rand1,data.i);ReleaseMutex(mutex);ReleaseSemaphore(empty,1,NULL);)intma

6、in(intargc,char*argv)(/*Getcommandlineargumentsargv1)(thenumberofproducerthreads),argv2(thenumberofconsumerthreads),argv3(sleeptime)*/*Initializebuffer*/intsleeptime,pnum,snum;int*ThreadIdP,*ThreadIdS,i;structv*countp,*counts;HANDLE*ThreadHandleP,*ThreadHandleS;sleeptime=atoi(argv1);pnum=atoi(argv2)

7、;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*)malloc(pnum*sizeof(int);ThreadIdS=(int*)malloc(pnum*sizeof(int);mutex=CreateMutex(NULL,FALSE,NULL);empty=CreateSemap

8、hore(NULL,BUFFER_SIZE,BUFFER_SIZE,NULL);full=CreateSemaphore(NULL,0,BUFFER_SIZE+1,NULL);/*Createproducerthread(s)*/countp=(structv*)malloc(pnum+1)*sizeof(structv);counts=(structv*)malloc(snum+1)*sizeof(structv);for(i=0;i<pnum;i+)(countpi+1.i=i+1;ThreadHandlePi=CreateThread(NULL,0,producer,&co

9、untpi+1,0,&ThreadIdPi);)/*Createconsumerthread(s)*/for(i=0;i<snum;i+)(countsi+1.i=i+1;ThreadHandleSi=CreateThread(NULL,0,consumer,&countsi+1,0,&ThreadIdSi);)/*Sleep*/Sleep(sleeptime);/*Exit*/return0;#include"stdafx.h#include<stdio.h>#include<stdlib.h>#include<time.

10、h>#include<windows.h>#defineBUFFER_SIZE5typedefintbuffer_itemstructvinti;buffer_itembufferBUFFER_SIZE1;buffer_itemfront=0,rear=0;HANDLEmutex,empty,full;intinsert_item(buffer_itemitem)/*insertitemintobufferreturn0ifsuccessful,otherwisereturn-1indicatinganerrorcondition*/if(rear+1)%(BUFFER_SI

11、ZE1)=front)return1;bufferrear=item;rear=(rear+1)%(BUFFER_SIZ+1);return0;intremove_item(buffer_item*item)/*removeanobjectfrombufferplacingitinitemreturn0ifsuccessful,otherwisereutrn-1indicationanerrorcondition*/if(front=rear)return1;*item=bufferfront;front=(front+1)%(BUFFER_SIZ+1);return0;DWORWINAPIp

12、roducer(PVOIDParam)intrand1;structvdata=*(structv*)Param;srand(unsigned)time(0);while(1)(Sleep(rand()%101*10);WaitForSingleObject(empty,INFINITE);WaitForSingleObject(mutex,INFINITE);rand1=rand();printf("producerhasproducerd%dBy%dn",rand1,data.i);if(insert_item(rand1)printf("insertdata

13、error!n");ReleaseMutex(mutex);ReleaseSemaphore(full,1,NULL;DWORWINAPIconsumer(PVOIDParam)(intrand1;structvdata=*(structv*)Param;srand(unsigned)time(0);while(1)(Sleep(rand()%101*10);WaitForSingleObject(full,INFINITE);WaitForSingleObject(mutex,INFINITE);if(remove_item(&rand1)printf("remo

14、vedataerror!n");elseprintf("consumerconsumed%dBy%dn",rand1,data.i);ReleaseMutex(mutex);ReleaseSemaphore(empty,1,NULL);intmain(intargc,char*argv)(/*Getcommandlineargumentsargv1)(thenumberofproducerthreads),argv2(thenumberofconsumerthreads),argv3(sleeptime)*/*Initializebuffer*/intsleept

15、ime,pnum,snum;DWOR*DThreadIdP,*ThreadIdS,i;structv*countp,*counts;HANDL巴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(HA

16、NDLE;ThreadIdP=(DWORD)malloc(pnum*sizeof(DWORDThreadIdS=(DWORD)malloc(pnum*sizeof(DWORDmutex=CreateMutex(NULLFALSENULL;empty=CreateSemaphore(NULLBUFFER_SIZEBUFFER_SIZENULL;full=CreateSemaphore(NULL。,BUFFER_SIZ+1,NULL;/*Createproducerthread(s)*/countp=(structv*)malloc(pnum+1)*sizeof(structv);counts=(structv*)malloc(snum+1)*sizeof(structv);for(i=0;i<pnumi+)(countpi+1.i=i+1;Thread

温馨提示

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

评论

0/150

提交评论