溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Linux中有名管道是什么意思

發(fā)布時(shí)間:2021-10-27 11:35:31 來源:億速云 閱讀:190 作者:小新 欄目:系統(tǒng)運(yùn)維

這篇文章主要介紹Linux中有名管道是什么意思,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

一、管道的概念

管道,又名「無名管理」,或「匿名管道」,管道是一種非?;荆彩鞘褂梅浅nl繁的IPC方式。

Linux中有名管道是什么意思

1. 管道本質(zhì)

  • 管道的本質(zhì)也是一種文件,不過是偽文件,實(shí)際上是一塊內(nèi)核緩沖區(qū),大小4K;

  • 管道創(chuàng)建以后會(huì)產(chǎn)生兩個(gè)文件描述符,一個(gè)是讀端,另一個(gè)是寫端;

  • 管道里的數(shù)據(jù)只能從寫端被寫入,從讀端被讀出;

1. 管道原理

管道是內(nèi)核的一塊緩沖區(qū),更具體一些,是一個(gè)環(huán)形隊(duì)列。數(shù)據(jù)從隊(duì)列的一端寫入數(shù)據(jù),另一端讀出,如下圖示:

Linux中有名管道是什么意思

3. 管道的優(yōu)點(diǎn)

簡(jiǎn)單

4.  管道的缺點(diǎn)

  • 只能單向通信,如果需要雙向通信則需要建立兩個(gè)管道;

  • 只能應(yīng)用于具有血緣關(guān)系的進(jìn)程,如父子進(jìn)程;

  • 緩沖區(qū)大小受限,通常為1頁,即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];

具體如下圖所示:

Linux中有名管道是什么意思

三、管道的讀寫行為

  • 管道的緩沖區(qū)大小固定為4k,所以如果管道內(nèi)數(shù)據(jù)已經(jīng)寫滿,則無法再寫入數(shù)據(jù),進(jìn)程的write調(diào)用將阻塞,直到有足夠的空間再寫入數(shù)據(jù);

  • 管道的讀動(dòng)作比寫動(dòng)作要快,數(shù)據(jù)一旦被讀走了,管道將釋放相應(yīng)的空間,以便后續(xù)數(shù)據(jù)的寫入。當(dāng)所有的數(shù)據(jù)都讀完之后,進(jìn)程的read()調(diào)用將阻塞,直到有數(shù)據(jù)再次寫入。

四、例程

父子間通信:

#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)閉寫端         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)程寫數(shù)據(jù),關(guān)閉讀端         write(fd[1], data, strlen(data));   // 向管道寫數(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;         }     }      /* 通過i來判斷創(chuàng)建的子進(jìn)程及父進(jìn)程 */     if (i == 0) {         /* 第一個(gè)子進(jìn)程,兄進(jìn)程 */         close(fd[0]);   // 兄進(jìn)程向弟進(jìn)程寫數(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; }

以上是“Linux中有名管道是什么意思”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI