操作系統(tǒng)生產(chǎn)者消費者問題C語言_第1頁
操作系統(tǒng)生產(chǎn)者消費者問題C語言_第2頁
操作系統(tǒng)生產(chǎn)者消費者問題C語言_第3頁
操作系統(tǒng)生產(chǎn)者消費者問題C語言_第4頁
操作系統(tǒng)生產(chǎn)者消費者問題C語言_第5頁
已閱讀5頁,還剩6頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

《操作系統(tǒng)概念》第七版中的實驗項目:生產(chǎn)者消費者問題。本程序中,main()函數(shù)需要三個參數(shù):主線程休眠時間;生產(chǎn)者線程數(shù);消費者線程數(shù)。各線程的休眠等待時間是隨機的。程序代碼:#include〈stdioh>#〉o#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,data);<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'rt12873By3consnmcrconsuniccl31438B

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論