操作系統(tǒng)實驗2_第1頁
操作系統(tǒng)實驗2_第2頁
操作系統(tǒng)實驗2_第3頁
操作系統(tǒng)實驗2_第4頁
操作系統(tǒng)實驗2_第5頁
已閱讀5頁,還剩3頁未讀, 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

1、操作系統(tǒng)實驗報告年級、專業(yè)、班級2013級計科6班姓名張行實驗題目進程間通信實驗時間2015-10-31實驗地點0410實驗成績 實驗性質驗證性 設計性 綜合性教師評價:算法/實驗過程正確; 源程序/實驗內容提交 程序結構/實驗步驟合理;實驗結果正確; 語法、語義正確; 報告規(guī)范; 其他: 評價教師簽名:一、實驗目的 Ø 了解管道通信的特點,掌握管道通信的使用方法。Ø 了解消息隊列通信機制及原理,掌握消息隊列相關系統(tǒng)調用的使用方法及功能。二、實驗項目內容管道通信-實驗內容:¢ 1.父進程創(chuàng)建管道和兩個子進程p1和p2¢ 2.子進程p1打開給定文件(如果沒

2、有,則創(chuàng)建文件),并向文件中寫數(shù)據(jù),寫完關閉文件,然后向管道寫入一條消息“ok",目的是通知進程p2可以讀取文件內容了。¢ 3.子進程p2通過管道讀取消息,如果消息是“ok”,則打開文件,讀取文件內容,并將其輸出到屏幕上,關閉文件.¢消息隊列-實驗內容Ø 父進程創(chuàng)建消息隊列和兩個子進程p1和p2Ø 子進程p1打開給定文件(如果沒有,則創(chuàng)建文件),并向文件中寫數(shù)據(jù),寫完關閉文件,然后向消息隊列寫入一條消息“ok”,目的是通知進程p2可以讀取文件內容了。Ø 子進程p2從消息隊列讀取消息,如果收到消息“ok”,則打開文件,讀取文件內容,并將

3、其輸出道屏幕上,關閉文件。三、實驗過程與算法int pipe(int fd2)功能:創(chuàng)建管道int read(int fd, void *buf, int count);功能:從參數(shù)fd指定的讀端讀取管道數(shù)據(jù)到大小為count的緩存buf中,返回實際讀取到的字節(jié)數(shù)。int write(int fd, void *buf, int count);功能:向參數(shù)fd指定的寫端從緩存buf中取出count個字節(jié)到管道中,返回值為實際寫入的字節(jié)數(shù)int msgsnd(int msqid, const void * ptr, size_t nbytes, int flag) 功能:往消息隊列寫消息,即發(fā)送

4、消息。int msgrcv(int msqid, const void * ptr, size_t nbytes ,long type, int flag) ;功能:從消息隊列讀消息,即接收消息。管道代碼如下:#include<stdio.h>#include<stdlib.h>#include<string.h>#include<unistd.h>#include<sys/types.h>int main(void) int fd2,byte; pid_t childpid1; pid_t childpid2; FILE *fp;

5、char s20; char string = "ok" char readbuffer80; pipe(fd); int r_fd = fd0; int w_fd = fd1; if(childpid1 = fork()= -1) perror("fork error!"); exit(1); else if(childpid1 = 0) printf("childpid1 pid is:%dn",getpid(); if(fp=fopen("hello.txt","w")=NULL) pri

6、ntf("The file %s not be open.n","hello.txt"); return 0; fputs("Hello!",fp); printf("successn"); fclose(fp); close(r_fd);/close write write(w_fd,string,strlen(string); exit(0); else wait(NULL); if(childpid2 = fork()= -1) perror("fork error!"); exit(1)

7、; else if(childpid2 = 0) printf("childpid2 pid is:%dn",getpid(); close(w_fd);/close read byte = read(r_fd,readbuffer,sizeof(readbuffer); if(strcmp("ok",readbuffer)=0) if(fp=fopen("hello.txt","r")=NULL) printf("The file %s notopen.n","hello.txt&q

8、uot;); return; while(fgets(s,20,fp)!=NULL) printf("%sn",s); fclose(fp); else wait(NULL); return 0;消息隊列代碼如下:#include<stdio.h>#include<sys/msg.h>#include<fcntl.h>#include<stdlib.h>#include<string.h>#define MAX 100struct msgbuff long mtype; char dataMAX;int main(

9、) FILE *fp; pid_t pid1,pid2; key_t key; char s20; char string = "ok" if(key=ftok("/home/zh",'g')<0) printf("key get error!n"); return -1; int mgsid; if(mgsid=msgget(key,IPC_CREAT|0666)= -1) printf("creat errorn"); return -1; pid1=fork(); if(pid1<

10、0) printf("fork creat error!n"); exit(1); else if(pid1=0) printf("pid1 pid is:%dn",getpid(); printf("Sending the message.n"); sleep(1); struct msgbuff msg1; msg1.mtype=getppid(); if(fp=fopen("hello.txt","w")=NULL) printf("The file %s not openn&q

11、uot;,"hello.txt"); return; fputs("Hello!",fp); fclose(fp); strcpy(msg1.data,"ok"); if(msgsnd(mgsid,&msg1,sizeof(msg1.data),0)<0) printf("Sending error!n"); exit(1); else printf("complete sending !n"); exit(0); else wait(NULL); pid2=fork(); if(

12、pid2<0) printf("fork creat error!n"); exit(1); else if(pid2=0) printf("pid2 pid is:%dn",getpid(); printf("Receiving the message.n"); sleep(1); struct msgbuff msg2; /msg2.mtype=getppid(); if(msgrcv(mgsid,&msg2,MAX,getppid(),0)<0) printf("receiving error!n&

13、quot;); exit(1); else printf(" complete receiving!n"); if(strcmp("ok",msg2.data)=0) if(fp=fopen("hello.txt","r")=NULL) printf("The file %s no opened.n","hello.txt"); return; while(fgets(s,20,fp)!=NULL) printf("the message is:%sn",s); fclose(fp); else wait(NULL); exit(0); return 0; 四、實驗結果及分析和(或)源程序調試過程(包含程序使用方法、程序運行截圖),實驗過程中遇到的問題分析與心得體會。(實驗報告中最重要的部分,應盡量詳細,重點描述自己遇到的問題以及解決方法)管道通信運行結果如下:消息隊列運行結果如下兩個通信都有一個消息傳輸?shù)倪^程在里面,

溫馨提示

  • 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. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論