<dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><s id="yhprb"><strike id="yhprb"></strike></s></dfn><small id="yhprb"></small><dfn id="yhprb"></dfn><small id="yhprb"><delect id="yhprb"></delect></small><small id="yhprb"></small><small id="yhprb"></small> <delect id="yhprb"><strike id="yhprb"></strike></delect><dfn id="yhprb"></dfn><dfn id="yhprb"></dfn><s id="yhprb"><noframes id="yhprb"><small id="yhprb"><dfn id="yhprb"></dfn></small><dfn id="yhprb"><delect id="yhprb"></delect></dfn><small id="yhprb"></small><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn> <small id="yhprb"></small><delect id="yhprb"><strike id="yhprb"></strike></delect><dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"><s id="yhprb"><strike id="yhprb"></strike></s></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn>

新聞中心

EEPW首頁(yè) > 嵌入式系統 > 設計應用 > Linux系統管道和有名管道的通信機制

Linux系統管道和有名管道的通信機制

作者: 時(shí)間:2007-05-15 來(lái)源:網(wǎng)絡(luò ) 收藏
進(jìn)程間通信的幾種主要手段。其中管道和有名管道是最早的進(jìn)程間通信機制之一,管道可用于具有親緣關(guān)系進(jìn)程間的通信,有名管道克服了管道沒(méi)有名字的限制,因此,除具有管道所具有的功能外,它還允許無(wú)親緣關(guān)系進(jìn)程間的通信。 認清管道和有名管道的讀寫(xiě)規則是在程序中應用它們的關(guān)鍵,本文在詳細討論了管道和有名管道的通信機制的基礎上,用實(shí)例對其讀寫(xiě)規則進(jìn)行了程序驗證,這樣做有利于增強讀者對讀寫(xiě)規則的感性認識,同時(shí)也提供了應用范例。

管道概述及相關(guān)API應用

管道相關(guān)的關(guān)鍵概念

管道是支持的最初Unix IPC形式之一,具有以下特點(diǎn):

管道是半雙工的,數據只能向一個(gè)方向流動(dòng);需要雙方通信時(shí),需要建立起兩個(gè)管道;

只能用于父子進(jìn)程或者兄弟進(jìn)程之間(具有親緣關(guān)系的進(jìn)程);

單獨構成一種獨立的文件系統:管道對于管道兩端的進(jìn)程而言,就是一個(gè)文件,但它不是普通的文件,它不屬于某種文件系統,而是自立門(mén)戶(hù),單獨構成一種文件系統,并且只存在與內存中。

數據的讀出和寫(xiě)入:一個(gè)進(jìn)程向管道中寫(xiě)的內容被管道另一端的進(jìn)程讀出。寫(xiě)入的內容每次都添加在管道緩沖區的末尾,并且每次都是從緩沖區的頭部讀出數據。

管道的創(chuàng )建:
#include int pipe(int fd[2])



該函數創(chuàng )建的管道的兩端處于一個(gè)進(jìn)程中間,在實(shí)際應用中沒(méi)有太大意義,因此,一個(gè)進(jìn)程在由pipe()創(chuàng )建管道后,一般再fork一個(gè)子進(jìn)程,然后通過(guò)管道實(shí)現父子進(jìn)程間的通信(因此也不難推出,只要兩個(gè)進(jìn)程中存在親緣關(guān)系,這里的親緣關(guān)系指的是具有共同的祖先,都可以采用管道方式來(lái)進(jìn)行通信)。

管道的讀寫(xiě)規則:

管道兩端可分別用描述字fd[0]以及fd[1]來(lái)描述,需要注意的是,管道的兩端是固定了任務(wù)的。即一端只能用于讀,由描述字fd[0]表示,稱(chēng)其為管道讀端;另一端則只能用于寫(xiě),由描述字fd[1]來(lái)表示,稱(chēng)其為管道寫(xiě)端。如果試圖從管道寫(xiě)端讀取數據,或者向管道讀端寫(xiě)入數據都將導致錯誤發(fā)生。一般文件的I/O函數都可以用于管道,如close、read、write等等。

從管道中讀取數據:如果管道的寫(xiě)端不存在,則認為已經(jīng)讀到了數據的末尾,讀函數返回的讀出字節數為0;當管道的寫(xiě)端存在時(shí),如果請求的字節數目大于PIPE_BUF,則返回管道中現有的數據字節數,如果請求的字節數目不大于PIPE_BUF,則返回管道中現有數據字節數(此時(shí),管道中數據量小于請求的數據量);或者返回請求的字節數(此時(shí),管道中數據量不小于請求的數據量)。注:(PIPE_BUF在include//limits.h中定義,不同的內核版本可能會(huì )有所不同。Posix.1要求PIPE_BUF至少為512字節,red hat 7.2中為4096)。

關(guān)于管道的讀規則驗證:

* readtest.c *
#include
#include
#include
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 errorn);
return -1;
}

if((pid=fork())==0)
{
printf(n);
close(pipe_fd[1]);
sleep(3);//確保父進(jìn)程關(guān)閉寫(xiě)端
r_num=read(pipe_fd[0],r_buf,100);
printf( read num is %d the data read from the pipe is %dn,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 overn);
close(pipe_fd[1]);//write
printf(parent close fd[1] overn);
sleep(10);
}
}







程序輸出結果:

* parent write over
* parent close fd[1] over
* read num is 4 the data read from the pipe is 111






附加結論:管道寫(xiě)端關(guān)閉后,寫(xiě)入的數據將一直存在,直到讀出為止。
linux操作系統文章專(zhuān)題:linux操作系統詳解(linux不再難懂)


關(guān)鍵詞: Linux

評論


相關(guān)推薦

技術(shù)專(zhuān)區

關(guān)閉
国产精品自在自线亚洲|国产精品无圣光一区二区|国产日产欧洲无码视频|久久久一本精品99久久K精品66|欧美人与动牲交片免费播放
<dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><s id="yhprb"><strike id="yhprb"></strike></s></dfn><small id="yhprb"></small><dfn id="yhprb"></dfn><small id="yhprb"><delect id="yhprb"></delect></small><small id="yhprb"></small><small id="yhprb"></small> <delect id="yhprb"><strike id="yhprb"></strike></delect><dfn id="yhprb"></dfn><dfn id="yhprb"></dfn><s id="yhprb"><noframes id="yhprb"><small id="yhprb"><dfn id="yhprb"></dfn></small><dfn id="yhprb"><delect id="yhprb"></delect></dfn><small id="yhprb"></small><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn> <small id="yhprb"></small><delect id="yhprb"><strike id="yhprb"></strike></delect><dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"><s id="yhprb"><strike id="yhprb"></strike></s></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn>