




版權(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)一 進(jìn)程與線程Linux 進(jìn)程與線程通訊實(shí)驗(yàn)?zāi)康膶?shí)驗(yàn)內(nèi)容實(shí)驗(yàn)準(zhǔn)備實(shí)驗(yàn)設(shè)計(jì)參考代碼實(shí)驗(yàn)結(jié)果思考題實(shí)驗(yàn)?zāi)康纳羁汤斫饩€程和進(jìn)程的概念,掌握線程與進(jìn)程在組成成分上的差別,以及與其相適應(yīng)的通訊方式和應(yīng)用目標(biāo)。實(shí)驗(yàn)內(nèi)容以Linux系統(tǒng)進(jìn)程和線程機(jī)制為背景,掌握f(shuō)ork()和clone()系統(tǒng)調(diào)用的形式和功能,以及與其相適應(yīng)的高級(jí)通訊方式。由fork派生的子進(jìn)程之間通過(guò)pipe通訊,由clone創(chuàng)建的線程之間通過(guò)共享內(nèi)存通訊,對(duì)于后者需要考慮互斥問(wèn)題。以生產(chǎn)者/消費(fèi)者問(wèn)題為例,通過(guò)實(shí)驗(yàn)理解fork()和clone()兩個(gè)系統(tǒng)調(diào)用的區(qū)別。程序要求能夠創(chuàng)建4個(gè)進(jìn)程或線程,其中包括兩個(gè)生產(chǎn)者和兩個(gè)消費(fèi)者,生產(chǎn)
2、者和消費(fèi)者之間能夠傳遞數(shù)據(jù)。實(shí)驗(yàn)準(zhǔn)備fork系統(tǒng)調(diào)用clone系統(tǒng)調(diào)用pipe系統(tǒng)調(diào)用sem_wait(&s)和sem_post(&s) pthread_mutex_lock(&mutex)和pthread_mutex_unlock(&mutex) fork系統(tǒng)調(diào)用pid=fork()創(chuàng)建一個(gè)子進(jìn)程,子進(jìn)程是父進(jìn)程的完整復(fù)制,正常返回值為非負(fù)整數(shù),對(duì)于父進(jìn)程來(lái)說(shuō)該數(shù)大于0,是子進(jìn)程的編號(hào)(pid);對(duì)于子進(jìn)程來(lái)說(shuō)該數(shù)為0。正是利用反回值的差別可以決定二者不同的后繼動(dòng)作。clone系統(tǒng)調(diào)用int clone (int ( *fn ) (void *arg) , void *stack , int
3、 flag , void *arg) ;其中,fn是輕進(jìn)程所執(zhí)行的函數(shù);stack是輕進(jìn)程所使用的棧;flag是CLONE_VM, CLONE_FS, CLONE_FILES, CLONE_SIGNAND, CLONE_PID的組合;arg是調(diào)用過(guò)程的對(duì)應(yīng)參數(shù)。Clone()的關(guān)鍵是flag的設(shè)定,CLONE_VM表示子進(jìn)程共享父進(jìn)程內(nèi)存,CLONE_FS表示子進(jìn)程共享父進(jìn)程的文件系統(tǒng),CLONE_SIGNAND表示子進(jìn)程共享父進(jìn)程的消息處理機(jī)制,CLONE_PID是指子進(jìn)程繼承父進(jìn)程的id號(hào)。pipe系統(tǒng)調(diào)用ret_val=pipe(fd);參數(shù)定義為int fd2。創(chuàng)建一個(gè)管道文件,返回兩
4、個(gè)文件描述符fd0和fd1分別用于管道文件的讀和寫操作。管道文件創(chuàng)建后,可以被fork創(chuàng)建的子進(jìn)程共享。sem_wait(&s)和sem_post(&s)sem_wait(&s)和sem_post(&s)分別相當(dāng)于信號(hào)燈的P操作和V操作。其中s是說(shuō)明為說(shuō)明為sem_t類型的信號(hào)燈。初始化函數(shù)sem_init(s,0,8)。pthread_mutex_lock(&mutex)和pthread_mutex_unlock(&mutex)pthread_mutex_lock(&mutex)和pthread_mutex_unlock(&mutex)分別用于加鎖和解鎖。參數(shù)為pthread_mutex_t
5、 mutex定義的互斥鎖。初始化tthread_mutex_init(&mutex,NULL)。實(shí)驗(yàn)設(shè)計(jì) 用pipe()創(chuàng)建一個(gè)管道文件,然后用fork()創(chuàng)建兩個(gè)生產(chǎn)進(jìn)程和兩個(gè)消費(fèi)進(jìn)程,它們之間通過(guò)pipe()傳遞信息。用clone()創(chuàng)建四個(gè)輕進(jìn)程(線程),用參數(shù)指明共享內(nèi)存等資源,通過(guò)共享內(nèi)存模擬生產(chǎn)消費(fèi)問(wèn)題,利用pthread_mutex_lock(), pthread_mutex_unlock()等函數(shù)實(shí)現(xiàn)對(duì)共享存儲(chǔ)區(qū)訪問(wèn)的互斥。參考代碼基于fork()系統(tǒng)調(diào)用 基于clone()系統(tǒng)調(diào)用基于fork()系統(tǒng)調(diào)用 #include sys/types.h #include sys/f
6、ile.h #include unistd.h char r_buf4; /讀緩沖 char w_buf4; /寫緩沖 int pipe_fd2; pid_t pid1, pid2, pid3, pid4; int producer(int id); int consumer(int id);int main(int argc,char *argv) if(pipe(pipe_fd)0) printf(pipe create error n); exit(-1);elseprintf(pipe is created successfully!n);if(pid1=fork()=0) produ
7、cer(1);if(pid2=fork()=0) producer(2);if(pid3=fork()=0) consumer(1);if(pid4=fork()=0) consumer(2); close(pipe_fd0); /需要加上這兩句close(pipe_fd1); /否這會(huì)有讀者或者寫者永遠(yuǎn)等待 int i,pid,status;for(i=0;i4;i+) pid=wait(&status); exit(0);int producer(int id) printf(producer %d is running!n,id); close(pipe_fd0); int i=0; f
8、or(i=1;i10;i+) sleep(3); if(id=1) /生產(chǎn)者1 strcpy(w_buf,aaa0); else /生產(chǎn)者2 strcpy(w_buf,bbb0); if(write(pipe_fd1,w_buf,4)=-1) printf(write to pipe errorn); close(pipe_fd1); printf(producer %d is over!n,id); exit(id);int consumer(int id) close(pipe_fd1); printf(producer %d is running!n,id); if (id=1) /消費(fèi)
9、者1strcpy(w_buf,ccc0); else /消費(fèi)者2strcpy(w_buf,ddd0); while(1) sleep(1); strcpy(r_buf,eee0); if(read(pipe_fd0,r_buf,4)=0) break; printf(consumer %d get %s, while the w_buf is %sn,id,r_buf,w_buf); close(pipe_fd0); printf(consumer %d is over!n, id); exit(id);#include sched.h#include pthread.h#include st
10、dio.h#include stdlib.h#include semaphore.hint producer(void * args);int consumer(void *args);pthread_mutex_t mutex;sem_t product;sem_t warehouse;char buffer84;int bp=0;基于clone()系統(tǒng)調(diào)用main(int argc,char* argv) pthread_mutex_init(&mutex,NULL); sem_init(&product,0,0); sem_init(&warehouse,0,8); int clone_
11、flag,arg,retval; char *stack; clone_flag=CLONE_VM|CLONE_SIGNAND|CLONE_FS| CLONE_FILES; int i; for(i=0;i2;i+) /創(chuàng)建四個(gè)線程 arg = i; stack =(char*)malloc(4096); retval=clone(void*)producer,&(stack4095),clone_flag, (void*)&arg); stack =(char*)malloc(4096); retval=clone(void*)consumer,&(stack4095),clone_flag
12、, (void*)&arg); exit(1);int producer(void* args) int id = *(int*)args); int i; for(i=0;i10;i+) sleep(i+1); /表現(xiàn)線程速度差別 sem_wait(&warehouse); pthread_mutex_lock(&mutex); if(id=0) strcpy(bufferbp,aaa0); else strcpy(bufferbp,bbb0); bp+; printf(producer%d produce %s in %dn,id,bufferbp,bp-1); pthread_mutex
13、_unlock(&mutex); sem_post(&product); printf(producer%d is over!n,id);int consumer(void *args) int id = *(int*)args); int i; for(i=0;i10;i+) sleep(10-i); /表現(xiàn)線程速度差別 sem_wait(&product); pthread_mutex_lock(&mutex); bp-; printf(consumer%d get %s in%dn,id,bufferbp,bp+1); strcpy(bufferbp,zzz0); pthread_mut
14、ex_unlock(&mutex); sem_post(&warehouse); printf(consumer%d is over!n,id);實(shí)驗(yàn)結(jié)果 程序(1)的輸出 程序(2)的輸出 結(jié)果分析程序(1)的輸出 pipe is created successfully!producer 1 is running!producer 2 is running!producer 1 is running!producer 2 is running!consumer 2 get aaa, while the w_buf is dddconsumer 1 get bbb, while the w_
15、buf is cccconsumer 1 get aaa, while the w_buf is cccconsumer 2 get bbb, while the w_buf is dddconsumer 2 get aaa, while the w_buf is dddconsumer 1 get bbb, while the w_buf is cccconsumer 1 get aaa, while the w_buf is cccconsumer 2 get bbb, while the w_buf is dddconsumer 2 get aaa, while the w_buf is
16、 dddconsumer 1 get bbb, while the w_buf is cccconsumer 1 get aaa, while the w_buf is cccconsumer 2 get bbb, while the w_buf is dddconsumer 2 get aaa, while the w_buf is dddconsumer 1 get bbb, while the w_buf is cccconsumer 1 get aaa, while the w_buf is cccconsumer 2 get bbb, while the w_buf is dddpr
17、oducer 1 is over!producer 2 is over!consumer 2 get aaa, while the w_buf is dddconsumer 1 get bbb, while the w_buf is cccconsumer 2 is over!consumer 1 is over!程序(2)的輸出 producer0 produce aaa in 0producer1 produce bbb in 1producer0 produce aaa in 2producer1 produce bbb in 3producer0 produce aaa in 4pro
18、ducer1 produce bbb in 5consumer0 get bbb in 5consumer1 get aaa in 4producer0 produce aaa in 4producer1 produce bbb in 5producer0 produce aaa in 6producer1 produce bbb in 7consumer0 get bbb in 7consumer1 get aaa in 6producer0 produce aaa in 6producer1 produce bbb in 7consumer0 get bbb in 7consumer1 g
19、et aaa in 6producer0 produce aaa in 6producer1 produce bbb in 7consumer0 get bbb in 7consumer1 get aaa in 6producer0 produce aaa in 6producer1 produce bbb in 7consumer0 get bbb in 7consumer1 get aaa in 6consumer0 get bbb in 5consumer1 get aaa in 4producer0 produce aaa in 4producer1 produce bbb in 5c
20、onsumer0 get bbb in 5consumer1 get aaa in 4consumer0 get bbb in 3consumer1 get aaa in 2consumer0 get bbb in 1consumer1 get aaa in 0producer0 produce aaa in 0producer0 is over!producer1 produce bbb in 1producer1 is over!consumer0 get bbb in 1consumer0 is over!consumer1 get aaa in 0consumer1 is over!結(jié)果分析程序1結(jié)果分析程序2結(jié)果分析程序(1)由程序(1)結(jié)果
溫馨提示
- 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ù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 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ì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年中國(guó)DVI信號(hào)光傳輸線數(shù)據(jù)監(jiān)測(cè)研究報(bào)告
- 2025年中國(guó)60%玉米芯型氯化膽堿數(shù)據(jù)監(jiān)測(cè)報(bào)告
- 2025年中國(guó)19.5毫米粗紗機(jī)下銷數(shù)據(jù)監(jiān)測(cè)研究報(bào)告
- 2025至2030年中國(guó)高溫陶瓷發(fā)熱器市場(chǎng)分析及競(jìng)爭(zhēng)策略研究報(bào)告
- 2025至2030年中國(guó)長(zhǎng)柄引磬市場(chǎng)分析及競(jìng)爭(zhēng)策略研究報(bào)告
- 2025至2030年中國(guó)金屬鎧裝中置開(kāi)關(guān)柜市場(chǎng)分析及競(jìng)爭(zhēng)策略研究報(bào)告
- 2025至2030年中國(guó)粘固粉調(diào)板市場(chǎng)分析及競(jìng)爭(zhēng)策略研究報(bào)告
- 2025至2030年中國(guó)白椿木刨光料市場(chǎng)分析及競(jìng)爭(zhēng)策略研究報(bào)告
- 2025至2030年中國(guó)海竿漁竿市場(chǎng)分析及競(jìng)爭(zhēng)策略研究報(bào)告
- 2025至2030年中國(guó)無(wú)菌干手器市場(chǎng)分析及競(jìng)爭(zhēng)策略研究報(bào)告
- 科學(xué)版二年級(jí)《隊(duì)列隊(duì)形原地由一路縱隊(duì)變成二路縱隊(duì)》教案及教學(xué)反思
- 醫(yī)療器械公司咨詢培訓(xùn)記錄表(全套)-
- 供應(yīng)鏈整體運(yùn)作流程
- MT/T 548-1996單體液壓支柱使用規(guī)范
- GB/T 9765-2009輪胎氣門嘴螺紋
- GB/T 23806-2009精細(xì)陶瓷斷裂韌性試驗(yàn)方法單邊預(yù)裂紋梁(SEPB)法
- 2022年04月四川宜賓市敘州區(qū)面向區(qū)內(nèi)外考試選調(diào)在編在職教師136人考試押題庫(kù)【1000題】含答案附帶詳解析
- FZ/T 74001-2020紡織品針織運(yùn)動(dòng)護(hù)具
- 圖解“雙均線雙交叉”期貨、股票操作系統(tǒng)課件
- 美軍標(biāo)電子裝備環(huán)境試驗(yàn)-mil-std-810g
- 應(yīng)用回歸分析(第三版)何曉群_劉文卿_課后習(xí)題答案_完整版
評(píng)論
0/150
提交評(píng)論