版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
1、操作系統(tǒng)概念第七版中的實(shí)驗(yàn)項(xiàng)目:生產(chǎn)者消費(fèi)者問題。本程序中,main() 函數(shù)需要三個(gè)參數(shù):主線程休眠時(shí)間;生產(chǎn)者線程數(shù);消費(fèi)者線程數(shù)。各線程的休眠等待時(shí)間是隨機(jī)的。程序代碼:#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)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年度專業(yè)演出場地租賃及活動(dòng)策劃服務(wù)協(xié)議3篇
- 2025年度碼頭貨物短途運(yùn)輸及環(huán)保處理服務(wù)合同4篇
- 2024-2025學(xué)年高中歷史第五單元近現(xiàn)代中國的先進(jìn)思想第20課西學(xué)東漸課后習(xí)題含解析岳麓版必修3
- 二零二五版生態(tài)修復(fù)工程承攬合同模板-施工與生態(tài)保護(hù)2篇
- 2025年度門衛(wèi)人員安全教育與聘用合同
- 2024版派遣員工合同樣本2篇
- 2025版高端商務(wù)辦公空間租賃合同4篇
- 2024碼頭場地租賃合同
- 2024版天然氣安全運(yùn)輸合同
- 2024鐵路旅客運(yùn)輸服務(wù)質(zhì)量監(jiān)督合同3篇
- 運(yùn)輸供應(yīng)商年度評價(jià)表
- 機(jī)械點(diǎn)檢員職業(yè)技能知識(shí)考試題庫與答案(900題)
- 成熙高級(jí)英語聽力腳本
- 北京語言大學(xué)保衛(wèi)處管理崗位工作人員招考聘用【共500題附答案解析】模擬試卷
- 肺癌的診治指南課件
- 人教版七年級(jí)下冊數(shù)學(xué)全冊完整版課件
- 商場裝修改造施工組織設(shè)計(jì)
- (中職)Dreamweaver-CC網(wǎng)頁設(shè)計(jì)與制作(3版)電子課件(完整版)
- 統(tǒng)編版一年級(jí)語文上冊 第5單元教材解讀 PPT
- 加減乘除混合運(yùn)算600題直接打印
- ASCO7000系列GROUP5控制盤使用手冊
評論
0/150
提交評論