1. 程式人生 > >Linux 多工程式設計——程序間通訊:無名管道(PIPE)

Linux 多工程式設計——程序間通訊:無名管道(PIPE)

管道的概述
管道也叫無名管道,它是是 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;
}

執行結果如下:

 


--------------------- 
作者:Mike__Jiang 
來源:CSDN 
原文:https://blog.csdn.net/tennysonsky/article/details/46315517 
版權宣告:本文為博主原創文章,轉載請附上博文連結!