Linux 命名管道FIFO

2021-07-27 01:38:19 字數 1292 閱讀 7007

管道的乙個不足之處是沒有名字,因此,只能用於具有親緣關係的程序間通訊,在命名管

道(named pipe或fifo)提出後,該限制得到了克服。fifo不同於管道之處在於它提供一

個路徑名與之關聯,以fifo的檔案形式儲存於檔案系統中。命名管道是乙個裝置檔案,因

此,即使程序與建立fifo的程序不存在親緣關係,只要可以訪問該路徑,就能夠通過fifo

相互通訊。值得注意的是,fifo(first input first output)總是按照先進先出的原則工作,第一

個被寫入的資料將首先從管道中讀出。

管道read端

#include#include#include#include#include#include#define _path_ ". "

#define _size_ 100

int main()

char buf[_size_];

memset(buf,'\0',sizeof(buf));

while(1)

printf("%s\n",buf);

if(strncmp(buf,"quit",4)==0)

break;

} close(fd);

return 0;

}

管道write端

#include#include#include#include#include#include#define _path_ ". "

#define _size_ 100

int main()

int fd=open(_path_,o_wronly);

if(fd<0)

char buf[_size_];

memset(buf,'\0',sizeof(buf));

while(1)

if(strncmp(buf,"quit",4)==0)

break; }

close(fd);

return 0;

}

測試結果進入

輸入內容

由於linux中所有的事物都可被視為檔案,所以對命名管道的使用也就變得與檔案操作非常

的統一,也使它的使用非常方便,同時我們也可以像平常的檔名一樣在命令中使使。

linux命名管道 fifo

除了建立方式,fifo檔案與管道極為相似。fifo是非匿名的管道,通過系統呼叫mkfifo 存在於檔案系統中。函式名稱 int mkfifo const char pathname,mode t mode 標頭檔案函式引數 說明pathname 建立fifo所用的路徑名 mode 指定fifo的屬性...

命名管道(FIFO)

client.c include include include include include define path home chen 7month 7 28 fifo my fifo 這個就是管道的名字,和建立的路徑 int main char buf 1024 while 1 close ...

命名管道 FIFO

解決了無關程序不能使用管道通訊的問題。pipe是線性的,乙個pipe只能有乙個輸入輸出,fifo是非線性的,乙個fifo可以有多個輸入或輸出。fifo的用途 1 shell命令使用fifo將資料從一條管道傳送到另一條時,無需建立中間臨時檔案。2 客戶程序 伺服器程序應用程式中,fifo用作匯聚點,在...