1、 管道概述及相關(guān)API應(yīng)用
1.1 管道相關(guān)的關(guān)鍵概念
管道是Linux支持的最初Unix IPC形式之一,具有以下特點(diǎn):
- 管道是半雙工的,數(shù)據(jù)只能向一個(gè)方向流動;需要雙方通信時(shí),需要建立起兩個(gè)管道;
- 只能用于父子進(jìn)程或者兄弟進(jìn)程之間(具有親緣關(guān)系的進(jìn)程);
- 單獨(dú)構(gòu)成一種獨(dú)立的文件系統(tǒng):管道對于管道兩端的進(jìn)程而言,就是一個(gè)文件,但它不是普通的文件,它不屬于某種文件系統(tǒng),而是自立門戶,單獨(dú)構(gòu)成一種文件系統(tǒng),并且只存在與內(nèi)存中。
- 數(shù)據(jù)的讀出和寫入:一個(gè)進(jìn)程向管道中寫的內(nèi)容被管道另一端的進(jìn)程讀出。寫入的內(nèi)容每次都添加在管道緩沖區(qū)的末尾,并且每次都是從緩沖區(qū)的頭部讀出數(shù)據(jù)。
1.2管道的創(chuàng)建:
#include <unistd.h>
int pipe(int fd[2])
該函數(shù)創(chuàng)建的管道的兩端處于一個(gè)進(jìn)程中間,在實(shí)際應(yīng)用中沒有太大意義,因此,一個(gè)進(jìn)程在由pipe()創(chuàng)建管道后,一般再fork一個(gè)子進(jìn)程,然后通過管道實(shí)現(xiàn)父子進(jìn)程間的通信(因此也不難推出,只要兩個(gè)進(jìn)程中存在親緣關(guān)系,這里的親緣關(guān)系指的是具有共同的祖先,都可以采用管道方式來進(jìn)行通信)。
1.3管道的讀寫規(guī)則:
管道兩端可分別用描述字fd[0]以及fd[1]來描述,需要注意的是,管道的兩端是固定了任務(wù)的。即一端只能用于讀,由描述字fd[0]表示,稱其為管道讀端;另一端則只能用于寫,由描述字fd[1]來表示,稱其為管道寫端。如果試圖從管道寫端讀取數(shù)據(jù),或者向管道讀端寫入數(shù)據(jù)都將導(dǎo)致錯(cuò)誤發(fā)生。一般文件的I/O函數(shù)都可以用于管道,如close、read、write等等。
從管道中讀取數(shù)據(jù):
- 如果管道的寫端不存在,則認(rèn)為已經(jīng)讀到了數(shù)據(jù)的末尾,讀函數(shù)返回的讀出字節(jié)數(shù)為0;
- 當(dāng)管道的寫端存在時(shí),如果請求的字節(jié)數(shù)目大于PIPE_BUF,則返回管道中現(xiàn)有的數(shù)據(jù)字節(jié)數(shù),如果請求的字節(jié)數(shù)目不大于PIPE_BUF,則返回管道中現(xiàn)有數(shù)據(jù)字節(jié)數(shù)(此時(shí),管道中數(shù)據(jù)量小于請求的數(shù)據(jù)量);或者返回請求的字節(jié)數(shù)(此時(shí),管道中數(shù)據(jù)量不小于請求的數(shù)據(jù)量)。注:(PIPE_BUF在include/linux/limits.h中定義,不同的內(nèi)核版本可能會有所不同。Posix.1要求PIPE_BUF至少為512字節(jié),red hat 7.2中為4096)。
關(guān)于管道的讀規(guī)則驗(yàn)證:
/**************
* readtest.c *
**************/
#include <unistd.h>
#include <sys/types.h>
#include <errno.h>
main()
{
int pipe_fd[2];
pid_t pid;
char r_buf[100];
char w_buf[4];
char* p_wbuf;
int r_num;
int cmd;
memset(r_buf,0,sizeof(r_buf));
memset(w_buf,0,sizeof(r_buf));
p_wbuf=w_buf;
if(pipe(pipe_fd)<0)
{
printf("pipe create error\n");
return -1;
}
if((pid=fork())==0)
{
printf("\n");
close(pipe_fd[1]);
sleep(3);//確保父進(jìn)程關(guān)閉寫端
r_num=read(pipe_fd[0],r_buf,100);
printf( "read num is %d the data read from the pipe is %d\n",r_num,atoi(r_buf));
close(pipe_fd[0]);
exit();
}
else if(pid>0)
{
close(pipe_fd[0]);//read
strcpy(w_buf,"111");
if(write(pipe_fd[1],w_buf,4)!=-1)
printf("parent write over\n");
close(pipe_fd[1]);//write
printf("parent close fd[1] over\n");
sleep(10);
}
}
/**************************************************
* 程序輸出結(jié)果:
* parent write over
* parent close fd[1] over
* read num is 4 the data read from the pipe is 111
* 附加結(jié)論:
* 管道寫端關(guān)閉后,寫入的數(shù)據(jù)將一直存在,直到讀出為止.
****************************************************/
向管道中寫入數(shù)據(jù):
- 向管道中寫入數(shù)據(jù)時(shí),linux將不保證寫入的原子性,管道緩沖區(qū)一有空閑區(qū)域,寫進(jìn)程就會試圖向管道寫入數(shù)據(jù)。如果讀進(jìn)程不讀走管道緩沖區(qū)中的數(shù)據(jù),那么寫操作將一直阻塞。
注:只有在管道的讀端存在時(shí),向管道中寫入數(shù)據(jù)才有意義。否則,向管道中寫入數(shù)據(jù)的進(jìn)程將收到內(nèi)核傳來的SIFPIPE信號,應(yīng)用程序可以處理該信號,也可以忽略(默認(rèn)動作則是應(yīng)用程序終止)。
對管道的寫規(guī)則的驗(yàn)證1:寫端對讀端存在的依賴性
#include <unistd.h>
#include <sys/types.h>
main()
{
int pipe_fd[2];
pid_t pid;
char r_buf[4];
char* w_buf;
int writenum;
int cmd;
memset(r_buf,0,sizeof(r_buf));
if(pipe(pipe_fd)<0)
{
printf("pipe create error\n");
return -1;
}
if((pid=fork())==0)
{
close(pipe_fd[0]);
close(pipe_fd[1]);
sleep(10);
exit();
}
else if(pid>0)
{
sleep(1); //等待子進(jìn)程完成關(guān)閉讀端的操作
close(pipe_fd[0]);//write
w_buf="111";
if((writenum=write(pipe_fd[1],w_buf,4))==-1)
printf("write to pipe error\n");
else
printf("the bytes write to pipe is %d \n", writenum);
close(pipe_fd[1]);
}
}
則輸出結(jié)果為: Broken pipe,原因就是該管道以及它的所有fork()產(chǎn)物的讀端都已經(jīng)被關(guān)閉。如果在父進(jìn)程中保留讀端,即在寫完pipe后,再關(guān)閉父進(jìn)程的讀端,也會正常寫入pipe,讀者可自己驗(yàn)證一下該結(jié)論。因此,在向管道寫入數(shù)據(jù)時(shí),至少應(yīng)該存在某一個(gè)進(jìn)程,其中管道讀端沒有被關(guān)閉,否則就會出現(xiàn)上述錯(cuò)誤(管道斷裂,進(jìn)程收到了SIGPIPE信號,默認(rèn)動作是進(jìn)程終止)
對管道的寫規(guī)則的驗(yàn)證2:linux不保證寫管道的原子性驗(yàn)證
結(jié)論:
寫入數(shù)目小于4096時(shí)寫入是非原子的!
如果把父進(jìn)程中的兩次寫入字節(jié)數(shù)都改為5000,則很容易得出下面結(jié)論:
寫入管道的數(shù)據(jù)量大于4096字節(jié)時(shí),緩沖區(qū)的空閑空間將被寫入數(shù)據(jù)(補(bǔ)齊),直到寫完所有數(shù)據(jù)為止,如果沒有進(jìn)程讀數(shù)據(jù),則一直阻塞。
1.4管道應(yīng)用實(shí)例:
實(shí)例一:用于shell
管道可用于輸入輸出重定向,它將一個(gè)命令的輸出直接定向到另一個(gè)命令的輸入。比如,當(dāng)在某個(gè)shell程序(Bourne shell或C shell等)鍵入who│wc -l后,相應(yīng)shell程序?qū)?chuàng)建who以及wc兩個(gè)進(jìn)程和這兩個(gè)進(jìn)程間的管道。考慮下面的命令行:
$kill -l
$kill -l | grep SIGRTMIN
實(shí)例二:用于具有親緣關(guān)系的進(jìn)程間通信
下面例子給出了管道的具體應(yīng)用,父進(jìn)程通過管道發(fā)送一些命令給子進(jìn)程,子進(jìn)程解析命令,并根據(jù)命令作相應(yīng)處理。
#include <unistd.h>
#include <sys/types.h>
main()
{
int pipe_fd[2];
pid_t pid;
char r_buf[4];
char* w_buf;
int writenum;
int cmd;
memset(r_buf,0,sizeof(r_buf));
if(pipe(pipe_fd)<0)
{
printf("pipe create error\n");
return -1;
}
if((pid=fork())==0)
{
close(pipe_fd[0]);
close(pipe_fd[1]);
sleep(10);
exit();
}
else if(pid>0)
{
sleep(1); //等待子進(jìn)程完成關(guān)閉讀端的操作
close(pipe_fd[0]);//write
w_buf="111";
if((writenum=write(pipe_fd[1],w_buf,4))==-1)
printf("write to pipe error\n");
else
printf("the bytes write to pipe is %d \n", writenum);
close(pipe_fd[1]);
}
}
輸出結(jié)果:
the bytes write to pipe 1000
the bytes write to pipe 1000 //注意,此行輸出說明了寫入的非原子性
the bytes write to pipe 1000
the bytes write to pipe 1000
the bytes write to pipe 1000
the bytes write to pipe 120 //注意,此行輸出說明了寫入的非原子性
the bytes write to pipe 0
the bytes write to pipe 0
......
1.5 管道的局限性
管道的主要局限性正體現(xiàn)在它的特點(diǎn)上:
- 只支持單向數(shù)據(jù)流;
- 只能用于具有親緣關(guān)系的進(jìn)程之間;
- 沒有名字;
- 管道的緩沖區(qū)是有限的(管道制存在于內(nèi)存中,在管道創(chuàng)建時(shí),為緩沖區(qū)分配一個(gè)頁面大小);
- 管道所傳送的是無格式字節(jié)流,這就要求管道的讀出方和寫入方必須事先約定好數(shù)據(jù)的格式,比如多少字節(jié)算作一個(gè)消息(或命令、或記錄)等等