實(shí)驗(yàn)七——線程同步與多線程編程_第1頁(yè)
實(shí)驗(yàn)七——線程同步與多線程編程_第2頁(yè)
實(shí)驗(yàn)七——線程同步與多線程編程_第3頁(yè)
實(shí)驗(yàn)七——線程同步與多線程編程_第4頁(yè)
實(shí)驗(yàn)七——線程同步與多線程編程_第5頁(yè)
已閱讀5頁(yè),還剩3頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、實(shí)驗(yàn)七 線程同步與多線程編程實(shí)驗(yàn)?zāi)康?1. 了解系統(tǒng)中線程同步的基本原理。2. 了解和熟悉多線程編程和線程訪問(wèn)控制實(shí)驗(yàn)內(nèi)容及步驟:1. 生產(chǎn)者和消費(fèi)者問(wèn)題: 截圖和分析源程序:程序代碼注釋:#include #include #include #include #include #include #include #include #include #include #define FIFO myfifo#define N 5int lock_var;time_t end_time;char buf_r100; /定義 buf_r 數(shù)組表示緩沖區(qū)sem_t mutex,full,empty;/

2、 / 互斥信號(hào)量 mutex, 信號(hào)量 empty 表示緩沖池中空緩 沖區(qū)數(shù)量, full 表示滿緩沖區(qū)數(shù)量int fd;void producer(void *arg);void consumer(void *arg);int main(int argc,char *argv)pthread_t id1,id2;pthread_t mon_th_id;int ret;end_time=time(NULL)+10;/*create a named pipe*/創(chuàng)建管道 if(mkfifo(FIFO,0777|O_CREAT)0)&(errno!=EEXIST) printf(cannot cr

3、eat fifoservern); printf(Preparing for reading bytesn); memset(buf_r,0,sizeof(buf_r);/*open the pipe*/打開管道 fd=open(FIFO,O_RDWR|O_NONBLOCK,0); if(fd=-1) perror(open); exit(1);/*initialize the mutex to 1*/初始化互斥信號(hào)量和 empty,full 緩沖區(qū) ret=sem_init(&mutex,0,1); ret=sem_init(&empty,0,N); ret=sem_init(&full,0

4、,0);if(ret!=0) perror(sem_init);ret=pthread_create(&id1,NULL,(void*)producer,NULL);/創(chuàng)建 producer線程,創(chuàng)建成功返回 0if(ret!=0) perror(pthread create1);ret=pthread_create(&id2,NULL,(void*)producer,NULL); / 創(chuàng) 建 consumer 線程,創(chuàng)建成功返回 0if(ret!=0) perror(pthread create2);pthread_join(id1,NULL); /等待 producer 線程結(jié)束 pthr

5、ead_join(id2,NULL); /等待 consumer 線程結(jié)束 exit(0);void producer(void *arg)int i,nwrite; while(time(NULL)end_time) sem_wait(&empty); / 空緩沖區(qū)的信號(hào)量值減 1 sem_wait(&mutex); / 互斥信號(hào)量值減 1 變?yōu)?0,對(duì)資源鎖定 if(nwrite=write(fd,hello,5)=-1)if(errno=EAGAIN) printf(The FIFO has not been read yet, please try latern); elseprint

6、f(write hello to the FIFOn);sem_post(&mutex); / 互斥信號(hào)量值加 1 變回 1,釋放資源 sem_post(&full); / 滿緩沖區(qū)的信號(hào)量值加 1 sleep(1);void consumer(void *arg)int nwrite;while(time(NULL)end_time)sem_wait(&full); /滿緩沖區(qū)的信號(hào)量值減 1 sem_wait(&mutex); /互斥信號(hào)量值減 1 變?yōu)?0,對(duì)資源鎖定 if(nwrite=write(fd,hello,5)=-1)if(errno=EAGAIN)printf(The FI

7、FO has not been read yet, pleasetry latern);elseprintf(read hello to the FIFOn);sem_post(&mutex); /互斥信號(hào)量值加 1 變回 1sem_post(&empty); /空緩沖區(qū)的信號(hào)量值加 1,對(duì)資源釋放 sleep(1);程序分析:主函數(shù)的主要功能是創(chuàng)建管道,然后初始化緩沖區(qū)buf_r,之后再打開管道,初始化互斥信號(hào)量和 empty,full 緩沖區(qū)。之后再分別創(chuàng)建 producer線程和consumer線程。producer線程:先給空緩沖區(qū)的信號(hào)量值減1,互斥信號(hào)量值減1變?yōu)?,鎖定資源,打印

8、出“ write hello to the FIFO”。然后互斥信號(hào)量值加 1 變回 1,滿緩沖區(qū)的信號(hào)量值加 1。consumer線程:先給滿緩沖區(qū)的信號(hào)量值減1,互斥信號(hào)量值減1變?yōu)?,釋放資源,打印出“ read hello to the FIFO”。然后互斥信號(hào)量值加 1 變回 1,空緩沖區(qū)的信號(hào)量值加 1。 Consumer 函數(shù)代碼:int nwrite;while(time(NULL)end_time)sem_wait(&full);sem_wait(&mutex);if(nwrite=write(fd,hello,5)=-1)if(errno=EAGAIN)prin tf(Th

9、e FIFO has not bee n read yet, please try later n);elseprin tf(read hello to the FIFOn);sem_post (&m utex);sem_post (&empty); sleep(1); 將 red=pthread_create 中參數(shù)由 consumer 改為 producer ,運(yùn)行程序并分析結(jié)果:g J5B. 30*01 - SecureCRTL |D fx刪選頂 榷輔(T)TAO.)罄購(gòu)GOI 192 163 30 filmt wiile.ihile(Tins WLL) end_t ime)jeicva

10、it (4full;ssn_vait (ftnutei).if: (write=write f dj hello, 6) )=)if (errnc=EAGiIIpp r mtf (The FIFO hag not begh re ad yetj please try late r r/:1iviseprjnif匸冃箱“片1舍】1門 tn Thf; FTFOIt/)yeTi_pns+ l.&nutex)seni_pn3+ A pwpty)Q):1r jiiiGhrnJ, :新9TLj 29T7C 已寫人12Olway-Virtual-Machine: J :gcc 一0 jmchengl jin

11、chenS. c -Iplbread1201ftual-Machine: J ./j incheng3Freparing for reading bytesvrit ehel 丄。tqitheFIFOvrit ehe丄丄。totheFIFOvrit ehellototheFIFOvrit ehellototheFIFOvrit ehellototheFIFO就錯(cuò)泛血;ftZS-ZK 2T j j NT行” 91列 W100遨于分析原因:當(dāng)線程2也改成執(zhí)行生產(chǎn)者函數(shù)時(shí),整個(gè)程序就沒有消費(fèi)者線程了,而空緩沖池的最大容量N我們定義了 5,所以,在輸出五次“ write hello to the FI

12、FO ”之后,緩沖池已滿,而同時(shí)沒有消費(fèi)者線程去清空,就不再執(zhí)行生產(chǎn)者線程了,所以,最后程序的執(zhí)行結(jié)果就是打印出五句“ write hello to the FIFO2. 進(jìn)程,線程綜合運(yùn)用:程序分析:1.為程序給出注釋:#include #include int value=0;void *runner(void *param);*runner函數(shù)聲明int main(int argc, char* argv)int pid;pthread_t tid; /定義一個(gè)線程標(biāo)識(shí)符為tid pthread_attr_t attr; / 設(shè)定線程屬性 pthread_attr_t 的變量為 attr

13、 pid=fork(); / 為父進(jìn)程 fork 一個(gè)子進(jìn)程,返回值為 pid if(pid=0) / 子進(jìn)程的執(zhí)行內(nèi)容pthread_attr_init(&attr); /對(duì)線程屬性變量進(jìn)行初始化,使用默認(rèn)值pthread_create(&tid,&attr,runner,NULL); /創(chuàng)建一個(gè)標(biāo)識(shí)符 為 tid ,線程屬性為默認(rèn)值,線程運(yùn)行函數(shù)起始地址為 runner 的 線程pthread_join(tid,NULL); /使子進(jìn)程一直等待剛創(chuàng)建的 tid 線程運(yùn)行結(jié)束,才開始繼續(xù)運(yùn)行printf(CHILD:value=%dn,value);else if(pid0) /父進(jìn)程的執(zhí)行內(nèi)容wait(NULL); printf(PARENT:value=%dn,value);void *runner(void *param) / 自定義一個(gè)線程運(yùn)行函數(shù),起始地址為 runnervalue=5;pthread_exit(0);2. 程序運(yùn)行結(jié)果分析:程序開始賦值給 value 為 0,而 pid 線進(jìn)程的運(yùn)行函數(shù) runner 任務(wù)是給 value 賦值為 5。之后在父進(jìn)程中 fork 一個(gè)子進(jìn)程,而子進(jìn)程執(zhí)行的內(nèi)容是:創(chuàng)建了 pid線程,用pthread_join()等待pid線程運(yùn)行結(jié)束。所以這時(shí)候,value值就變?yōu)榱?5所以程序先輸

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論