自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Linux系統(tǒng)編程—有名管道

系統(tǒng) Linux
管道,又名「無名管理」,或「匿名管道」,管道是一種非常基本,也是使用非常頻繁的IPC方式。

一、管道的概念

管道,又名「無名管理」,或「匿名管道」,管道是一種非常基本,也是使用非常頻繁的IPC方式。

[[345252]]

1. 管道本質(zhì)

  • 管道的本質(zhì)也是一種文件,不過是偽文件,實際上是一塊內(nèi)核緩沖區(qū),大小4K;
  • 管道創(chuàng)建以后會產(chǎn)生兩個文件描述符,一個是讀端,另一個是寫端;
  • 管道里的數(shù)據(jù)只能從寫端被寫入,從讀端被讀出;

1. 管道原理

管道是內(nèi)核的一塊緩沖區(qū),更具體一些,是一個環(huán)形隊列。數(shù)據(jù)從隊列的一端寫入數(shù)據(jù),另一端讀出,如下圖示:

3. 管道的優(yōu)點

簡單

4.  管道的缺點

  • 只能單向通信,如果需要雙向通信則需要建立兩個管道;
  • 只能應(yīng)用于具有血緣關(guān)系的進程,如父子進程;
  • 緩沖區(qū)大小受限,通常為1頁,即4k;

二、管道的創(chuàng)建

管道創(chuàng)建三步曲:

  • 父進程調(diào)用pipe函數(shù)創(chuàng)建管道;
  • 父進程調(diào)用fork函數(shù)創(chuàng)建子進程;
  • 父進程關(guān)閉fd[0],子進程關(guān)閉fd[1];

具體如下圖所示:

三、管道的讀寫行為

  • 管道的緩沖區(qū)大小固定為4k,所以如果管道內(nèi)數(shù)據(jù)已經(jīng)寫滿,則無法再寫入數(shù)據(jù),進程的write調(diào)用將阻塞,直到有足夠的空間再寫入數(shù)據(jù);
  • 管道的讀動作比寫動作要快,數(shù)據(jù)一旦被讀走了,管道將釋放相應(yīng)的空間,以便后續(xù)數(shù)據(jù)的寫入。當所有的數(shù)據(jù)都讀完之后,進程的read()調(diào)用將阻塞,直到有數(shù)據(jù)再次寫入。

四、例程

父子間通信:

  1. #include <stdio.h> 
  2.  #include <sys/types.h> 
  3.  #include <unistd.h> 
  4.  #include <string.h> 
  5.   
  6.  int main() 
  7.  { 
  8.      int fd[2]; 
  9.      pid_t pid; 
  10.     char buf[1024]; 
  11.     char *data = "hello world!"
  12.  
  13.     /* 創(chuàng)建管道 */ 
  14.     if (pipe(fd) == -1) { 
  15.         printf("ERROR: pipe create failed!\n"); 
  16.         return -1; 
  17.     } 
  18.  
  19.     pid = fork(); 
  20.     if (pid == 0) { 
  21.         /* 子進程 */ 
  22.         close(fd[1]);   // 子進程讀取數(shù)據(jù),關(guān)閉寫端 
  23.         read(fd[0], buf, sizeof(buf));  // 從管道讀數(shù)據(jù) 
  24.         printf("child process read: %s\n", buf); 
  25.         close(fd[0]); 
  26.     } else if (pid > 0) { 
  27.         /* 父進程 */ 
  28.         close(fd[0]);   //父進程寫數(shù)據(jù),關(guān)閉讀端 
  29.         write(fd[1], data, strlen(data));   // 向管道寫數(shù)據(jù) 
  30.         printf("parent process write: %s\n", data); 
  31.         close(fd[1]); 
  32.     } 
  33.  
  34.     return 0; 

兄弟間通信:

  1.  #include <stdio.h> 
  2.  #include <sys/types.h> 
  3.  #include <unistd.h> 
  4.  #include <string.h> 
  5.  #include <sys/wait.h> 
  6.   
  7.  int main () 
  8.  { 
  9.      int fd[2]; 
  10.     int i = 0
  11.     pid_t pid; 
  12.     char buf[1024]; 
  13.     char *data = "hello world!"
  14.  
  15.     /* 創(chuàng)建管道 */ 
  16.     if (pipe(fd) == -1) { 
  17.         printf("ERROR: pipe create failed!\n"); 
  18.         return -1; 
  19.     } 
  20.  
  21.     for (i = 0; i < 2; i++) { 
  22.         pid = fork(); 
  23.         if (pid == -1) { 
  24.             printf("ERROR: fork error!\n"); 
  25.             return -1; 
  26.         } else if (pid == 0) { 
  27.             break; 
  28.         } 
  29.     } 
  30.  
  31.     /* 通過i來判斷創(chuàng)建的子進程及父進程 */ 
  32.     if (i == 0) { 
  33.         /* 第一個子進程,兄進程 */ 
  34.         close(fd[0]);   // 兄進程向弟進程寫數(shù)據(jù),關(guān)閉讀端 
  35.         write(fd[1], data, strlen(data)); 
  36.         printf("elder brother send: %s\n", data); 
  37.         close(fd[1]); 
  38.     } else if (i == 1) { 
  39.         /* 第二個子進程,弟進程 */ 
  40.         close(fd[1]); 
  41.         read(fd[0], buf, sizeof(buf)); 
  42.         printf("younger brother receive: %s\n", buf); 
  43.         close(fd[0]); 
  44.     } else { 
  45.         /* 父進程 */ 
  46.         close(fd[0]); 
  47.         close(fd[1]); 
  48.         for (i = 0; i < 2; i++) { 
  49.             wait(NULL); 
  50.         } 
  51.     } 
  52.  
  53.     return 0; 

本文授權(quán)轉(zhuǎn)載自公眾號「良許Linux」。良許,世界500強外企Linux開發(fā)工程師,公眾號里分享大量Linux干貨,歡迎關(guān)注!

 

責任編輯:趙寧寧 來源: 今日頭條
相關(guān)推薦

2021-02-20 20:36:56

Linux無名管道

2020-10-18 07:13:44

Linux系統(tǒng)編程信號捕捉

2020-09-26 21:43:59

Linux系統(tǒng)編程條件變量

2020-10-05 22:01:02

Linux系統(tǒng)編程線程屬性

2020-09-28 06:49:50

Linux系統(tǒng)編程互斥量mutex

2020-09-26 23:09:00

Linux系統(tǒng)編程讀寫鎖

2020-09-22 07:35:06

Linux線程進程

2020-09-25 07:34:40

Linux系統(tǒng)編程信號量

2020-10-05 22:05:10

Linux系統(tǒng)編程時序競態(tài)

2020-10-08 10:10:51

Linux系統(tǒng)編程信號集

2020-10-09 07:13:11

Linux系統(tǒng)編程mmap

2017-02-28 18:26:09

Linuxinput子系統(tǒng)編程

2010-03-05 13:34:54

2019-03-15 09:30:09

Linux系統(tǒng)CPU

2009-07-03 11:57:18

系統(tǒng)編程安全linux

2009-10-23 16:35:44

linux Debia

2010-02-02 13:26:53

Linux內(nèi)核

2025-04-16 04:22:00

2018-09-10 08:45:04

Linux管道命令

2021-05-16 18:02:52

系統(tǒng)編程JavaScript
點贊
收藏

51CTO技術(shù)棧公眾號