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