


版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、操作系統(tǒng)概念第七版中的實驗項目:生產(chǎn)者消費者問題。本程序中,main() 函數(shù)需要三個參數(shù):主線程休眠時間;生產(chǎn)者線程數(shù);消費者線程數(shù)。各線程的休眠等待時間是隨機的。程序代碼:#include #include #include #include #define buffer_size 5 typedef int buffer_item; struct v ( int i; ;buffer_item bufferbuffer_size+1; buffer_item front=0,rear=0; handle mutex,empty,full; int insert_item(buffer_i
2、tem item) ( /*insert item into buffer return 0 if successful,otherwise return -1 indicating an error condition*/ if(rear+1)%(buffer_size+1)=front) return 1; bufferrear=item; rear=(rear+1)%(buffer_size+1); return 0; int remove_item(buffer_item *item) ( /*remove an object from buffer placing it in ite
3、m return 0 if successful,otherwise reutrn -1 indication an error condition */ if(front = rear) return 1; *item=bufferfront; front=(front+1) % (buffer_size+1); return 0; dword winapi producer(pvoid param) ( int rand1; struct v data=*(struct v *)param; srand(unsigned)time(0); while (1) ( sleep(rand()%
4、101*10); waitforsingleobject(empty,infinite); waitforsingleobject(mutex,infinite); randl =rand(); n”,rand1,data.i);if(insert_item(rand1) printf(insert data error!n); releasemutex(mutex); releasesemaphore(full,1,null); dword winapi consumer(pvoid param) ( int rand1; struct v data=*(struct v *)param;
5、srand(unsigned)time(0); while (1) ( sleep(rand()%101*10); waitforsingleobject(full,infinite); waitforsingleobject(mutex,infinite); if(remove_item(&rand1) printf(remove data error! n); else printf(consumer consumed %d by %d n,rand1,data.i); releasemutex(mutex); releasesemaphore(empty,1,null); int
6、 main(int argc,char *argv) ( /*get command line arguments argv1)(the number of producer threads),argv2(the number of consumer threads),argv3(sleep time)*/ /*initialize buffer*/ int sleeptime,pnum,snum; int *threadidp,*threadids,i; struct v *countp,*counts; handle *threadhandlep,*threadhandles; sleep
7、time=atoi(argv1); pnum=atoi(argv2); snum=atoi(argv3); /*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 * sizeo
8、f(int); mutex=createmutex(null,false,null); empty=createsemaphore(null,buffer_size,buffer_size,null); full=createsemaphore(null,0,buffer_size+1,null); /*create producer thread(s)*/ countp=(struct v *)malloc(pnum+1)*sizeof(struct v); counts=(struct v *)malloc(snum+1)*sizeof(struct v); for(i=0;ipnum;i
9、+) ( countpi+1.i=i+1; threadhandlepi=createthread(null,0,producer,&countpi+1,0,&threadidpi); /*create consumer thread(s)*/ for(i=0;isnum;i+) ( countsi+1.i=i+1; threadhandlesi=createthread(null,0,consumer,&countsi+1,0,&threadidsi); /*sleep*/ sleep(sleeptime); /*exit*/ return 0; #inclu
10、de stdafx.h #include #include #include #include #define buffer size5 typedef int buffer_item struct v int i ; ; buffer_item buffer buffer_size1; buffer_item front =0, rear =0; handlemutex, empty, full ; int insert_item ( buffer_item item ) /*insert item into buffer return 0 if successful,otherwise r
11、eturn -1 indicating an error condition*/ if ( rear +1)%(buffer_size1)= front ) return 1; buffer rear = item ; rear =( rear +1)%(buffer_siz+1); return 0; int remove_item ( buffer_item * item ) /*remove an object from buffer placing it in item return 0 if successful,otherwise reutrn -1 indication an e
12、rror condition */ if (front = rear ) return 1; * item =buffer front ; front =(front +1) % ( buffer_siz+1); return 0; dworwinapi producer (pvoid param) int rand1 ; struct v data =*( struct v *) param; srand ( unsigned )time (0); while (1) ( sleep( rand ()%101*10); waitforsingleobject ( empty, infinit
13、e ); waitforsingleobject ( mutex, infinite ); rand1 = rand (); printf ( producer has producerd %d by %dn , rand1 , data . i ); if ( insert_item ( rand1 ) printf ( insert data error!n ); releasemutex (mutex); releasesemaphore(full ,1, null; dworwinapi consumer(pvoid param) ( int rand1 ; struct v data
14、 =*( struct v *) param; srand ( unsigned ) time (0); while (1) ( sleep( rand ()%101*10); waitforsingleobject ( full , infinite ); waitforsingleobject ( mutex, infinite ); if ( remove_item (& rand1 ) printf ( remove data error! n ); else printf ( consumer consumed %d by %d n , rand1 , data . i );
15、 releasemutex (mutex); releasesemaphore(empty,1, null); int main( int argc , char * argv ) ( /*get command line arguments argv1)(the number of producer threads),argv2(the number of consumer threads),argv3(sleep time)*/ /*initialize buffer*/ int sleeptime , pnum, snum; dwor*dthreadidp ,* threadids ,
16、i ; struct v * countp ,* counts ; handle threadhandlep ,* threadhandles ; /*sleeptime=atoi(argv1); pnum=atoi(argv2); snum=atoi(argv3);*/ /srand(time(null); sleeptime =9000; pnum=3; snum=3; threadhandlep =(handle* ) malloc (pnum* sizeof (handle; threadhandles =(handle* ) malloc (snum * sizeof (handle
17、; threadidp =(dword) malloc (pnum * sizeof ( dword threadids =(dword) malloc (pnum * sizeof ( dwofrd mutex=createmutex (null false null; empty=createsemaphore( null buffer_sizebuffer_sizenull; full =createsemaphore( null。,buffer_siz+1, null; /*create producer thread(s)*/ countp =( struct v *) malloc ( pnum+1)* sizeof (struct v); counts =( struct v *) malloc ( snum+1)* sizeof (struct v); for (i =0; i pnum i +) ( countp i +1. i =i +1; threadhandlep i = createthread
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年度足浴店店面轉(zhuǎn)讓及特色項目開發(fā)合同
- 小學(xué)英語哪個試卷難
- 2025年度智能倉儲物流中心廠房產(chǎn)權(quán)轉(zhuǎn)讓協(xié)議
- 二零二五年度城市貨運司機責(zé)任與權(quán)益保障合同
- 二零二五年度特殊崗位員工勞動合同解除經(jīng)濟補償協(xié)議
- 二零二五年度房產(chǎn)中介個人購房傭金結(jié)算標準合同
- 2025年度餐飲業(yè)裝修工程增項及餐飲衛(wèi)生安全合同
- 山東省2025年度新建商品房買賣合同預(yù)售與社區(qū)公共安全協(xié)議
- 二零二五年度常年法律顧問服務(wù)及公司法律事務(wù)全流程管理合同
- 2025年度砂石產(chǎn)品質(zhì)量與售后服務(wù)保障合同
- 畢業(yè)設(shè)計論文-貝類脫殼機設(shè)計
- 八項規(guī)定學(xué)習(xí)課件
- 《工程電磁場》配套教學(xué)課件
- 《過零丁洋》公開課件
- 從生產(chǎn)工藝角度詳解磷酸鐵鋰
- 全套橋梁施工技術(shù)交底記錄
- 《教師職業(yè)道德》全書word版
- 城市定制型商業(yè)醫(yī)療保險(惠民保)知識圖譜
- GB∕T 3836.31-2021 爆炸性環(huán)境 第31部分:由防粉塵點燃外殼“t”保護的設(shè)備
- AMDAR資料的分析和應(yīng)用
- 橋梁缺陷與預(yù)防
評論
0/150
提交評論