什么是管道?
管道是單向的、先進先出的,它把一個進程的輸出和另一個進程的輸入連接在一起。一個進程(寫進程)在管道的尾部寫入數(shù)據(jù),另一個進程(讀進程)從管道的頭部讀出數(shù)據(jù)。
管道的分類
管道包括無名管道和命名管道兩種,前者用于父進程和子進程間的通信,后者可用于運行于同一系統(tǒng)中的任意兩個進程間的通信。
無名管道的創(chuàng)建
無名管道由pipe( )函數(shù)創(chuàng)建:
int pipe(int filedis[2]);
當一個管道被創(chuàng)建時,它會創(chuàng)建兩個文件描述符:filedis[0]用于讀管道,filedis[1]用于寫管道。
管道通信示意圖如圖1所示:
圖1 管道通信示意圖
管道關(guān)閉
關(guān)閉管道只是將兩個文件描述符關(guān)閉即可,可以使用普通的close函數(shù)逐個關(guān)閉。
無名管道讀寫
管道用于不同進程間通信。通常先創(chuàng)建一個管道,再通過fork函數(shù)創(chuàng)建一個子進程,該子進程會繼承父進程創(chuàng)建的管道。注意事項:必須在系統(tǒng)調(diào)用fork()前調(diào)用pipe(),否則子進程將不會繼承文件描述符。否則,會創(chuàng)建兩個管道,因為父子進程共享同一段代碼段,都會各自調(diào)用pipe(),即建立兩個管道,出現(xiàn)異常錯誤。無名管道讀寫過程如圖2所示:
圖2 無名管道讀寫示意圖
無名管道實例:
- /**********************************************************
- *實驗要求: 使用pipe創(chuàng)建無名管道并實現(xiàn)父子進程之間的通訊。
- *功能描述: 在父進程中通過無名管道的寫端寫入數(shù)據(jù),通過子進程從管道讀端讀出相* 應(yīng)的數(shù)據(jù)。
- *日 期: 2010-9-17
- *作 者: 國嵌
- **********************************************************/
- #include <unistd.h>
- #include <sys/types.h>
- #include <errno.h>
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.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));
-
- /*創(chuàng)建管道*/
- if(pipe(pipe_fd)<0)
- {
- printf("pipe create error\n");
- return -1;
- }
-
- /*創(chuàng)建子進程*/
- if((pid=fork())==0) //子進程執(zhí)行序列
- {
- printf("\n");
- close(pipe_fd[1]);//子進程先關(guān)閉了管道的寫端
- sleep(2); /*讓父進程先運行,這樣父進程先寫子進程才有內(nèi)容讀*/
- 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) //父進程執(zhí)行序列
- {
- close(pipe_fd[0]); //父進程先關(guān)閉了管道的讀端
- if(write(pipe_fd[1],"Hello",5)!=-1)
- printf("parent write1 Hello!\n");
- if(write(pipe_fd[1]," Pipe",5)!=-1)
- printf("parent write2 Pipe!\n");
- close(pipe_fd[1]);
- waitpid(pid,NULL,0); /*等待子進程結(jié)束*/
- exit(0);
- }
- return 0;
- }
命名管道
命名管道和無名管道基本相同,但也有不同點:無名管道只能有父進程使用;但是通過命名管道,不相關(guān)的進程也能交換數(shù)據(jù)。
命名管道的創(chuàng)建
#include <sys/types.h>
#include <sys/stat.h>
int mkfifo(const char *pathname,mode_t mode)
pathname:FIFO文件名
mode:屬性(與文件操作相同)
一旦創(chuàng)建了一個FIFO,就可以用open打開它,一般的文件訪問函數(shù)(close,read,write)都可以用于FIFO
當打開FIFO時,非阻塞標志(O_NONBLOCK)
將對以后的讀寫產(chǎn)生如下影響:
1 沒有使用 O_NONBLOCK:訪問要求無法滿足時進程阻塞。如讀取空的FIFO時,或者FIFO已滿時。
2 使用O_NONBLOCK:訪問要求無法滿足時不阻塞,立即出錯返回,error是ENXIO。
FIFO讀進程:
- /**********************************************************
- *實驗要求: 使用mkfifo創(chuàng)建有名管道并實現(xiàn)兩個進程之間的通訊。
- *功能描述: 創(chuàng)建一個進程,并從已經(jīng)建立好的有名管道中,讀出事先寫入的
- * 數(shù)據(jù)。
- *日 期: 2010-9-17
- *作 者: 國嵌
- **********************************************************/
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <errno.h>
- #include <fcntl.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define FIFO "/tmp/myfifo"
-
- /*
- * 程序入口
- * */
- int main(int argc,char** argv)
- {
- char buf_r[100];
- int fd;
- int nread;
-
- printf("Preparing for reading bytes...\n");
- memset(buf_r,0,sizeof(buf_r));
-
- /* 打開管道 */
- fd=open(FIFO,O_RDONLY|O_NONBLOCK,0);
- if(fd==-1)
- {
- perror("open");
- exit(1);
- }
- while(1)
- {
- memset(buf_r,0,sizeof(buf_r));
-
- if((nread=read(fd,buf_r,100))==-1)
- {
- if(errno==EAGAIN)
- printf("no data yet\n");
- }
- printf("read %s from FIFO\n",buf_r);
- sleep(1);
- }
- /*后面三句話是不會被運行到的,但不會影響程序運行的效果當程序在上面的死循環(huán)中執(zhí)行時收到信號后會馬上結(jié)束運行而沒有執(zhí)行后面的三句話。這些會在后面的信號處理中講到,現(xiàn)在不理解沒有關(guān)系,這個問題留給大家學(xué)習了信號處理之后來解決。*/
- close(fd); //關(guān)閉管道
- pause(); /*暫停,等待信號*/
- unlink(FIFO); //刪除文件
- }
FIFO寫進程:
- /**********************************************************
- *實驗要求: 使用mkfifo創(chuàng)建有名管道并實現(xiàn)兩個進程之間的通訊。
- *功能描述: 創(chuàng)建一個進程,并在其中創(chuàng)建一個有名管道,并向其寫入數(shù)據(jù)。
- *日 期: 2010-9-17
- *作 者: 國嵌
- **********************************************************/
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <errno.h>
- #include <fcntl.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define FIFO_SERVER "/tmp/myfifo"
-
- /*
- * 程序入口
- * */
- int main(int argc,char** argv)
- {
- int fd;
- char w_buf[100];
- int nwrite;
-
- /*創(chuàng)建有名管道*/
- if((mkfifo(FIFO_SERVER,O_CREAT|O_EXCL|O_RDWR)<0)&&(errno!=EEXIST))
- {
- printf("cannot create fifoserver\n");
- }
-
- /*打開管道*/
- fd=open(FIFO_SERVER,O_WRONLY |O_NONBLOCK,0);
- if(fd==-1)
- {
- perror("open");
- exit(1);
- }
-
- /*入?yún)z測*/
- if(argc==1)
- {
- printf("Please send something\n");
- exit(-1);
- }
- strcpy(w_buf,argv[1]);
-
- /* 向管道寫入數(shù)據(jù) */
- if((nwrite=write(fd,w_buf,100))==-1)
- {
- if(errno==EAGAIN)
- printf("The FIFO has not been read yet.Please try later\n");
- }
- else
- {
- printf("write %s to the FIFO\n",w_buf);
- }
- close(fd); //關(guān)閉管道
- return 0;
- }
運行讀進程結(jié)果如下:
打開另外一個終端,運行寫進程結(jié)果如下:
同時讀進程結(jié)果發(fā)生變化如下:
本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請
點擊舉報。