【Linux系统编程】进程间通信之无名管道
00. 目錄
文章目錄
- 00. 目錄
- 01. 管道概述
- 02. 管道創(chuàng)建函數(shù)
- 03. 管道的特性
- 04. 管道設(shè)置非阻塞
- 05. 附錄
01. 管道概述
管道也叫無(wú)名管道,它是是 UNIX 系統(tǒng) IPC(進(jìn)程間通信) 的最古老形式,所有的 UNIX 系統(tǒng)都支持這種通信機(jī)制。
無(wú)名管道的特點(diǎn)
1、半雙工,數(shù)據(jù)在同一時(shí)刻只能在一個(gè)方向上流動(dòng)。
2、數(shù)據(jù)只能從管道的一端寫入,從另一端讀出。
3、寫入管道中的數(shù)據(jù)遵循先入先出的規(guī)則。
4、管道所傳送的數(shù)據(jù)是無(wú)格式的,這要求管道的讀出方與寫入方必須事先約定好數(shù)據(jù)的格式,如多少字節(jié)算一個(gè)消息等。
5、管道不是普通的文件,不屬于某個(gè)文件系統(tǒng),其只存在于內(nèi)存中。
6、管道在內(nèi)存中對(duì)應(yīng)一個(gè)緩沖區(qū)。不同的系統(tǒng)其大小不一定相同。
7、從管道讀數(shù)據(jù)是一次性操作,數(shù)據(jù)一旦被讀走,它就從管道中被拋棄,釋放空間以便寫更多的數(shù)據(jù)。
8、管道沒有名字,只能在具有公共祖先的進(jìn)程(父進(jìn)程與子進(jìn)程,或者兩個(gè)兄弟進(jìn)程,具有親緣關(guān)系)之間使用。
對(duì)于無(wú)名管道特點(diǎn)的理解,我們可以類比現(xiàn)實(shí)生活中管子,管子的一端塞東西,管子的另一端取東西。
無(wú)名管道是一種特殊類型的文件,在應(yīng)用層體現(xiàn)為兩個(gè)打開的文件描述符。
02. 管道創(chuàng)建函數(shù)
#include <unistd.h>int pipe(int pipefd[2]); 功能:創(chuàng)建無(wú)名管道。參數(shù):pipefd : 為 int 型數(shù)組的首地址,其存放了管道的文件描述符 pipefd[0]、pipefd[1]。當(dāng)一個(gè)管道建立時(shí),它會(huì)創(chuàng)建兩個(gè)文件描述符 fd[0] 和 fd[1]。其中 fd[0] 固定用于讀管道,而 fd[1] 固定用于寫管道。一般文件 I / O 的函數(shù)都可以用來(lái)操作管道(lseek() 除外)。返回值:成功:0失敗:-1下面我們寫這個(gè)一個(gè)例子,子進(jìn)程通過無(wú)名管道給父進(jìn)程傳遞字符串?dāng)?shù)據(jù)
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h>#define SIZE 64int main(void) {int fd[2];int i = 0;pid_t pid = -1;char buf[SIZE];//創(chuàng)建一個(gè)無(wú)名管道 用在具有共同祖先的進(jìn)程if (pipe(fd) == -1){perror("pipe"); goto err0;}printf("fd[0]: %d fd[1]: %d\n", fd[0], fd[1]);//創(chuàng)建子進(jìn)程pid = fork();if (-1 == pid){perror("fork"); goto err1;}else if (0 == pid){while(1){//子進(jìn)程 寫管道memset(buf, 0, SIZE); sprintf(buf, "hello uplooking %d", i++);write(fd[1], buf, strlen(buf));sleep(1);if (i >= 10)break;}//關(guān)閉描述符close(fd[0]);close(fd[1]);exit(0);}else{//父進(jìn)程 讀管道while(1){memset(buf, 0, SIZE); if (read(fd[0], buf, SIZE) <= 0)break;printf("\033[31mbuf: %s\033[0m\n", buf); }//關(guān)閉描述符close(fd[0]);close(fd[1]);}return 0; err1:close(fd[0]);close(fd[1]); err0:return 1; }測(cè)試結(jié)果:
deng@itcast:/mnt/hgfs/LinuxHome/code.bak2/4sys/4th/code$ gcc 17pipe.c deng@itcast:/mnt/hgfs/LinuxHome/code.bak2/4sys/4th/code$ ./a.out fd[0]: 3 fd[1]: 4 buf: hello uplooking 0 buf: hello uplooking 1 buf: hello uplooking 2 buf: hello uplooking 3 buf: hello uplooking 4 buf: hello uplooking 5 buf: hello uplooking 603. 管道的特性
每個(gè)管道只有一個(gè)頁(yè)面作為緩沖區(qū),該頁(yè)面是按照環(huán)形緩沖區(qū)的方式來(lái)使用的。這種訪問方式是典型的“生產(chǎn)者——消費(fèi)者”模型。當(dāng)“生產(chǎn)者”進(jìn)程有大量的數(shù)據(jù)需要寫時(shí),而且每當(dāng)寫滿一個(gè)頁(yè)面就需要進(jìn)行睡眠等待,等待“消費(fèi)者”從管道中讀走一些數(shù)據(jù),為其騰出一些空間。相應(yīng)的,如果管道中沒有可讀數(shù)據(jù),“消費(fèi)者” 進(jìn)程就要睡眠等待,具體過程如下圖所示:
默認(rèn)的情況下,從管道中讀寫數(shù)據(jù),最主要的特點(diǎn)就是阻塞問題(這一特點(diǎn)應(yīng)該記住),當(dāng)管道里沒有數(shù)據(jù),另一個(gè)進(jìn)程默認(rèn)用 read() 函數(shù)從管道中讀數(shù)據(jù)是阻塞的。
測(cè)試代碼:
#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 ){// 創(chuàng)建無(wú)名管道perror("pipe");}pid = fork(); // 創(chuàng)建進(jìn)程if( pid < 0 ){ // 出錯(cuò)perror("fork");exit(-1);}if( pid == 0 ){ // 子進(jìn)程_exit(0);}else if( pid > 0){// 父進(jìn)程wait(NULL); // 等待子進(jìn)程結(jié)束,回收其資源char str[50] = {0};printf("before read\n");// 從管道里讀數(shù)據(jù),如果管道沒有數(shù)據(jù), read()會(huì)阻塞read(fd_pipe[0], str, sizeof(str));printf("after read\n");printf("str=[%s]\n", str); // 打印數(shù)據(jù)}return 0; }測(cè)試結(jié)果:
deng@itcast:/mnt/hgfs/LinuxHome/code.bak2$ gcc 1.c deng@itcast:/mnt/hgfs/LinuxHome/code.bak2$ ./a.out before read04. 管道設(shè)置非阻塞
當(dāng)然,我們編程時(shí)可通過 fcntl() 函數(shù)設(shè)置文件的阻塞特性。
設(shè)置為阻塞:fcntl(fd, F_SETFL, 0); 設(shè)置為非阻塞:fcntl(fd, F_SETFL, O_NONBLOCK);測(cè)試代碼:
#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 ){// 創(chuàng)建無(wú)名管道perror("pipe");}pid = fork(); // 創(chuàng)建進(jìn)程if( pid < 0 ){ // 出錯(cuò)perror("fork");exit(-1);}if( pid == 0 ){ // 子進(jìn)程sleep(3);char buf[] = "hello, tom";write(fd_pipe[1], buf, strlen(buf)); // 寫數(shù)據(jù)_exit(0);}else if( pid > 0){// 父進(jìn)程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) );//讀數(shù)據(jù)printf("str=[%s]\n", str);sleep(1);}}return 0; }測(cè)試結(jié)果:
deng@itcast:/mnt/hgfs/LinuxHome/code.bak2$ gcc 1.c deng@itcast:/mnt/hgfs/LinuxHome/code.bak2$ ./a.out str=[] str=[] str=[] str=[hello, tom] str=[] str=[] str=[] str=[] str=[]默認(rèn)的情況下,從管道中讀寫數(shù)據(jù),還有如下特性:
1)調(diào)用 write() 函數(shù)向管道里寫數(shù)據(jù),當(dāng)緩沖區(qū)已滿時(shí) write() 也會(huì)阻塞。
測(cè)試代碼如下:
#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)); // 往管道寫的內(nèi)容int i = 0;if( pipe(fd_pipe) < 0 ){// 創(chuàng)建無(wú)名管道perror("pipe");}pid = fork(); // 創(chuàng)建進(jìn)程if( pid < 0 ){ // 出錯(cuò)perror("fork");exit(-1);}if( pid == 0 ){ // 子進(jìn)程while(1){write(fd_pipe[1], buf, sizeof(buf));i++;printf("i ======== %d\n", i);}_exit(0);}else if( pid > 0){// 父進(jìn)程wait(NULL); // 等待子進(jìn)程結(jié)束,回收其資源}return 0; }測(cè)試結(jié)果:
deng@itcast:/mnt/hgfs/LinuxHome/code.bak2$ gcc 1.c deng@itcast:/mnt/hgfs/LinuxHome/code.bak2$ ./a.out i ======== 1 i ======== 2 i ======== 3 i ======== 4 i ======== 5 i ======== 6 i ======== 7 i ======== 8 i ======== 9 i ======== 10 i ======== 11 i ======== 12 i ======== 13 i ======== 14 i ======== 15 i ======== 16 i ======== 17 i ======== 18 i ======== 19 i ======== 20 i ======== 21 i ======== 22 i ======== 23 i ======== 24 i ======== 25 i ======== 26 i ======== 27 i ======== 28 i ======== 29 i ======== 30 i ======== 31 i ======== 32 i ======== 33 i ======== 34 i ======== 35 i ======== 36 i ======== 37 i ======== 38 i ======== 39 i ======== 40 i ======== 41 i ======== 42 i ======== 43 i ======== 44 i ======== 45 i ======== 46 i ======== 47 i ======== 48 i ======== 49 i ======== 50 i ======== 51 i ======== 52 i ======== 53 i ======== 54 i ======== 55 i ======== 56 i ======== 57 i ======== 58 i ======== 59 i ======== 60 i ======== 61 i ======== 62 i ======== 63 i ======== 64到了64沒有打印,說(shuō)明管道的緩沖區(qū)大小是64K
2)通信過程中,別的進(jìn)程先結(jié)束后,當(dāng)前進(jìn)程讀端口關(guān)閉后,向管道內(nèi)寫數(shù)據(jù)時(shí),write() 所在進(jìn)程會(huì)(收到 SIGPIPE 信號(hào))退出,收到 SIGPIPE 默認(rèn)動(dòng)作為中斷當(dāng)前進(jìn)程。
測(cè)試代碼:
#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 ){// 創(chuàng)建無(wú)名管道perror("pipe");}pid = fork(); // 創(chuàng)建進(jìn)程if( pid < 0 ){ // 出錯(cuò)perror("fork");exit(-1);}if( pid == 0 ){ // 子進(jìn)程//close(fd_pipe[0]);_exit(0);}else if( pid > 0 ){// 父進(jìn)程wait(NULL); // 等待子進(jìn)程結(jié)束,回收其資源close(fd_pipe[0]); // 當(dāng)前進(jìn)程讀端口關(guān)閉char buf[50] = "12345";// 當(dāng)前進(jìn)程讀端口關(guān)閉// write()會(huì)收到 SIGPIPE 信號(hào),默認(rèn)動(dòng)作為中斷當(dāng)前進(jìn)程write(fd_pipe[1], buf, strlen(buf));while(1); // 阻塞}return 0; }測(cè)試結(jié)果:
deng@itcast:/mnt/hgfs/LinuxHome/code.bak2$ gcc 1.c deng@itcast:/mnt/hgfs/LinuxHome/code.bak2$ ./a.out deng@itcast:/mnt/hgfs/LinuxHome/code.bak2$程序直接中斷退出,沒有任何結(jié)果。
05. 附錄
5.1 參考博客:【Linux系統(tǒng)編程】進(jìn)程間通信–無(wú)名管道(pipe)
總結(jié)
以上是生活随笔為你收集整理的【Linux系统编程】进程间通信之无名管道的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【Linux系统编程】信号 (下)
- 下一篇: 【Linux系统编程】进程间通信之命名管