管道通訊操作

來源:互聯網
上載者:User

標籤:limits   size_t   進一步   lib   oid   nbsp   led   size   字串   

 

1.管道的開啟以及關閉操作

#include <unistd.h>#include <stdio.h>#include <stdlib.h>int main( void ){       int fd[2];                                            /* 管道的檔案描述符數組 */       char str[256];       if ( (pipe(fd)) < 0 ){              perror("pipe");              exit(1);       }       write(fd[1],  "create the pipe successfully !\n", 1024 );                                                                      /*向管道寫入端寫入資料*/       read(fd[0], str, sizeof(str) );   /*從管道讀出端讀出資料*/       printf ("%s", str );       printf ( "pipe file descriptors are %d,%d \n", fd[0], fd[1]) ;       close (fd[0]);                                      /* 關閉管道的讀入檔案描述符*/       close (fd[1]);                                      /* 關閉管道的讀出檔案描述符*/       return 0;}

上邊這段程式是管道的開啟和關閉的執行個體,首先定義管道的檔案描述符數組fd[2],可以理解為管道的兩端,分別是讀出端fd[0]和寫入端fd[1]端。

用函數write()向管道寫入端寫入資料, write(fd[1], "create the pipe successfully !\n", 1024 ); ,向寫入端fd[1]寫入"create the pipe successfully !\n"這句話,數字1024是寫入的資料的最大長度。

用函數read()從管道讀出端讀出資料, read(fd[0], str, sizeof(str) ); ,讀出管道端的資料,儲存在字串str中。

2.管道在父子進程之間的應用

根據之前學習的進程操作的知識結合管道的知識我們應該能夠寫出來如何通過管道使父子進程之間進行通訊。

第一步,定義基本的東西,比如檔案描述符數組、讀出資料存放的數組、進程標識符等。

第二步,建立管道。

第三步,建立一個子進程。

第四步,在父進程中向管道寫入資料。

第五步,在子進程中從管道讀取資料。

第六步,輸出子進程中列印的資料。

這六步是父進程向子進程發送資料的流程,是一個架構,具體的程式寫入架構中,這個簡單的程式就可以寫出來了。但是在寫代碼的過程中還要注意一個問題,先建立一個管道,再建立一個子進程,父進程中有一個管道,子進程中也有一個管道,父進程中寫入的內容怎樣才能從子進程的管道中讀出來?書上有一個父進程寫入資料,子進程讀出資料的程式。這個程式在應用中,在父進程中將父進程管道的讀資料端關閉,然後寫入資料,在子進程中將子進程管道的寫資料端關閉,然後再讀資料。這樣就能在父進程中寫資料在子進程中讀資料。具體代碼如下。

#include <unistd.h>#include <stdio.h>#include <stdlib.h>#include <fcntl.h>#include <limits.h>#include <sys/types.h>#define BUFSZ PIPE_BUF  /* PIPE_BUF管道預設一次性讀寫的資料長度*/int main ( void ){       int fd[2];       char buf[BUFSZ];       pid_t pid;       ssize_t len;       if ( (pipe(fd)) < 0 ){                                    /*建立管道*/              perror ( "failed to pipe" );              exit( 1 );       }       if ( (pid = fork()) < 0 ){                              /* 建立一個子進程 */              perror ( "failed to fork " );              exit( 1 );       }       else if ( pid > 0 ){              close ( fd[0] );                                    /*父進程中關閉管道的讀出端*/              write (fd[1], "hello my son!\n", 14 );    /*父進程向管道寫入資料*/              exit (0);       }       else {              close ( fd[1] );                                    /*子進程關閉管道的寫入端*/              len = read (fd[0], buf, BUFSZ );           /*子進程從管道中讀出資料*/              if ( len < 0 ){                     perror ( "process failed when read a pipe " );                     exit( 1 );              }              else                     write(STDOUT_FILENO, buf, len); /*輸出到標準輸出*/              exit(0);       }}

上邊的代碼經過調試之後,發現沒有問題,那就證明一開始的思路是正確的,可以通過這樣的方式使父子進程之間通過管道進行通訊。但是,這是關閉了父進程中管道的讀出端才將資料發到了子進程中,如果不關閉父進程中管道的讀出能不能傳過去?然後修改代碼發現不關閉管道的讀出端也是可以的。

進一步思考,在父進程中寫入的資料能不能在父進程和子進程中同時讀出來?經過調試代碼,在父進程中添加讀管道中的資料並顯示的代碼如下,探索資料只能從父進程中讀出來,不能從子進程中讀出來。

else if ( pid > 0 ){              write (fd[1], "hello my son!\n", 14 );    /*父進程向管道寫入資料*/              read(fd[0],buf1,BUFSZ);printf(“%s\n%s\n”,buf1,buf1);              exit (0);       }

這樣來看只有兩個可能,一種可能就是管道是一次性的,發送接收之後管道就自動清除了。另一種是在父進程的管道寫入端寫入資料,可以在父進程或子進程的管道讀出端讀出,但是只能讀一次,從父進程中讀了之後就不能再在子進程中讀,在子進程中讀了就不能再在父進程中讀。繼續修改代碼進行調試,先驗證第一種可能性。

else if ( pid > 0 ){              write (fd[1], "hello my son!\n", 14);     /*父進程向管道寫入資料*/              read(fd[0],buf1,BUFSZ);printf(“%s\n%s\n”,buf1,buf1);//第二次寫入資料write (fd[1], "hello my son2!\n", 15 );  /*父進程向管道寫入資料*/              read(fd[0],buf1,BUFSZ);printf(“%s\n%s\n”,buf1,buf1);              exit (0);       }

經過調試發現,管道是一直能用的,這樣就推翻了第一種假設。那就剩下第二種可能了,關閉父進程中管道的讀出端,代碼如下,再調試發現,可以只能從子進程中讀出資料。

else if ( pid > 0 ){              close(fd[0]);              write (fd[1], "hello my son!\n", 14 );    /*父進程向管道寫入資料*/              read(fd[0],buf1,BUFSZ);printf(“%s\n%s\n”,buf1,buf1);              exit (0);       }

從這一系列的調試過程可以理解管道的工作過程,瞭解管道在父子進程之間進行通訊的過程。

                                           

中第一幅圖是建立管道和子進程之後的管道,但是在通訊的時候只能從左邊選一根寫入的線,在右邊選一根讀出的線。用的比較多的就是圖2中在父進程中通過管道發送資料到子進程中。

3.管道在兄弟進程之間的應用

管道在父子進程之間的應用明白之後,在兄弟之間的應用就很好理解了。

第一步,定義基本的東西,比如檔案描述符數組、讀出資料存放的數組、進程標識符等。

第二步,建立管道。

第三步,建立第一個子進程。

第四步,在第一個子進程中向管道中寫入資料。

第五步,建立第二個子進程

第六步,在第二個子進程中讀取管道中的資料。

第七步,輸出第二個子進程中列印的資料。

具體代碼如下所示:

#include <unistd.h>#include <stdio.h>#include <stdlib.h>#include <limits.h>#include <fcntl.h>#include <sys/types.h>#define BUFSZ PIPE_BUFvoid err_quit(char * msg){       perror( msg );       exit(1);}int main ( void ){       int fd[2];       char buf[BUFSZ];          /* 緩衝區 */       pid_t pid;       int len;       if ( (pipe(fd)) < 0 )  /*建立管道*/              err_quit( "pipe" );       if ( (pid = fork()) < 0 )                               /*建立第一個子進程*/              err_quit("fork");       else if ( pid == 0 ){                                   /*子進程中*/              close ( fd[0] );                                    /*關閉不使用的檔案描述符*/              write(fd[1], "hello brother!\n", 15 );     /*發送訊息*/              exit(0);       }       if ( (pid = fork()) < 0 )                               /*建立第二個子進程*/              err_quit("fork");       else if ( pid > 0 ){                                      /*父進程中*/              close ( fd[0] );              close ( fd[1] );              exit ( 0 );       }       else {                                                                     /*子進程中*/              close ( fd[1] );                                    /*關閉不使用的檔案描述符*/              len = read (fd[0], buf, BUFSZ );           /*讀取訊息*/              write(STDOUT_FILENO, buf, len);              exit(0);       }}

 

管道通訊操作

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.