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

下载本文档

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

文档简介

《操作系统概念》第七版中的实验项目:生产者消费者问题。本程序中,main()函数需要三个参数:主线程休眠时间;生产者线程数;消费者线程数。各线程的休眠等待时间是随机的。程序代码:#include〈stdioh>include<stdloilh〉include<timeh>#include〈windows.h>#defineBUFFER_SIZE5typedefintbuffer_;itemstructv{int;i);buffer_itembuffer[BUFFER_SIZE+1buffer_itemfront=0,re;ar=0HANDLEmutex,empty,full;intinsert_i(ebuffer_itemitem){/*insertitemintobufferreturn0ifsuccessful,otherwisereturn-1indicatinganerrorcond/tionif((rear+1弘(BUFFER_SIZE+1)==front)return1;buffer[rear=itemrear=(rear+1)%(BUFFER_SIZE+1);return0;}intremove_item(buffer_item*item){/*removeanobjectfrombufferplacingitinitemreturn0ifsuccessful,otherwisereutrn-1indicationanerrorcondition*/if(front==r)earreturn;1*item=buffeffront];front=(front+)%(BUFFER_SIZE+1);return0;DWORDWINAPIproducer(PVOIDParam){intrandlstructvdata=*(struct)Waram;srand((unsigned)time0));while(1{Sleep(rand()%101*10WaitForSingleObjectempty,INFINITE);WaitForSingleObject(mutexINFINITE);rand1=ran();printf"producerhasproducerd%dByn%drand1datai);if(insert_itemrand1))printf("insertdatan’rrb;r!\ReleaseMutex(mutex)ReleaseSemaphore(full,NULL);}}DWORDWINAPIconsumer(PVOIDParam){intrand1;structvdat<a=structv)Param;srand((unsign)edtime(0));while(1){Sleep(rand()%101*10);WaitForSingleObject(f,ulIlNFINITE);WaitForSingleObjectmutex,INFINITE);if(remove_item&rand1))printf("removedataerr\f,);elseprintf"consumerconsumed^dBy%d\nn,rand1,d<ata);ReleaseMutex(mutex);ReleaseSemaphore(empty,1,NULL);}}intmain(intargxhar*argv[]{/*Getcommandlineargumentsargv)1(thenumberofproducerthreads),argtAh/numberofconsumerthread,sargv[3](sleepti)e*//*Initializebufferintsleeptime,pnun$num;int*ThreadIdP*ThreadIdS,i;struct*countp*counts;HANDLE*ThreadHandleP,*ThreadHandleS;sleeptime=atoi(argv)[[]pnum=atoi(argv[2]);snum=atoi(argv[3]);/*srand(time(NULL));sleeptime=9;0pnum=3;snum=3;*/ThreadHandleP=(HANDLE*)malloc(pnum*sizeof(HANDLE));ThreadHandleS=(HANDLE*)malloc(snum*sizeof(HANDLE));ThreadIdP=(int*)mallOpnum*sizeofin));ThreadIdS=(int*)malloc(pnum*sizeoffint));mutex=CreateMutex(NULL,FALSE,NULL);empty=CreateSemaphore(NULL,BUFFER_SIZE,BUFFER_SIZE,NULL);full=CreateSemaphore(NULL0,BUFFER_SIZE+1,NULL);/*Createproducerthread(s)countp=(structv)malloc((pnum+1)*sizeof(struqtv))counts=(structv*)malloc((sn)um*+1sizeo(fstruct))v;for(i=0i<pnum;i++){countp[i+L]i=i+1ThreadHandleP[i]=CreateThreaNULL,0,producer&countpi+1],0,&ThreadIdP[i;])}/*Createconsumerthread(s)*/for(i=0;i〈snum;i++){counts[i+1]。i=i+1;ThreadHandleS[i]=CreateThreaNULL,0,consumer,&count[i+1],0,&ThreadIdS[i])}/*Sleep*/Sleep(sleepti)m;e/*Exi*t/return0;include"stdafx.h"#include〈stdio。h〉#include〈stdlib.h>include〈time。h>#include<windows。h>#defineBUFFER-SIZE5typedefintbuffer-item;structv{inti;);buffer-itembuffer[BUFFER-SIZE+1];buffer-itemfront=0,rear=0;HANDLEmutex,empty,full;intinsert-item(buffer-itemitem)(/*insertitemintobufferreturn0ifsuccessful,otherwisereturn-1indicatinganerrorcondition*/if((rear+1)%(BUFFER-SIZE+1)==front)return1;buffer[rear]=item;rear=(rear+1)%(BUFFER-SIZE+1);return0;}intremove-item(buffer-item*item){/*removeanobjectfrombufferplacingitinitemreturn0ifsuccessful,otherwisereutrn-1indicationanerrorcondition*/if(front==rear)return1;*item=buffer[front];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,INFINITE);WaitForSingleObject(mutex,INFINITE);rand1=rand();printf("producerhasproducerd%dBy%d\n”,rand1,data.i);if(insert_item(rand1))printf("insertdataerror!\n");ReleaseMutex(mutex);ReleaseSemaphore(full,1,NULL);})DWORDWINAPIconsumer(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("removedataerror!\n");elseprintf("consumerconsumed%dBy%d\n",rand1,data。i);ReleaseMutex(mutex);ReleaseSemaphore(empty,1,NULL);)}intmain(intargc,char*argv[])(/*Getcommandlineargumentsargv[1])(thenumberofproducerthreads),argv[2](thenumberofconsumerthreads),argv[3](sleeptime)*//*Initializebuffer*/intsleeptime,pnum,snum;DWORD*ThreadIdP,*ThreadIdS,i;structv*countp,*counts;HANDLE*ThreadHandleP,*ThreadHandleS;/*sleeptime=atoi(argv[1]);pnum=atoi(argv[2]);snum=atoi(argv[3]);*///srand(time(NULL));sleeptime=90;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(DWORD));mutex=CreateMutex(NULL,FALSE,NULL);empty=CreateSemaphore(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++){countp[i+1].i=i+1;ThreadHandleP[i]=CreateThread(NULL,0,producer,&countp[i+1],0,&ThreadIdP[i]);)/*Createconsumerthread(s)*/for(i=0;i〈snum;i++){counts[i+1].i=i+1;ThreadHandleS[i]=CreateThread(NULL,0,consumer,&counts[i+1],0,&ThreadIdS[i]);)/*Sleep*/Sleep(sleeptime);/*Exit*/return0;consitmepconBunecl32240By2pi'oclucephasprortucei'rt12873By3consnmcrconsuniccl31438By1vi'QducepJiasproclueepel17194ByconsitmepconBunecl32240By2pi'oclucephasprortucei'rt12873By3consnmcrconsuniccl3143

温馨提示

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

评论

0/150

提交评论