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

下載本文檔

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

文檔簡介

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

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

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

4、要用得函數(shù)就是s hmge t ,它獲得一個共享存儲標識符.# i n c 1 u d e # in c lude h#in c lude sys/shm、hin t s h m g et (key_t key. int si z e , i nt flag);當共享內(nèi)存創(chuàng)建后,其余進程可以調(diào)用shmat()將其連接到自身得地址空 間中。void * shm a t ( i nt shmid, void 火add門 int flag);s hmid為s hmg e t函數(shù)返回得共享存儲標識符,add r與fl a g參數(shù)決定 7以什么方式來確定連接得地址,函數(shù)得返回值即就是該進程數(shù)據(jù)段所連接得

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

6、記錄,具有特定得格 式以及特定得優(yōu)先級.1、創(chuàng)建新消息隊列或取得已存在消息隊列原型:in t msgget(key_t key, i nt m s gflg);參數(shù):key:鍵值,可以指定,也可以由函數(shù)ftok生成。msgf lg:IPC_CREAT值,若沒有該隊列,則創(chuàng)建一個并返回新標識符; 若已存在,則返回原標識符。IPC_EXCL值,若沒有該隊列,則返回若已存在,則返回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

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

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

9、例子,創(chuàng)建2個進程完成原來父子進程對應得操作.子進程代碼:#i n c 1 u d e#includeVuni s td、h#inc 1 ude s t d io. h)# i n c lude # inclu d c # i nclude# i n c I u deffdef i neDELAY JTIME3unio n s emu n、int val;struct semid_ds * b uf;、u n signe d shor t *a r r a y ;in t i n it_sem(int s c md, int init_ value)(union semunsem_union;

10、s c munionx val = i n i t_value:i f (s c me t l(sem_ i d , 0, SETVAL,sc m_union) =1)、 P error (I n itiali z e semap h ore11);)心 t urn 0:i nt d e I_s e m(int semd )(、u nio n s emu n s c m_u n ion:“f (s c met I (semd, 0PC_RMID,Sm_un ion) =1)* 、 pe r ror (Delet e semaphore);return -1;打int s em_p (int s

11、 emd)str u c t sembuf sem_b:sem b、sem_num =0 ;、s cm_b、s em_op =1;sem_ b、s em_f 1 g =SEM_UNDO: 、if(s e mop(s e m_id & spe r r or ( n P op e ra t io n ”):“return)r e tur n 0 :i nt s em_ v (int s c md)stru c t s em b uf sem b :s em_num = 0 :sem_b、sem_o p = 1 :、sem_b. s em_fl g =S E M_UNDO:i f ( s em o

12、p (sem_id,&sem_b 1 )=1)p e r r or (Vope rati o n ):。retu r n 1 :)4 re t urn 0:int m a in()(沖 i d_ t re s ult:i n t sem_ i d :4 s em_id = s emg e t ( f tok( M/ af ), 1 0 666 I I PC_CREAT);“n i t_ s e m(semd, 0):sprint f(Ch i Id proces s wi 1 I w a i t for some seco n d s、 nu):s leep(DELAY_TIME):# pri

13、ntf (Th e r e turned va 1 ud is %d in t h e chi 1 d proce s s (PID = %d) n result, getpid();osem_v (sc m_id);等待進程:#include# i nc I u d e #in c 1 u d e (stdi o、h# i nc 1 u d e#inelude #inclu devsys/sem、h #include# define D ELAY_T I ME 3u n ion sem u n。i n t val;。struct s e mid_d s *buf:。un s igned s

14、h ort *array;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;。sem_u n ion、v a I = i n i t _value;if (s e me 11( s e md, 0, SETVAL,sem_u n io n ) = = 1 ) o P error(HI n i tialize sem a phore M ;。 return 1 :3retu r n 0 ;int d el_s e m ( i nt s em_ i d) un i o n $ emun $

15、 em_union:if (s e mctl ( s em id, 0 , IPC RMID, s em un i on) =-!.)o p e r r or ( H Dele t e semap h ore ” ):re l u r n 1;int s em_p (in t semjd) 。struct sem b uf sem_b:sem_b、s em_n um =0 :sem_b、s e m_op =- 1 : s e m_bx sem_flg=SEM_UNDO: i f (semop (s e m_ i dz&sem_b, 1 ) = = 1) nape rror (P operati

16、on ” );。a ret u r n 1:n r e t urn 0;in t sem_v(int s e m_id)st r uct sem b uf s em_b;。sem_b、sem_num =0 :。s e m_b、sem_o p = 1 :sem_b、sem_f 1 g= S EM_U NDO:。i f ( s emop(sem_id, &sem_b, 1 ) = = 1) o p e rror(nV operat i onH);o r eturn3。return 0;i nt m a in() 。pid_t res u It:i n t s e mjd;semjd =semge

17、I (f t ok(z J 3), 1, 0666 I IPC_CREAT);i n i t_s e m ( s e m_id,0);。s e m_ p ( s em_ i d );pri n tf(nTh e ret u r n ed value i s %d in the father p r oc e s s ( P ID =%d) nt resultzgetpid ();。 s e m_v (sem J d ):。 de 1 _s e m(sem_id);2、完成教材上共孕內(nèi)存實例,查瞧運行情況。然后修改ftok函數(shù)得參數(shù),并編 寫兩個進程完成實例原來得工作.代碼:# i n elud

18、e #inc 1 ude #include #in c lu d e std i o、h# i n c lude #in c lude string、h# d e f ine BU F FE RESIZE 2048int main () p i d_t pi d :i n t shmi d :a c har *shm_addr;ocha r f 1 ag= u WROTE ” :q char buffB UFFE R_SIZE oif( s hmid =shmg e t (IPC_PRI VAT 巳 BUFFER_SIZE, 06 6 6) ( 0) perrorCshm g et);exi

19、t (1):else p ri n tf (MCre ate share d -memory: %d n: s h m i d);a sy s tem(i pcs;pi d = fork ();i f(p i d =1) a per r or (f o rkH);nex i t ( 1 );o e 1 se i f (pid = 0) a i f(shm_addr = shma t (shmid.O, 0)= (void*)-l) a o pe r r o r(z/ Child: s hmat);e x it ( 1 ): e I s es hm_a ddr);prin t f( C hil

20、d : Att a ch s h are d m emo r y: %p n ” sys t em(Hipcs o w h i 1 e ( s trncmp (shm_a dd r, f I a g,s t rlen (flag) qprintf (HChi 1 d :Wait fo r e n a ble dat a 八、nH);a os I eep (5):o oq s t rep y(bufts h m_ad d r+s t r len(flag):“ p r intf(Chi 1 : S h a re d memor y :%sn b u ff) 。if (shmd t (shm_add r )0) a operror ( M s h mdt);a ex i t (1);o e Is e printf (/ZC h ild: De a t tach s h are d memo r yn ”);。a s ystem(Hip c s m):?!癷f (shmctl ( s hmi d , I P C_RMID,NULL) =-l) “perror(”Child : shmctl ( I PC_R

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 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

提交評論