版權(quán)說(shuō)明:本文檔由用戶(hù)提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
《操作系統(tǒng)概念》第七版中的實(shí)驗(yàn)項(xiàng)目:生產(chǎn)者消費(fèi)者問(wèn)題。本程序中,main()函數(shù)需要三個(gè)參數(shù):主線程休眠時(shí)間;生產(chǎn)者線程數(shù);消費(fèi)者線程數(shù)。各線程的休眠等待時(shí)間是隨機(jī)的。程序代碼:#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. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 第1單元 古代亞非文明(高頻非選擇題25題)(原卷版)
- 《波蘭歪屋設(shè)計(jì)》課件
- 《證券市場(chǎng)概述周》課件
- 玩具設(shè)計(jì)美工工作總結(jié)
- 2023-2024年項(xiàng)目管理人員安全培訓(xùn)考試題帶答案(黃金題型)
- 關(guān)于認(rèn)識(shí)實(shí)習(xí)報(bào)告匯編六篇
- 《系統(tǒng)安全評(píng)價(jià)概述》課件
- 《婦產(chǎn)科學(xué)緒論》課件
- 《監(jiān)理工作程序》課件
- 《應(yīng)用開(kāi)發(fā)和管理》課件
- 青島市2022-2023學(xué)年七年級(jí)上學(xué)期期末道德與法治試題
- 高空作業(yè)安全免責(zé)協(xié)議書(shū)范本
- 石油化學(xué)智慧樹(shù)知到期末考試答案章節(jié)答案2024年中國(guó)石油大學(xué)(華東)
- 手術(shù)后如何防止排尿困難
- 特種設(shè)備“日管控、周排查、月調(diào)度”表格
- 重點(diǎn)關(guān)愛(ài)學(xué)生幫扶活動(dòng)記錄表
- 2021年10月自考00850廣告設(shè)計(jì)基礎(chǔ)試題及答案含解析
- 結(jié)構(gòu)化面試表格
- 地?zé)崮苜Y源的潛力及在能源領(lǐng)域中的應(yīng)用前景
- 2023版:美國(guó)眼科學(xué)會(huì)青光眼治療指南(全文)
- 家長(zhǎng)會(huì)課件:小學(xué)寒假家長(zhǎng)會(huì)課件
評(píng)論
0/150
提交評(píng)論