日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > linux >内容正文

linux

【Linux系统编程】进程间通信之无名管道

發布時間:2024/4/24 linux 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【Linux系统编程】进程间通信之无名管道 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

00. 目錄

文章目錄

    • 00. 目錄
    • 01. 管道概述
    • 02. 管道創建函數
    • 03. 管道的特性
    • 04. 管道設置非阻塞
    • 05. 附錄

01. 管道概述

管道也叫無名管道,它是是 UNIX 系統 IPC(進程間通信) 的最古老形式,所有的 UNIX 系統都支持這種通信機制。

無名管道的特點

1、半雙工,數據在同一時刻只能在一個方向上流動。

2、數據只能從管道的一端寫入,從另一端讀出。

3、寫入管道中的數據遵循先入先出的規則。

4、管道所傳送的數據是無格式的,這要求管道的讀出方與寫入方必須事先約定好數據的格式,如多少字節算一個消息等。

5、管道不是普通的文件,不屬于某個文件系統,其只存在于內存中。

6、管道在內存中對應一個緩沖區。不同的系統其大小不一定相同。

7、從管道讀數據是一次性操作,數據一旦被讀走,它就從管道中被拋棄,釋放空間以便寫更多的數據。

8、管道沒有名字,只能在具有公共祖先的進程(父進程與子進程,或者兩個兄弟進程,具有親緣關系)之間使用。

對于無名管道特點的理解,我們可以類比現實生活中管子,管子的一端塞東西,管子的另一端取東西。

無名管道是一種特殊類型的文件,在應用層體現為兩個打開的文件描述符。

02. 管道創建函數

#include <unistd.h>int pipe(int pipefd[2]); 功能:創建無名管道。參數:pipefd :int 型數組的首地址,其存放了管道的文件描述符 pipefd[0]、pipefd[1]。當一個管道建立時,它會創建兩個文件描述符 fd[0] 和 fd[1]。其中 fd[0] 固定用于讀管道,而 fd[1] 固定用于寫管道。一般文件 I / O 的函數都可以用來操作管道(lseek() 除外)。返回值:成功:0失敗:-1

下面我們寫這個一個例子,子進程通過無名管道給父進程傳遞字符串數據

#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h>#define SIZE 64int main(void) {int fd[2];int i = 0;pid_t pid = -1;char buf[SIZE];//創建一個無名管道 用在具有共同祖先的進程if (pipe(fd) == -1){perror("pipe"); goto err0;}printf("fd[0]: %d fd[1]: %d\n", fd[0], fd[1]);//創建子進程pid = fork();if (-1 == pid){perror("fork"); goto err1;}else if (0 == pid){while(1){//子進程 寫管道memset(buf, 0, SIZE); sprintf(buf, "hello uplooking %d", i++);write(fd[1], buf, strlen(buf));sleep(1);if (i >= 10)break;}//關閉描述符close(fd[0]);close(fd[1]);exit(0);}else{//父進程 讀管道while(1){memset(buf, 0, SIZE); if (read(fd[0], buf, SIZE) <= 0)break;printf("\033[31mbuf: %s\033[0m\n", buf); }//關閉描述符close(fd[0]);close(fd[1]);}return 0; err1:close(fd[0]);close(fd[1]); err0:return 1; }

測試結果:

deng@itcast:/mnt/hgfs/LinuxHome/code.bak2/4sys/4th/code$ gcc 17pipe.c deng@itcast:/mnt/hgfs/LinuxHome/code.bak2/4sys/4th/code$ ./a.out fd[0]: 3 fd[1]: 4 buf: hello uplooking 0 buf: hello uplooking 1 buf: hello uplooking 2 buf: hello uplooking 3 buf: hello uplooking 4 buf: hello uplooking 5 buf: hello uplooking 6

03. 管道的特性

每個管道只有一個頁面作為緩沖區,該頁面是按照環形緩沖區的方式來使用的。這種訪問方式是典型的“生產者——消費者”模型。當“生產者”進程有大量的數據需要寫時,而且每當寫滿一個頁面就需要進行睡眠等待,等待“消費者”從管道中讀走一些數據,為其騰出一些空間。相應的,如果管道中沒有可讀數據,“消費者” 進程就要睡眠等待,具體過程如下圖所示:

默認的情況下,從管道中讀寫數據,最主要的特點就是阻塞問題(這一特點應該記住),當管道里沒有數據,另一個進程默認用 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; }

測試結果:

deng@itcast:/mnt/hgfs/LinuxHome/code.bak2$ gcc 1.c deng@itcast:/mnt/hgfs/LinuxHome/code.bak2$ ./a.out before read

04. 管道設置非阻塞

當然,我們編程時可通過 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, tom";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; }

測試結果:

deng@itcast:/mnt/hgfs/LinuxHome/code.bak2$ gcc 1.c deng@itcast:/mnt/hgfs/LinuxHome/code.bak2$ ./a.out str=[] str=[] str=[] str=[hello, tom] str=[] str=[] str=[] str=[] str=[]

默認的情況下,從管道中讀寫數據,還有如下特性:

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; }

測試結果:

deng@itcast:/mnt/hgfs/LinuxHome/code.bak2$ gcc 1.c deng@itcast:/mnt/hgfs/LinuxHome/code.bak2$ ./a.out i ======== 1 i ======== 2 i ======== 3 i ======== 4 i ======== 5 i ======== 6 i ======== 7 i ======== 8 i ======== 9 i ======== 10 i ======== 11 i ======== 12 i ======== 13 i ======== 14 i ======== 15 i ======== 16 i ======== 17 i ======== 18 i ======== 19 i ======== 20 i ======== 21 i ======== 22 i ======== 23 i ======== 24 i ======== 25 i ======== 26 i ======== 27 i ======== 28 i ======== 29 i ======== 30 i ======== 31 i ======== 32 i ======== 33 i ======== 34 i ======== 35 i ======== 36 i ======== 37 i ======== 38 i ======== 39 i ======== 40 i ======== 41 i ======== 42 i ======== 43 i ======== 44 i ======== 45 i ======== 46 i ======== 47 i ======== 48 i ======== 49 i ======== 50 i ======== 51 i ======== 52 i ======== 53 i ======== 54 i ======== 55 i ======== 56 i ======== 57 i ======== 58 i ======== 59 i ======== 60 i ======== 61 i ======== 62 i ======== 63 i ======== 64

到了64沒有打印,說明管道的緩沖區大小是64K

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; }

測試結果:

deng@itcast:/mnt/hgfs/LinuxHome/code.bak2$ gcc 1.c deng@itcast:/mnt/hgfs/LinuxHome/code.bak2$ ./a.out deng@itcast:/mnt/hgfs/LinuxHome/code.bak2$

程序直接中斷退出,沒有任何結果。

05. 附錄

5.1 參考博客:【Linux系統編程】進程間通信–無名管道(pipe)

總結

以上是生活随笔為你收集整理的【Linux系统编程】进程间通信之无名管道的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。