操作系統(tǒng)生產(chǎn)者消費(fèi)者問題C語言_第1頁
操作系統(tǒng)生產(chǎn)者消費(fèi)者問題C語言_第2頁
操作系統(tǒng)生產(chǎn)者消費(fèi)者問題C語言_第3頁
已閱讀5頁,還剩2頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(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)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論