Linux系統(tǒng)編程—有名管道
一、管道的概念
管道,又名「無(wú)名管理」,或「匿名管道」,管道是一種非?;?,也是使用非常頻繁的IPC方式。
1. 管道本質(zhì)
- 管道的本質(zhì)也是一種文件,不過(guò)是偽文件,實(shí)際上是一塊內(nèi)核緩沖區(qū),大小4K;
 - 管道創(chuàng)建以后會(huì)產(chǎn)生兩個(gè)文件描述符,一個(gè)是讀端,另一個(gè)是寫(xiě)端;
 - 管道里的數(shù)據(jù)只能從寫(xiě)端被寫(xiě)入,從讀端被讀出;
 
1. 管道原理
管道是內(nèi)核的一塊緩沖區(qū),更具體一些,是一個(gè)環(huán)形隊(duì)列。數(shù)據(jù)從隊(duì)列的一端寫(xiě)入數(shù)據(jù),另一端讀出,如下圖示:
3. 管道的優(yōu)點(diǎn)
簡(jiǎn)單
4. 管道的缺點(diǎn)
- 只能單向通信,如果需要雙向通信則需要建立兩個(gè)管道;
 - 只能應(yīng)用于具有血緣關(guān)系的進(jìn)程,如父子進(jìn)程;
 - 緩沖區(qū)大小受限,通常為1頁(yè),即4k;
 
二、管道的創(chuàng)建
管道創(chuàng)建三步曲:
- 父進(jìn)程調(diào)用pipe函數(shù)創(chuàng)建管道;
 - 父進(jìn)程調(diào)用fork函數(shù)創(chuàng)建子進(jìn)程;
 - 父進(jìn)程關(guān)閉fd[0],子進(jìn)程關(guān)閉fd[1];
 
具體如下圖所示:
三、管道的讀寫(xiě)行為
- 管道的緩沖區(qū)大小固定為4k,所以如果管道內(nèi)數(shù)據(jù)已經(jīng)寫(xiě)滿,則無(wú)法再寫(xiě)入數(shù)據(jù),進(jìn)程的write調(diào)用將阻塞,直到有足夠的空間再寫(xiě)入數(shù)據(jù);
 - 管道的讀動(dòng)作比寫(xiě)動(dòng)作要快,數(shù)據(jù)一旦被讀走了,管道將釋放相應(yīng)的空間,以便后續(xù)數(shù)據(jù)的寫(xiě)入。當(dāng)所有的數(shù)據(jù)都讀完之后,進(jìn)程的read()調(diào)用將阻塞,直到有數(shù)據(jù)再次寫(xiě)入。
 
四、例程
父子間通信:
- #include <stdio.h>
 - #include <sys/types.h>
 - #include <unistd.h>
 - #include <string.h>
 - int main()
 - {
 - int fd[2];
 - pid_t pid;
 - char buf[1024];
 - char *data = "hello world!";
 - /* 創(chuàng)建管道 */
 - if (pipe(fd) == -1) {
 - printf("ERROR: pipe create failed!\n");
 - return -1;
 - }
 - pid = fork();
 - if (pid == 0) {
 - /* 子進(jìn)程 */
 - close(fd[1]); // 子進(jìn)程讀取數(shù)據(jù),關(guān)閉寫(xiě)端
 - read(fd[0], buf, sizeof(buf)); // 從管道讀數(shù)據(jù)
 - printf("child process read: %s\n", buf);
 - close(fd[0]);
 - } else if (pid > 0) {
 - /* 父進(jìn)程 */
 - close(fd[0]); //父進(jìn)程寫(xiě)數(shù)據(jù),關(guān)閉讀端
 - write(fd[1], data, strlen(data)); // 向管道寫(xiě)數(shù)據(jù)
 - printf("parent process write: %s\n", data);
 - close(fd[1]);
 - }
 - return 0;
 - }
 
兄弟間通信:
- #include <stdio.h>
 - #include <sys/types.h>
 - #include <unistd.h>
 - #include <string.h>
 - #include <sys/wait.h>
 - int main ()
 - {
 - int fd[2];
 - int i = 0;
 - pid_t pid;
 - char buf[1024];
 - char *data = "hello world!";
 - /* 創(chuàng)建管道 */
 - if (pipe(fd) == -1) {
 - printf("ERROR: pipe create failed!\n");
 - return -1;
 - }
 - for (i = 0; i < 2; i++) {
 - pid = fork();
 - if (pid == -1) {
 - printf("ERROR: fork error!\n");
 - return -1;
 - } else if (pid == 0) {
 - break;
 - }
 - }
 - /* 通過(guò)i來(lái)判斷創(chuàng)建的子進(jìn)程及父進(jìn)程 */
 - if (i == 0) {
 - /* 第一個(gè)子進(jìn)程,兄進(jìn)程 */
 - close(fd[0]); // 兄進(jìn)程向弟進(jìn)程寫(xiě)數(shù)據(jù),關(guān)閉讀端
 - write(fd[1], data, strlen(data));
 - printf("elder brother send: %s\n", data);
 - close(fd[1]);
 - } else if (i == 1) {
 - /* 第二個(gè)子進(jìn)程,弟進(jìn)程 */
 - close(fd[1]);
 - read(fd[0], buf, sizeof(buf));
 - printf("younger brother receive: %s\n", buf);
 - close(fd[0]);
 - } else {
 - /* 父進(jìn)程 */
 - close(fd[0]);
 - close(fd[1]);
 - for (i = 0; i < 2; i++) {
 - wait(NULL);
 - }
 - }
 - return 0;
 - }
 
本文授權(quán)轉(zhuǎn)載自公眾號(hào)「良許Linux」。良許,世界500強(qiáng)外企Linux開(kāi)發(fā)工程師,公眾號(hào)里分享大量Linux干貨,歡迎關(guān)注!


















 
 
 


 
 
 
 