版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領
文檔簡介
《操作系統(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)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2026年江西交通職業(yè)技術學院單招綜合素質(zhì)筆試參考題庫含詳細答案解析
- 2026年北京科技大學天津?qū)W院高職單招職業(yè)適應性測試備考題庫及答案詳細解析
- 2026年云南交通運輸職業(yè)學院單招綜合素質(zhì)考試備考題庫含詳細答案解析
- 2026年上海電力大學單招綜合素質(zhì)筆試參考題庫含詳細答案解析
- 2026年安徽工業(yè)經(jīng)濟職業(yè)技術學院單招綜合素質(zhì)筆試參考題庫含詳細答案解析
- 2026年內(nèi)蒙古美術職業(yè)學院單招綜合素質(zhì)筆試備考題庫含詳細答案解析
- 2026年鄭州商貿(mào)旅游職業(yè)學院高職單招職業(yè)適應性測試備考題庫及答案詳細解析
- 2026年天津機電職業(yè)技術學院單招綜合素質(zhì)考試備考試題含詳細答案解析
- 2026年江西司法警官職業(yè)學院單招職業(yè)技能考試備考題庫含詳細答案解析
- 2026年蚌埠經(jīng)濟技術職業(yè)學院單招職業(yè)技能考試模擬試題含詳細答案解析
- 《二氧化碳陸地封存工程地質(zhì)條件適宜性評價及選址指南》
- 《降低輸液外滲率》課件
- 治療性低溫技術臨床應用進展
- 住院醫(yī)師規(guī)范化培訓內(nèi)容與標準(2022年版)-骨科培訓細則
- GB/T 16288-2024塑料制品的標志
- 2024-2025學年人教版小升初英語試卷及解答參考
- 質(zhì)量信得過班組匯報材料
- 醫(yī)學倫理學案例分析
- 新概念英語第一冊階段測試題
- 金融科技對商業(yè)銀行業(yè)務的影響研究
- 寒假輔導班招生方案
評論
0/150
提交評論