int pipe(int pipefd[2]);
pipefd[0] : 表示讀管道 pipefd[1] : 表示寫管道 返回 0表示成功,非零表示建立失敗。linux
代碼事例數組
//匿名管道 int main() { int fds[2]; int len; char buf[100]={}; if(pipe(fds)==-1) //建立管道 perror("pipe"),exit(1); while(fgets(buf,100,stdin)) { len = strlen(buf); if(write(fds[1],buf,len)==-1) //把內容寫進管道 perror("write"),exit(1); memset(buf,0x00,sizeof(char)*100); if(read(fds[0],buf,len)==-1) //從管道里面讀取內容到數組中 perror("read"),exit(1); if(write(1,buf,len)==-1) //把從管道里讀出的內容寫到標準輸出 perror("write"),exit(1); } return 0; }
結果展現微信
平常運用事例 who | wc -l
這樣的事例咱們常常用到,用管道鏈接命令會令你駕輕就熟。函數
圖片解析spa
####利用管道進行父子進程通訊命令行
圖片解析原理 代碼示例:3d
//父子進程通訊 int main() { char buf[1024]="change world!\n"; int fds[2]; if(pipe(fds)==-1) perror("pipe"),exit(1); pid_t pid = fork(); //建立匿名管道 if(pid==0) { close(fds[0]); //關閉管道讀描述符 if(write(fds[1],buf,1024)==-1) //寫進管道 perror("write"),exit(1); close(fds[1]); exit(1); } else { memset(buf,0x00,1024); close(fds[1]); //關閉管道寫描述符 if(read(fds[0],buf,1024)==-1) //從管道讀內容 perror("read"),exit(1); if(write(1,buf,1024)==-1) perror("write"),exit(1); close(fds[0]); exit(1); } return 0; }
結果 詳細過程圖解
code
####管道讀寫規則blog
當沒有數據可讀時生命週期
當管道滿的時候
咱們剛剛能夠用匿名管道在父子進程之間通訊,那若是是兩個不想光的進程之間該如何通訊呢?
在命令行能夠直接建立mkfifo filename
也能夠在程序內部建立,相關函數
int mkfifo(const char *pathname, mode_t mode);
代碼示例:
int main() { mkfifo("my.p",0644); return 0; }
####無關進程之間通訊代碼示例
從標準輸入讀入內容進管道
#include<string.h> #include<stdlib.h> #include<unistd.h> #include<sys/types.h> #include<sys/stat.h> #include<fcntl.h> int main() { mkfifo("my.p",0664); int outfd = open("my.p",O_WRONLY); if(outfd==-1) perror("open my.txt"),exit(1); char buf[1024]={}; int n = 0; while(fgets(buf,1024,stdin)) { write(outfd,buf,1024); memset(buf,0x00,1024); } close(outfd);
從管道中讀內容,標準輸出輸出
#include<string.h> #include<stdlib.h> #include<unistd.h> #include<sys/types.h> #include<sys/stat.h> #include<fcntl.h> int main() { int infd = open("my.p",O_RDONLY); if(infd==-1) perror("open my.p"),exit(1); char buf[1024]={}; int n = 0; while((n = read(infd,buf,1024))>0) { write(1,buf,n); memset(buf,0x00,1024); } close(infd); unlink("my.p"); //刪除管道 return 0; }
運行結果: 這裏就利用管道實現了兩個無關進程之間的通訊。
###匿名管道和命名管道的區別。
精選文章都同步在公衆號裏面,公衆號看起會更方便,隨時隨地想看就看。微信搜索 龍躍十二 或者掃碼便可訂閱。
<p align="center"><image src="https://tva1.sinaimg.cn/large/006tNbRwly1galsp9a07kj30p00dwae3.jpg" ></image></p>