操作系統(tǒng)實驗一進程與線程_第1頁
操作系統(tǒng)實驗一進程與線程_第2頁
操作系統(tǒng)實驗一進程與線程_第3頁
操作系統(tǒng)實驗一進程與線程_第4頁
操作系統(tǒng)實驗一進程與線程_第5頁
已閱讀5頁,還剩27頁未讀, 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

1、實驗一 進程與線程Linux 進程與線程通訊實驗?zāi)康膶嶒瀮?nèi)容實驗準備實驗設(shè)計參考代碼實驗結(jié)果思考題實驗?zāi)康纳羁汤斫饩€程和進程的概念,掌握線程與進程在組成成分上的差別,以及與其相適應(yīng)的通訊方式和應(yīng)用目標。實驗內(nèi)容以Linux系統(tǒng)進程和線程機制為背景,掌握fork()和clone()系統(tǒng)調(diào)用的形式和功能,以及與其相適應(yīng)的高級通訊方式。由fork派生的子進程之間通過pipe通訊,由clone創(chuàng)建的線程之間通過共享內(nèi)存通訊,對于后者需要考慮互斥問題。以生產(chǎn)者/消費者問題為例,通過實驗理解fork()和clone()兩個系統(tǒng)調(diào)用的區(qū)別。程序要求能夠創(chuàng)建4個進程或線程,其中包括兩個生產(chǎn)者和兩個消費者,生產(chǎn)

2、者和消費者之間能夠傳遞數(shù)據(jù)。實驗準備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)建一個子進程,子進程是父進程的完整復制,正常返回值為非負整數(shù),對于父進程來說該數(shù)大于0,是子進程的編號(pid);對于子進程來說該數(shù)為0。正是利用反回值的差別可以決定二者不同的后繼動作。clone系統(tǒng)調(diào)用int clone (int ( *fn ) (void *arg) , void *stack , int

3、 flag , void *arg) ;其中,fn是輕進程所執(zhí)行的函數(shù);stack是輕進程所使用的棧;flag是CLONE_VM, CLONE_FS, CLONE_FILES, CLONE_SIGNAND, CLONE_PID的組合;arg是調(diào)用過程的對應(yīng)參數(shù)。Clone()的關(guān)鍵是flag的設(shè)定,CLONE_VM表示子進程共享父進程內(nèi)存,CLONE_FS表示子進程共享父進程的文件系統(tǒng),CLONE_SIGNAND表示子進程共享父進程的消息處理機制,CLONE_PID是指子進程繼承父進程的id號。pipe系統(tǒng)調(diào)用ret_val=pipe(fd);參數(shù)定義為int fd2。創(chuàng)建一個管道文件,返回兩

4、個文件描述符fd0和fd1分別用于管道文件的讀和寫操作。管道文件創(chuàng)建后,可以被fork創(chuàng)建的子進程共享。sem_wait(&s)和sem_post(&s)sem_wait(&s)和sem_post(&s)分別相當于信號燈的P操作和V操作。其中s是說明為說明為sem_t類型的信號燈。初始化函數(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è)計 用pipe()創(chuàng)建一個管道文件,然后用fork()創(chuàng)建兩個生產(chǎn)進程和兩個消費進程,它們之間通過pipe()傳遞信息。用clone()創(chuàng)建四個輕進程(線程),用參數(shù)指明共享內(nèi)存等資源,通過共享內(nèi)存模擬生產(chǎn)消費問題,利用pthread_mutex_lock(), pthread_mutex_unlock()等函數(shù)實現(xiàn)對共享存儲區(qū)訪問的互斥。參考代碼基于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); /否這會有讀者或者寫者永遠等待 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) /消費

9、者1strcpy(w_buf,ccc0); else /消費者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)建四個線程 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);實驗結(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. 本站所有資源如無特殊說明,都需要本地電腦安裝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)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論