實(shí)驗(yàn)7多進(jìn)程間的通信_第1頁
實(shí)驗(yàn)7多進(jìn)程間的通信_第2頁
實(shí)驗(yàn)7多進(jìn)程間的通信_第3頁
實(shí)驗(yàn)7多進(jìn)程間的通信_第4頁
實(shí)驗(yàn)7多進(jìn)程間的通信_第5頁
已閱讀5頁,還剩1頁未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡介

1、實(shí)驗(yàn)7 進(jìn)程間的通信l 實(shí)驗(yàn)?zāi)康模?、 掌握進(jìn)程間的通信機(jī)制,2、 掌握信號3、 掌握共享內(nèi)存、信號量4、 掌握消息隊(duì)列l(wèi) 實(shí)驗(yàn)要求:熟練使用該節(jié)所介紹進(jìn)程間通信的相關(guān)函數(shù)。l 實(shí)驗(yàn)器材:軟件:1.安裝了Ubunt的vmware虛擬機(jī)硬件:PC機(jī)一臺l 實(shí)驗(yàn)步驟:1、 信號實(shí)驗(yàn);參考下以關(guān)于信號注冊和信號捕捉的函數(shù)代碼/*signal.c*/#include <stdio.h>#include <unistd.h>#include<signal.h>void f(int sig)printf("catch signal %dn",sig)

2、;int main()int i=0;signal(SIGINT,f);printf("pid=%dn",getpid();while(1)printf("%dn",i+);sleep(1);return 0;編寫一段程序,使用系統(tǒng)調(diào)用函數(shù)fork( )創(chuàng)建兩個子進(jìn)程,再用系統(tǒng)調(diào)用函數(shù)signal( )讓父進(jìn)程捕捉信號SIGINT(可以鍵入ctrl+c 來觸發(fā),也可以用kill 命令來觸發(fā)),當(dāng)捕捉到中斷信號后,父進(jìn)程用系統(tǒng)調(diào)用函數(shù)kill( )向兩個子進(jìn)程發(fā)出信號,子進(jìn)程捕捉到父進(jìn)程發(fā)來的信號后,分別輸出下列信息后終止:Child process 1

3、is killed by parent!Child process 2 is killed by parent!參考代碼如下:#include "myhead.h"void f(int sig)kill(p1, SIGUSR1);kill(p2, SIGUSR2);void f1(int sig)printf("p1 catch the signal: %dn", sig);void f2(int sig)printf("p2 catch the signal: %dn", sig);int main(void)pid_t p1, p

4、2;p1 = fork();if(p1 > 0)p2 = fork();if(p2 > 0) / parent/ 告訴內(nèi)核,以后如果SIGINT來了,就去執(zhí)行函數(shù)fsignal(SIGINT, f);pause();else if(p2 = 0) / child 2signal(SIGUSR2, f2);pause();else if(p1 = 0) / child 1signal(SIGUSR1, f1);pause();return 0;2、 用共享內(nèi)存和信號量實(shí)現(xiàn)兩個進(jìn)程間的通信,一個寫進(jìn)程jack.c,向共享內(nèi)存中寫入數(shù)據(jù),一個讀進(jìn)程rose.c從共享內(nèi)存中讀取數(shù)據(jù)。jac

5、k.c的參考代碼如下:#include <stdio.h>#include <errno.h>#include <fcntl.h>#include <sys/types.h>#include <sys/ipc.h>#include <sys/shm.h>#include <strings.h>#include <sys/stat.h>#include <semaphore.h>#define BUFSZ 1024int main(void)key_t key = ftok(".

6、", 1);int shmid = shmget(key, BUFSZ, 0666|IPC_CREAT|IPC_EXCL);if(shmid = -1 && errno = EEXIST)shmid = shmget(key, BUFSZ, 0666);char *shmaddr = shmat(shmid, NULL, 0);bzero(shmaddr, BUFSZ);fgets(shmaddr, BUFSZ, stdin);sem_t *s = sem_open("/mysem", O_CREAT|O_EXCL, 0666, 0);if(s =

7、 SEM_FAILED && errno = EEXIST)s = sem_open("/mysem", 0);sem_post(s);return 0;rose.c的參考代碼如下:#include <stdio.h>#include <fcntl.h>#include <sys/types.h>#include <sys/shm.h>#include <sys/ipc.h>#include <sys/stat.h>#include <errno.h>#include <

8、;semaphore.h>#define BUFSZ 1024int main(void)key_t key = ftok(".", 1);int shmid = shmget(key, BUFSZ, 0666|IPC_CREAT|IPC_EXCL);if(shmid = -1 && errno = EEXIST)shmid = shmget(key, BUFSZ, 0666);char *shmaddr = shmat(shmid, NULL, 0);sem_t *s = sem_open("/mysem", O_CREAT|O_EXCL, 0666, 0);if(s = SEM_FAILED && errno = EEXIST)s = sem_open("/mysem", 0);sem_wait(s);printf("from Jack: %sn", shmaddr);shmctl(shmid, IPC_RMID, NULL);return 0;這個是因?yàn)閜thread并

溫馨提示

  • 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

提交評論