Linux环境编程--进程通信
實驗內(nèi)容
編寫程序?qū)崿F(xiàn)進程的管道通信。用系統(tǒng)調(diào)用pipe( )建立一管道,二個子進程P1和P2分別向管道各寫一句話:
??? Child 1 is sending a message!
??? Child 2 is sending a message!
父進程從管道中讀出二個來自子進程的信息并顯示(要求先接收P1,后P2)。
?
實驗指導
一、什么是管道
UNIX系統(tǒng)在OS的發(fā)展上,最重要的貢獻之一便是該系統(tǒng)首創(chuàng)了管道(pipe)。這也是UNIX系統(tǒng)的一大特色。
所謂管道,是指能夠連接一個寫進程和一個讀進程的、并允許它們以生產(chǎn)者—消費者方式進行通信的一個共享文件,又稱為pipe文件。由寫進程從管道的寫入端(句柄1)將數(shù)據(jù)寫入管道,而讀進程則從管道的讀出端(句柄0)讀出數(shù)據(jù)。
?
句柄fd[0] ? ? ? ? 句柄fd[1] | ?讀出端 ? ? ? ? 寫入端 |
二、管道的類型:
1、有名管道
一個可以在文件系統(tǒng)中長期存在的、具有路徑名的文件。用系統(tǒng)調(diào)用mknod( )建立。它克服無名管道使用上的局限性,可讓更多的進程也能利用管道進行通信。因而其它進程可以知道它的存在,并能利用路徑名來訪問該文件。對有名管道的訪問方式與訪問其他文件一樣,需先用open( )打開。
2、無名管道
一個臨時文件。利用pipe( )建立起來的無名文件(無路徑名)。只用該系統(tǒng)調(diào)用所返回的文件描述符來標識該文件,故只有調(diào)用pipe( )的進程及其子孫進程才能識別此文件描述符,才能利用該文件(管道)進行通信。當這些進程不再使用此管道時,核心收回其索引結點。
二種管道的讀寫方式是相同的,本文只講無名管道。
3、pipe文件的建立
分配磁盤和內(nèi)存索引結點、為讀進程分配文件表項、為寫進程分配文件表項、分配用戶文件描述符
4、讀/寫進程互斥
內(nèi)核為地址設置一個讀指針和一個寫指針,按先進先出順序讀、寫。
為使讀、寫進程互斥地訪問pipe文件,需使各進程互斥地訪問pipe文件索引結點中的直接地址項。因此,每次進程在訪問pipe文件前,都需檢查該索引文件是否已被上鎖。若是,進程便睡眠等待,否則,將其上鎖,進行讀/寫。操作結束后解鎖,并喚醒因該索引結點上鎖而睡眠的進程。
三、所涉及的系統(tǒng)調(diào)用???
1、pipe( )
建立一無名管道。
系統(tǒng)調(diào)用格式
??? ??????????pipe(filedes)
參數(shù)定義
int? pipe(filedes);
int ?filedes[2];
其中,filedes[1]是寫入端,filedes[0]是讀出端。
該函數(shù)使用頭文件如下:
#include <unistd.h>
#inlcude <signal.h>
#include <stdio.h>
?? 2、read( )
? 系統(tǒng)調(diào)用格式
???????????????? ?read(fd,buf,nbyte)
? 功能:從fd所指示的文件中讀出nbyte個字節(jié)的數(shù)據(jù),并將它們送至由指針buf所指示的緩沖區(qū)中。如該文件被加鎖,等待,直到鎖打開為止。
? 參數(shù)定義
??????????????? ??int? read(fd,buf,nbyte);
????????????????? int? fd;
????????????????? char *buf;
????????????????? unsigned? nbyte;
? 3、write( )
系統(tǒng)調(diào)用格式
???????????????? ?read(fd,buf,nbyte)
功能:把nbyte 個字節(jié)的數(shù)據(jù),從buf所指向的緩沖區(qū)寫到由fd所指向的文件中。如文件加鎖,暫停寫入,直至開鎖。
參數(shù)定義同read( )。
?
?
〈任務〉
??? 編制一段程序,實現(xiàn)進程的管道通信。使用系統(tǒng)調(diào)用pipe()建立一條管道線。兩個子進程p1和p2分別向通道個寫一句話:
? child1 process is sending message!
child2 process is sending message!
而父進程則從管道中讀出來自兩個進程的信息,顯示在屏幕上。
?
程序一:
#include<unistd.h>
#include<sys/types.h>
#include<errno.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>int main()
{
int pipe_fd[2];
pid_t pid;
char buf_r[100];
char* p_wbuf;
int r_num;
memset(buf_r,0,sizeof(buf_r));
if(pipe(pipe_fd)<0){
printf("pipe create error\n");
return -1;
}
if((pid=fork())==0)
{
printf("\n");
close(pipe_fd[1]);
sleep(2);
if((r_num=read(pipe_fd[0],buf_r,100))>0){printf("%d numbers read from the pipe is %s\n",r_num,buf_r);
}
close(pipe_fd[0]);
exit(0);
}
else if(pid>0)
{
close(pipe_fd[0]);
if(write(pipe_fd[1],"Hello",5)!=-1)
printf("parent writel success!\n");
if(write(pipe_fd[1],"pipe",5)!=-1)printf("parent write2 success!\n");
close(pipe_fd[1]);
sleep(3);
waitpid(pid,NULL,0);
exit(0);
}
}
效果:
parent writel success!
parent write2 success!
10 numbers read from the pipe is Hellopipe
?
更復雜的例子,程序二:
#include <unistd.h>
#include <signal.h>
#include <stdio.h>
#include<string.h>
int pid1,pid2;main( )
{
int fd[2];
char outpipe[100],inpipe[100];
pipe(fd); /*創(chuàng)建一個管道*/
while ((pid1=fork( ))==-1);
/*lockf()函數(shù)允許將文件區(qū)域用作信號量(監(jiān)視鎖),或用于控制對鎖定進程的訪問(強制模式記錄鎖定)。試圖訪問已鎖定資源的其他進程將返回錯誤或進入休眠狀態(tài),直到資源解除鎖定為止。當關閉文件時,將釋放進程的所有鎖定,即使進程仍然有打開的文件。當進程終止時,將釋放進程保留的所有鎖定。*/
if(pid1==0){
lockf(fd[1],1,0);sprintf(outpipe,"child 1 process is sending message!"); /*把串放入數(shù)組outpipe中*/write(fd[1],outpipe,50); /*向管道寫長為50字節(jié)的串*/sleep(5); /*自我阻塞5秒*/lockf(fd[1],0,0);exit(0);}
else{
while((pid2=fork( ))==-1);if(pid2==0)
{
lockf(fd[1],1,0); /*互斥*/sprintf(outpipe,"child 2 process is sending message!");write(fd[1],outpipe,50);sleep(5);lockf(fd[1],0,0);exit(0);}else{
wait(0); /*同步*/read(fd[0],inpipe,50); /*從管道中讀長為50字節(jié)的串*/printf("%s\n",inpipe);wait(0);read(fd[0],inpipe,50);printf("%s\n",inpipe);exit(0);}}
}
?
運行結果:
child 1 process is sending message!
child 2 process is sending message!
總結
以上是生活随笔為你收集整理的Linux环境编程--进程通信的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 聪明的姐姐们进来教一下,嘴巴笨的人在职场
- 下一篇: Linux环境编程--linux中的pe