linux中pipe和dup2詳解


1、什么是管道

  管道是半雙工的,數據只能向一個方向流動;需要雙方通信時,需要建立起兩個管道; 只能用於父子進程或者兄弟進程之間(具有親緣關系的進程); 單獨構成一種獨立的文件系統:管道對於管道兩端的進程而言,就是一個文件,但它不是普通的文件,它不屬於某種文件系統,而是自立門戶,單獨構成一種文件系統,並且只存在於內存中。數據的讀出和寫入:一個進程向管道中寫的內容被管道另一端的進程讀出。寫入的內容每次都添加在管道緩沖區的末尾,並且每次都是從緩沖區的頭部讀出數據。

2、管道的創建

  int pipe(int fd[2])

  該函數創建的管道的兩端處於一個中間進程,在實際應用中並沒有太大意義,一般在pipe()創建管道后,再fork()一個子進程,然后通過管道實現父子進程之間的通信。

3、管道的讀寫規則

  管道兩端可分別用描述字fd[0]以及fd[1]來描述,需要注意的是,管道的兩端是固定了任務的。即一端只能用於讀,由描述字fd[0]表示,稱其為管道讀端;另一端則只能用於寫,由描述字fd[1]來表示,稱其為管道寫端。

4、pipe函數

  頭文件:#include<unistd.h>

  函數原型: int pipe(int fd[2])

  函數參數:fd[2],管道的兩個文件描述符,之后就是可以直接操作這兩個文件描述符。其中fd[0]為讀取端,fd[1]為寫入端

  返回值:成功返回0,否則返回-1

  讀fd[0]: close(fd[1]); read(fd[0], buf, BUF_SIZE);

  寫fd[1]: close(fd[0]); read(fd[1], buf, strlen(buf));

5、例子

  

 1 #include <unistd.h>
 2 #include <stdio.h>
 3 
 4 int main()
 5 {
 6     int fd[2];
 7     char buf[1024];
 8     pid_t pid;
 9 
10     if(pipe(fd) != 0)
11     {
12         printf("pipe error\n");
13         return 0;
14     }
15 
16     pid = fork();
17 
18     if(pid == -1)
19     {
20         printf("fork error\n");
21         return 0;
22     }
23 
24     else if(pid > 0)
25     {
26         printf("This is the father process, here write a string to the pipe\n");
27         char s[] = "Hello! write a string in the father process\n";
28         close(fd[0]);
29         write(fd[1], s, sizeof(s));              // 向管道中寫入數據
30 
31     }
32     else
33     {
34         printf("This is the child process, here read a string from the pipe\n");
35         close(fd[1]);
36         read(fd[0], buf, sizeof(buf));          //從管道中讀取數據
37         printf("%s", buf);
38     }
39     waitpid(pid, NULL, 0);
40     return 0;
41 }
View Code

  輸出結果為:

  This is the father process, here write a string to the pipe

  This is the child process, here read a string from the pipe

  Hello! write a string in the father process

  當管道中數據被讀取后,管道為空。之后再read操作將默認的被阻塞,等待某些數據被寫入。如需要設置為非阻塞,則可做如下設置:

  fcntl(filedes[0], F_SETFL, O_NONBLOCK);

  fcntl(filedes[1], F_SETFL, O_NONBLOCK);

6、dup2詳解

  用來復制一個現存的文件描述符,使兩個文件描述符指向同一個file結構體。

  其中頭文件:#include <unistd.h>

  函數原型:int dup2(int oldhandle, int newhandle);

  例子還是使用上面那個例子,如下:

 1 #include <unistd.h>
 2 #include <stdio.h>
 3 
 4 int main()
 5 {
 6     int fd[2];
 7     char buf[1024];
 8     pid_t pid;
 9     int newfd;
10     if(pipe(fd) != 0)
11     {
12         printf("pipe error\n");
13         return 0;
14     }
15 
16     pid = fork();
17 
18     if(pid == -1)
19     {
20         printf("fork error\n");
21         return 0;
22     }
23 
24     else if(pid > 0)
25     {
26         printf("This is the father process, here write a string to the pipe\n");
27         char s[] = "Hello! write a string in the father process\n";
28         close(fd[0]);
29         write(fd[1], s, sizeof(s));              // 向管道中寫入數據
30 
31     }
32     else
33     {
34         printf("This is the child process, here read a string from the pipe\n");
35         close(fd[1]);
36         dup2(fd[0], newfd);
37         read(newfd, buf, sizeof(buf));          //從管道中讀取數據
38         printf("%s", buf);
39     }
40     waitpid(pid, NULL, 0);
41     return 0;
42 }
View Code

   運行的結果還是一樣。只是在36行代碼中調用了dup2,把fd[0]復制給newfd。



 


免責聲明!

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



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