程序間通訊(一) 管道

2021-07-02 02:34:01 字數 890 閱讀 3594

一,用管道進行父子程序通訊

**:#include

#include

#define maxline 120

#define msginfo "hurry up !\n"

int main(void)

;pid_t pid;

if(pipe(fd)<0)

if((pid=fork())<0)

if(pid > 0)  //father

write(stdout_fileno, msginfo, strlen(msginfo));

}else  //chilld

read(stdin_fileno, msg, maxline);

printf("get msg:%s \n", msg);

}return 0;}

執行結果:

get msg:hurry up !

二,popen和pcloseh函式

popen啟動乙個shell程序,並執行輸入的命令,把執行結果通過file輸出

**:#include

#include

#define maxline 120

int main(void)

;file *fin;

if((fin = popen("more /tmp/text.txt", "r"))<0)

while(1)

fputs(">>", stdout);

fputs(text, stdout);

}if(pclose(fin) == -1)

return 0;

}執行結果:

>>how areyou ?

>>fine,and you?

>>fine too.

>>

Linux程序間通訊(一)管道

乙個程序在管道的尾部寫入資料,另乙個程序從管道的頭部讀出資料。管道包括無名管道和有名管道兩種,前者只能用於父程序和子程序間的通訊,後者可用於執行於同一系統中 的任意兩個程序間的通訊。管道通訊 特點 管道通訊是單向的,有固定的讀端和寫端。資料被程序從管道讀出後,在管道中該資料就不存在了。當程序去讀取空...

Linux IPC程序間通訊 一 管道

系列文章 linux ipc程序間通訊 一 管道 linux ipc程序間通訊 二 共享記憶體 linux ipc程序間通訊 三 訊號量 linux ipc程序間通訊 四 訊息佇列 linux程序間通訊 ipc 的乙個重要方法就是管道,在學習管道之前,要理解兩個概念 不論是無名管道還是有名管道,都屬...

IPC程序間通訊 一 管道

ipc inter process communication 管道 訊號 共享記憶體 訊息佇列 資訊量 套接字 管道分為 無名 匿名 管道和有名 命名 管道 區別 缺點 侷限於父子程序間的通訊且會發生阻塞 fcntl 步驟建立 pipe 函式原型 int pipe int filedes 2 引數...