#include <unistd.h> #include <stdio.h> #include <sys/types.h> #include <stdlib.h> #include <errno.h> int main(int argc,char *argv[]) { int n, fd[2]; pid_t pid; char buffer[25]; //緩沖區 if(pipe(fd)<0) //創建一個管道,兩個文件描述符在fd數組中 { printf("創建管道失敗!\n "); exit(0); } if((pid=fork())<0) //創建一個子進程 { printf("創建子進程失敗!\n "); exit(0); } else if (pid>0) //父進程 { close(fd[0]); write(fd[1],"This is a pipe test!\n",22); //向管道寫入數據,注意回車換行符 } else //子進程 { close(fd[1]); //關閉 n = read(fd[0],buffer,25); //從通道中讀出數據 printf("%s",buffer); //將數據寫到標準輸出設備 } exit(0); }
新聞標題:[Linux管道和IPC]在父子進程中使用管道
URL網址:http://vcdvsql.cn/article48/gjephp.html
成都網站建設公司_創新互聯,為您提供電子商務、域名注冊、關鍵詞優化、網站設計、網頁設計公司、網站維護
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯