生产者消费者问题C语言

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

《操作系统概念》第七版中的实验项目:生产者消费者问题。本程序中,main()函数需要三个参数:主线程休眠时间;生产者线程数;消费者线程数。各线程的休眠等待时间是随机的。程序代码:#includestdio.h#includestdlib.h#includetime.h#includewindows.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){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%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;int*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=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=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;ipnum;i++){countp[i+1].i=i+1;ThreadHandleP[i]=CreateThread(NULL,0,producer,&countp[i+1],0,&ThreadIdP[i]);}/*Createconsumerthread(s)*/for(i=0;isnum;i++){counts[i+1].i=i+1;ThreadHandleS[i]=CreateThread(NULL,0,consumer,&counts[i+1],0,&ThreadIdS[i]);}/*Sleep*/Sleep(sleeptime);/*Exit*/return0;}#includestdafx.h#includestdio.h#includestdlib.h#includetime.h#includewindows.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){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%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=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(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;ipnum;i++){countp[i+1].i=i+1;ThreadHandleP[i]=CreateThread(NULL,0,producer,&countp[i+1],0,&ThreadIdP[i]);}/*Createconsumerthread(s)*/for(i=0;isnum;i++){counts[i+1].i=i+1;Thr

1 / 6
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功