linux管道通訊(pipe)

2021-06-22 08:48:14 字數 390 閱讀 1198

linux pipe適合於父子程序之間進行通訊。如下面**所示:

#include #include #include int main()

/*create sub-process*/

pid = fork();

if (-1 == pid)

else if (0 == pid)

else

return 0;

}

當呼叫fork函式後,fork將會返回兩個值(記住記性了,是兩個值)。

當返回值為 -1時,表示fork函式呼叫失敗。

當返回值為0時, 表示子程序。

當返回值》0時,表示父程序。

下面推薦乙個寫的很詳細的blog:

pipe管道通訊

pipe函式是用來建立管道的,當它建立成功會返回兩個檔案描述符,0是讀,1是寫 在用管道時,當我們使用讀端就要關閉寫,使用寫端就要關閉讀的功能。include include include include int main double shou 2 int i 0 int ret double ...

無名管道(pipe)通訊

這個程式用無名管道實現命令 cat etc passwd grep root 其實在終端命令中 就是乙個管道 cat etc passwd會把結果列印到標準輸出 grep root 會把結果從標準輸入 實現流程 1.父程序生產兩個子程序 程序扇的概念 2.子程序a a 把標準輸出定位到管道寫端,因為...

Linux程序間通訊之管道 pipe

管道是unix系統ipc的最古老的形式,並且所有unix系統都提供此種通訊機制。但是管道存在如下特點 管道是半雙工的。管道只能用在具有公共祖先的程序之間。管道的建立 管道是通過呼叫pipe函式建立的 include int pipe int filedes 2 引數filedes 2 是兩個檔案描述...