山東大學(xué)操作系統(tǒng)實驗報告4進程同步實驗_第1頁
山東大學(xué)操作系統(tǒng)實驗報告4進程同步實驗_第2頁
山東大學(xué)操作系統(tǒng)實驗報告4進程同步實驗_第3頁
山東大學(xué)操作系統(tǒng)實驗報告4進程同步實驗_第4頁
山東大學(xué)操作系統(tǒng)實驗報告4進程同步實驗_第5頁
已閱讀5頁,還剩5頁未讀, 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

計算機科學(xué)與技術(shù)學(xué)院實驗報告實驗題目:實驗四、進程同步實驗學(xué)號:日期:20120409班級:計基地12姓名:實驗?zāi)康模杭由顚Σl(fā)協(xié)作進程同步與互斥概念的理解,觀察和體驗并發(fā)進程同步與互斥操作的效果,分析與研究經(jīng)典進程同步與互斥問題的實際解決方案。了解Linux系統(tǒng)中IPC進程同步工具的用法,練習并發(fā)協(xié)作進程的同步與互斥操作的編程與調(diào)試技術(shù)。實驗內(nèi)容:抽煙者問題。假設(shè)一個系統(tǒng)中有三個抽煙者進程,每個抽煙者不斷地卷煙并抽煙。抽煙者卷起并抽掉一顆煙需要有三種材料:煙草、紙和膠水。一個抽煙者有煙草,一個有紙,另一個有膠水。系統(tǒng)中還有兩個供應(yīng)者進程,它們無限地供應(yīng)所有三種材料,但每次僅輪流提供三種材料中的兩種。得到缺失的兩種材料的抽煙者在卷起并抽掉一顆煙后會發(fā)信號通知供應(yīng)者,讓它繼續(xù)提供另外的兩種材料。這一過程重復(fù)進行。請用以上介紹的IPC同步機制編程,實現(xiàn)該問題要求的功能。硬件環(huán)境:處理器:Intel?Core?X4圖形:Intel?SandybridgeMobilex86/MMX/SSE2內(nèi)存:4G操作系統(tǒng):32位磁盤:20.1GB軟件環(huán)境:ubuntu13.04實驗步驟:新建定義了producer和consumer共用的IPC函數(shù)原型和變量的ipc.h文件。新建ipc.c文件,編寫producer和consumer共用的IPC的具體相應(yīng)函數(shù)。新建Producer文件,首先定義producer的一些行為,利用系統(tǒng)調(diào)用,建立共享內(nèi)存區(qū)域,設(shè)定其長度并獲取共享內(nèi)存的首地址。然后設(shè)定生產(chǎn)者互斥與同步的信號燈,并為他們設(shè)置相應(yīng)的初值。當有生產(chǎn)者進程在運行而其他生產(chǎn)者請求時,相應(yīng)的信號燈就會阻止他,當共享內(nèi)存區(qū)域已滿時,信號等也會提示生產(chǎn)者不能再往共享內(nèi)存中放入內(nèi)容。新建Consumer文件,定義consumer的一些行為,利用系統(tǒng)調(diào)用來創(chuàng)建共享內(nèi)存區(qū)域,并設(shè)定他的長度并獲取共享內(nèi)存的首地址。然后設(shè)定消費者互斥與同步的信號燈,并為他們設(shè)置相應(yīng)的初值。當有消費進程在運行而其他消費者請求時,相應(yīng)的信號燈就會阻止它,當共享內(nèi)存區(qū)域已空時,信號等也會提示生產(chǎn)者不能再從共享內(nèi)存中取出相應(yīng)的內(nèi)容。運行的消費者應(yīng)該與相應(yīng)的生產(chǎn)者對應(yīng)起來,只有這樣運行結(jié)果才會正確。結(jié)論分析與體會:實現(xiàn)方式:Consumer:#include"ipc.h"intmain(intargc,char*argv[]){intrate=3;intconsumerid二atoi(argv[1]);buff_h=101;buff_number=1;cget_h=103;cget_number=1;shm_flg=IPC_CREAT|0644;buff_ptr=(char*)set_shm(buff_h,buff_number,shm_flg);cget_ptr=(int*)set_shm(cget_h,cget_number,shm_flg);prod_h=201;pmtx_h=202;cons_h=301;cmtx_h=302;sem_flg=IPC_CREAT|0644;sem_val=buff_number;prod_sem=set_sem(prod_h,sem_val,sem_flg);sem_val=0;cons_sem=set_sem(cons_h,sem_val,sem_flg);sem_val=1;cmtx_sem=set_sem(cmtx_h,sem_val,sem_flg);if(consumerid==0)*cget_ptr=0;while(1){if(buff_ptr[0]-'A'=consumerid){down(cons_sem);down(cmtx_sem);sleep(rate);if(buff_ptr[0]='A'){printf("%dTheconsumerhasglue.\nTheconsumergetstobaccoandpaper\n",getpid());}if(buff_ptr[0]='B'){printf("%dTheconsumerhaspaper.\nTheconsumergetstobaccoandglue\n",getpid());}if(buff_ptr[0]='C'){printf("%dTheconsumerhastobacco.\nTheconsumergetsglueandpaper\n",getpid());}*cget_ptr=(*cget_ptr+1);if(*cget_ptr%2==0)buff_ptr[0]='D';elsebuff_ptr[0]='E';up(cmtx_sem);up(prod_sem);}}returnEXIT_SUCCESS;}Producer:#include"ipc.h"intmain(intargc,char*argv[]){intrate=3;intproducerid二atoi(argv[1]);buff_h=101;buff_number=1;pput_h=102;pput_number=1;shm_flg=IPC_CREAT|0644;buff_ptr=(char*)set_shm(buff_h,buff_number,shm_flg);pput_ptr=(int*)set_shm(pput_h,pput_number,shm_flg);prod_h=201;pmtx_h=202;cons_h=301;cmtx_h=302;sem_flg=IPC_CREAT|0644;sem_val=buff_number;prod_sem=set_sem(prod_h,sem_val,sem_flg);sem_val=0;cons_sem=set_sem(cons_h,sem_val,sem_flg);sem_val=1;pmtxsem=setsem(pmtxh,semval,semflg);if(producerid==0){buff_ptr[0]='D';*pput_ptr=0;}while(1){if(buff_ptr[0]-'D'=producerid){down(prod_sem);down(pmtx_sem);*pput_ptr=(*pput_ptr+1)%3;if(*pput_ptr==0){buff_ptr[0]='A';printf("%dTheproducergivestobaccoandpaper\n",getpid());}if(*pput_ptr==1){buff_ptr[0]=printf("%d'B';Theproducergivestobaccoandglue\n",getpid());}if(*pput_ptr==2){buff_ptr[0]='C';printf("%dTheproducergivesglueandpaper\n",getpid());}sleep(rate);up(pmtx_sem);up(cons_sem);}}returnEXIT_SUCCESS;}Ipc.h:#include"ipc.h"intget_ipc_id(char*proc_file,h_th){FILE*pf;intm,n;charline[BUFSZ],colum[BUFSZ];if((pf=fopen(proc_file,"r"))=NULL){perror("Procfilenotopen");exit(EXIT_FAILURE);}fgets(line,BUFSZ,pf);while(!feof(pf)){m=n=0;fgets(line,BUFSZ,pf);while(line[m]='')m++;while(line[m]!='')colum[n++]=line[m++];colum[n]='\0';if(atoi(colum)!=h)continue;n=0;while(line[m]='')m++;while(line[m]!='')colum[n++]=line[m++];colum[n]='\0';m=atoi(colum);fclose(pf);returnm;}fclose(pf);return-1;}intdown(intsem_id){structsembufbuf;buf.sem_op=-1;buf.sem_number=0;buf.sem_flg=SEM_UNDO;if((semop(sem_id,&buf,1))<0){perror("downerror");exit(EXIT_FAILURE);}returnEXIT_SUCCESS;}intup(intsem_id){structsembufbuf;buf.sem_op=1;buf.sem_number=0;buf.sem_flg=SEM_UNDO;if((semop(sem_id,&buf,1))<0){perror("uperror");exit(EXIT_FAILURE);returnEXIT_SUCCESS;}intset_sem(h_tsem_h,intsem_val,intsem_flg){intsem_id;Sem_unssem_arg;if((sem_id=get_ipc_id("/proc/sysvipc/sem",sem_h))<0){if((sem_id=semget(sem_h,1,sem_flg))<0){perror("semaphorecreateerror");exit(EXIT_FAILURE);}sem_arg.val=sem_val;if(semctl(sem_id,0,SETVAL,sem_arg)<0){perror("semaphoreseterror");exit(EXIT_FAILURE);}}returnsem_id;}char*set_shm(h_tshm_h,intshm_number,intshm_flg){intm,shm_id;char*shm_buf;if((shm_id=get_ipc_id("/proc/sysvipc/shm",shm_h))<0){if((shm_id=shmget(shm_h,shm_number,shm_flg))<0){perror("shareMemoryseterror");exit(EXIT_FAILURE);}if((shm_buf=(char*)shmat(shm_id,0,0))<(char*)0){perror("getshareMemoryerror");exit(EXIT_FAILURE);}for(m=0;m<shm_number;m++)shm_buf[m]=0;}—if((shm_buf=(char*)shmat(shm_id,0,0))<(char*)0){perror("getshareMemoryerror");exit(EXIT_FAILURE);}returnshm_buf;}intsetmsq(htmsqh,intmsqflg){

intmsq_id;if((msq_id=get_ipc_id("/proc/sysvipc/msg",msq_h))<0){if((msq_id=msgget(msq_h,msq_flg))<0){perror("messageQueueseterror");exit(EXIT_FAILURE);}}returnmsq_id;}實驗結(jié)果:ITkunikun-LcnovoideaPad-Y471A:-/lab4likunglikun-Lenovo-IdedPdd-Y471A:~Scdldb43320The33ZOThe3329The3329The33ZBThe33zaTheThe33ZOThesszaThe3328The3320The33zaThe3328The33源The33zaThe3328Theproducerprodu匚如producerproducerproduce「producerproducerproducerproducerproducerproducerproducerproducerproducerproducerproducerItkungltkun-Lenovo-ldeaPad-V471A:~/lab4$./prodjcer0givestobaccoandgluegivesglueandpapergtvestobaccoandgluegivestobaccoandpapergtve53320The33ZOThe3329The3329The33ZBThe33zaTheThe33ZOThesszaThe3328The3320The33zaThe3328The33源The33zaThe3328Theproducerprodu匚如producerproducerproduce「producerproducerproducerproducerproducerproducerproducerproducerproducerproducerproducer?土0Ukun@>lflcun-Lcnovo-ldeaPad-Y471A:?/laMlikunSlikun-Lenovo-ldedPdd-Y471A:~$cdlab4ttkungltkun-Lenovo-ldeaPdd-Y471A:~/lab4$./consjmer1331BTheconsumerhaspaper*TheconsuriErgets3319TheconsumerTheconsumergets3318TheconsumerTheconsumergets3318TheconsumerTheconsumergets331STheconsumerTheconsumergets3318TheconsumerTheconsumergets3318Theconsumer?土0Ukun@>lflcun-Lcnovo-ldeaPad-Y471A:?/laMlikunSlikun-Lenovo-ldedPdd-Y471A:~$cdlab4ttkungltkun-Lenovo-ldeaPdd-Y471A:~/lab4$./consjmer1331BTheconsumerhaspaper*tobaccoandgluehaspaper-tobaccoandgluehaspaper-tobaccoendgluehaspaper.tobaccoandgluehaspaper-tobaccoandgluehaspaper-tobaccoandgluehaspaper.tobaccoandgluehaspaper-tobaccoandgluehaspaper.tobaccoandgluehaspaper.tobaccoandglue?*Ukun@>Hlcun-Lenovo-IdeaPad-Y471A:?/laMlikunOlikun-Lenovo-ldedPad-Y471A:~Scdlab4ttkungltkun-Lenovo-ldeaPdd-Y471A:~/lab45./consumer23319Theconsumerhastobacco.Theconsumergetsglueandpaper3319Theconsumerhastobacco.Theconsumergetsglueandpaper3319TheconsumerhastobaccotTheconsumergetsgtueandpaper3319Theconsumerhastobacco.Theconsumergetsglueandpaper3319Theconsumerhastobacco.Theconsumergetsglueandpaper3319Theconsumerhastobacco.Theconsumergetsglueandpaper3319TheconsumerhastobaccofTheconsumergetsglueandpaper3319TheconsumerhastotiaccoLTheconsumergetsglueandpaper3319Theconsumerhastobacco.Theconsumergetsglueandpaper2S19Theconsumerhastobacco.Theconsumergetsglueandpaper3319Theconsumerhastobacco.TheconsumergetsgLueandpaper分析:多進程的系統(tǒng)中避免不了進程間的相互關(guān)系。進程互斥是進程之間發(fā)生的一種間接性作用,一般是程序不希望的。通常的情況是兩個或兩個以上的進程需要同時訪問某個共享變量。我們一般將發(fā)生能夠問共享變量的程序段稱為臨界區(qū)。兩個進程不能同時進入臨界區(qū),否則就會導(dǎo)致數(shù)據(jù)的不一致,產(chǎn)生與時間有關(guān)的錯誤。解決互斥問題應(yīng)該滿足互斥和公平兩個原則,即任意時刻只能允許一個進程處于同一共享變量的臨界區(qū),而且不能讓任一進程無限期地等待。進程同步是進程之間直接的相互作用,是合作進程間有意識的行為,典型的例子是公共汽車上司機與售票員的合作。只有當售票員關(guān)門之后司機才能啟動車輛,只有司機停車之后售票員才能開車門。司機和售票員的行動需要一定的協(xié)調(diào)。同樣地,兩個進程之間有時也有這樣的依賴關(guān)系,因此我們也要有一定的同步機制保證它們的執(zhí)行次序。信號量機制就是其中的一種。信號燈機制即利用pv操作來對信號量進行處理。PV操作由P操作原語和V操作原語組成(原語是不可中斷的過程),對信號量進行操作,具體定義如下:P(S):①將信號量S的值減1,即S=S-

溫馨提示

  • 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

提交評論