Linu進(jìn)程間通信(2)實驗報告_第1頁
Linu進(jìn)程間通信(2)實驗報告_第2頁
Linu進(jìn)程間通信(2)實驗報告_第3頁
Linu進(jìn)程間通信(2)實驗報告_第4頁
Linu進(jìn)程間通信(2)實驗報告_第5頁
已閱讀5頁,還剩6頁未讀, 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

1、實驗六:Linux進(jìn)程間通信(2) (4課時)實驗?zāi)康茫豪斫膺M(jìn)程通信原理;掌握進(jìn)程中信號量、共學(xué)內(nèi)存、消息隊列相關(guān)得函數(shù)得使用. 實驗原理:Linux下進(jìn)程通信相關(guān)函數(shù)除上次實驗所用得兒個還有: 信號int nsems.口信號量乂稱為信號燈,它就是用來協(xié)調(diào)不同進(jìn)程間得數(shù)據(jù)對象得,而最主要 得應(yīng)用就是前一節(jié)得共學(xué)內(nèi)存方式得進(jìn)程間通信。要調(diào)用得笫一個函數(shù)就是 semget,用以獲得一個信號量ID。int s emg e t ( k ey_t key, int nsems, i nt fla g );key就是IPC結(jié)構(gòu)得關(guān)鍵字,flag將來決定就是創(chuàng)建新得信號量集合,還 就是引用一個現(xiàn)有得信號量集

2、合。nsems就是該集合中得信號量數(shù)。如果就是 創(chuàng)建新集合(一般在服務(wù)器中),則必須指定n sems;如果就是引用一個現(xiàn) 有得信號量集合(一般在客戶機中)則將nsems指定為0。se mcti函數(shù)用來對信號量進(jìn)行操作int S e mctKi nt semi d , i nt sem n um, in tcmd , union semun arg);不同得操作就是通過cmd參數(shù)來實現(xiàn)得,在頭文件sem、h中定義了 7種 不同得操作,實際編程時可以參照使用semop函數(shù)自動執(zhí)行信號量集合上得操作數(shù)組。in t sem op(int s e mid, struct s e m b u f sem

3、op array, size_t nops);se moparray就是一個指針,它指向一個信號量操作數(shù)組。nops規(guī)定該 數(shù)組中操作得數(shù)量.ftok原型如下;k e y_t fto k ( cha r * fname. int id )f n a me就就是指定得文件名(該文件必須就是存在而且可以訪問得),i d就是子序號,雖然為int,但就是只有8個比特被使用(0-25 5)。當(dāng)成功執(zhí)行得時候,一個key_t值將會被返回否則一1被返回。共拿內(nèi)存共享內(nèi)存就是運行在同一臺機器上得進(jìn)程間通信最快得方式,W為數(shù)據(jù)不需 要在不同得進(jìn)程間復(fù)制通常山一個進(jìn)程創(chuàng)建一塊共學(xué)內(nèi)存區(qū),其余進(jìn)程對這塊 內(nèi)存區(qū)進(jìn)行

4、讀寫首先要用得函數(shù)就是S hmge t ,它獲得一個共學(xué)存儲標(biāo)識符.#! n C 1 U d e #in c lude #in c lude sys /shm、h)in t s h m g et key_t key, int si z e , int flag);當(dāng)共享內(nèi)存創(chuàng)建后,其余進(jìn)程可以調(diào)用S hmat()將其連接到自身得地址空 間中。V 01 d *shm a t(in t shmicL void *addr, int flag);shmid為shmget函數(shù)返回得共孚存儲標(biāo)識符,add r與flag參數(shù)決定 了以什么方式來確定連接得地址,函數(shù)得返回值即就是該進(jìn)程數(shù)據(jù)段所連接得實 際地

5、址,進(jìn)程可以對此進(jìn)程進(jìn)行讀寫操作。斷開共享內(nèi)存連接:與shma t函數(shù)相反,shmd t就是用來斷開與共學(xué)內(nèi)存附加點得地址, 禁止本進(jìn)程訪問此片共學(xué)內(nèi)存函數(shù)原型int shm d t (const void *shmad d r)函數(shù)傳入值Shmadd r:連接得共學(xué)內(nèi)存得起始地址函數(shù)返回值成功:0出錯:7,錯誤原因存于error中附加說明本函數(shù)調(diào)用并不刪除所指定得共學(xué)內(nèi)存區(qū),而只就是將先前用shm at函數(shù) 連接(attach)好得共學(xué)內(nèi)存脫離(d eta ch) U前得進(jìn)程錯誤代碼E I NVAL:無效得參數(shù)shma d dr.消息隊列消息隊列就就是一個消息得鏈表??梢园严⑶谱饕粋€記錄,

6、具有特定得格 式以及特定得優(yōu)先級1、創(chuàng)建新消息隊列或取得已存在消息隊列原型:in t msgget(key_t key, i nt m s gfig);參數(shù):key:鍵值何以指定,也可以山函數(shù)ftok生成。msgflg:IPC_CREAT值,若沒有該隊列,則創(chuàng)建一個并返回新標(biāo)識符; 若已存在,則返回原標(biāo)識符。IPC_EXCL值,若沒有該隊列,則返回J;若已存在,則返回0。2、向隊列讀/寫消.富原型:msg rev從隊列中取用消息:SSI Z e_t m s g r cv(int msqid, void *msgp, siz e_ t msg s z , Ion g msg t y p , in

7、t m s gfIg);msgs n d將數(shù)據(jù)放到消息隊列中:int msg s nd (i nt m s qid, const v oid s g p , siz e_t msgs z , i nt msgf 1 g);參數(shù):msq i d:消息隊列得標(biāo)識碼msg p :指向消息緩沖區(qū)得指針,此位置用來暫時存儲發(fā)送與接收得消 息m s gsz:消息得大小。msgtyp:從消息隊列內(nèi)讀取得消息形態(tài)如果值為零,則表示消息隊列中 得所有消息都會被讀取.m sgfig:用來指明核心程序在隊列沒有數(shù)據(jù)得情況下所應(yīng)采取得行動。3、設(shè)置消息隊列屬性原型:int m s gcti ( int m s gq

8、i d, i ntd s *bu f );參數(shù):msgcti系統(tǒng)調(diào)用對msgq i d標(biāo)識得消息隊列執(zhí)行統(tǒng)定義了 3 種 emd 操作:IPC_STAT , IPC_SET ,struct msqid_c md操作,系IPC RM I DIPC_STAT :該命令用來獲取消息隊列對應(yīng)得m S q i d_d s 數(shù)拯 結(jié)構(gòu),并將其保存到buf指定得地址空間。IPC_SET :該命令用來設(shè)置消息隊列得屬性,要設(shè)置得屬性存儲在b uf中。IPC_RMID :從內(nèi)核中刪除msqid 標(biāo)識得消息隊列。實驗內(nèi)容:1、完成教材上信號量實例,想一下ftok函數(shù)得作用?修改例子,創(chuàng)建2個進(jìn)程完成原來父子進(jìn)程對

9、應(yīng)得操作.子進(jìn)程代碼:#i n c I u d e#includeVuni $ td、h#inc 1 ude $ t d io. h)#f i n c lude #includ c (sys/ t ypcs、h# i ncludeff include Vsy$ / ipc、h#def i ne delay TIME 3union $ emun“nt val;struct semid_ds * b uf;3unSigned shor t *a r r aint i nit v a lue)union semunsem_union; s c m union、val = i n i tvalue;i

10、f (s c me t l(sem_ i d 0, SETVAL,sc m_union) =1)* P error (I n itiah z e semap h ore*); r c tu rn I :3 re t urn 0:i nt d c I_s c mint semj d )、u nio n s emu n $ e m u n ion:“f (s c met I semjd, 0 JPC_RMID,sefn_un ion) =1)pe r ror ( D elet c semapho r ;return -1;int $ em_p s em_id = s emgc t f to ka

11、) 1. “n i t-S c m(semjd 0):* p r i n t f(Ch i Id proces $ wi I I w a i t for some seco n d 5、 rT):$ leepDELAy_TIME):* printf (The rcturnedva I udis%d in the chi I dproce $ s (PIO = %d) n* result, getpidd):。張m_v (sc m_id);等待進(jìn)程:#include# i nc 1 ud e #in elude (stdi o . h# i nc 1 u d e# inclu d e、h1 ME

12、 3# include s ys / ip c# d efine DELAY_T u n ion sem u n n i n t val;一 *buf:s h ort *array;0 struct s e mid_d s0 un $ igned;int init_ s em(i n t sem_id; i n t ini I _val u e)u n i on s e mun s em_uni o n;3 sem_u n ion、v a I = i n i t _value;if (se me 11( s e m_id 0 SETVAL,sem_u n io n )= = 3 P error

13、CI n i tialize sem a phore “ );n return 1 :int d el s e m ( i nt s em i d) un i on $ emun s em_u nion :if (s e mcti ( $ em id. 0 , IPC RMID, s em un i on) =-!)3 p e r r or ( Dele I e semap h ore );0 o re t u r n 1;intn structsem_b、s em bs em_p (in t sem_id) sem b uf sem_b; $ em_num =0 : se m_op =-1

14、;b s em_b、sem_flg=SEM_UNDO;b if (sem op (s e m_ i d, &$em_b, 1 ) = =!)( p e rror (”P operation ”);n d ret u r n 1; r e t ur n 0;in t sem_v(int s e m_id)st r uct sem b uf s em_b;n sem_b、sem_n um =0 ;n se m_b、sem_o p = 1 :sem_b、sem_f 1 g= SEM_U NDO:b i f ( s emop(sem_id, &sem_b. 1 ) =1) 3 p e rrorCV o

15、p erat i on);3 return -1:0 return 0;i nt m a in() pid_t res u It;i nt se m_id;n semjd =semget (f tok( J Q) 1, 0666 I IPC_CREAT);i n i t_s e m ( s e m_id,0);n s e m_ p (s em_ i d ):pri ntf(Th e retu rn ed value i s %d in th e father process (P ID =%dn result,getpid ();n s e m_ V (sem_i d ):n de 1 _s

16、e m(sem_id);2、完成教材上共享內(nèi)存實例,査瞧運行情況。然后修改ftok函數(shù)得參數(shù),并編寫兩個進(jìn)程完成實例原來得工作代碼:# i n elude #inc 1 ude #include hh#in c lud e std i o .# i n c lude st d li b、#in c lude SIZE 2048# d e f ine BUFFERint main () p i d_t pi d :i nt shmi d :。c har *shm_addr;ochar f i ag= WROTE:。char buff( B U阡 E RESIZE if( s hmid =shmg

17、e t (IP C_PRIVATE,BU 阡 ER_S|ZE, 06 6 6) ( 0 ) perror(sh m g et);exi t (1):else p ri n tf $ if (shmd t (shm_add r )0) 。dp error (” sh mdt);d ex i t (1);e Is e printf (C h ild: De a t tach s h are d memo r yn ):。 s ystemip c s-m):。if (shmcti ( s hmi d, I P C_RMID,NULL) = -1) “p e r r 0 r ( Ch i I d :

18、shmcti ( I P C_RM I D)n*)! oexit (1); els e op r i nt f (De 1 et e s h a redm emo ryn); sy s t e m (ipcs m )els e “f (shm_addr=s h mat (shmid 0, 0)=void*) 1) pe r ro r (Pare nt: s h mat); e X i t(l);el s ep r i nt f (Pa r ent: Attach sh a re d memory: %p n 舄 s h m_add r );dsle e p (1);p rint f (n Input som e s tring : n ): oofgets(buf f ,BUFF E R_SIZE,std i n);strncpy (shm_addr+strl e n (fla g ),bu f f

溫馨提示

  • 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)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論