匿名管道

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.mzph.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…