下載本文檔
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
1、計算機(jī)科學(xué)與技術(shù)學(xué)院實驗報告實驗題目:實驗四、進(jìn)程同步實驗學(xué)號:日期:20120409班級:計基地12姓名:實驗?zāi)康模杭由顚Σl(fā)協(xié)作進(jìn)程同步與互斥概念的理解,觀察和體驗并發(fā)進(jìn)程同步與互斥 操作的效果,分析與研究經(jīng)典進(jìn)程同步與互斥問題的實際解決方案。了解Lin ux系統(tǒng)中IPC進(jìn)程同步工具的用法,練習(xí)并發(fā)協(xié)作進(jìn)程的同步與互斥操作的編程 與調(diào)試技術(shù)。實驗內(nèi)容:抽煙者問題。假設(shè)一個系統(tǒng)中有三個抽煙者進(jìn)程,每個抽煙者不斷地卷煙并抽煙。 抽煙者卷起并抽掉一顆煙需要有三種材料: 煙草、紙和膠水。一個抽煙者有煙草, 一個有紙,另一個有膠水。系統(tǒng)中還有兩個供應(yīng)者進(jìn)程,它們無限地供應(yīng)所有三 種材料,但每次僅輪流
2、提供三種材料中的兩種。得到缺失的兩種材料的抽煙者在 卷起并抽掉一顆煙后會發(fā)信號通知供應(yīng)者,讓它繼續(xù)提供另外的兩種材料。這一 過程重復(fù)進(jìn)行。請用以上介紹的IPC同步機(jī)制編程,實現(xiàn)該問題要求的功能。硬件環(huán)境:處理器:In tel? Core? i3-2350M CPU 2.30GHzx 4圖形:In tel? Sa ndybridge Mobile x86/MMX/SSE2內(nèi)存:4G操作系統(tǒng):32位磁盤:20.1 GB軟件環(huán)境:ubu ntu13.04實驗步驟:(1) 新建定義了 producer和consumer共用的IPC函數(shù)原型和變量的ipc.h文件。(2) 新建ipc.c文件,編寫prod
3、ucer 和consumer 共用的IPC的具體相應(yīng)函數(shù)。(3) 新建Producer文件,首先定義producer的一些行為,利用系統(tǒng)調(diào)用,建立共享內(nèi)存區(qū)域,設(shè)定其長度并獲取共享內(nèi)存的首地址。然后設(shè)定生產(chǎn)者互斥與同步的信號燈,并為他 們設(shè)置相應(yīng)的初值。當(dāng)有生產(chǎn)者進(jìn)程在運(yùn)行而其他生產(chǎn)者請求時,相應(yīng)的信號燈就會阻止他,當(dāng)共享內(nèi)存區(qū)域已滿時,信號等也會提示生產(chǎn)者不能再往共享內(nèi)存中放入內(nèi)容。(4) 新建Consumer文件,定義consumer的一些行為,利用系統(tǒng)調(diào)用來創(chuàng)建共享內(nèi)存 區(qū)域,并設(shè)定他的長度并獲取共享內(nèi)存的首地址。然后設(shè)定消費(fèi)者互斥與同步的信號燈,并 為他們設(shè)置相應(yīng)的初值。當(dāng)有消費(fèi)進(jìn)程在
4、運(yùn)行而其他消費(fèi)者請求時,相應(yīng)的信號燈就會阻止它,當(dāng)共享內(nèi)存區(qū)域已空時,信號等也會提示生產(chǎn)者不能再從共享內(nèi)存中取出相應(yīng)的內(nèi)容。運(yùn)行的消費(fèi)者應(yīng)該與相應(yīng)的生產(chǎn)者對應(yīng)起來,只有這樣運(yùn)行結(jié)果才會正確。結(jié)論分析與體會:實現(xiàn)方式:Consumer :#i nclude "ipc.h"int main (i nt argc,char *argv) int rate = 3;int con sumerid=atoi(argv1);buff_h = 101;buff_ nu mber = 1;cget_h = 103;cget_ nu mber = 1;shm flg = IPC CREAT
5、| 0644;buff_ptr = (char *)set_shm(buff_h,buff_ nu mber,shm_flg); cget_ptr = (int *)set_shm(cget_h,cget_ nu mber,shm_flg); prod_h = 201;pmtx_h = 202;cons_h = 301;cmtx_h = 302;sem_flg = IPC_CREAT | 0644;sem_val = buff_ nu mber;prod_sem = set_sem(prod_h,sem_val,sem_flg);sem_val = 0;con s_sem = set_sem(
6、co ns_h,sem_val,sem_flg);sem_val = 1;cmtx_sem = set_sem(cmtx_h,sem_val,sem_flg);if(con sumerid=0)*cget_ptr=0;while(1)if(buff_ptr0-'A'=c on sumerid)dow n(co ns_sem);dow n(cmtx_sem);sleep(rate);if(buff_ptrO='A')printf("%dThe consumer has glue.nThe consumer getstobacco and paper n&
7、quot;,getpid();if(buff_ptr0='B')prin tf("%d The con sumer has paper.' nThe con sumer getstobacco and gluen",getpid();if(buff_ptrO='C')printf("%dThe consumerhas tobacco.'nTheconsumergets glue and paper' n",getpid();*cget ptr 二(*cget pt葉1);if(*cget_ptr%2
8、=0) buff_ptrO='D' else buff_ptrO='E' up(cmtx_sem); up(prod_sem);return EXIT_SUCCESS;Producer :#i nclude "ipc.h"int main (i nt argc,char *argv)int rate=3;int producerid=atoi(argv1);buff_h=101;buff_ nu mber=1;pput_h=102;pput_ nu mber=1;shm_flg=IPC_CREAT|0644;buff_ptr = (char
9、*)set_shm(buff_h,buff_ nu mber,shm_flg); pput_ptr = (int *)set_shm(pput_h,pput_ nu mber,shm_flg);prod_h = 201;pmtx_h = 202;cons_h = 301;cmtx_h = 302;sem flg = IPC CREAT|0644;sem_val = buff_ nu mber;prod_sem = set_sem(prod_h,sem_val,sem_flg);sem_val = 0;con s_sem = set_sem(co ns_h,sem_val,sem_flg);se
10、m_val = 1;pmtx_sem = set_sem(pmtx_h,sem_val,sem_flg);if(producerid=0)buff_ptrO='D'*pput_ptr=0;while(1)if(buff_ptr0-'D'=producerid)dow n( prod_sem);dow n(pmtx_sem);*pput_ptr = (*pput_pt r+1)%3;if(*pput_ptr=0)buff_ptr0 = 'A'prin tf("%dThe producer gives tobacco andpapern&q
11、uot;,getpid();if(*pput_ptr=1)buff_ptr0 = 'B'prin tf("%dThe producer gives tobacco andgluen",getpid();if(*pput_ptr=2)buff_ptr0 = C;prin tf("%d The producer gives glue and paper n",getpid();sleep(rate);up(pmtx_sem);up(c on s_sem);return EXIT_SUCCESS;Ipc.h :#i nclude "i
12、pc.h"int get_ipc_id(char *proc_file,h_t h) FILE *pf;int m,n;char lin eBUFSZ,columBUFSZ;if(pf = fope n(proc_file,"r") = NULL) perror('卩roc file not ope n"); exit(EXIT_FAILURE);fgets(line, BUFSZ,pf);while(!feof(pf)m = n = 0;fgets(line, BUFSZ,pf);while(l in em='')m+;whil
13、e(linem !='') colu mn+ = lin em+;colum n = '0' if(atoi(colum) != h) con ti nue;n=0;while(l in em='')m+;while(linem !='') colu mn+ = lin em+;colum n = '0'm = atoi(colum);fclose(pf);return m; fclose(pf); return -1;int dow n(i nt sem_id) struct sembuf buf;buf.sem
14、_op = -1; buf.sem_ nu mber = 0; buf.sem_flg = SEM_UNDO; if(semop(semd,&buf,1) <0) perror("dow n error "); exit(EXIT_FAILURE);return EXIT_SUCCESS; int up(i nt sem_id) struct sembuf buf; buf.sem_op = 1; buf.sem_ nu mber = 0; buf.sem_flg = SEM_UNDO; if(semop(semd,&buf,1) <0) per
15、ror("up error "); exit(EXIT_FAILURE);return EXIT_SUCCESS; int set sem(h t sem h,i nt sem val,i nt sem flg) int semd;Sem _uns sem_arg;if(semd = get_ipc_id("/proc/sysvipc/sem",sem_h) < 0 ) if(sem_id = semget(sem_h,1,sem_flg) < 0) perror("semaphore create error"); ex
16、it(EXIT_FAILURE);sem_arg.val = sem_val;if(semctl(semd,O,SETVAL,sem_arg) <0) perror("semaphore set error"); exit(EXIT_FAILURE);return semd;char * set_shm(h_t shm_h,i nt shm_ nu mber,i nt shm_flg) int m,shmd;char * shm_buf;if(shm_id = get_ipc_id("/proc/sysvipc/shm",shm_h) < 0
17、 ) if(shmd = shmget(shm_h,shm _nu mber,shm_flg) <0) perror("shareMemory set error"); exit(EXIT_FAILURE);if(shm_buf = (char *)shmat(shmd,0,0) < (char *)0) perror("get shareMemory error"); exit(EXIT_FAILURE);for(m=0; m<shm_ nu mber; m+) shm_bufm = 0;if(shm_buf = (char *)sh
18、mat(shmd,0,0) < (char *)0) perror("get shareMemory error");exit(EXIT_FAILURE);retur n shm_buf;int set_msq(h_t msq_h,i nt msq_flg) int msqd;if(msq_id = get_ipc_id("/proc/sysvipc/msg",msq_h) < 0 ) if(msq_id = msgget(msq_h,msq_flg) < 0) perror("messageQueue set error&q
19、uot;); exit(EXIT_FAILURE);return msqd;實驗結(jié)果:0鈔 likunlikunMjenovo-IdeaPad-Y471A: /lab4ILkurillkivi- Lenovo - IdeaPdd - Y471A: -$ cd lab4likunyltkun * Lenovo*ideapad-Y471A:*/Iato4 ,/producer 0 332&33203320232053ZO3201329332033203329332013201320332033201320The The T heThe The lheTheTheThe TheThe The
20、 The The T heTheproducer producer producer producer prpducer producer producer producer producer producer producer producer producer producer produce prodjeerIves gtvea gtves gives give 5 give*; gtves gives glvz gives gtves gitve« gtves gives gives gtvestobacco and glu總 glue dnd pdper tabacco a
21、nd glue tobacco and paper ylue and paper tabacco and glue tobacco and pep©r qIlic and pper tobacco and glue tobacco and popcr glue and pdper tobacco and glu tobacco and papr glue and paper tabaccD and glue tobacco and paperlikunlikunMjenowddeaPad-Y471A:likimtllkun-Lenovo-IdePsd-Y471A:-/lBb4S */
22、producer 1J3Z1 lhe3321 The1321 The3321 The331 The33?1 The3321 The3321 The3321 The3321 The3321 The3121 The33Z1 The a丄 TheJ321 The3321 The3321 Thei321 The33?1 Thp3321 The3321 The3321 Th?producer producer producei producer producer producer producer producer producer producer producer producer producer
23、 producer producer producer producer producer producer producer producer produceigives tobacco and pap«r gtves gLp亡 and paper ytvei LobdccQ and glue gives tobacco and paper gtves glue and paper gtves tabacca and glue tobacco and paper gives glue and paper gives tobacco and glue gives tobacco an
24、d paper gtvt?s glue 由nd pper gives tobacco and glw gives tobacco and ppr glvei glue 日nd paper gtves tabacto 白nd glue gtves tabacca and paper gtves glue and paper gives tabacco end qlue givf3 tobacri rind pper gives glue and psper gives tobacco end glue glvps toba< co and ppprl ik u n 1 f kunMje n
25、 o vo- Ide a Pad -Y 471 At /lab4ItkurifLikin -Lenovo-IdeaPdd-Y471A:-/Lab4$ */consunerd bah: ./consu-nere:沒有那個文件或口錄 likurilikun Lenovo IdaPad-V471A:-/lab4$ r/cohsuner 9 5056 The cunsunerThe consumer gets工0北 The consumerThe consurer gets玉旳5 lhe rnnunerThe consumer gets3030 The consunerThe consulr gers
26、J096 The consurwrThe consumer gets1096 The cansurwrThe consumer gets3036 The consumerThe consumer gets3036 The consumerThe consumer qet5"4096 The ennsumerThe consumer getsJ036 The consunerThe consuwr gets3096 The consurwrhas glu巳* tobacco and has glum” tobacco nd glum* tobacco end has glue toba
27、cco and hos gluc« tobacco «nd h白左glue, tab白tto and has glue, tobacco and has gLue, tobacco and gl_u樓. tobacco and hd5 glue, tobacco 8nd ha各 glw.paperpaperpaperpaperpaperpaperpaperpaperpaperpaper0 likunilikun-Lenovo-IdeaPad-Y471A; /lab4ItkimfL Ikun - Leno vc -1 a Pad - Y471A: - 5 cd lab4Itk
28、unyltkun-Lenovo*LdeaPad-Y47iA:"/lab4$ ,/consumer 13IS The consuiner The consumer geti 3318 rhe ccnsuner The consumer gets 3318 The cqnsuner The con';iiTpr gets J318 The consuner The consumer sets 3318 Thp consuner The consurncr gets 3318 The consoner Tha conurrr gets 対iE The consular The co
29、nsurer gets 33is rhe consumer The consumer gets 3318 The cqnsuner The ronsLjF.pr gets 3318 The consumer The consumer getsThe consuwi The consurer getsh甘右 paper, t口bacco dnd has paper. tobacco and ha paper * tDhacco 2nd has pape* tobacco «r>d has priper, tobacco and has paper. tobacco and has
30、 paper. tobacco and hmm paper. tobacco and has paper + mbacco 2nd has paper, tobacco «r>d has paper. tobacco "dilueglueueglueglueglumgluegLuegl lieglueglue0Q UkunlikunMjenovoddeaPad-Y471A: /lab4ltkim<llkun-Lencvo-IdcAPjd-Y471A:-S cd lab4Itkunyltkun - Lenovo* idea Pad-Y4 71A:*/Iab4 ,/
31、con«upner 1315 The cohsup>er has tobacco.The consuner gets glut dnd paper3319 rhe consuner has Tobacco.glue and paper h日乞 tobacco.glue and paper has tobacco- glue and paper has tobacco.glue and ppor has tobacco.glue and paper has tobaccoglue and paper hmm tobacco.glue and paper has tobacco.g
32、lue and paper haw tobacco glue and paper has tobacco.glue /nd paperThe consumer gets 3319 The cansuner The conLjnter gets J319 The consuner The consumer sets 3319 Thp consurner The consumer gets J319 The consunerThe conurrr gets 1519 The consularThe consumer gets3319 The consumerhe consjrr gets 3319
33、 The consunerThe conuiner gets 3319 The consumerThe consumer gets 3.319 Tht? consumeiThe consumer get呂分析:多進(jìn)程的系統(tǒng)中避免不了進(jìn)程間的相互關(guān)系。進(jìn)程互斥是進(jìn)程之間發(fā)生的一 種間接性作用,一般是程序不希望的。通常的情況是兩個或兩個以上的進(jìn)程需要 同時訪問某個共享變量。我們一般將發(fā)生能夠問共享變量的程序段稱為臨界區(qū)。 兩個進(jìn)程不能同時進(jìn)入臨界區(qū),否則就會導(dǎo)致數(shù)據(jù)的不一致,產(chǎn)生與時間有關(guān)的 錯誤。解決互斥問題應(yīng)該滿足互斥和公平兩個原則,即任意時刻只能允許一個進(jìn) 程處于同一共享變量的臨界區(qū),而且不能讓任一進(jìn)程無限期地等待。進(jìn)程同步是進(jìn)程之間直接的相互作用,是合作進(jìn)
溫馨提示
- 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)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2024年湖州考從業(yè)資格證客運(yùn)試題
- 2024年武漢客運(yùn)資格證急救止血法
- 2024年湛江c1客運(yùn)從業(yè)資格證考試
- 2024年南京客運(yùn)從業(yè)資格證需要什么條件
- 2024年河北道路客運(yùn)駕駛員從業(yè)資格模擬考試題
- 2024-2025學(xué)年山東省聊城市冠縣統(tǒng)編版二年級上冊期中考試道德與法治試卷
- (完整版)水工建筑物基礎(chǔ)基礎(chǔ)試題及答案
- 招投標(biāo)安全生產(chǎn)風(fēng)險防控承諾書
- 圖書館消防設(shè)施施工協(xié)議
- 火車觀光車廂窗安裝協(xié)議
- 超聲科質(zhì)量控制報告及整改
- 血透病人皮膚瘙癢護(hù)理查房課件
- 幼兒園大班韻律《朱迪警官破案記》課件
- 電子商務(wù)法課件
- 《強(qiáng)化學(xué)習(xí)簡介》課件
- 2024年護(hù)士職業(yè)心理健康關(guān)注護(hù)士心理健康問題和應(yīng)對方法
- 招標(biāo)代理應(yīng)急響應(yīng)預(yù)案
- 國開2023秋《人文英語4》期末復(fù)習(xí)寫作練習(xí)參考答案
- GB/Z 43410-2023無損檢測自動超聲檢測系統(tǒng)選擇和應(yīng)用
- 四級高頻詞匯
- 央國企信創(chuàng)化與數(shù)字化轉(zhuǎn)型規(guī)劃實施
評論
0/150
提交評論