Posted on 2009-07-18 13:51
天邊藍 閱讀(1983)
評論(0) 編輯 收藏 引用 所屬分類:
Linux
管道簡介
常說的管道都是匿名半雙工管道,匿名半雙工管道在系統(tǒng)中是沒有實名的,并不可以在文件系統(tǒng)中以任何方式看到該管道。它只是進程的一種資源,會隨著進程的結(jié)束而被系統(tǒng)清除。管道通信是在UNIX系統(tǒng)中應(yīng)用比較頻繁的一種方式,例如使用grep查找:ls |grep ipc
顧名思義,匿名半雙工管道沒有名字,這個特性導(dǎo)致了它在應(yīng)用方面的局限性:
1.只能在有具有公共祖先的進程間通信,即或是父子關(guān)系進程間、或是在兄弟關(guān)系進程間通信。
2.數(shù)據(jù)只能在一個方向上移動
管道創(chuàng)建
盡管這樣,半雙工管道還是Linux系統(tǒng)上最常見的通信方式。Linux系統(tǒng)用pipe函數(shù)創(chuàng)建一個半雙工管道,期函數(shù)原型為:
include<unistd.h>
int pipe(int fd[2]);
參數(shù)fd是一個長度為2的文件描述符,fd[1]為輸入文件描述符,fd[1]為輸出文件描述符;函數(shù)返回類型為int,0表示成功,-1表示失敗。當(dāng)成功返回時,則自動維護一個從fd[1]到fd[0]的同行管道。
管道操作
對管道進行讀寫,可以通過write和read進行:
write(fd[1], "create the pipe successfully !\n", 31 ); /*向管道寫入端寫入數(shù)據(jù)*/
char str[256];
read(fd[0], str, sizeof(str) ); /*從管道讀出端讀出數(shù)據(jù)*/
讀寫時,需要注意的問題:
1.當(dāng)輸出端關(guān)閉時,對輸入端的寫操作會產(chǎn)生信號SIGPIPE,說明管道讀端已經(jīng)關(guān)閉,并且write操作返回為–1,errno的值為EPIPE,對于SIGPIPE信號可以進行捕捉處理。
2.read返回0說明管道中沒數(shù)據(jù),但不能說明輸入端是否關(guān)閉
操作流程
1.創(chuàng)建管道
2.利用fork創(chuàng)建子進程
3.控制管道流向
4.從fd[1]向管道寫信息
5.通過fd[0]從管道渡信息
創(chuàng)建管道的標(biāo)準(zhǔn)庫函數(shù)
#include <stdio.h>
FILE *popen( const char * command, const char *mode );
int pclose ( FILE *stream );
示例
引用《Linux C程序設(shè)計大全》 中的例子
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <sys/types.h>
#define BUFES PIPE_BUF /* PIPE_BUF管道默認一次性讀寫的數(shù)據(jù)長度*/
int main ( void ) {
int fd[2];
char buf[BUFSZ];
pid_t pid;
int len;
if ( (pipe(fd)) < 0 ){ /*創(chuàng)建管道*/
perror ( "failed to pipe" );
exit( 1 );
}
if ( (pid = fork()) < 0 ){ /* 創(chuàng)建一個子進程 */
perror ( "failed to fork " );
exit( 1 );
}
else if ( pid > 0 ){
close ( fd[0] ); /*父進程中關(guān)閉管道的讀出端*/
write (fd[1], "hello my son!\n", 14 ); /*父進程向管道寫入數(shù)據(jù)*/
exit ( 0);
}
else {
close ( fd[1] ); /*子進程關(guān)閉管道的寫入端*/
len = read (fd[0], buf, BUFS ); /*子進程從管道中讀出數(shù)據(jù)*/
if ( len < 0 ){
perror ( "process failed when read a pipe " );
exit( 1 );
}
else
write(STDOUT_FILENO, buf, len); /*輸出到標(biāo)準(zhǔn)輸出*/
exit(0);
}
}