Linux系統編程——處理序間通訊:管道(pipe),linuxpipe
管道的概述
管道也叫無名管道,它是是 UNIX 系統 IPC(處理序間通訊) 的最古老形式,所有的 UNIX 系統都支援這種通訊機制。
無名管道有如下特點:
1、半雙工,資料在同一時刻只能在一個方向上流動。
2、資料只能從管道的一端寫入,從另一端讀出。
3、寫入管道中的資料遵循先入先出的規則。
4、管道所傳送的資料是無格式的,這要求管道的讀出方與寫入方必須事先約定好資料的格式,如多少位元組算一個訊息等。
5、管道不是普通的檔案,不屬於某個檔案系統,其只存在於記憶體中。
6、管道在記憶體中對應一個緩衝區。不同的系統其大小不一定相同。
7、從管道讀資料是一次性操作,資料一旦被讀走,它就從管道中被拋棄,釋放空間以便寫更多的資料。
8、管道沒有名字,只能在具有公用祖先的進程(父進程與子進程,或者兩個兄弟進程,具有親緣關係)之間使用。
對於無名管道特點的理解,我們可以類比現實生活中管子,管子的一端塞東西,管子的另一端取東西。
無名管道是一種特殊類型的檔案,在應用程式層體現為兩個開啟的檔案描述符。
管道的操作
所需標頭檔:
#include <unistd.h>
int pipe(int filedes[2]);
功能:
建立無名管道。
參數:
filedes: 為 int 型數組的首地址,其存放了管道的檔案描述符 filedes[0]、filedes[1]。
當一個管道建立時,它會建立兩個檔案描述符 fd[0] 和 fd[1]。其中 fd[0] 固定用於讀管道,而 fd[1] 固定用於寫管道。一般檔案 I/O 的函數都可以用來操作管道( lseek() 除外)。
返回值:
成功:0
失敗:-1
下面我們寫這個一個例子,子進程通過無名管道給父進程傳遞一個字串資料:
#include <stdio.h>#include <string.h>#include <unistd.h>#include <stdlib.h>#include <sys/types.h>#include <sys/wait.h>int main(int argc, char *argv[]){int fd_pipe[2] = {0};pid_t pid;if( pipe(fd_pipe) < 0 ){// 建立無名管道perror("pipe");}pid = fork(); // 建立進程if( pid < 0 ){ // 出錯perror("fork");exit(-1);}if( pid == 0 ){ // 子進程char buf[] = "I am mike";// 往管道寫端寫資料write(fd_pipe[1], buf, strlen(buf));_exit(0);}else if( pid > 0){// 父進程wait(NULL);// 等待子進程結束,回收其資源char str[50] = {0};// 從管道裡讀資料read(fd_pipe[0], str, sizeof(str));printf("str=[%s]\n", str); // 列印資料}return 0;}
運行結果如下:
管道的特點
每個管道只有一個頁面作為緩衝區,該頁面是按照環形緩衝區的方式來使用的。這種訪問方式是典型的“生產者——消費者”模型。當“生產者”進程有大量的資料需要寫時,而且每當寫滿一個頁面就需要進行睡眠等待,等待“消費者”從管道中讀走一些資料,為其騰出一些空間。相應的,如果管道中沒有可讀資料,“消費者” 進程就要睡眠等待,具體過程如所示:
預設的情況下,從管道中讀寫資料,最主要的特點就是阻塞問題(這一特點應該記住),當管道裡沒有資料,另一個進程預設用 read() 函數從管道中讀資料是阻塞的。
測試代碼如下:
#include <stdio.h>#include <string.h>#include <unistd.h>#include <stdlib.h>#include <sys/types.h>#include <sys/wait.h>int main(int argc, char *argv[]){int fd_pipe[2] = {0};pid_t pid;if( pipe(fd_pipe) < 0 ){// 建立無名管道perror("pipe");}pid = fork(); // 建立進程if( pid < 0 ){ // 出錯perror("fork");exit(-1);}if( pid == 0 ){ // 子進程_exit(0);}else if( pid > 0){// 父進程wait(NULL);// 等待子進程結束,回收其資源char str[50] = {0};printf("before read\n");// 從管道裡讀資料,如果管道沒有資料, read()會阻塞read(fd_pipe[0], str, sizeof(str));printf("after read\n");printf("str=[%s]\n", str); // 列印資料}return 0;}
運行結果如下:
當然,我們編程時可通過 fcntl() 函數設定檔案的阻塞特性。
設定為阻塞:fcntl(fd, F_SETFL, 0);
設定為非阻塞:fcntl(fd, F_SETFL, O_NONBLOCK);
測試代碼如下:
#include <stdio.h>#include <string.h>#include <unistd.h>#include <stdlib.h>#include <sys/types.h>#include <sys/wait.h>#include <fcntl.h>int main(int argc, char *argv[]){int fd_pipe[2] = {0};pid_t pid;if( pipe(fd_pipe) < 0 ){// 建立無名管道perror("pipe");}pid = fork(); // 建立進程if( pid < 0 ){ // 出錯perror("fork");exit(-1);}if( pid == 0 ){ // 子進程sleep(3);char buf[] = "hello, mike";write(fd_pipe[1], buf, strlen(buf)); // 寫資料_exit(0);}else if( pid > 0){// 父進程fcntl(fd_pipe[0], F_SETFL, O_NONBLOCK); // 非阻塞//fcntl(fd_pipe[0], F_SETFL, 0); // 阻塞while(1){char str[50] = {0};read( fd_pipe[0], str, sizeof(str) );//讀資料printf("str=[%s]\n", str);sleep(1);}}return 0;}
運行結果如下:
預設的情況下,從管道中讀寫資料,還有如下特點(知道有這麼回事就夠了,不用刻意去記這些特點):
1)調用 write() 函數向管道裡寫資料,當緩衝區已滿時 write() 也會阻塞。
測試代碼如下:
#include <stdio.h>#include <string.h>#include <unistd.h>#include <stdlib.h>#include <sys/types.h>#include <sys/wait.h>int main(int argc, char *argv[]){int fd_pipe[2] = {0};pid_t pid;char buf[1024] = {0};memset(buf, 'a', sizeof(buf)); // 往管道寫的內容int i = 0;if( pipe(fd_pipe) < 0 ){// 建立無名管道perror("pipe");}pid = fork(); // 建立進程if( pid < 0 ){ // 出錯perror("fork");exit(-1);}if( pid == 0 ){ // 子進程while(1){write(fd_pipe[1], buf, sizeof(buf));i++;printf("i ======== %d\n", i);}_exit(0);}else if( pid > 0){// 父進程wait(NULL);// 等待子進程結束,回收其資源}return 0;}
運行結果如下:
2)通訊過程中,當前進程讀連接埠關閉後,向管道內寫資料時,write() 所在進程會(收到 SIGPIPE 訊號)退出,收到 SIGPIPE 預設動作為中斷當前進程。
測試代碼如下:
#include <stdio.h>#include <string.h>#include <unistd.h>#include <stdlib.h>#include <sys/types.h>#include <sys/wait.h>int main(int argc, char *argv[]){int fd_pipe[2] = {0};pid_t pid;if( pipe(fd_pipe) < 0 ){// 建立無名管道perror("pipe");}pid = fork(); // 建立進程if( pid < 0 ){ // 出錯perror("fork");exit(-1);}if( pid == 0 ){ // 子進程//close(fd_pipe[0]);_exit(0);}else if( pid > 0 ){// 父進程wait(NULL);// 等待子進程結束,回收其資源close(fd_pipe[0]); // 當前進程讀連接埠關閉char buf[50] = "12345";// 當前進程讀連接埠關閉// write()會收到 SIGPIPE 訊號,預設動作為中斷當前進程write(fd_pipe[1], buf, strlen(buf));while(1);// 阻塞}return 0;}
運行結果如下:
本教程範例程式碼下載請點此處。