版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
《操作系統(tǒng)概念》第七版中的實驗項目:生產者消費者問題。本程序中,main()函數(shù)需要三個參數(shù):主線程休眠時間;生產者線程數(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)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 刷牙漱口課程設計
- 企業(yè)品牌故事講述與傳播策略研究
- 社區(qū)健康教育提高居民健康素養(yǎng)
- 企業(yè)辦公空間規(guī)劃設計全解匯報
- 城市可持續(xù)發(fā)展規(guī)劃案例分享
- 夾具課程設計花鍵
- AI智能語音交互技術
- 微機原理課程設計閃爍燈
- 青島恒星科技學院《服務營銷》2023-2024學年第一學期期末試卷
- 大唐三絕課程設計
- 高滲性昏迷護理查房
- 江西省-三新-協(xié)同教研共同體2023-2024學年高二上學期12月聯(lián)考英語試卷
- 《SolidWorks建模實例教程》第4章 綜合應用實例
- 2024上海高校大學《輔導員》招聘考試題庫及答案
- 大班-科學語言-塑料瓶到哪里去了?-課件
- 三層電梯PLC控制系統(tǒng)設計(S7-200)含程序
- 幼兒園玩具教具投標方案(技術標)
- 文印服務投標方案(技術方案)
- 消防控制室值班服務投標方案
- 語文部編版九年級語文上冊練習題-口語交際與綜合性學習含答案50
- 直播合法授權書范本
評論
0/150
提交評論