版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
1、班級 姓名: 學(xué)號: 成績: 實驗名稱: 生產(chǎn)者和消費者問題1.實驗?zāi)康模骸吧a(chǎn)者消費者”問題是一個著名的同時性編程問題的集合。通過編寫經(jīng)典的“生產(chǎn)者消費者”問題的實驗,讀者可以進(jìn)一步熟悉Linux 中多線程編程,并且掌握用信號量處理線程間的同步互斥問題。2.實驗內(nèi)容:“生產(chǎn)者消費者”問題描述如下。有一個有限緩沖區(qū)和兩個線程:生產(chǎn)者和消費者。他們分別把產(chǎn)品放入緩沖區(qū)和從緩沖區(qū)中拿走產(chǎn)品。當(dāng)一個生產(chǎn)者在緩沖區(qū)滿時必須等待,當(dāng)一個消費者在緩沖區(qū)空時也必須等待。它們之間的關(guān)系如下圖所示:這里要求用有名管道來模擬有限緩沖區(qū),用信號量來解決生產(chǎn)者消費者問題中的同步和互斥問題。3.實驗方法:(1)使用信號
2、量解決(2)思考使用條件變量解決4.實驗過程(1)信號量的考慮這里使用3個信號量,其中兩個信號量avail和full分別用于解決生產(chǎn)者和消費者線程之間的同步問題,mutex是用于這兩個線程之間的互斥問題。其中avail初始化為N(有界緩沖區(qū)的空單元數(shù)),mutex 初始化為1,full初始化為0。/*product.c*/#include #include #include #include #include #include #include #include #define FIFO myfifo#define N 5int lock_var;time_t end_time;char bu
3、f_r100;sem_t mutex,full,avail;int fd;void pthread1(void *arg);void pthread2(void *arg);int main(int argc, char *argv)pthread_t id1,id2;pthread_t mon_th_id;int ret;end_time = time(NULL)+30;/*創(chuàng)建有名管道*/if(mkfifo(FIFO,O_CREAT|O_EXCL)0)&(errno!=EEXIST)printf(cannot create fifoservern);printf(Preparing for
4、 reading bytes.n);memset(buf_r,0,sizeof(buf_r);/*打開管道*/fd=open(FIFO,O_RDWR|O_NONBLOCK,0);if(fd=-1)perror(open);exit(1);/*初始化互斥信號量為1*/ret=sem_init(&mutex,0,1);/*初始化avail信號量為N*/ret=sem_init(&avail,0,N);/*初始化full信號量為0*/ret=sem_init(&full,0,0);if(ret!=0)perror(sem_init);/*創(chuàng)建兩個線程*/ret=pthread_create(&id1
5、,NULL,(void *)productor, NULL);if(ret!=0)perror(pthread cread1);ret=pthread_create(&id2,NULL,(void *)consumer, NULL);if(ret!=0)perror(pthread cread2);pthread_join(id1,NULL);pthread_join(id2,NULL);exit(0);/*生產(chǎn)者線程*/void productor(void *arg)int i,nwrite;while(time(NULL) end_time)/*P操作信號量avail和mutex*/se
6、m_wait(&avail);sem_wait(&mutex);/*生產(chǎn)者寫入數(shù)據(jù)*/if(nwrite=write(fd,hello,5)=-1)if(errno=EAGAIN)printf(The FIFO has not been read yet.Please try latern);elseprintf(write hello to the FIFOn);/*V操作信號量full和mutex*/sem_post(&full);sem_post(&mutex);sleep(1);/*消費者線程*/void consumer(void *arg)int nolock=0;int ret,
7、nread;while(time(NULL) end_time)/*P操作信號量full和mutex*/sem_wait(&full);sem_wait(&mutex);memset(buf_r,0,sizeof(buf_r);if(nread=read(fd,buf_r,100)=-1)if(errno=EAGAIN)printf(no data yetn);printf(read %s from FIFOn,buf_r);/*V操作信號量avail和mutex*/sem_post(&avail);sem_post(&mutex);sleep(1);(2)條件變量的考慮#include #i
8、nclude #define BUFFER_SIZE 4#define OVER (-1)struct producers/定義生產(chǎn)者條件變量結(jié)構(gòu) int bufferBUFFER_SIZE; pthread_mutex_t lock; int readpos, writepos; pthread_cond_t notempty; pthread_cond_t notfull;/初始化緩沖區(qū)void init(struct producers *b) pthread_mutex_init(&b-lock,NULL); pthread_cond_init(&b-notempty,NULL); p
9、thread_cond_init(&b-notfull,NULL); b-readpos=0; b-writepos=0;/在緩沖區(qū)存放一個整數(shù)void put(struct producers *b, int data) pthread_mutex_lock(&b-lock);/當(dāng)緩沖區(qū)為滿時等待 while(b-writepos+1)%BUFFER_SIZE=b-readpos) pthread_cond_wait(&b-notfull,&b-lock); b-bufferb-writepos=data; b-writepos+; if(b-writepos=BUFFER_SIZE) b-
10、writepos=0;/發(fā)送當(dāng)前緩沖區(qū)中有數(shù)據(jù)的信號 pthread_cond_signal(&b-notempty); pthread_mutex_unlock(&b-lock);int get(struct producers *b) int data; pthread_mutex_lock(&b-lock); while(b-writepos=b-readpos) pthread_cond_wait(&b-notempty,&b-lock); data=b-bufferb-readpos; b-readpos+; if(b-readpos=BUFFER_SIZE) b-readpos=0
11、; pthread_cond_signal(&b-notfull); pthread_mutex_unlock(&b-lock); return data;struct producers buffer;void *producer(void *data) int n; for(n=0;nn,n); put(&buffer,n); put(&buffer,OVER); return NULL;void *consumer(void *data) int d; while(1) d=get(&buffer); if(d=OVER) break; printf(Consumer: - %dn,d); return NULL;int main() pthread_t tha
溫馨提示
- 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)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2026年山西藥科職業(yè)學(xué)院單招職業(yè)技能筆試模擬試題帶答案解析
- Unit 1 Making friends 公開課一等獎創(chuàng)新教案 人教PEP英語三年級上冊
- 滬教牛津 八年級上Unit 8 English Week 詞匯講義練習(xí) 語法講義(教師版+學(xué)生版)
- Unit 2 My friends 公開課一等獎創(chuàng)新教案(7個課時 含反思)
- 病句題目技巧及答案
- 2026年智能腹肌訓(xùn)練器項目公司成立分析報告
- 護(hù)士資格題目及答案
- 2026年智能水牙線項目公司成立分析報告
- 門崗工作時間協(xié)議書
- 精神病協(xié)議書離婚
- 《好睡新的睡眠科學(xué)與醫(yī)學(xué)》閱讀筆記
- GB 20101-2025涂裝有機(jī)廢氣凈化裝置安全技術(shù)要求
- 熔鋁爐施工方案及流程
- 折彎工技能等級評定標(biāo)準(zhǔn)
- 全屋定制家具合同
- 2025年私人銀行行業(yè)分析報告及未來發(fā)展趨勢預(yù)測
- (正式版)DB32∕T 5179-2025 《智能建筑工程檢測與施工質(zhì)量驗收規(guī)程》
- 國際道路運輸安全生產(chǎn)管理制度文本
- 輝綠巖粉的用途
- 食堂消防安全制度培訓(xùn)課件
- 2025-2030房地產(chǎn)行業(yè)人才結(jié)構(gòu)轉(zhuǎn)型與復(fù)合型培養(yǎng)體系構(gòu)建
評論
0/150
提交評論