linux pipe使用小結


  pipe作為linux中最基礎的進程間通信機制,經常在shell中使用,例如ps aux | grep aaa 即建立了一個管道,而linux 下C程序同樣可以通過系統調用pipe在父子進程間使用管道功能。

pipe函數原型如下:

#include <unistd.h>

int pipe(int pipefd[2]);
  1.  通過函數參數返回兩個描述符(fd),pipefd[0] 用來讀,pipefd[1]用來寫, 寫入到pipefd[1]的數據將可以從pipefd[0]讀出,管道作為半雙工通道,數據只能沿一個方向前進;
  2. pipe函數返回0表示調用成功,返回-1表示調用失敗;

 

  • 讀取一個空的pipe將導致read操作阻塞,直到有數據被寫入到pipe中;向一個已經滿的pipe寫數據將導致write操作阻塞,直到pipe中的數據被讀取出去;

如果想避免read和write阻塞,可通過fcntl將pipefd設置成O_NONBLOCK,read和write無論是否成功,都將直接返回,這時需要判斷read和write返回錯誤碼,判斷操作是否成功。

  • 如果有多個進程同時向一個pipe寫入時,只有在每個進程寫入的數據長度都小於PIPE_BUF時,才可以保證pipe寫入的原子性,不然可能會出現數據錯亂的情況;
  • 管道作為半雙工通道,如果想實現雙向通信,則需要打開兩個管道,一個從父進程->子進程,另一個從子進程->父進程。
#include <unistd.h>

void server(int wfd) {
    write(wfd, "HELLO PIPE\n", 11);
}

void client(int rfd) {
    char buffer[128];
    int nRead;

    nRead = read(rfd, buffer, 128);
    if(nRead > 0) {
        write(1, buffer, nRead);
    }
}

int main(int argc, char **argv) {
    pid_t cpid;
    int pipefd[2];
    
    if(pipe(pipefd) < 0) {
        perror("pipe error:");
        return 1;
    }

    cpid = fork();
    if(cpid < 0) {
        perror("fork error:");
        return 1;
    }

    if(cpid == 0) {            /*client*/
        close(pipefd[1]);    /*close the write fd*/
        client(pipefd[0]);
     close(pipefd[0]); }
else { /*server*/ close(pipefd[0]); /*close the read fd*/ server(pipefd[1]);
     close(pipefd[1]);
     waitpid(cpid, NULL, 0); }
return 0; }

 以上示例完成了最基本的pipe進程間通信,父進程fork出子進程,子進程會繼承父進程打開着的pipefd描述符,子進程關閉pipefd[1],保留pipefd[0]用來寫入;

而父進程關閉pipefd[0],保留pipefd[1]用來寫入。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM