匿名管道

1.進程通信的目的

????(1) 數據傳輸: 一個進程需要將它的數據傳輸給另一個進程
????(2) 資源共享: 多個進程之間共享同樣的資源
????(3) 通知事件: 一個進程需要向另一個或一組進程發送消息, 通知它們發生了什么事情

2.管道

????管道是一種進程之間通信的一種方式, 我們把從一個進程連接到另一個進程的數據流叫做管道

3.匿名管道

????(1) 匿名管道的創建

int pipe(int fd[2]);
fd是一個文件描述符數組, fd[0] 代表讀端, fd[1] 代表寫端
返回值:成功過時返回0, 失敗時返回錯誤代碼
4.代碼演示
//clientPipe.c
#include<stdio.h>
#include<unistd.h>
#include<string.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<fcntl.h>
#include<stdlib.h>int main()
{int wfd = open("mypipe", O_WRONLY);if(wfd == -1)//dakashibai{perror("open");exit(1);}char buf[1024];buf[0] = 0;ssize_t s;while(1){printf("Please Enter#");fflush(stdout);s = read(0, buf, sizeof(buf));if(s > 0)//成功讀取{buf[s] = 0;write(wfd, buf, s);}else if(s <= 0)//讀取失敗{perror("read");exit(1);}}
}//serverPipe.c
#include<stdio.h>
#include<unistd.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<stdlib.h>
#include<sys/types.h>int main()
{int fifo = mkfifo("mypipe", 0644);if(fifo == -1)//管道創建失敗{perror("mkfifo");exit(1);}//打開管道int rfd = open("mypipe", O_RDONLY);if(rfd == -1)//打開失敗{perror("open");exit(1);}//讀管道數據char buf[1024];ssize_t s;while(1){buf[0] = 0;printf("Please wait ... \n");s = read(rfd, buf, sizeof(buf) -1);if(s > 0)//讀到數據{buf[s] = 0;printf("client say# %s", buf);}else if(s == 0)//讀完{printf("client quit, exit now\n");exit(0);}else//讀取失敗{perror("read");exit(1);}}close(rfd);return 0;
}

?????????????這里寫圖片描述

5.站在文件描述符角度理解

這里寫圖片描述
這里寫圖片描述
????父進程先創建管道, 創建完管道,同時父進程打打開對應的讀端和寫端,接著父進程創建子進程, 由于子進程會繼承父進程的特性, 因此子進程也會打開和父進程一樣的讀端和寫端, 此時父子進程就看到了一份公共資源, 緊接著父進程將讀端關閉, 子進程將寫端關閉, 于是便可以父進程進行對數據的寫,子進程只管從管道中讀數據即可,于此父子進程合作完成讀寫工作.

6.管道讀寫規則

???? (1) 當管道讀端關閉, 寫端還在繼續寫的時候此時操作系統會給寫端發送一個 SIGPIPE 的信號,從而使得寫端退出
???? (2) 如果寫端對應的描述符關閉, 讀端則會正常退出.
???? (3) 管道具有上限,當寫到 PIPE_BUF 時, Linux 將不再保證其寫入的原子性.
???? (4) 注意匿名管道對應的兩個進程之間一定是由血緣關系的

7. 管道特點

???? (1) 管道具有單向性
???? (2) 有血緣關系的進程之間才能進行通信(匿名管道)
???? (3) 管道必須滿足同步互斥關系(管道沒有數據時,讀端進程將會不讀,阻塞等待, 當管道已經滿的時候就不能對管道進行寫了)
???? (4) 管道的生命周期隨進程
???? (5) 管道提供字節流服務(從管道中一次讀多少由操作系統決定, 即一次讀寫多少不確定)

8. 相關的幾個概念

???? (1) 數據不一致: 一個進程的讀寫影響到另外一個進程的讀寫
???? (2) 臨界資源: 兩個進程看到的一份公共資源,并且一次只允許一個進程使用
???? (3) 臨界區: 進程訪問臨界資源的那段代碼就叫做臨界資源
???? (4) 互斥: 各進程有時需要共享資源, 而且有些資源需要互斥訪問, 因此進程之間競爭使用這些資源, 進程之間的這種關系叫做互斥
???? (5) 進程訪問資源的原子性: 進程在操作某些資源時要不做完, 要不不做, 中間不會收到如何其他進程的干擾.
???? (6) 同步: 進程之間以一種比較安全的順序訪問資源, 這種安全機制就叫做同步
???? (7) 管道自帶同步互斥機制, 當管道中沒有數據時父進程會等待子進程的退出

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/384090.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/384090.shtml
英文地址,請注明出處:http://en.pswp.cn/news/384090.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Currency Exchange——最短路Bellman-Ford算法

【題目描述】 Several currency exchange points are working in our city. Let us suppose that each point specializes in two particular currencies and performs exchange operations only with these currencies. There can be several points specializing in the sam…

C++實現String類

http://blog.csdn.net/randyjiawenjie/article/details/6709539 C實現String類&#xff0c;還沒有完成&#xff0c;待繼續。 有以下注意的點&#xff1a; &#xff08;1&#xff09;賦值操作符返回的是一個MyString&&#xff0c;而重載的返回的是一個MyString。其中的原因…

POJ 3370 Halloween treats——鴿巢原理+思維

【題目描述】 POJ 3370 Halloween treats Description Every year there is the same problem at Halloween: Each neighbour is only willing to give a certain total number of sweets on that day, no matter how many children call on him, so it may happen that a chi…

將信號量代碼生成靜態庫以及動態庫

1.信號量相關代碼生成靜態庫 2.信號量相關代碼生成動態庫

Wormholes——Bellman-Ford判斷負環

【題目描述】 While exploring his many farms, Farmer John has discovered a number of amazing wormholes. A wormhole is very peculiar because it is a one-way path that delivers you to its destination at a time that is BEFORE you entered the wormhole! Each of…

C++11 標準新特性:Defaulted 和 Deleted 函數

https://www.ibm.com/developerworks/cn/aix/library/1212_lufang_c11new/index.html Defaulted 函數 背景問題 C 的類有四類特殊成員函數&#xff0c;它們分別是&#xff1a;默認構造函數、析構函數、拷貝構造函數以及拷貝賦值運算符。這些類的特殊成員函數負責創建、初始化、…

順序表實現棧相關操作

1.棧的相關概念 棧是一種特殊的線性表, 其中只允許在固定的一端進行插入和刪除元素.進行數據插入和刪除的一端叫做棧頂, 另一端成為棧底. 不含任何元素的棧稱為空棧, 棧又稱為先進先出的線性表. 2. 順序棧的結構 3. 順序棧的具體操作 (1). 數據結構 typedef char SeqStackTyp…

MPI Maelstrom——Dijkstra

【題目描述】 BIT has recently taken delivery of their new supercomputer, a 32 processor Apollo Odyssey distributed shared memory machine with a hierarchical communication subsystem. Valentine McKee’s research advisor, Jack Swigert, has asked her to bench…

雙向帶環帶頭結點的鏈表實現棧

1. 數據結構 利用帶頭結點帶環的結點實現棧的相關操作.因此, 每一個結點包括了一個前驅, 一個后繼, 還有一個數據成員 typedef char DLinkStackType;typedef struct DLinkStack {DLinkStackType data;struct DLinkStack* next;struct DLinkStack* prev; }DLinkStack;2. 初始化…

Cow Contest——Floyed+連通性判斷

【題目描述】 N (1 ≤ N ≤ 100) cows, conveniently numbered 1…N, are participating in a programming contest. As we all know, some cows code better than others. Each cow has a certain constant skill rating that is unique among the competitors. The contest …

C++11 標準新特性:委派構造函數

https://www.ibm.com/developerworks/cn/rational/1508_chenjing_c11/index.html陳 晶2015 年 8 月 11 日發布WeiboGoogle用電子郵件發送本頁面 1本文首先介紹了在委派構造函數提出之前類成員構造所面臨的問題&#xff0c;再結合實例介紹了委派構造函數的用法&#xff0c;并說明…

順序表實現隊列

一. 隊列相關概念 隊列是只允許在一段進行插入元素, 在另一端進行刪除元素的線性表,即只允許對隊列進行尾插,頭刪的操作.隊列具有先進先出, 后進后出的特性. ???????? 1.初始化 void SeqQueInit(SeqQue* q) {if(q NULL){return;//非法輸入}q -> head 0;q -> …

Arbitrage——判斷正環Bellman-Ford/SPFA

【題目描述】 Arbitrage is the use of discrepancies in currency exchange rates to transform one unit of a currency into more than one unit of the same currency. For example, suppose that 1 US Dollar buys 0.5 British pound, 1 British pound buys 10.0 French …

鏈表實現隊列

上篇博客是用順序表實現隊列, 現在用雙向帶頭結點帶環鏈表實現對隊列的出隊列, 入隊列, 取隊首元素, 以及銷毀隊列的相關操作 1.初始化鏈表 void DLinkQueInit(DLinkQue** q) {if(q NULL){return;//非法輸入}if(*q NULL){return;//非法輸入帶頭結點的鏈表至少有一個傀儡結點…

HDU - 1796——容斥原理+二進制枚舉

【題目描述】 Now you get a number N, and a M-integers set, you should find out how many integers which are small than N, that they can divided exactly by any integers in the set. For example, N12, and M-integer set is {2,3}, so there is another set {2,3,4,…

數據結構學習(二)——單鏈表的操作之頭插法和尾插法創建鏈表

http://blog.csdn.net/abclixu123/article/details/8210109 鏈表也是線性表的一種&#xff0c;與順序表不同的是&#xff0c;它在內存中不是連續存放的。在C語言中&#xff0c;鏈表是通過指針相關實現的。而單鏈表是鏈表的其中一種&#xff0c;關于單鏈表就是其節點中有數據域和…

信號的基本概念以及信號的產生

一. 信號產生的場景 1. 用戶輸入命令, 在shell 啟動一個前臺進程 ???? 2. 當用戶按一下 Ctrl C 的時候,從鍵盤產生一個硬件中斷 ???? 3. 此時CPU 正在執行這個進程的帶代碼, 則該進程的執行代碼暫停執行, CPU 從用戶態切換到內核態處理該硬件中斷. ???? 4. 中斷…

HDU - 1028——母函數入門

【題目描述】 “Well, it seems the first problem is too easy. I will let you know how foolish you are later.” feng5166 says. “The second problem is, given an positive integer N, we define an equation like this: Na[1]a[2]a[3]…a[m]; a[i]>0,1<m<N;…

信號的阻塞

一. 阻塞信號 1.信號的相關概念 ????(1) 遞達: 實際執行信號的處理動作稱為信號的遞達 ????(2) 未決: 信號從產生到遞達之間的過程叫做信號的未決 ????(3) 阻塞: 進程可以選擇阻塞某個信號, 被阻塞的信號產生時將保持在未決狀態, 直到進程解除該信號的屏蔽, 才…

POJ 1511 Invitation Cards——Dijkstra優先隊列優化+反向建圖

【題目描述】 In the age of television, not many people attend theater performances. Antique Comedians of Malidinesia are aware of this fact. They want to propagate theater and, most of all, Antique Comedies. They have printed invitation cards with all the…