




下載本文檔
版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、山東大學操作系統(tǒng)實驗報告4進 程同步實驗計算機科學與技術學院實驗報告實驗題目:實驗四、進程同步實驗學號:日期:20120409班級:計基地12姓名:實驗目的:加深對并發(fā)協(xié)作進程同步與互斥概念的理解,觀察和體驗并發(fā)進程同步與互斥 操作的效果,分析與研究經(jīng)典進程同步與互斥問題的實際解決方案。了解Lin UX系統(tǒng)中IPC進程同步工具的用法,練習并發(fā)協(xié)作進程的同步與互斥操作的編程 與調試技術。實驗內容:抽煙者問題。假設一個系統(tǒng)中有三個抽煙者進程,每個抽煙者不斷地卷煙并抽煙。 抽煙者卷起并抽掉一顆煙需要有三種材料: 煙草、紙和膠水。一個抽煙者有煙草, 一個有紙,另一個有膠水。系統(tǒng)中還有兩個供應者進程,它
2、們無限地供應所有三 種材料,但每次僅輪流提供三種材料中的兩種。得到缺失的兩種材料的抽煙者在 卷起并抽掉一顆煙后會發(fā)信號通知供應者,讓它繼續(xù)提供另外的兩種材料。這一 過程重復進行。請用以上介紹的IPC同步機制編程,實現(xiàn)該問題要求的功能。硬件環(huán)境:處理器:Intel? COre ?i3-2350M CPU 2.30GHZ × 4圖形:In tel?San dybridgeMobilex86MMXSSE2內存:4G操作系統(tǒng):32位磁盤:20.1 GB軟件環(huán)境:UbU ntu13.04實驗步驟:(1) 新建定義了 PrOdUCer 和 conSUmer共用的IPC函數(shù)原型和變量的ipc.h文
3、件。(2) 新建 ipc.c 文件,編寫 PrOdUCer 和 COnSUmer 共用的IPC的具體相應函數(shù)。(3) 新建PrOdUCer文件,首先定義 PrOdUCer 的一些行為,利用系統(tǒng)調用,建立共享內存區(qū)域, 設定其長度并獲取共享內存的首地址。 然后設定 生產者互斥與同步的信號燈,并為他們設置相應 的初值。當有生產者進程在運行而其他生產者請 求時,相應的信號燈就會阻止他,當共享內存區(qū) 域已滿時,信號等也會提示生產者不能再往共享 內存中放入內容。(4) 新建 ConSUmer 文件,定義 conSUmer的一些行為,利用系統(tǒng)調用來創(chuàng)建共享內存區(qū)域,并 設定他的長度并獲取共享內存的首地址。
4、然后設定消費者互斥與同步的信號燈,并為他們設置相 應的初值。當有消費進程在運行而其他消費者請 求時,相應的信號燈就會阻止它,當共享內存區(qū) 域已空時,信號等也會提示生產者不能再從共享 內存中取出相應的內容。運行的消費者應該與相應的生產者對應起來, 只有這樣運行結果才會正確。結論分析與體會: 實現(xiàn)方式:COn sume:#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 = 1
5、03;Cget_ nu mber = 1;Shm_flg = IPC_CREAT | 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,s
6、em_flg);Sem_val = 0;con s_Sem = Set_Sem(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_ptr0='A')Printf("%dThe conSUmer has gl
7、ue.nTheconSUmer getstobacco and paper n",getpid(); if(buff_ptr0='B')Printf("%dThe conSUmer has paper.nTheconSUmer getstobacco and gluen",getpid(); if(buff_ptr0='C')Prin tf("%d The con SUmer has tobacco.nThe con SUmer getsglue and paper n",getpid(); *cget_ptr
8、= (*cget_pt r+1); if(*cget_ptr%2=0) buff_ptrO='D'elsebuff_ptrO='E'UP(CmtX_sem);UP(PrOd_sem);retur n EXIT_SUCCESS;PrOdUCer :#i nclude "ipc.h"nt 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
9、_CREAT|0644;buff_Ptr = (Char *)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_Se
10、m(CO ns_h,sem_val,sem_flg);Sem_val = 1;PmtX_Sem = Set_Sem(PmtX_h,sem_val,sem_flg); if(producerid=0)buff_ptr0='D'*pput ptr=0;while(1)if(buff_ptrO-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("%dPaPern",g
11、etpid();if(*PPut_Ptr=1) buff_ptr0 = 'B' Prin tf("%dThePrOdUCergivestobaccoandThePrOdUCergivestobaccoandgluen",getpid();if(*pput_ptr=2)buff_ptr0 = C;Printf("%d The PrOdUCer gives glue and papern",getpid(); sleep(rate);UP(PmtX_sem);UP(C on s_sem); _retur n EXIT_SUCCESS;pc.h
12、 :#i nclude "ipc.h"nt get_ipc_id(Char *proc_file,h_t h) FlLE *pf;int m,n;Char Iin 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
13、 em='')m+;While(Ii nem !='') COIu mn+ = Iin em+;colum n = '0' if(atoi(colum) != h) con ti nue;n=0; while(l in em='') m+;while(linem !='') colu mn+ = Iin em+;colum n = '0' m = atoi(colum); fclose(pf); return m; fclose(pf); return -1;nt dow n(i nt Sem_id
14、) StrUCt SembUf buf; buf.sem_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;nt up(i nt Sem_id) StrUCt SembUf buf; buf.sem_op = 1; buf.sem_ nu mber = 0; buf.sem_flg = SEM_UNDO; if(semop(
15、semd,&buf,1) <0) PerrOr("up error "); exit(EXIT_FAILURE); retur n 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) V 0 ) if(sem_id = Semget(Sem_h,1,sem_flg) V 0) PerrOr("semaphore C
16、reate error"); exit(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
17、/shm",shm_h) V O ) 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) V (Char *)0) PerrOr("get ShareMemOry error");exit(EXIT_FAILURE);for(m=0; m<shm_ nu mber; m+) Shm_bufm = 0; _if(s
18、hm_buf = (Char *)Shmat(Shm_id,0,0) V (Char *)0)PerrOr("get ShareMemOry error"); exit(EXIT_FAILURE);retur n Shm_buf;Iint Set_msq(h_t msq_h,i nt msq_flg) int msqd;if(msq_id = get_ipc_id("/proc/SySViPC/msg",msq_h) V 0 ) if(msq id = msgget(msq h,msq flg) V 0)PerrOr("messageQueue
19、 Set error"); exit(EXIT_FAILURE);return msqd;實驗結果:稱宀""IllCUrku-LeOYO-IdCaPad-Y471 A: lab4 ILkUrIfLtkUn * LenDVO * I a P ad - Y471A:-S Cd lb4 ltkunyitkurLeovo-ideaad-Y47iAj*,Lab4 ,/producer 0132B7hePrDdUCergivestobaccoand glue33Z0Theproducergivesglue andPaPer3320TheProclUCergluestobacc
20、oandglue3329TheProdUCergivestbaccandPaPer3320Theproducergive¾Lkje andPaPerThBPrnElUCergtesrohsrcanrlglue3329ThePrOdUCergivestobaccoandPaPer3320TheProdUCergives9lue rdPdPerSJ2&ThPPrOdUCFrgivestobaccoandglue3320ThePrOdiJCCrgivestobaccodPOPCr3320ThePrQdUCergivesglue andPdPer3320Th色producertoba
21、ccoandglu332BTheproducergM古tobaccoandPaP 電 r*5320IhePrDdUCergivesglue andPaPer33<0ThePrOelUCergluestbaccDandglue132TheProClUCergivestobaccaandPaPerOsG IikUnUlCUn-LenOVOJdeaPad-Y471 A: kb4IlkUneUkUnLenovo-IdeaPad-Y471A:*/lab4S ./producer 1 3321 3321 3321 3321 3321 332133213322 3321 3321 3321 3321
22、3321 3321 3321 3321 3321 3321 3321 3321 3321 3321The The Ihe Ihe The The 7he The The Ihe IhC The The The The Ihe The The Ihe The The ThePrOdUCer PrOdUCer producer producer producer PrOdUCer PrOdUCer PrOdUCer PrOdUCer PrOdUCer PrOdUCCr PrOdUCer producer PrOdUCer producer producer producer PrOdUCer Pr
23、OdUCer PrOdUCer PrOdUCer PrOddCergives gives gives gives gives gives gives gives gives gives gives gives gives gives gives gives gives gives gives gives gives givestobacco and PaPer glue and PdPer tobacco and glue tobacco and PaPer glue and PaPer tobacco and glue tobacco and PaPer glue and PaPer tob
24、acco and glue tobacco and PaPer glue and PaPGr tobcco and glue tobacco and PaPer glue and PdPer tobacco and glue tobacco and PaPer glue and PaPer tobacco and glue tobacco and PaPer glue and PaPer tobacco and glue tobacco and PaPerOsG IikUnUlCUn-LenOVOJdeaPad-Y471 A: kb4IlkUneUkUnLenovo-IdeaPad-Y471A
25、:*/lab4S ./consumer5 bash: .cosuner:沒有那個文件或口錄ItkUnlikun-Lenovo-IdeaPad-Y471A:-/Iab4$ ./consumer 3096 The COnSUnerThe consumer gets3096 The COnSUnerThe consumer getsMfIqfi Tha roumprThe COnSUner gets3096 The COnSUnerThe consumer gets3096 ThC COnSUnCrThe consumer gets3096 Ihe COnSUfnerThe consumer get
26、s3096 The COnSUnerThe consumer gets3096 The COnSUnerThe consumer gets3096 7he COnSUnerThe COnSUner gets3096 The COnSUnerThe COnSUmer gets3096 The COnSUInerhas g*lue tobacco and has glue, tobacco and hx gl up. tobacco and has glue, tobacco and has glue, tobcco and has glue, tobacco and has glue, toba
27、cco and has glue tobacco and has glue, tobacco and has glue, tobacco and has glue.PaPerPaPerPaPerPaPerPaPerPaPerPaPerPaPerPaPerPaPer CG IikUnukUn-LenOVO-IdeaPad-Y471 A: Ub4ItkUnIllkUn-Lenovo-IdeaPad-Y471A:*$ Cd Idb4ItkUn¢1IkUnLQnovoIdwaPadY47ia:/lab4$ /consumer 1 3318 The COnSUnerThe consumer g
28、ets3318 Ihe consumerThe consumer gets3318 The consumerThe COnSUmer gets3318 The consumerThe consumer gets3318 7he COnSUfnerThG consumer gets3318 The COnSUnerThe COnSUmer gets3318 The consumerThe COnSUmer gets3318 Ihe consutnerThe COnSUmer gets3318 The consumerThe COnSUmer gets3318 The COnSUmerThe co
29、nsumer gets3318 The consumerhas paper, tobacco and has PaPer tobacco and has paper, tobacco and has paper, tobacco and has paper, tobacco and has paper, tobacco and has paper, tobacco and has PaPer tobacco and has paper, tobacco and has paper, tobacco and has PaPer.gluegluegluegluegluegluegluegluegl
30、ueglueThQ SnSUEgets toba"o and glueO * LlkurHkun-Lcnovo-ldcaPad-Y471 A: lab4 Hku0ltkun-LenaVO-IdedPdd-Y471A:$ Cd Idb4I-Lkunyltkur * Lenovo-ideaPad-Y471A:*/Iato45 couFler 2 1319 The CclfiSUl*r h白右 tobaccoThe consumer geti glut and PdPer3319 The CCnlSUner has tobacco.The COnSUr,r gets glu and PaP
31、er3319 The CQnSUrTer h日毎 tQbac»The consumer gets glue And pppr J319 The cosurcr has tobacco.The COnSrr sets glue ODd PdPer 3319 ThP Consurner has tobacco.The COnSUrrc 9cts glue d PHaPCr 3319 The COnSner has tobacco. Tha COrirer gets glue ad PaPer Ui9 The consumer has tobacco The CDhSJrr gets gl
32、ue and PaPer 3319 The CeInlSUner hmm tobaccoI The ConSJr-r gets glu and PaPer 3319 The csuner has tobaccot The COnSLHTPr gets gLup AnCI PaPPr J319 The consumer has tobacco. The consrr gets glue and PdPer 5319 The COnSUWl has tobacco. Thm COnurr get呂 glumpafm分析:多進程的系統(tǒng)中避免不了進程間的相互關系。進程互斥是進程之間發(fā)生的一種間接性作用,一般是程序不希望的。 通常的情況是兩個或兩個以上的進程需要 同時訪問某個共享變量。我們一般將發(fā)生能夠問共享變量的程序段稱為臨界區(qū)。 兩個進程不能同時進入臨界區(qū),否則就會導致數(shù)據(jù)的不一致,產生與時間有關的錯誤。解決互斥問題應該滿足互斥和公平兩個原則,即任意時刻只能允許一個進 程處于同一共享變量的臨界區(qū),而且不能讓任一進程無限期地等待。進程同步是進程之間直接的相互作用,是合作進程間有意識的行為,典型的 例子是公共汽車上
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 衡陽師范學院《藥學類專業(yè)寫作》2023-2024學年第二學期期末試卷
- 住宅空間照明設計
- 2025年執(zhí)業(yè)藥師資格證之《西藥學專業(yè)一》預測試題附答案詳解【b卷】
- 重慶商務職業(yè)學院《樂理基礎與視唱(一)》2023-2024學年第二學期期末試卷
- 廣州城建職業(yè)學院《平面構成》2023-2024學年第二學期期末試卷
- 內蒙古民族大學《材料生物學》2023-2024學年第二學期期末試卷
- 咸寧職業(yè)技術學院《中醫(yī)養(yǎng)生康復技術》2023-2024學年第二學期期末試卷
- 蘇州健雄職業(yè)技術學院《嬰幼兒護理與急救》2023-2024學年第二學期期末試卷
- 吉林大學《法醫(yī)物證學》2023-2024學年第二學期期末試卷
- 歷史文化主題VR-AR體驗館企業(yè)制定與實施新質生產力項目商業(yè)計劃書
- 2025五年級道德與法治下冊期末綜合測試卷(含答案)
- 2025至2030中國LNG運輸行業(yè)市場發(fā)展分析及前景預測與戰(zhàn)略規(guī)劃報告
- 主題3 鄉(xiāng)土情懷-2025年中考語文現(xiàn)代文閱讀主題預測與答題技巧指導(原卷版)
- 湘教版七年級數(shù)學下冊期末考試卷(含答案與解析)
- DB32T3614-2019 工貿企業(yè)安全風險管控基本規(guī)范
- 高效規(guī)劃優(yōu)化工業(yè)園區(qū)的基礎設施布局
- (王瑞元版本)運動生理學-課件-3-第三章-血液
- 浙江省醫(yī)療服務價格項目目錄
- 玻璃吊裝施工專項施工方案
- 焊接安全知識考核試題及答案
- 2025燃氣電廠智能巡檢系統(tǒng)技術方案
評論
0/150
提交評論